plane3d.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "irrMath.h"
  6. #include "vector3d.h"
  7. namespace irr
  8. {
  9. namespace core
  10. {
  11. //! Enumeration for intersection relations of 3d objects
  12. enum EIntersectionRelation3D
  13. {
  14. ISREL3D_FRONT = 0,
  15. ISREL3D_BACK,
  16. ISREL3D_PLANAR,
  17. ISREL3D_SPANNING,
  18. ISREL3D_CLIPPED
  19. };
  20. //! Template plane class with some intersection testing methods.
  21. /** It has to be ensured, that the normal is always normalized. The constructors
  22. and setters of this class will not ensure this automatically. So any normal
  23. passed in has to be normalized in advance. No change to the normal will be
  24. made by any of the class methods.
  25. */
  26. template <class T>
  27. class plane3d
  28. {
  29. public:
  30. // Constructors
  31. plane3d() :
  32. Normal(0, 1, 0) { recalculateD(vector3d<T>(0, 0, 0)); }
  33. plane3d(const vector3d<T> &MPoint, const vector3d<T> &Normal) :
  34. Normal(Normal) { recalculateD(MPoint); }
  35. plane3d(T px, T py, T pz, T nx, T ny, T nz) :
  36. Normal(nx, ny, nz) { recalculateD(vector3d<T>(px, py, pz)); }
  37. plane3d(const vector3d<T> &point1, const vector3d<T> &point2, const vector3d<T> &point3)
  38. {
  39. setPlane(point1, point2, point3);
  40. }
  41. plane3d(const vector3d<T> &normal, const T d) :
  42. Normal(normal), D(d) {}
  43. // operators
  44. inline bool operator==(const plane3d<T> &other) const { return (equals(D, other.D) && Normal == other.Normal); }
  45. inline bool operator!=(const plane3d<T> &other) const { return !(*this == other); }
  46. // functions
  47. void setPlane(const vector3d<T> &point, const vector3d<T> &nvector)
  48. {
  49. Normal = nvector;
  50. recalculateD(point);
  51. }
  52. void setPlane(const vector3d<T> &nvect, T d)
  53. {
  54. Normal = nvect;
  55. D = d;
  56. }
  57. void setPlane(const vector3d<T> &point1, const vector3d<T> &point2, const vector3d<T> &point3)
  58. {
  59. // creates the plane from 3 memberpoints
  60. Normal = (point2 - point1).crossProduct(point3 - point1);
  61. Normal.normalize();
  62. recalculateD(point1);
  63. }
  64. //! Get an intersection with a 3d line.
  65. /** \param lineVect Vector of the line to intersect with.
  66. \param linePoint Point of the line to intersect with.
  67. \param outIntersection Place to store the intersection point, if there is one.
  68. \return True if there was an intersection, false if there was not.
  69. */
  70. bool getIntersectionWithLine(const vector3d<T> &linePoint,
  71. const vector3d<T> &lineVect,
  72. vector3d<T> &outIntersection) const
  73. {
  74. T t2 = Normal.dotProduct(lineVect);
  75. if (t2 == 0)
  76. return false;
  77. T t = -(Normal.dotProduct(linePoint) + D) / t2;
  78. outIntersection = linePoint + (lineVect * t);
  79. return true;
  80. }
  81. //! Get percentage of line between two points where an intersection with this plane happens.
  82. /** Only useful if known that there is an intersection.
  83. \param linePoint1 Point1 of the line to intersect with.
  84. \param linePoint2 Point2 of the line to intersect with.
  85. \return Where on a line between two points an intersection with this plane happened.
  86. For example, 0.5 is returned if the intersection happened exactly in the middle of the two points.
  87. */
  88. f32 getKnownIntersectionWithLine(const vector3d<T> &linePoint1,
  89. const vector3d<T> &linePoint2) const
  90. {
  91. vector3d<T> vect = linePoint2 - linePoint1;
  92. T t2 = (f32)Normal.dotProduct(vect);
  93. return (f32) - ((Normal.dotProduct(linePoint1) + D) / t2);
  94. }
  95. //! Get an intersection with a 3d line, limited between two 3d points.
  96. /** \param linePoint1 Point 1 of the line.
  97. \param linePoint2 Point 2 of the line.
  98. \param outIntersection Place to store the intersection point, if there is one.
  99. \return True if there was an intersection, false if there was not.
  100. */
  101. bool getIntersectionWithLimitedLine(
  102. const vector3d<T> &linePoint1,
  103. const vector3d<T> &linePoint2,
  104. vector3d<T> &outIntersection) const
  105. {
  106. return (getIntersectionWithLine(linePoint1, linePoint2 - linePoint1, outIntersection) &&
  107. outIntersection.isBetweenPoints(linePoint1, linePoint2));
  108. }
  109. //! Classifies the relation of a point to this plane.
  110. /** \param point Point to classify its relation.
  111. \return ISREL3D_FRONT if the point is in front of the plane,
  112. ISREL3D_BACK if the point is behind of the plane, and
  113. ISREL3D_PLANAR if the point is within the plane. */
  114. EIntersectionRelation3D classifyPointRelation(const vector3d<T> &point) const
  115. {
  116. const T d = Normal.dotProduct(point) + D;
  117. if (d < -ROUNDING_ERROR_f32)
  118. return ISREL3D_BACK;
  119. if (d > ROUNDING_ERROR_f32)
  120. return ISREL3D_FRONT;
  121. return ISREL3D_PLANAR;
  122. }
  123. //! Recalculates the distance from origin by applying a new member point to the plane.
  124. void recalculateD(const vector3d<T> &MPoint)
  125. {
  126. D = -MPoint.dotProduct(Normal);
  127. }
  128. //! Gets a member point of the plane.
  129. vector3d<T> getMemberPoint() const
  130. {
  131. return Normal * -D;
  132. }
  133. //! Tests if there is an intersection with the other plane
  134. /** \return True if there is a intersection. */
  135. bool existsIntersection(const plane3d<T> &other) const
  136. {
  137. vector3d<T> cross = other.Normal.crossProduct(Normal);
  138. return cross.getLength() > core::ROUNDING_ERROR_f32;
  139. }
  140. //! Intersects this plane with another.
  141. /** \param other Other plane to intersect with.
  142. \param outLinePoint Base point of intersection line.
  143. \param outLineVect Vector of intersection.
  144. \return True if there is a intersection, false if not. */
  145. bool getIntersectionWithPlane(const plane3d<T> &other,
  146. vector3d<T> &outLinePoint,
  147. vector3d<T> &outLineVect) const
  148. {
  149. const T fn00 = Normal.getLength();
  150. const T fn01 = Normal.dotProduct(other.Normal);
  151. const T fn11 = other.Normal.getLength();
  152. const f64 det = fn00 * fn11 - fn01 * fn01;
  153. if (fabs(det) < ROUNDING_ERROR_f64)
  154. return false;
  155. const f64 invdet = 1.0 / det;
  156. const f64 fc0 = (fn11 * -D + fn01 * other.D) * invdet;
  157. const f64 fc1 = (fn00 * -other.D + fn01 * D) * invdet;
  158. outLineVect = Normal.crossProduct(other.Normal);
  159. outLinePoint = Normal * (T)fc0 + other.Normal * (T)fc1;
  160. return true;
  161. }
  162. //! Get the intersection point with two other planes if there is one.
  163. bool getIntersectionWithPlanes(const plane3d<T> &o1,
  164. const plane3d<T> &o2, vector3d<T> &outPoint) const
  165. {
  166. vector3d<T> linePoint, lineVect;
  167. if (getIntersectionWithPlane(o1, linePoint, lineVect))
  168. return o2.getIntersectionWithLine(linePoint, lineVect, outPoint);
  169. return false;
  170. }
  171. //! Test if the triangle would be front or backfacing from any point.
  172. /** Thus, this method assumes a camera position from
  173. which the triangle is definitely visible when looking into
  174. the given direction.
  175. Note that this only works if the normal is Normalized.
  176. Do not use this method with points as it will give wrong results!
  177. \param lookDirection: Look direction.
  178. \return True if the plane is front facing and
  179. false if it is backfacing. */
  180. bool isFrontFacing(const vector3d<T> &lookDirection) const
  181. {
  182. const f32 d = Normal.dotProduct(lookDirection);
  183. return F32_LOWER_EQUAL_0(d);
  184. }
  185. //! Get the distance to a point.
  186. /** Note that this only works if the normal is normalized. */
  187. T getDistanceTo(const vector3d<T> &point) const
  188. {
  189. return point.dotProduct(Normal) + D;
  190. }
  191. //! Normal vector of the plane.
  192. vector3d<T> Normal;
  193. //! Distance from origin.
  194. T D;
  195. };
  196. //! Typedef for a f32 3d plane.
  197. typedef plane3d<f32> plane3df;
  198. //! Typedef for an integer 3d plane.
  199. typedef plane3d<s32> plane3di;
  200. } // end namespace core
  201. } // end namespace irr