2015-02-11 18:14:22 +08:00
|
|
|
/****************************************************************************
|
2015-02-25 18:05:41 +08:00
|
|
|
Copyright (C) 2013 Henry van Merode. All rights reserved.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-07-15 23:03:43 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-07-16 11:04:38 +08:00
|
|
|
|
2015-02-11 18:14:22 +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
|
|
|
|
2015-02-11 18:14:22 +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
|
|
|
|
2015-02-11 18:14:22 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
#include "CCPUPlane.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUPlane::PUPlane()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
// Vec3 default constructor zero
|
2021-12-25 10:04:45 +08:00
|
|
|
// normal = Vec3::ZERO;
|
|
|
|
d = 0.0;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUPlane::PUPlane(const PUPlane& rhs) : normal(rhs.normal), d(rhs.d) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUPlane::PUPlane(const Vec3& rkNormal, float fConstant) : normal(rkNormal), d(-fConstant) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
//---------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUPlane::PUPlane(float a, float b, float c, float _d) : normal(a, b, c), d(_d) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUPlane::PUPlane(const Vec3& rkNormal, const Vec3& rkPoint)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
redefine(rkNormal, rkPoint);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUPlane::PUPlane(const Vec3& rkPoint0, const Vec3& rkPoint1, const Vec3& rkPoint2)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
redefine(rkPoint0, rkPoint1, rkPoint2);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
float PUPlane::getDistance(const Vec3& rkPoint) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return normal.dot(rkPoint) + d;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
// Plane::Side Plane::getSide (const Vec3& rkPoint) const
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// float fDistance = getDistance(rkPoint);
|
|
|
|
//
|
|
|
|
// if ( fDistance < 0.0 )
|
|
|
|
// return Plane::NEGATIVE_SIDE;
|
|
|
|
//
|
|
|
|
// if ( fDistance > 0.0 )
|
|
|
|
// return Plane::POSITIVE_SIDE;
|
|
|
|
//
|
|
|
|
// return Plane::NO_SIDE;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//
|
|
|
|
////-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
// Plane::Side Plane::getSide (const AABB& box) const
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (box.isEmpty())
|
2015-02-11 18:14:22 +08:00
|
|
|
// return NO_SIDE;
|
|
|
|
// if (box.isInfinite())
|
|
|
|
// return BOTH_SIDE;
|
|
|
|
//
|
|
|
|
// return getSide(box.getCenter(), box);
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
2015-02-11 18:14:22 +08:00
|
|
|
////-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
// Plane::Side Plane::getSide (const Vec3& centre, const Vec3& halfSize) const
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// // Calculate the distance between box centre and the plane
|
|
|
|
// float dist = getDistance(centre);
|
|
|
|
//
|
|
|
|
// // Calculate the maximise allows absolute distance for
|
|
|
|
// // the distance between box centre and plane
|
|
|
|
// float maxAbsDist = normal.absDotProduct(halfSize);
|
|
|
|
//
|
|
|
|
// if (dist < -maxAbsDist)
|
|
|
|
// return Plane::NEGATIVE_SIDE;
|
|
|
|
//
|
|
|
|
// if (dist > +maxAbsDist)
|
|
|
|
// return Plane::POSITIVE_SIDE;
|
|
|
|
//
|
|
|
|
// return Plane::BOTH_SIDE;
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUPlane::redefine(const Vec3& rkPoint0, const Vec3& rkPoint1, const Vec3& rkPoint2)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3 kEdge1 = rkPoint1 - rkPoint0;
|
|
|
|
Vec3 kEdge2 = rkPoint2 - rkPoint0;
|
|
|
|
Vec3::cross(kEdge1, kEdge2, &normal);
|
|
|
|
normal.normalize();
|
|
|
|
d = -normal.dot(rkPoint0);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUPlane::redefine(const Vec3& rkNormal, const Vec3& rkPoint)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
normal = rkNormal;
|
|
|
|
d = -rkNormal.dot(rkPoint);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
Vec3 PUPlane::projectVector(const Vec3& p) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// We know plane normal is unit length, so use simple method
|
|
|
|
// Matrix3 xform;
|
|
|
|
// xform[0][0] = 1.0f - normal.x * normal.x;
|
|
|
|
// xform[0][1] = -normal.x * normal.y;
|
|
|
|
// xform[0][2] = -normal.x * normal.z;
|
|
|
|
// xform[1][0] = -normal.y * normal.x;
|
|
|
|
// xform[1][1] = 1.0f - normal.y * normal.y;
|
|
|
|
// xform[1][2] = -normal.y * normal.z;
|
|
|
|
// xform[2][0] = -normal.z * normal.x;
|
|
|
|
// xform[2][1] = -normal.z * normal.y;
|
|
|
|
// xform[2][2] = 1.0f - normal.z * normal.z;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 xform;
|
|
|
|
xform.m[0] = 1.0f - normal.x * normal.x;
|
|
|
|
xform.m[1] = -normal.x * normal.y;
|
|
|
|
xform.m[2] = -normal.x * normal.z;
|
|
|
|
xform.m[4] = -normal.y * normal.x;
|
|
|
|
xform.m[5] = 1.0f - normal.y * normal.y;
|
|
|
|
xform.m[6] = -normal.y * normal.z;
|
|
|
|
xform.m[8] = -normal.z * normal.x;
|
|
|
|
xform.m[9] = -normal.z * normal.y;
|
|
|
|
xform.m[10] = 1.0f - normal.z * normal.z;
|
|
|
|
return xform * p;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PUPlane::normalize()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float fLength = normal.length();
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// Will also work for zero-sized vectors, but will change nothing
|
|
|
|
// We're not using epsilons because we don't need to.
|
|
|
|
// Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
|
|
|
|
if (fLength > float(0.0f))
|
|
|
|
{
|
|
|
|
float fInvLength = 1.0f / fLength;
|
|
|
|
normal *= fInvLength;
|
|
|
|
d *= fInvLength;
|
|
|
|
}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return fLength;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|