[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 | #include "OgrePlane.h"
|
---|
| 27 | #include "OgreMatrix3.h"
|
---|
| 28 |
|
---|
| 29 | namespace Ogre {
|
---|
| 30 | //-----------------------------------------------------------------------
|
---|
| 31 | Plane::Plane ()
|
---|
| 32 | {
|
---|
| 33 | normal = Vector3::ZERO;
|
---|
| 34 | d = 0.0;
|
---|
| 35 | }
|
---|
| 36 | //-----------------------------------------------------------------------
|
---|
| 37 | Plane::Plane (const Plane& rhs)
|
---|
| 38 | {
|
---|
| 39 | normal = rhs.normal;
|
---|
| 40 | d = rhs.d;
|
---|
| 41 | }
|
---|
| 42 | //-----------------------------------------------------------------------
|
---|
| 43 | Plane::Plane (const Vector3& rkNormal, Real fConstant)
|
---|
| 44 | {
|
---|
| 45 | normal = rkNormal;
|
---|
| 46 | d = -fConstant;
|
---|
| 47 | }
|
---|
| 48 | //-----------------------------------------------------------------------
|
---|
| 49 | Plane::Plane (const Vector3& rkNormal, const Vector3& rkPoint)
|
---|
| 50 | {
|
---|
| 51 | normal = rkNormal;
|
---|
| 52 | d = -rkNormal.dotProduct(rkPoint);
|
---|
| 53 | }
|
---|
| 54 | //-----------------------------------------------------------------------
|
---|
| 55 | Plane::Plane (const Vector3& rkPoint0, const Vector3& rkPoint1,
|
---|
| 56 | const Vector3& rkPoint2)
|
---|
| 57 | {
|
---|
| 58 | redefine(rkPoint0, rkPoint1, rkPoint2);
|
---|
| 59 | }
|
---|
| 60 | //-----------------------------------------------------------------------
|
---|
| 61 | Real Plane::getDistance (const Vector3& rkPoint) const
|
---|
| 62 | {
|
---|
| 63 | return normal.dotProduct(rkPoint) + d;
|
---|
| 64 | }
|
---|
| 65 | //-----------------------------------------------------------------------
|
---|
| 66 | Plane::Side Plane::getSide (const Vector3& rkPoint) const
|
---|
| 67 | {
|
---|
| 68 | Real fDistance = getDistance(rkPoint);
|
---|
| 69 |
|
---|
| 70 | if ( fDistance < 0.0 )
|
---|
| 71 | return Plane::NEGATIVE_SIDE;
|
---|
| 72 |
|
---|
| 73 | if ( fDistance > 0.0 )
|
---|
| 74 | return Plane::POSITIVE_SIDE;
|
---|
| 75 |
|
---|
| 76 | return Plane::NO_SIDE;
|
---|
| 77 | }
|
---|
| 78 | //-----------------------------------------------------------------------
|
---|
| 79 | void Plane::redefine(const Vector3& rkPoint0, const Vector3& rkPoint1,
|
---|
| 80 | const Vector3& rkPoint2)
|
---|
| 81 | {
|
---|
| 82 | Vector3 kEdge1 = rkPoint1 - rkPoint0;
|
---|
| 83 | Vector3 kEdge2 = rkPoint2 - rkPoint0;
|
---|
| 84 | normal = kEdge1.crossProduct(kEdge2);
|
---|
| 85 | normal.normalise();
|
---|
| 86 | d = -normal.dotProduct(rkPoint0);
|
---|
| 87 | }
|
---|
| 88 | //-----------------------------------------------------------------------
|
---|
| 89 | Vector3 Plane::projectVector(const Vector3& p)
|
---|
| 90 | {
|
---|
| 91 | // We know plane normal is unit length, so use simple method
|
---|
| 92 | Matrix3 xform;
|
---|
| 93 | xform[0][0] = normal.x * normal.x - 1.0f;
|
---|
| 94 | xform[0][1] = normal.x * normal.y;
|
---|
| 95 | xform[0][2] = normal.x * normal.z;
|
---|
| 96 | xform[1][0] = normal.y * normal.x;
|
---|
| 97 | xform[1][1] = normal.y * normal.y - 1.0f;
|
---|
| 98 | xform[1][2] = normal.y * normal.z;
|
---|
| 99 | xform[2][0] = normal.z * normal.x;
|
---|
| 100 | xform[2][1] = normal.z * normal.y;
|
---|
| 101 | xform[2][2] = normal.z * normal.z - 1.0f;
|
---|
| 102 | return xform * p;
|
---|
| 103 |
|
---|
| 104 | }
|
---|
| 105 | //-----------------------------------------------------------------------
|
---|
| 106 | std::ostream& operator<< (std::ostream& o, Plane& p)
|
---|
| 107 | {
|
---|
| 108 | o << "Plane(normal=" << p.normal << ", d=" << p.d << ")";
|
---|
| 109 | return o;
|
---|
| 110 | }
|
---|
| 111 | } // namespace Ogre
|
---|