Plane.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "Base.h"
  2. #include "Plane.h"
  3. #include "Frustum.h"
  4. #include "Ray.h"
  5. #include "BoundingSphere.h"
  6. #include "BoundingBox.h"
  7. namespace gameplay
  8. {
  9. Plane::Plane()
  10. {
  11. }
  12. Plane::Plane(const Vector3& normal, float distance)
  13. {
  14. set(normal, distance);
  15. }
  16. Plane::Plane(const Plane& copy)
  17. {
  18. set(copy);
  19. }
  20. Plane::~Plane()
  21. {
  22. }
  23. const Vector3& Plane::getNormal() const
  24. {
  25. return _normal;
  26. }
  27. void Plane::setNormal(const Vector3& normal)
  28. {
  29. _normal = normal;
  30. normalize();
  31. }
  32. float Plane::getDistance() const
  33. {
  34. return _distance;
  35. }
  36. void Plane::setDistance(float distance)
  37. {
  38. _distance = distance;
  39. }
  40. float Plane::distance(const Vector3& point) const
  41. {
  42. return _normal.x * point.x + _normal.y * point.y + _normal.z * point.z + _distance;
  43. }
  44. void Plane::intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3* point)
  45. {
  46. // The planes' normals must be all normalized (which we guarantee in the Plane class).
  47. // Calculate the determinant of the matrix (i.e | n1 n2 n3 |).
  48. float det = p1._normal.x * (p2._normal.y * p3._normal.z -
  49. p2._normal.z * p3._normal.y) - p2._normal.x *(p1._normal.y * p3._normal.z -
  50. p1._normal.z * p3._normal.y) + p3._normal.x * (p1._normal.y * p2._normal.z - p1._normal.z * p2._normal.y);
  51. // If the determinant is zero, then the planes do not all intersect.
  52. if (det == 0.0f)
  53. return;
  54. // Create 3 points, one on each plane.
  55. // (We just pick the point on the plane directly along its normal from the origin).
  56. float p1x = -p1._normal.x * p1._distance;
  57. float p1y = -p1._normal.y * p1._distance;
  58. float p1z = -p1._normal.z * p1._distance;
  59. float p2x = -p2._normal.x * p2._distance;
  60. float p2y = -p2._normal.y * p2._distance;
  61. float p2z = -p2._normal.z * p2._distance;
  62. float p3x = -p3._normal.x * p3._distance;
  63. float p3y = -p3._normal.y * p3._distance;
  64. float p3z = -p3._normal.z * p3._distance;
  65. // Calculate the cross products of the normals.
  66. float c1x = (p2._normal.y * p3._normal.z) - (p2._normal.z * p3._normal.y);
  67. float c1y = (p2._normal.z * p3._normal.x) - (p2._normal.x * p3._normal.z);
  68. float c1z = (p2._normal.x * p3._normal.y) - (p2._normal.y * p3._normal.x);
  69. float c2x = (p3._normal.y * p1._normal.z) - (p3._normal.z * p1._normal.y);
  70. float c2y = (p3._normal.z * p1._normal.x) - (p3._normal.x * p1._normal.z);
  71. float c2z = (p3._normal.x * p1._normal.y) - (p3._normal.y * p1._normal.x);
  72. float c3x = (p1._normal.y * p2._normal.z) - (p1._normal.z * p2._normal.y);
  73. float c3y = (p1._normal.z * p2._normal.x) - (p1._normal.x * p2._normal.z);
  74. float c3z = (p1._normal.x * p2._normal.y) - (p1._normal.y * p2._normal.x);
  75. // Calculate the point of intersection using the formula:
  76. // x = (| n1 n2 n3 |)^-1 * [(x1 * n1)(n2 x n3) + (x2 * n2)(n3 x n1) + (x3 * n3)(n1 x n2)]
  77. float s1 = p1x * p1._normal.x + p1y * p1._normal.y + p1z * p1._normal.z;
  78. float s2 = p2x * p2._normal.x + p2y * p2._normal.y + p2z * p2._normal.z;
  79. float s3 = p3x * p3._normal.x + p3y * p3._normal.y + p3z * p3._normal.z;
  80. float detI = 1.0f / det;
  81. point->x = (s1 * c1x + s2 * c2x + s3 * c3x) * detI;
  82. point->y = (s1 * c1y + s2 * c2y + s3 * c3y) * detI;
  83. point->z = (s1 * c1z + s2 * c2z + s3 * c3z) * detI;
  84. }
  85. float Plane::intersects(const BoundingSphere& sphere) const
  86. {
  87. return sphere.intersects(*this);
  88. }
  89. float Plane::intersects(const BoundingBox& box) const
  90. {
  91. return box.intersects(*this);
  92. }
  93. float Plane::intersects(const Frustum& frustum) const
  94. {
  95. // Get the corners of the frustum.
  96. Vector3 corners[8];
  97. frustum.getCorners(corners);
  98. // Calculate the distances from all of the corners to the plane.
  99. // If all of the distances are positive, then the frustum is in the
  100. // positive half-space of this plane; if all the distances are negative,
  101. // then the frustum is in the negative half-space of this plane; if some of
  102. // the distances are positive and some are negative, the frustum intersects.
  103. float d = distance(corners[0]);
  104. if (d > 0.0f)
  105. {
  106. if (distance(corners[1]) <= 0.0f ||
  107. distance(corners[2]) <= 0.0f ||
  108. distance(corners[3]) <= 0.0f ||
  109. distance(corners[4]) <= 0.0f ||
  110. distance(corners[5]) <= 0.0f ||
  111. distance(corners[6]) <= 0.0f ||
  112. distance(corners[7]) <= 0.0f)
  113. {
  114. return Plane::INTERSECTS_INTERSECTING;
  115. }
  116. return Plane::INTERSECTS_FRONT;
  117. }
  118. else if (d < 0.0f)
  119. {
  120. if (distance(corners[1]) >= 0.0f ||
  121. distance(corners[2]) >= 0.0f ||
  122. distance(corners[3]) >= 0.0f ||
  123. distance(corners[4]) >= 0.0f ||
  124. distance(corners[5]) >= 0.0f ||
  125. distance(corners[6]) >= 0.0f ||
  126. distance(corners[7]) >= 0.0f)
  127. {
  128. return Plane::INTERSECTS_INTERSECTING;
  129. }
  130. return Plane::INTERSECTS_BACK;
  131. }
  132. else
  133. {
  134. return Plane::INTERSECTS_INTERSECTING;
  135. }
  136. }
  137. float Plane::intersects(const Plane& plane) const
  138. {
  139. // Check if the planes intersect.
  140. if (!isParallel(plane))
  141. {
  142. return Plane::INTERSECTS_INTERSECTING;
  143. }
  144. // Calculate the point where the given plane's normal vector intersects the given plane.
  145. Vector3 point(plane._normal.x * -plane._distance, plane._normal.y * -plane._distance, plane._normal.z * -plane._distance);
  146. // Calculate whether the given plane is in the positive or negative half-space of this plane
  147. // (corresponds directly to the sign of the distance from the point calculated above to this plane).
  148. if (distance(point) > 0.0f)
  149. {
  150. return Plane::INTERSECTS_FRONT;
  151. }
  152. else
  153. {
  154. return Plane::INTERSECTS_BACK;
  155. }
  156. }
  157. float Plane::intersects(const Ray& ray) const
  158. {
  159. // Calculate the distance from the ray's origin to the plane.
  160. float d = distance(ray.getOrigin());
  161. // If the origin of the ray lies in the plane, then it intersects.
  162. if (d == 0.0f)
  163. {
  164. return Plane::INTERSECTS_INTERSECTING;
  165. }
  166. else
  167. {
  168. Vector3 rayDirection = ray.getDirection();
  169. // If the dot product of this plane's normal and the ray's direction is positive, and
  170. // if the distance from this plane to the ray's origin is negative -> intersection, OR
  171. // if the dot product of this plane's normal and the ray's direction is negative, and
  172. // if the distance from this plane to the ray's origin is positive -> intersection.
  173. if (_normal.x * rayDirection.x + _normal.y * rayDirection.y + _normal.z * rayDirection.z > 0.0f)
  174. {
  175. if (d < 0.0f)
  176. {
  177. return Plane::INTERSECTS_INTERSECTING;
  178. }
  179. else
  180. {
  181. return Plane::INTERSECTS_FRONT;
  182. }
  183. }
  184. else
  185. {
  186. if (d > 0.0f)
  187. {
  188. return Plane::INTERSECTS_INTERSECTING;
  189. }
  190. else
  191. {
  192. return Plane::INTERSECTS_BACK;
  193. }
  194. }
  195. }
  196. }
  197. bool Plane::isParallel(const Plane& plane) const
  198. {
  199. return (_normal.y * plane._normal.z) - (_normal.z * plane._normal.y) == 0.0f &&
  200. (_normal.z * plane._normal.x) - (_normal.x * plane._normal.z) == 0.0f &&
  201. (_normal.x * plane._normal.y) - (_normal.y * plane._normal.x) == 0.0f;
  202. }
  203. void Plane::set(const Vector3& normal, float distance)
  204. {
  205. _normal = normal;
  206. _distance = distance;
  207. normalize();
  208. }
  209. void Plane::set(const Plane& plane)
  210. {
  211. _normal = plane._normal;
  212. _distance = plane._distance;
  213. }
  214. void Plane::transform(const Matrix& matrix)
  215. {
  216. Matrix inverted;
  217. if (matrix.invert(&inverted))
  218. {
  219. // Treat the plane as a four-tuple and multiply by the inverse transpose of the matrix to get the transformed plane.
  220. // Then we normalize the plane by dividing both the normal and the distance by the length of the normal.
  221. float nx = _normal.x * inverted.m[0] + _normal.y * inverted.m[1] + _normal.z * inverted.m[2] + _distance * inverted.m[3];
  222. float ny = _normal.x * inverted.m[4] + _normal.y * inverted.m[5] + _normal.z * inverted.m[6] + _distance * inverted.m[7];
  223. float nz = _normal.x * inverted.m[8] + _normal.y * inverted.m[9] + _normal.z * inverted.m[10] + _distance * inverted.m[11];
  224. float d = _normal.x * inverted.m[12]+ _normal.y * inverted.m[13] + _normal.z * inverted.m[14] + _distance * inverted.m[15];
  225. float factor = 1.0f / sqrt(nx * nx + ny * ny + nz * nz);
  226. _normal.x = nx * factor;
  227. _normal.y = ny * factor;
  228. _normal.z = nz * factor;
  229. _distance = d * factor;
  230. }
  231. }
  232. void Plane::normalize()
  233. {
  234. if (_normal.isZero())
  235. return;
  236. // Normalize the plane's normal.
  237. float normalizeFactor = 1.0f / sqrt(_normal.x * _normal.x + _normal.y * _normal.y + _normal.z * _normal.z);
  238. if (normalizeFactor != 1.0f)
  239. {
  240. _normal.x *= normalizeFactor;
  241. _normal.y *= normalizeFactor;
  242. _normal.z *= normalizeFactor;
  243. _distance *= normalizeFactor;
  244. }
  245. }
  246. }