DistTriangle3Triangle3.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/DistSegment3Triangle3.h>
  9. namespace WwiseGTE
  10. {
  11. template <typename Real>
  12. class DCPQuery<Real, Triangle3<Real>, Triangle3<Real>>
  13. {
  14. public:
  15. struct Result
  16. {
  17. Real distance, sqrDistance;
  18. Real triangle0Parameter[3], triangle1Parameter[3];
  19. Vector3<Real> closestPoint[2];
  20. };
  21. Result operator()(Triangle3<Real> const& triangle0, Triangle3<Real> const& triangle1)
  22. {
  23. Result result;
  24. DCPQuery<Real, Segment3<Real>, Triangle3<Real>> stQuery;
  25. typename DCPQuery<Real, Segment3<Real>, Triangle3<Real>>::Result
  26. stResult;
  27. result.sqrDistance = std::numeric_limits<Real>::max();
  28. // Compare edges of triangle0 to the interior of triangle1.
  29. for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
  30. {
  31. Vector3<Real> segCenter = (Real)0.5 * (triangle0.v[i0] + triangle0.v[i1]);
  32. Vector3<Real> segDirection = triangle0.v[i1] - triangle0.v[i0];
  33. Real segExtent = (Real)0.5 * Normalize(segDirection);
  34. Segment3<Real> edge(segCenter, segDirection, segExtent);
  35. stResult = stQuery(edge, triangle1);
  36. if (stResult.sqrDistance < result.sqrDistance)
  37. {
  38. result.distance = stResult.distance;
  39. result.sqrDistance = stResult.sqrDistance;
  40. // ratio is in [-1,1]
  41. Real ratio = stResult.segmentParameter / segExtent;
  42. result.triangle0Parameter[i0] = (Real)0.5 * ((Real)1 - ratio);
  43. result.triangle0Parameter[i1] = (Real)1 - result.triangle0Parameter[i0];
  44. result.triangle0Parameter[3 - i0 - i1] = (Real)0;
  45. result.triangle1Parameter[0] = stResult.triangleParameter[0];
  46. result.triangle1Parameter[1] = stResult.triangleParameter[1];
  47. result.triangle1Parameter[2] = stResult.triangleParameter[2];
  48. result.closestPoint[0] = stResult.closestPoint[0];
  49. result.closestPoint[1] = stResult.closestPoint[1];
  50. }
  51. }
  52. // Compare edges of triangle1 to the interior of triangle0.
  53. for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
  54. {
  55. Vector3<Real> segCenter = (Real)0.5 * (triangle1.v[i0] + triangle1.v[i1]);
  56. Vector3<Real> segDirection = triangle1.v[i1] - triangle1.v[i0];
  57. Real segExtent = (Real)0.5 * Normalize(segDirection);
  58. Segment3<Real> edge(segCenter, segDirection, segExtent);
  59. stResult = stQuery(edge, triangle0);
  60. if (stResult.sqrDistance < result.sqrDistance)
  61. {
  62. result.distance = stResult.distance;
  63. result.sqrDistance = stResult.sqrDistance;
  64. Real ratio = stResult.segmentParameter / segExtent; // in [-1,1]
  65. result.triangle0Parameter[0] = stResult.triangleParameter[0];
  66. result.triangle0Parameter[1] = stResult.triangleParameter[1];
  67. result.triangle0Parameter[2] = stResult.triangleParameter[2];
  68. result.triangle1Parameter[i0] = (Real)0.5 * ((Real)1 - ratio);
  69. result.triangle1Parameter[i1] = (Real)1 - result.triangle0Parameter[i0];
  70. result.triangle1Parameter[3 - i0 - i1] = (Real)0;
  71. result.closestPoint[0] = stResult.closestPoint[0];
  72. result.closestPoint[1] = stResult.closestPoint[1];
  73. }
  74. }
  75. return result;
  76. }
  77. };
  78. }