BsPlane.cpp 3.8 KB

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