/src/FreeImage/Source/OpenEXR/Imath/ImathPlane.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 256 lines · 143 code · 50 blank · 63 comment · 6 complexity · eb0ce53f69fcc1dc96034ea60140e580 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, 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. namespace Imath {
  53. template <class T>
  54. class Plane3
  55. {
  56. public:
  57. Vec3<T> normal;
  58. T distance;
  59. Plane3() {}
  60. Plane3(const Vec3<T> &normal, T distance);
  61. Plane3(const Vec3<T> &point, const Vec3<T> &normal);
  62. Plane3(const Vec3<T> &point1,
  63. const Vec3<T> &point2,
  64. const Vec3<T> &point3);
  65. //----------------------
  66. // Various set methods
  67. //----------------------
  68. void set(const Vec3<T> &normal,
  69. T distance);
  70. void set(const Vec3<T> &point,
  71. const Vec3<T> &normal);
  72. void set(const Vec3<T> &point1,
  73. const Vec3<T> &point2,
  74. const Vec3<T> &point3 );
  75. //----------------------
  76. // Utilities
  77. //----------------------
  78. bool intersect(const Line3<T> &line,
  79. Vec3<T> &intersection) const;
  80. bool intersectT(const Line3<T> &line,
  81. T &parameter) const;
  82. T distanceTo(const Vec3<T> &) const;
  83. Vec3<T> reflectPoint(const Vec3<T> &) const;
  84. Vec3<T> reflectVector(const Vec3<T> &) const;
  85. };
  86. //--------------------
  87. // Convenient typedefs
  88. //--------------------
  89. typedef Plane3<float> Plane3f;
  90. typedef Plane3<double> Plane3d;
  91. //---------------
  92. // Implementation
  93. //---------------
  94. template <class T>
  95. inline Plane3<T>::Plane3(const Vec3<T> &p0,
  96. const Vec3<T> &p1,
  97. const Vec3<T> &p2)
  98. {
  99. set(p0,p1,p2);
  100. }
  101. template <class T>
  102. inline Plane3<T>::Plane3(const Vec3<T> &n, T d)
  103. {
  104. set(n, d);
  105. }
  106. template <class T>
  107. inline Plane3<T>::Plane3(const Vec3<T> &p, const Vec3<T> &n)
  108. {
  109. set(p, n);
  110. }
  111. template <class T>
  112. inline void Plane3<T>::set(const Vec3<T>& point1,
  113. const Vec3<T>& point2,
  114. const Vec3<T>& point3)
  115. {
  116. normal = (point2 - point1) % (point3 - point1);
  117. normal.normalize();
  118. distance = normal ^ point1;
  119. }
  120. template <class T>
  121. inline void Plane3<T>::set(const Vec3<T>& point, const Vec3<T>& n)
  122. {
  123. normal = n;
  124. normal.normalize();
  125. distance = normal ^ point;
  126. }
  127. template <class T>
  128. inline void Plane3<T>::set(const Vec3<T>& n, T d)
  129. {
  130. normal = n;
  131. normal.normalize();
  132. distance = d;
  133. }
  134. template <class T>
  135. inline T Plane3<T>::distanceTo(const Vec3<T> &point) const
  136. {
  137. return (point ^ normal) - distance;
  138. }
  139. template <class T>
  140. inline Vec3<T> Plane3<T>::reflectPoint(const Vec3<T> &point) const
  141. {
  142. return normal * distanceTo(point) * -2.0 + point;
  143. }
  144. template <class T>
  145. inline Vec3<T> Plane3<T>::reflectVector(const Vec3<T> &v) const
  146. {
  147. return normal * (normal ^ v) * 2.0 - v;
  148. }
  149. template <class T>
  150. inline bool Plane3<T>::intersect(const Line3<T>& line, Vec3<T>& point) const
  151. {
  152. T d = normal ^ line.dir;
  153. if ( d == 0.0 ) return false;
  154. T t = - ((normal ^ line.pos) - distance) / d;
  155. point = line(t);
  156. return true;
  157. }
  158. template <class T>
  159. inline bool Plane3<T>::intersectT(const Line3<T>& line, T &t) const
  160. {
  161. T d = normal ^ line.dir;
  162. if ( d == 0.0 ) return false;
  163. t = - ((normal ^ line.pos) - distance) / d;
  164. return true;
  165. }
  166. template<class T>
  167. std::ostream &operator<< (std::ostream &o, const Plane3<T> &plane)
  168. {
  169. return o << "(" << plane.normal << ", " << plane.distance
  170. << ")";
  171. }
  172. template<class T>
  173. Plane3<T> operator* (const Plane3<T> &plane, const Matrix44<T> &M)
  174. {
  175. // T
  176. // -1
  177. // Could also compute M but that would suck.
  178. //
  179. Vec3<T> dir1 = Vec3<T> (1, 0, 0) % plane.normal;
  180. T dir1Len = dir1 ^ dir1;
  181. Vec3<T> tmp = Vec3<T> (0, 1, 0) % plane.normal;
  182. T tmpLen = tmp ^ tmp;
  183. if (tmpLen > dir1Len)
  184. {
  185. dir1 = tmp;
  186. dir1Len = tmpLen;
  187. }
  188. tmp = Vec3<T> (0, 0, 1) % plane.normal;
  189. tmpLen = tmp ^ tmp;
  190. if (tmpLen > dir1Len)
  191. {
  192. dir1 = tmp;
  193. }
  194. Vec3<T> dir2 = dir1 % plane.normal;
  195. Vec3<T> point = plane.distance * plane.normal;
  196. return Plane3<T> ( point * M,
  197. (point + dir2) * M,
  198. (point + dir1) * M );
  199. }
  200. template<class T>
  201. Plane3<T> operator- (const Plane3<T> &plane)
  202. {
  203. return Plane3<T>(-plane.normal,-plane.distance);
  204. }
  205. } // namespace Imath
  206. #endif