BsPlane.cpp 3.1 KB

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