BsPlane.cpp 3.7 KB

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