axmol/core/3d/CCPlane.cpp

90 lines
2.7 KiB
C++
Raw Normal View History

2014-12-16 16:34:35 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2021-12-25 10:04:45 +08:00
2014-12-16 16:34:35 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2014-12-16 16:34:35 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2014-12-16 16:34:35 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "3d/CCPlane.h"
2014-12-16 16:34:35 +08:00
NS_AX_BEGIN
2014-12-16 16:34:35 +08:00
2021-12-25 10:04:45 +08:00
Plane::Plane() : _normal(0.f, 0.f, 1.f), _dist(0.f) {}
2014-12-16 16:34:35 +08:00
// create plane from tree point
Plane::Plane(const Vec3& p1, const Vec3& p2, const Vec3& p3)
{
initPlane(p1, p2, p3);
}
// create plane from normal and dist
Plane::Plane(const Vec3& normal, float dist)
{
initPlane(normal, dist);
}
// create plane from normal and a point on plane
Plane::Plane(const Vec3& normal, const Vec3& point)
{
initPlane(normal, point);
}
void Plane::initPlane(const Vec3& p1, const Vec3& p2, const Vec3& p3)
{
Vec3 p21 = p2 - p1;
Vec3 p32 = p3 - p2;
Vec3::cross(p21, p32, &_normal);
_normal.normalize();
_dist = _normal.dot(p1);
}
void Plane::initPlane(const Vec3& normal, float dist)
{
float oneOverLength = 1 / normal.length();
2021-12-25 10:04:45 +08:00
_normal = normal * oneOverLength;
_dist = dist * oneOverLength;
2014-12-16 16:34:35 +08:00
}
void Plane::initPlane(const Vec3& normal, const Vec3& point)
{
_normal = normal;
_normal.normalize();
_dist = _normal.dot(point);
}
float Plane::dist2Plane(const Vec3& p) const
{
return _normal.dot(p) - _dist;
}
2014-12-22 18:13:07 +08:00
PointSide Plane::getSide(const Vec3& point) const
2014-12-16 16:34:35 +08:00
{
float dist = dist2Plane(point);
if (dist > 0)
2014-12-22 18:13:07 +08:00
return PointSide::FRONT_PLANE;
2014-12-16 16:34:35 +08:00
else if (dist < 0)
2014-12-22 18:13:07 +08:00
return PointSide::BEHIND_PLANE;
else
return PointSide::IN_PLANE;
2014-12-16 16:34:35 +08:00
}
NS_AX_END