DistSegment3Triangle3.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/DistLine3Triangle3.h>
  9. #include <Mathematics/DistPointTriangle.h>
  10. #include <Mathematics/Segment.h>
  11. namespace WwiseGTE
  12. {
  13. template <typename Real>
  14. class DCPQuery<Real, Segment3<Real>, Triangle3<Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. Real segmentParameter, triangleParameter[3];
  21. Vector3<Real> closestPoint[2];
  22. };
  23. Result operator()(Segment3<Real> const& segment, Triangle3<Real> const& triangle)
  24. {
  25. Result result;
  26. Vector3<Real> segCenter, segDirection;
  27. Real segExtent;
  28. segment.GetCenteredForm(segCenter, segDirection, segExtent);
  29. Line3<Real> line(segCenter, segDirection);
  30. DCPQuery<Real, Line3<Real>, Triangle3<Real>> ltQuery;
  31. auto ltResult = ltQuery(line, triangle);
  32. if (ltResult.lineParameter >= -segExtent)
  33. {
  34. if (ltResult.lineParameter <= segExtent)
  35. {
  36. result.distance = ltResult.distance;
  37. result.sqrDistance = ltResult.sqrDistance;
  38. result.segmentParameter = ltResult.lineParameter;
  39. result.triangleParameter[0] = ltResult.triangleParameter[0];
  40. result.triangleParameter[1] = ltResult.triangleParameter[1];
  41. result.triangleParameter[2] = ltResult.triangleParameter[2];
  42. result.closestPoint[0] = ltResult.closestPoint[0];
  43. result.closestPoint[1] = ltResult.closestPoint[1];
  44. }
  45. else
  46. {
  47. DCPQuery<Real, Vector3<Real>, Triangle3<Real>> ptQuery;
  48. Vector3<Real> point = segCenter + segExtent * segDirection;
  49. auto ptResult = ptQuery(point, triangle);
  50. result.sqrDistance = ptResult.sqrDistance;
  51. result.distance = ptResult.distance;
  52. result.segmentParameter = segExtent;
  53. result.closestPoint[0] = point;
  54. result.closestPoint[1] = ptResult.closest;
  55. }
  56. }
  57. else
  58. {
  59. DCPQuery<Real, Vector3<Real>, Triangle3<Real>> ptQuery;
  60. Vector3<Real> point = segCenter - segExtent * segDirection;
  61. auto ptResult = ptQuery(point, triangle);
  62. result.sqrDistance = ptResult.sqrDistance;
  63. result.distance = ptResult.distance;
  64. result.segmentParameter = segExtent;
  65. result.closestPoint[0] = point;
  66. result.closestPoint[1] = ptResult.closest;
  67. }
  68. return result;
  69. }
  70. };
  71. }