BsRect3.cpp 4.2 KB

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