BsRect3.cpp 4.0 KB

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