2
0

BsPlane.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Math/BsPlane.h"
  4. #include "Math/BsMatrix3.h"
  5. #include "Math/BsAABox.h"
  6. #include "Math/BsSphere.h"
  7. #include "Math/BsRay.h"
  8. #include "Math/BsMath.h"
  9. namespace bs
  10. {
  11. Plane::Plane()
  12. :normal(BsZero), d(0.0f)
  13. { }
  14. Plane::Plane(const Plane& copy)
  15. :normal(copy.normal), d(copy.d)
  16. { }
  17. Plane::Plane(const Vector3& normal, float d)
  18. :normal(normal), d(d)
  19. { }
  20. Plane::Plane(float a, float b, float c, float _d)
  21. :normal(a, b, c), d(_d)
  22. { }
  23. Plane::Plane(const Vector3& normal, const Vector3& point)
  24. :normal(normal), d(normal.dot(point))
  25. { }
  26. Plane::Plane(const Vector3& point0, const Vector3& point1, const Vector3& point2)
  27. {
  28. Vector3 kEdge1 = point1 - point0;
  29. Vector3 kEdge2 = point2 - point0;
  30. normal = kEdge1.cross(kEdge2);
  31. normal.normalize();
  32. d = normal.dot(point0);
  33. }
  34. float Plane::getDistance(const Vector3& point) const
  35. {
  36. return normal.dot(point) - d;
  37. }
  38. Plane::Side Plane::getSide(const Vector3& point) const
  39. {
  40. float dist = getDistance(point);
  41. if (dist < 0.0f)
  42. return Plane::NEGATIVE_SIDE;
  43. if (dist > 0.0f)
  44. return Plane::POSITIVE_SIDE;
  45. return Plane::NO_SIDE;
  46. }
  47. Plane::Side Plane::getSide(const AABox& box) const
  48. {
  49. // Calculate the distance between box centre and the plane
  50. float dist = getDistance(box.getCenter());
  51. // Calculate the maximize allows absolute distance for
  52. // the distance between box centre and plane
  53. Vector3 halfSize = box.getHalfSize();
  54. float maxAbsDist = Math::abs(normal.x * halfSize.x) + Math::abs(normal.y * halfSize.y) + Math::abs(normal.z * halfSize.z);
  55. if (dist < -maxAbsDist)
  56. return Plane::NEGATIVE_SIDE;
  57. if (dist > +maxAbsDist)
  58. return Plane::POSITIVE_SIDE;
  59. return Plane::BOTH_SIDE;
  60. }
  61. Plane::Side Plane::getSide(const Sphere& sphere) const
  62. {
  63. // Calculate the distance between box centre and the plane
  64. float dist = getDistance(sphere.getCenter());
  65. float radius = sphere.getRadius();
  66. if (dist < -radius)
  67. return Plane::NEGATIVE_SIDE;
  68. if (dist > +radius)
  69. return Plane::POSITIVE_SIDE;
  70. return Plane::BOTH_SIDE;
  71. }
  72. Vector3 Plane::projectVector(const Vector3& point) const
  73. {
  74. // We know plane normal is unit length, so use simple method
  75. Matrix3 xform;
  76. xform[0][0] = 1.0f - normal.x * normal.x;
  77. xform[0][1] = -normal.x * normal.y;
  78. xform[0][2] = -normal.x * normal.z;
  79. xform[1][0] = -normal.y * normal.x;
  80. xform[1][1] = 1.0f - normal.y * normal.y;
  81. xform[1][2] = -normal.y * normal.z;
  82. xform[2][0] = -normal.z * normal.x;
  83. xform[2][1] = -normal.z * normal.y;
  84. xform[2][2] = 1.0f - normal.z * normal.z;
  85. return xform.transform(point);
  86. }
  87. float Plane::normalize()
  88. {
  89. float fLength = normal.length();
  90. // Will also work for zero-sized vectors, but will change nothing
  91. if (fLength > 1e-08f)
  92. {
  93. float fInvLength = 1.0f / fLength;
  94. normal *= fInvLength;
  95. d *= fInvLength;
  96. }
  97. return fLength;
  98. }
  99. bool Plane::intersects(const AABox& box) const
  100. {
  101. return box.intersects(*this);
  102. }
  103. bool Plane::intersects(const Sphere& sphere) const
  104. {
  105. return sphere.intersects(*this);
  106. }
  107. std::pair<bool, float> Plane::intersects(const Ray& ray) const
  108. {
  109. float denom = normal.dot(ray.getDirection());
  110. if (Math::abs(denom) < std::numeric_limits<float>::epsilon())
  111. {
  112. // Parallel
  113. return std::pair<bool, float>(false, 0.0f);
  114. }
  115. else
  116. {
  117. float nom = normal.dot(ray.getOrigin()) - d;
  118. float t = -(nom/denom);
  119. return std::pair<bool, float>(t >= 0.0f, t);
  120. }
  121. }
  122. }