2
0

BsRect3.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "BsRect3.h"
  2. #include "BsRay.h"
  3. #include "BsLineSegment3.h"
  4. namespace BansheeEngine
  5. {
  6. Rect3::Rect3()
  7. { }
  8. Rect3::Rect3(const Vector3& center, const std::array<Vector3, 2>& axes,
  9. const std::array<float, 2>& extents)
  10. :mCenter(center), mAxes(axes), mExtents(extents)
  11. {
  12. }
  13. std::pair<std::array<Vector3, 2>, float> Rect3::getNearestPoint(const Ray& ray) const
  14. {
  15. const Vector3& org = ray.getOrigin();
  16. const Vector3& dir = ray.getDirection();
  17. bool foundNearest = false;
  18. float t = 0.0f;
  19. std::array<Vector3, 2> nearestPoints;
  20. float distance = 0.0f;
  21. // Check if Ray intersects the rectangle
  22. auto intersectResult = intersects(ray);
  23. if (intersectResult.first)
  24. {
  25. t = intersectResult.second;
  26. nearestPoints[0] = org + dir * t;
  27. nearestPoints[1] = nearestPoints[0]; // Just one point of intersection
  28. foundNearest = true;
  29. }
  30. // Ray is either passing next to the rectangle or parallel to it,
  31. // compare ray to 4 edges of the rectangle
  32. if (!foundNearest)
  33. {
  34. Vector3 scaledAxes[2];
  35. scaledAxes[0] = mExtents[0] * mAxes[0];
  36. scaledAxes[1] = mExtents[1] * mAxes[1];
  37. distance = std::numeric_limits<float>::max();
  38. for (UINT32 i = 0; i < 2; i++)
  39. {
  40. for (UINT32 j = 0; j < 2; j++)
  41. {
  42. float sign = (float)(2 * j - 1);
  43. Vector3 segCenter = mCenter + sign * scaledAxes[i];
  44. Vector3 segStart = segCenter - scaledAxes[1 - i];
  45. Vector3 segEnd = segCenter + scaledAxes[1 - i];
  46. LineSegment3 segment(segStart, segEnd);
  47. auto segResult = segment.getNearestPoint(ray);
  48. if (segResult.second < distance)
  49. {
  50. nearestPoints = segResult.first;
  51. distance = segResult.second;
  52. }
  53. }
  54. }
  55. }
  56. // Front of the ray is nearest, use found points
  57. if (t >= 0.0f)
  58. {
  59. // Do nothing, we already have the points
  60. }
  61. else // Rectangle is behind the ray origin, find nearest point to origin
  62. {
  63. auto nearestPointToOrg = getNearestPoint(org);
  64. nearestPoints[0] = org;
  65. nearestPoints[1] = nearestPointToOrg.first;
  66. distance = nearestPointToOrg.second;
  67. }
  68. return std::make_pair(nearestPoints, distance);
  69. }
  70. std::pair<Vector3, float> Rect3::getNearestPoint(const Vector3& point) const
  71. {
  72. Vector3 diff = mCenter - point;
  73. float b0 = diff.dot(mAxes[0]);
  74. float b1 = diff.dot(mAxes[1]);
  75. float s0 = -b0, s1 = -b1;
  76. float sqrDistance = diff.dot(diff);
  77. if (s0 < -mExtents[0])
  78. s0 = -mExtents[0];
  79. else if (s0 > mExtents[0])
  80. s0 = mExtents[0];
  81. sqrDistance += s0*(s0 + 2.0f*b0);
  82. if (s1 < -mExtents[1])
  83. s1 = -mExtents[1];
  84. else if (s1 > mExtents[1])
  85. s1 = mExtents[1];
  86. sqrDistance += s1*(s1 + 2.0f*b1);
  87. if (sqrDistance < 0.0f)
  88. sqrDistance = 0.0f;
  89. float dist = std::sqrt(sqrDistance);
  90. Vector3 nearestPoint = mCenter + s0 * mAxes[0] + s1 * mAxes[1];
  91. return std::make_pair(nearestPoint, dist);
  92. }
  93. std::pair<bool, float> Rect3::intersects(const Ray& ray) const
  94. {
  95. const Vector3& org = ray.getOrigin();
  96. const Vector3& dir = ray.getDirection();
  97. Vector3 normal = mAxes[0].cross(mAxes[1]);
  98. float NdotD = normal.dot(ray.getDirection());
  99. if (fabs(NdotD) > 0.0f)
  100. {
  101. Vector3 diff = ray.getOrigin() - mCenter;
  102. Vector3 basis[3];
  103. basis[0] = ray.getDirection();
  104. basis[0].orthogonalComplement(basis[1], basis[2]);
  105. float UdD0 = basis[1].dot(mAxes[0]);
  106. float UdD1 = basis[1].dot(mAxes[1]);
  107. float UdPmC = basis[1].dot(diff);
  108. float VdD0 = basis[2].dot(mAxes[0]);
  109. float VdD1 = basis[2].dot(mAxes[1]);
  110. float VdPmC = basis[2].dot(diff);
  111. float invDet = 1.0f / (UdD0*VdD1 - UdD1*VdD0);
  112. float s0 = (VdD1*UdPmC - UdD1*VdPmC)*invDet;
  113. float s1 = (UdD0*VdPmC - VdD0*UdPmC)*invDet;
  114. if (fabs(s0) <= mExtents[0] && fabs(s1) <= mExtents[1])
  115. {
  116. float DdD0 = dir.dot(mAxes[0]);
  117. float DdD1 = dir.dot(mAxes[1]);
  118. float DdDiff = dir.dot(diff);
  119. float t = s0 * DdD0 + s1 * DdD1 - DdDiff;
  120. return std::make_pair(true, t);
  121. }
  122. }
  123. return std::make_pair(false, 0.0f);
  124. }
  125. }