ImathPlane.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #ifndef INCLUDED_IMATHPLANE_H
  35. #define INCLUDED_IMATHPLANE_H
  36. //----------------------------------------------------------------------
  37. //
  38. // template class Plane3
  39. //
  40. // The Imath::Plane3<> class represents a half space, so the
  41. // normal may point either towards or away from origin. The
  42. // plane P can be represented by Imath::Plane3 as either p or -p
  43. // corresponding to the two half-spaces on either side of the
  44. // plane. Any function which computes a distance will return
  45. // either negative or positive values for the distance indicating
  46. // which half-space the point is in. Note that reflection, and
  47. // intersection functions will operate as expected.
  48. //
  49. //----------------------------------------------------------------------
  50. #include "ImathVec.h"
  51. #include "ImathLine.h"
  52. #include "ImathNamespace.h"
  53. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  54. template <class T>
  55. class Plane3
  56. {
  57. public:
  58. Vec3<T> normal;
  59. T distance;
  60. Plane3() {}
  61. Plane3(const Vec3<T> &normal, T distance);
  62. Plane3(const Vec3<T> &point, const Vec3<T> &normal);
  63. Plane3(const Vec3<T> &point1,
  64. const Vec3<T> &point2,
  65. const Vec3<T> &point3);
  66. //----------------------
  67. // Various set methods
  68. //----------------------
  69. void set(const Vec3<T> &normal,
  70. T distance);
  71. void set(const Vec3<T> &point,
  72. const Vec3<T> &normal);
  73. void set(const Vec3<T> &point1,
  74. const Vec3<T> &point2,
  75. const Vec3<T> &point3 );
  76. //----------------------
  77. // Utilities
  78. //----------------------
  79. bool intersect(const Line3<T> &line,
  80. Vec3<T> &intersection) const;
  81. bool intersectT(const Line3<T> &line,
  82. T &parameter) const;
  83. T distanceTo(const Vec3<T> &) const;
  84. Vec3<T> reflectPoint(const Vec3<T> &) const;
  85. Vec3<T> reflectVector(const Vec3<T> &) const;
  86. };
  87. //--------------------
  88. // Convenient typedefs
  89. //--------------------
  90. typedef Plane3<float> Plane3f;
  91. typedef Plane3<double> Plane3d;
  92. //---------------
  93. // Implementation
  94. //---------------
  95. template <class T>
  96. inline Plane3<T>::Plane3(const Vec3<T> &p0,
  97. const Vec3<T> &p1,
  98. const Vec3<T> &p2)
  99. {
  100. set(p0,p1,p2);
  101. }
  102. template <class T>
  103. inline Plane3<T>::Plane3(const Vec3<T> &n, T d)
  104. {
  105. set(n, d);
  106. }
  107. template <class T>
  108. inline Plane3<T>::Plane3(const Vec3<T> &p, const Vec3<T> &n)
  109. {
  110. set(p, n);
  111. }
  112. template <class T>
  113. inline void Plane3<T>::set(const Vec3<T>& point1,
  114. const Vec3<T>& point2,
  115. const Vec3<T>& point3)
  116. {
  117. normal = (point2 - point1) % (point3 - point1);
  118. normal.normalize();
  119. distance = normal ^ point1;
  120. }
  121. template <class T>
  122. inline void Plane3<T>::set(const Vec3<T>& point, const Vec3<T>& n)
  123. {
  124. normal = n;
  125. normal.normalize();
  126. distance = normal ^ point;
  127. }
  128. template <class T>
  129. inline void Plane3<T>::set(const Vec3<T>& n, T d)
  130. {
  131. normal = n;
  132. normal.normalize();
  133. distance = d;
  134. }
  135. template <class T>
  136. inline T Plane3<T>::distanceTo(const Vec3<T> &point) const
  137. {
  138. return (point ^ normal) - distance;
  139. }
  140. template <class T>
  141. inline Vec3<T> Plane3<T>::reflectPoint(const Vec3<T> &point) const
  142. {
  143. return normal * distanceTo(point) * -2.0 + point;
  144. }
  145. template <class T>
  146. inline Vec3<T> Plane3<T>::reflectVector(const Vec3<T> &v) const
  147. {
  148. return normal * (normal ^ v) * 2.0 - v;
  149. }
  150. template <class T>
  151. inline bool Plane3<T>::intersect(const Line3<T>& line, Vec3<T>& point) const
  152. {
  153. T d = normal ^ line.dir;
  154. if ( d == 0.0 ) return false;
  155. T t = - ((normal ^ line.pos) - distance) / d;
  156. point = line(t);
  157. return true;
  158. }
  159. template <class T>
  160. inline bool Plane3<T>::intersectT(const Line3<T>& line, T &t) const
  161. {
  162. T d = normal ^ line.dir;
  163. if ( d == 0.0 ) return false;
  164. t = - ((normal ^ line.pos) - distance) / d;
  165. return true;
  166. }
  167. template<class T>
  168. std::ostream &operator<< (std::ostream &o, const Plane3<T> &plane)
  169. {
  170. return o << "(" << plane.normal << ", " << plane.distance
  171. << ")";
  172. }
  173. template<class T>
  174. Plane3<T> operator* (const Plane3<T> &plane, const Matrix44<T> &M)
  175. {
  176. // T
  177. // -1
  178. // Could also compute M but that would suck.
  179. //
  180. Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
  181. T dir1Len = dir1 ^ dir1;
  182. Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
  183. T tmpLen = tmp ^ tmp;
  184. if (tmpLen > dir1Len)
  185. {
  186. dir1 = tmp;
  187. dir1Len = tmpLen;
  188. }
  189. tmp = Vec3<T> (0, 0, 1) % plane.normal;
  190. tmpLen = tmp ^ tmp;
  191. if (tmpLen > dir1Len)
  192. {
  193. dir1 = tmp;
  194. }
  195. Vec3<T> dir2 = dir1 % plane.normal;
  196. Vec3<T> point = plane.distance * plane.normal;
  197. return Plane3<T> ( point * M,
  198. (point + dir2) * M,
  199. (point + dir1) * M );
  200. }
  201. template<class T>
  202. Plane3<T> operator- (const Plane3<T> &plane)
  203. {
  204. return Plane3<T>(-plane.normal,-plane.distance);
  205. }
  206. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  207. #endif // INCLUDED_IMATHPLANE_H