Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgrePlane.cpp @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 5.6 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgrePlane.h"
31#include "OgreMatrix3.h"
32#include "OgreAxisAlignedBox.h"
33
34namespace Ogre {
35        //-----------------------------------------------------------------------
36        Plane::Plane ()
37        {
38                normal = Vector3::ZERO;
39                d = 0.0;
40        }
41        //-----------------------------------------------------------------------
42        Plane::Plane (const Plane& rhs)
43        {
44                normal = rhs.normal;
45                d = rhs.d;
46        }
47        //-----------------------------------------------------------------------
48        Plane::Plane (const Vector3& rkNormal, Real fConstant)
49        {
50                normal = rkNormal;
51                d = -fConstant;
52        }
53        //-----------------------------------------------------------------------
54        Plane::Plane (const Vector3& rkNormal, const Vector3& rkPoint)
55        {
56                redefine(rkNormal, rkPoint);
57        }
58        //-----------------------------------------------------------------------
59        Plane::Plane (const Vector3& rkPoint0, const Vector3& rkPoint1,
60                const Vector3& rkPoint2)
61        {
62                redefine(rkPoint0, rkPoint1, rkPoint2);
63        }
64        //-----------------------------------------------------------------------
65        Real Plane::getDistance (const Vector3& rkPoint) const
66        {
67                return normal.dotProduct(rkPoint) + d;
68        }
69        //-----------------------------------------------------------------------
70        Plane::Side Plane::getSide (const Vector3& rkPoint) const
71        {
72                Real fDistance = getDistance(rkPoint);
73
74                if ( fDistance < 0.0 )
75                        return Plane::NEGATIVE_SIDE;
76
77                if ( fDistance > 0.0 )
78                        return Plane::POSITIVE_SIDE;
79
80                return Plane::NO_SIDE;
81        }
82
83
84        //-----------------------------------------------------------------------
85        Plane::Side Plane::getSide (const AxisAlignedBox& box) const
86        {
87                if (box.isNull()) 
88                        return NO_SIDE;
89                if (box.isInfinite())
90                        return BOTH_SIDE;
91
92        return getSide(box.getCenter(), box.getHalfSize());
93        }
94    //-----------------------------------------------------------------------
95    Plane::Side Plane::getSide (const Vector3& centre, const Vector3& halfSize) const
96    {
97        // Calculate the distance between box centre and the plane
98        Real dist = getDistance(centre);
99
100        // Calculate the maximise allows absolute distance for
101        // the distance between box centre and plane
102        Real maxAbsDist = normal.absDotProduct(halfSize);
103
104        if (dist < -maxAbsDist)
105            return Plane::NEGATIVE_SIDE;
106
107        if (dist > +maxAbsDist)
108            return Plane::POSITIVE_SIDE;
109
110        return Plane::BOTH_SIDE;
111    }
112        //-----------------------------------------------------------------------
113        void Plane::redefine(const Vector3& rkPoint0, const Vector3& rkPoint1,
114                const Vector3& rkPoint2)
115        {
116                Vector3 kEdge1 = rkPoint1 - rkPoint0;
117                Vector3 kEdge2 = rkPoint2 - rkPoint0;
118                normal = kEdge1.crossProduct(kEdge2);
119                normal.normalise();
120                d = -normal.dotProduct(rkPoint0);
121        }
122        //-----------------------------------------------------------------------
123        void Plane::redefine(const Vector3& rkNormal, const Vector3& rkPoint)
124        {
125                normal = rkNormal;
126                d = -rkNormal.dotProduct(rkPoint);
127        }
128        //-----------------------------------------------------------------------
129        Vector3 Plane::projectVector(const Vector3& p) const
130        {
131                // We know plane normal is unit length, so use simple method
132                Matrix3 xform;
133                xform[0][0] = 1.0f - normal.x * normal.x;
134                xform[0][1] = -normal.x * normal.y;
135                xform[0][2] = -normal.x * normal.z;
136                xform[1][0] = -normal.y * normal.x;
137                xform[1][1] = 1.0f - normal.y * normal.y;
138                xform[1][2] = -normal.y * normal.z;
139                xform[2][0] = -normal.z * normal.x;
140                xform[2][1] = -normal.z * normal.y;
141                xform[2][2] = 1.0f - normal.z * normal.z;
142                return xform * p;
143
144        }
145        //-----------------------------------------------------------------------
146    Real Plane::normalise(void)
147    {
148        Real fLength = normal.length();
149
150        // Will also work for zero-sized vectors, but will change nothing
151        if (fLength > 1e-08f)
152        {
153            Real fInvLength = 1.0f / fLength;
154            normal *= fInvLength;
155            d *= fInvLength;
156        }
157
158        return fLength;
159    }
160        //-----------------------------------------------------------------------
161        std::ostream& operator<< (std::ostream& o, const Plane& p)
162        {
163                o << "Plane(normal=" << p.normal << ", d=" << p.d << ")";
164                return o;
165        }
166} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.