Plane.cpp 9.4 KB

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