1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
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
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.
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.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHPLANE_H
38 #define INCLUDED_IMATHPLANE_H
40 //----------------------------------------------------------------------
42 // template class Plane3
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.
53 //----------------------------------------------------------------------
56 #include "ImathLine.h"
70 Plane3(const Vec3<T> &normal, T distance);
71 Plane3(const Vec3<T> &point, const Vec3<T> &normal);
72 Plane3(const Vec3<T> &point1,
73 const Vec3<T> &point2,
74 const Vec3<T> &point3);
76 //----------------------
77 // Various set methods
78 //----------------------
80 void set(const Vec3<T> &normal,
83 void set(const Vec3<T> &point,
84 const Vec3<T> &normal);
86 void set(const Vec3<T> &point1,
87 const Vec3<T> &point2,
88 const Vec3<T> &point3 );
90 //----------------------
92 //----------------------
94 bool intersect(const Line3<T> &line,
95 Vec3<T> &intersection) const;
97 bool intersectT(const Line3<T> &line,
100 T distanceTo(const Vec3<T> &) const;
102 Vec3<T> reflectPoint(const Vec3<T> &) const;
103 Vec3<T> reflectVector(const Vec3<T> &) const;
107 //--------------------
108 // Convenient typedefs
109 //--------------------
111 typedef Plane3<float> Plane3f;
112 typedef Plane3<double> Plane3d;
120 inline Plane3<T>::Plane3(const Vec3<T> &p0,
128 inline Plane3<T>::Plane3(const Vec3<T> &n, T d)
134 inline Plane3<T>::Plane3(const Vec3<T> &p, const Vec3<T> &n)
140 inline void Plane3<T>::set(const Vec3<T>& point1,
141 const Vec3<T>& point2,
142 const Vec3<T>& point3)
144 normal = (point2 - point1) % (point3 - point1);
146 distance = normal ^ point1;
150 inline void Plane3<T>::set(const Vec3<T>& point, const Vec3<T>& n)
154 distance = normal ^ point;
158 inline void Plane3<T>::set(const Vec3<T>& n, T d)
166 inline T Plane3<T>::distanceTo(const Vec3<T> &point) const
168 return (point ^ normal) - distance;
172 inline Vec3<T> Plane3<T>::reflectPoint(const Vec3<T> &point) const
174 return normal * distanceTo(point) * -2.0 + point;
179 inline Vec3<T> Plane3<T>::reflectVector(const Vec3<T> &v) const
181 return normal * (normal ^ v) * 2.0 - v;
186 inline bool Plane3<T>::intersect(const Line3<T>& line, Vec3<T>& point) const
188 T d = normal ^ line.dir;
189 if ( d == 0.0 ) return false;
190 T t = - ((normal ^ line.pos) - distance) / d;
196 inline bool Plane3<T>::intersectT(const Line3<T>& line, T &t) const
198 T d = normal ^ line.dir;
199 if ( d == 0.0 ) return false;
200 t = - ((normal ^ line.pos) - distance) / d;
205 std::ostream &operator<< (std::ostream &o, const Plane3<T> &plane)
207 return o << "(" << plane.normal << ", " << plane.distance
212 Plane3<T> operator* (const Plane3<T> &plane, const Matrix44<T> &M)
216 // Could also compute M but that would suck.
219 Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
220 T dir1Len = dir1 ^ dir1;
222 Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
223 T tmpLen = tmp ^ tmp;
225 if (tmpLen > dir1Len)
231 tmp = Vec3<T> (0, 0, 1) % plane.normal;
234 if (tmpLen > dir1Len)
239 Vec3<T> dir2 = dir1 % plane.normal;
240 Vec3<T> point = plane.distance * plane.normal;
242 return Plane3<T> ( point * M,
244 (point + dir1) * M );
248 Plane3<T> operator- (const Plane3<T> &plane)
250 return Plane3<T>(-plane.normal,-plane.distance);