DistLine3Triangle3.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/DistLineSegment.h>
  9. #include <Mathematics/Triangle.h>
  10. #include <Mathematics/Vector3.h>
  11. namespace WwiseGTE
  12. {
  13. template <typename Real>
  14. class DCPQuery<Real, Line3<Real>, Triangle3<Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. Real lineParameter, triangleParameter[3];
  21. Vector3<Real> closestPoint[2];
  22. };
  23. Result operator()(Line3<Real> const& line, Triangle3<Real> const& triangle)
  24. {
  25. Result result;
  26. // Test if line intersects triangle. If so, the squared distance
  27. // is zero.
  28. Vector3<Real> edge0 = triangle.v[1] - triangle.v[0];
  29. Vector3<Real> edge1 = triangle.v[2] - triangle.v[0];
  30. Vector3<Real> normal = UnitCross(edge0, edge1);
  31. Real NdD = Dot(normal, line.direction);
  32. if (std::fabs(NdD) > (Real)0)
  33. {
  34. // The line and triangle are not parallel, so the line
  35. // intersects/ the plane of the triangle.
  36. Vector3<Real> diff = line.origin - triangle.v[0];
  37. Vector3<Real> basis[3]; // {D, U, V}
  38. basis[0] = line.direction;
  39. ComputeOrthogonalComplement<Real>(1, basis);
  40. Real UdE0 = Dot(basis[1], edge0);
  41. Real UdE1 = Dot(basis[1], edge1);
  42. Real UdDiff = Dot(basis[1], diff);
  43. Real VdE0 = Dot(basis[2], edge0);
  44. Real VdE1 = Dot(basis[2], edge1);
  45. Real VdDiff = Dot(basis[2], diff);
  46. Real invDet = ((Real)1) / (UdE0 * VdE1 - UdE1 * VdE0);
  47. // Barycentric coordinates for the point of intersection.
  48. Real b1 = (VdE1 * UdDiff - UdE1 * VdDiff) * invDet;
  49. Real b2 = (UdE0 * VdDiff - VdE0 * UdDiff) * invDet;
  50. Real b0 = (Real)1 - b1 - b2;
  51. if (b0 >= (Real)0 && b1 >= (Real)0 && b2 >= (Real)0)
  52. {
  53. // Line parameter for the point of intersection.
  54. Real DdE0 = Dot(line.direction, edge0);
  55. Real DdE1 = Dot(line.direction, edge1);
  56. Real DdDiff = Dot(line.direction, diff);
  57. result.lineParameter = b1 * DdE0 + b2 * DdE1 - DdDiff;
  58. // Barycentric coordinates for the point of intersection.
  59. result.triangleParameter[0] = b0;
  60. result.triangleParameter[1] = b1;
  61. result.triangleParameter[2] = b2;
  62. // The intersection point is inside or on the triangle.
  63. result.closestPoint[0] = line.origin + result.lineParameter * line.direction;
  64. result.closestPoint[1] = triangle.v[0] + b1 * edge0 + b2 * edge1;
  65. result.distance = (Real)0;
  66. result.sqrDistance = (Real)0;
  67. return result;
  68. }
  69. }
  70. // Either (1) the line is not parallel to the triangle and the
  71. // point of intersection of the line and the plane of the triangle
  72. // is outside the triangle or (2) the line and triangle are
  73. // parallel. Regardless, the closest point on the triangle is on
  74. // an edge of the triangle. Compare the line to all three edges
  75. // of the triangle.
  76. result.distance = std::numeric_limits<Real>::max();
  77. result.sqrDistance = std::numeric_limits<Real>::max();
  78. for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
  79. {
  80. Vector3<Real> segCenter = (Real)0.5 * (triangle.v[i0] + triangle.v[i1]);
  81. Vector3<Real> segDirection = triangle.v[i1] - triangle.v[i0];
  82. Real segExtent = (Real)0.5 * Normalize(segDirection);
  83. Segment3<Real> segment(segCenter, segDirection, segExtent);
  84. DCPQuery<Real, Line3<Real>, Segment3<Real>> query;
  85. auto lsResult = query(line, segment);
  86. if (lsResult.sqrDistance < result.sqrDistance)
  87. {
  88. result.sqrDistance = lsResult.sqrDistance;
  89. result.distance = lsResult.distance;
  90. result.lineParameter = lsResult.parameter[0];
  91. result.triangleParameter[i0] = (Real)0.5 * ((Real)1 -
  92. lsResult.parameter[0] / segExtent);
  93. result.triangleParameter[i1] = (Real)1 -
  94. result.triangleParameter[i0];
  95. result.triangleParameter[3 - i0 - i1] = (Real)0;
  96. result.closestPoint[0] = lsResult.closestPoint[0];
  97. result.closestPoint[1] = lsResult.closestPoint[1];
  98. }
  99. }
  100. return result;
  101. }
  102. };
  103. }