HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathPlane.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMATHPLANE_H
38 #define INCLUDED_IMATHPLANE_H
39 
40 //----------------------------------------------------------------------
41 //
42 // template class Plane3
43 //
44 // The Imath::Plane3<> class represents a half space, so the
45 // normal may point either towards or away from origin. The
46 // plane P can be represented by Imath::Plane3 as either p or -p
47 // corresponding to the two half-spaces on either side of the
48 // plane. Any function which computes a distance will return
49 // either negative or positive values for the distance indicating
50 // which half-space the point is in. Note that reflection, and
51 // intersection functions will operate as expected.
52 //
53 //----------------------------------------------------------------------
54 
55 #include "ImathVec.h"
56 #include "ImathLine.h"
57 #include "ImathNamespace.h"
58 
60 
61 
62 template <class T>
63 class Plane3
64 {
65  public:
66 
69 
70  Plane3() {}
71  Plane3(const Vec3<T> &normal, T distance);
72  Plane3(const Vec3<T> &point, const Vec3<T> &normal);
73  Plane3(const Vec3<T> &point1,
74  const Vec3<T> &point2,
75  const Vec3<T> &point3);
76 
77  //----------------------
78  // Various set methods
79  //----------------------
80 
81  void set(const Vec3<T> &normal,
82  T distance);
83 
84  void set(const Vec3<T> &point,
85  const Vec3<T> &normal);
86 
87  void set(const Vec3<T> &point1,
88  const Vec3<T> &point2,
89  const Vec3<T> &point3 );
90 
91  //----------------------
92  // Utilities
93  //----------------------
94 
95  bool intersect(const Line3<T> &line,
96  Vec3<T> &intersection) const;
97 
98  bool intersectT(const Line3<T> &line,
99  T &parameter) const;
100 
101  T distanceTo(const Vec3<T> &) const;
102 
103  Vec3<T> reflectPoint(const Vec3<T> &) const;
104  Vec3<T> reflectVector(const Vec3<T> &) const;
105 };
106 
107 
108 //--------------------
109 // Convenient typedefs
110 //--------------------
111 
114 
115 
116 //---------------
117 // Implementation
118 //---------------
119 
120 template <class T>
121 inline Plane3<T>::Plane3(const Vec3<T> &p0,
122  const Vec3<T> &p1,
123  const Vec3<T> &p2)
124 {
125  set(p0,p1,p2);
126 }
127 
128 template <class T>
129 inline Plane3<T>::Plane3(const Vec3<T> &n, T d)
130 {
131  set(n, d);
132 }
133 
134 template <class T>
135 inline Plane3<T>::Plane3(const Vec3<T> &p, const Vec3<T> &n)
136 {
137  set(p, n);
138 }
139 
140 template <class T>
141 inline void Plane3<T>::set(const Vec3<T>& point1,
142  const Vec3<T>& point2,
143  const Vec3<T>& point3)
144 {
145  normal = (point2 - point1) % (point3 - point1);
146  normal.normalize();
147  distance = normal ^ point1;
148 }
149 
150 template <class T>
151 inline void Plane3<T>::set(const Vec3<T>& point, const Vec3<T>& n)
152 {
153  normal = n;
154  normal.normalize();
155  distance = normal ^ point;
156 }
157 
158 template <class T>
159 inline void Plane3<T>::set(const Vec3<T>& n, T d)
160 {
161  normal = n;
162  normal.normalize();
163  distance = d;
164 }
165 
166 template <class T>
167 inline T Plane3<T>::distanceTo(const Vec3<T> &point) const
168 {
169  return (point ^ normal) - distance;
170 }
171 
172 template <class T>
173 inline Vec3<T> Plane3<T>::reflectPoint(const Vec3<T> &point) const
174 {
175  return normal * distanceTo(point) * -2.0 + point;
176 }
177 
178 
179 template <class T>
181 {
182  return normal * (normal ^ v) * 2.0 - v;
183 }
184 
185 
186 template <class T>
187 inline bool Plane3<T>::intersect(const Line3<T>& line, Vec3<T>& point) const
188 {
189  T d = normal ^ line.dir;
190  if ( d == 0.0 ) return false;
191  T t = - ((normal ^ line.pos) - distance) / d;
192  point = line(t);
193  return true;
194 }
195 
196 template <class T>
197 inline bool Plane3<T>::intersectT(const Line3<T>& line, T &t) const
198 {
199  T d = normal ^ line.dir;
200  if ( d == 0.0 ) return false;
201  t = - ((normal ^ line.pos) - distance) / d;
202  return true;
203 }
204 
205 template<class T>
206 std::ostream &operator<< (std::ostream &o, const Plane3<T> &plane)
207 {
208  return o << "(" << plane.normal << ", " << plane.distance
209  << ")";
210 }
211 
212 template<class T>
213 Plane3<T> operator* (const Plane3<T> &plane, const Matrix44<T> &M)
214 {
215  // T
216  // -1
217  // Could also compute M but that would suck.
218  //
219 
220  Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
221  T dir1Len = dir1 ^ dir1;
222 
223  Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
224  T tmpLen = tmp ^ tmp;
225 
226  if (tmpLen > dir1Len)
227  {
228  dir1 = tmp;
229  dir1Len = tmpLen;
230  }
231 
232  tmp = Vec3<T> (0, 0, 1) % plane.normal;
233  tmpLen = tmp ^ tmp;
234 
235  if (tmpLen > dir1Len)
236  {
237  dir1 = tmp;
238  }
239 
240  Vec3<T> dir2 = dir1 % plane.normal;
241  Vec3<T> point = plane.distance * plane.normal;
242 
243  return Plane3<T> ( point * M,
244  (point + dir2) * M,
245  (point + dir1) * M );
246 }
247 
248 template<class T>
250 {
251  return Plane3<T>(-plane.normal,-plane.distance);
252 }
253 
254 
256 
257 #endif // INCLUDED_IMATHPLANE_H
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:609
Vec3< T > pos
Definition: ImathLine.h:59
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
const GLdouble * v
Definition: glcorearb.h:836
Plane3()
Definition: ImathPlane.h:70
T distance
Definition: ImathPlane.h:68
GLdouble n
Definition: glcorearb.h:2007
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:635
T distance(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:698
bool intersect(const Line3< T > &line, Vec3< T > &intersection) const
Definition: ImathPlane.h:187
Vec3< T > dir
Definition: ImathLine.h:60
bool intersectT(const Line3< T > &line, T &parameter) const
Definition: ImathPlane.h:197
Plane3< double > Plane3d
Definition: ImathPlane.h:113
Plane3< float > Plane3f
Definition: ImathPlane.h:112
Vec3< T > reflectPoint(const Vec3< T > &) const
Definition: ImathPlane.h:173
Vec3< T > reflectVector(const Vec3< T > &) const
Definition: ImathPlane.h:180
T distanceTo(const Vec3< T > &) const
Definition: ImathPlane.h:167
if(rank==1) return rank
Vec3< T > normal
Definition: ImathPlane.h:67
void set(const Vec3< T > &normal, T distance)
Definition: ImathPlane.h:159