| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- -----------------------------------------------------------------------------
- This source file is part of OGRE
- (Object-oriented Graphics Rendering Engine)
- For the latest info, see http://www.ogre3d.org/
- Copyright (c) 2000-2011 Torus Knot Software Ltd
- 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:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- 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 "CmPlane.h"
- #include "CmMatrix3.h"
- #include "CmAxisAlignedBox.h"
- namespace CamelotEngine {
- //-----------------------------------------------------------------------
- Plane::Plane ()
- {
- normal = Vector3::ZERO;
- d = 0.0;
- }
- //-----------------------------------------------------------------------
- Plane::Plane (const Plane& rhs)
- {
- normal = rhs.normal;
- d = rhs.d;
- }
- //-----------------------------------------------------------------------
- Plane::Plane (const Vector3& rkNormal, float fConstant)
- {
- normal = rkNormal;
- d = -fConstant;
- }
- //---------------------------------------------------------------------
- Plane::Plane (float a, float b, float c, float _d)
- : normal(a, b, c), d(_d)
- {
- }
- //-----------------------------------------------------------------------
- Plane::Plane (const Vector3& rkNormal, const Vector3& rkPoint)
- {
- redefine(rkNormal, rkPoint);
- }
- //-----------------------------------------------------------------------
- Plane::Plane (const Vector3& rkPoint0, const Vector3& rkPoint1,
- const Vector3& rkPoint2)
- {
- redefine(rkPoint0, rkPoint1, rkPoint2);
- }
- //-----------------------------------------------------------------------
- float Plane::getDistance (const Vector3& rkPoint) const
- {
- return normal.dotProduct(rkPoint) + d;
- }
- //-----------------------------------------------------------------------
- Plane::Side Plane::getSide (const Vector3& rkPoint) const
- {
- float fDistance = getDistance(rkPoint);
- if ( fDistance < 0.0 )
- return Plane::NEGATIVE_SIDE;
- if ( fDistance > 0.0 )
- return Plane::POSITIVE_SIDE;
- return Plane::NO_SIDE;
- }
- //-----------------------------------------------------------------------
- Plane::Side Plane::getSide (const AxisAlignedBox& box) const
- {
- if (box.isNull())
- return NO_SIDE;
- if (box.isInfinite())
- return BOTH_SIDE;
- return getSide(box.getCenter(), box.getHalfSize());
- }
- //-----------------------------------------------------------------------
- Plane::Side Plane::getSide (const Vector3& centre, const Vector3& halfSize) const
- {
- // 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;
- }
- //-----------------------------------------------------------------------
- void Plane::redefine(const Vector3& rkPoint0, const Vector3& rkPoint1,
- const Vector3& rkPoint2)
- {
- Vector3 kEdge1 = rkPoint1 - rkPoint0;
- Vector3 kEdge2 = rkPoint2 - rkPoint0;
- normal = kEdge1.crossProduct(kEdge2);
- normal.normalise();
- d = -normal.dotProduct(rkPoint0);
- }
- //-----------------------------------------------------------------------
- void Plane::redefine(const Vector3& rkNormal, const Vector3& rkPoint)
- {
- normal = rkNormal;
- d = -rkNormal.dotProduct(rkPoint);
- }
- //-----------------------------------------------------------------------
- Vector3 Plane::projectVector(const Vector3& p) const
- {
- // 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;
- return xform * p;
- }
- //-----------------------------------------------------------------------
- float Plane::normalise(void)
- {
- float fLength = normal.length();
- // Will also work for zero-sized vectors, but will change nothing
- if (fLength > 1e-08f)
- {
- float fInvLength = 1.0f / fLength;
- normal *= fInvLength;
- d *= fInvLength;
- }
- return fLength;
- }
- //-----------------------------------------------------------------------
- std::ostream& operator<< (std::ostream& o, const Plane& p)
- {
- o << "Plane(normal=" << p.normal << ", d=" << p.d << ")";
- return o;
- }
- } // namespace CamelotEngine
|