BsRect3.cpp 3.9 KB

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