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-2006 Torus Knot Software Ltd |
---|
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 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgrePlane.h" |
---|
30 | #include "OgreMatrix3.h" |
---|
31 | |
---|
32 | namespace Ogre { |
---|
33 | //----------------------------------------------------------------------- |
---|
34 | Plane::Plane () |
---|
35 | { |
---|
36 | normal = Vector3::ZERO; |
---|
37 | d = 0.0; |
---|
38 | } |
---|
39 | //----------------------------------------------------------------------- |
---|
40 | Plane::Plane (const Plane& rhs) |
---|
41 | { |
---|
42 | normal = rhs.normal; |
---|
43 | d = rhs.d; |
---|
44 | } |
---|
45 | //----------------------------------------------------------------------- |
---|
46 | Plane::Plane (const Vector3& rkNormal, Real fConstant) |
---|
47 | { |
---|
48 | normal = rkNormal; |
---|
49 | d = -fConstant; |
---|
50 | } |
---|
51 | //--------------------------------------------------------------------- |
---|
52 | Plane::Plane (Real a, Real b, Real c, Real _d) |
---|
53 | : normal(a, b, c), d(_d) |
---|
54 | { |
---|
55 | } |
---|
56 | //----------------------------------------------------------------------- |
---|
57 | Plane::Plane (const Vector3& rkNormal, const Vector3& rkPoint) |
---|
58 | { |
---|
59 | redefine(rkNormal, rkPoint); |
---|
60 | } |
---|
61 | //----------------------------------------------------------------------- |
---|
62 | Plane::Plane (const Vector3& rkPoint0, const Vector3& rkPoint1, |
---|
63 | const Vector3& rkPoint2) |
---|
64 | { |
---|
65 | redefine(rkPoint0, rkPoint1, rkPoint2); |
---|
66 | } |
---|
67 | //----------------------------------------------------------------------- |
---|
68 | Real Plane::getDistance (const Vector3& rkPoint) const |
---|
69 | { |
---|
70 | return normal.dotProduct(rkPoint) + d; |
---|
71 | } |
---|
72 | //----------------------------------------------------------------------- |
---|
73 | Plane::Side Plane::getSide (const Vector3& rkPoint) const |
---|
74 | { |
---|
75 | Real fDistance = getDistance(rkPoint); |
---|
76 | |
---|
77 | if ( fDistance < 0.0 ) |
---|
78 | return Plane::NEGATIVE_SIDE; |
---|
79 | |
---|
80 | if ( fDistance > 0.0 ) |
---|
81 | return Plane::POSITIVE_SIDE; |
---|
82 | |
---|
83 | return Plane::NO_SIDE; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | //----------------------------------------------------------------------- |
---|
88 | Plane::Side Plane::getSide (const Vector3& centre, const Vector3& halfSize) const |
---|
89 | { |
---|
90 | // Calculate the distance between box centre and the plane |
---|
91 | Real dist = getDistance(centre); |
---|
92 | |
---|
93 | // Calculate the maximise allows absolute distance for |
---|
94 | // the distance between box centre and plane |
---|
95 | Real maxAbsDist = normal.absDotProduct(halfSize); |
---|
96 | |
---|
97 | if (dist < -maxAbsDist) |
---|
98 | return Plane::NEGATIVE_SIDE; |
---|
99 | |
---|
100 | if (dist > +maxAbsDist) |
---|
101 | return Plane::POSITIVE_SIDE; |
---|
102 | |
---|
103 | return Plane::BOTH_SIDE; |
---|
104 | } |
---|
105 | //----------------------------------------------------------------------- |
---|
106 | void Plane::redefine(const Vector3& rkPoint0, const Vector3& rkPoint1, |
---|
107 | const Vector3& rkPoint2) |
---|
108 | { |
---|
109 | Vector3 kEdge1 = rkPoint1 - rkPoint0; |
---|
110 | Vector3 kEdge2 = rkPoint2 - rkPoint0; |
---|
111 | normal = kEdge1.crossProduct(kEdge2); |
---|
112 | normal.normalise(); |
---|
113 | d = -normal.dotProduct(rkPoint0); |
---|
114 | } |
---|
115 | //----------------------------------------------------------------------- |
---|
116 | void Plane::redefine(const Vector3& rkNormal, const Vector3& rkPoint) |
---|
117 | { |
---|
118 | normal = rkNormal; |
---|
119 | d = -rkNormal.dotProduct(rkPoint); |
---|
120 | } |
---|
121 | //----------------------------------------------------------------------- |
---|
122 | Vector3 Plane::projectVector(const Vector3& p) const |
---|
123 | { |
---|
124 | // We know plane normal is unit length, so use simple method |
---|
125 | Matrix3 xform; |
---|
126 | xform[0][0] = 1.0f - normal.x * normal.x; |
---|
127 | xform[0][1] = -normal.x * normal.y; |
---|
128 | xform[0][2] = -normal.x * normal.z; |
---|
129 | xform[1][0] = -normal.y * normal.x; |
---|
130 | xform[1][1] = 1.0f - normal.y * normal.y; |
---|
131 | xform[1][2] = -normal.y * normal.z; |
---|
132 | xform[2][0] = -normal.z * normal.x; |
---|
133 | xform[2][1] = -normal.z * normal.y; |
---|
134 | xform[2][2] = 1.0f - normal.z * normal.z; |
---|
135 | return xform * p; |
---|
136 | |
---|
137 | } |
---|
138 | //----------------------------------------------------------------------- |
---|
139 | Real Plane::normalise(void) |
---|
140 | { |
---|
141 | Real fLength = normal.length(); |
---|
142 | |
---|
143 | // Will also work for zero-sized vectors, but will change nothing |
---|
144 | if (fLength > 1e-08f) |
---|
145 | { |
---|
146 | Real fInvLength = 1.0f / fLength; |
---|
147 | normal *= fInvLength; |
---|
148 | d *= fInvLength; |
---|
149 | } |
---|
150 | |
---|
151 | return fLength; |
---|
152 | } |
---|
153 | //----------------------------------------------------------------------- |
---|
154 | std::ostream& operator<< (std::ostream& o, const Plane& p) |
---|
155 | { |
---|
156 | o << "Plane(normal=" << p.normal << ", d=" << p.d << ")"; |
---|
157 | return o; |
---|
158 | } |
---|
159 | } // namespace Ogre |
---|