DistLineSegment.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/DCPQuery.h>
  9. #include <Mathematics/Line.h>
  10. #include <Mathematics/Segment.h>
  11. namespace WwiseGTE
  12. {
  13. template <int N, typename Real>
  14. class DCPQuery<Real, Line<N, Real>, Segment<N, Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. Real parameter[2];
  21. Vector<N, Real> closestPoint[2];
  22. };
  23. // The centered form of the 'segment' is used. Thus, parameter[1] of
  24. // the result is in [-e,e], where e = |segment.p[1] - segment.p[0]|/2.
  25. Result operator()(Line<N, Real> const& line, Segment<N, Real> const& segment)
  26. {
  27. Result result;
  28. Vector<N, Real> segCenter, segDirection;
  29. Real segExtent;
  30. segment.GetCenteredForm(segCenter, segDirection, segExtent);
  31. Vector<N, Real> diff = line.origin - segCenter;
  32. Real a01 = -Dot(line.direction, segDirection);
  33. Real b0 = Dot(diff, line.direction);
  34. Real s0, s1;
  35. if (std::fabs(a01) < (Real)1)
  36. {
  37. // The line and segment are not parallel.
  38. Real det = (Real)1 - a01 * a01;
  39. Real extDet = segExtent * det;
  40. Real b1 = -Dot(diff, segDirection);
  41. s1 = a01 * b0 - b1;
  42. if (s1 >= -extDet)
  43. {
  44. if (s1 <= extDet)
  45. {
  46. // Two interior points are closest, one on the line
  47. // and one on the segment.
  48. s0 = (a01 * b1 - b0) / det;
  49. s1 /= det;
  50. }
  51. else
  52. {
  53. // The endpoint e1 of the segment and an interior
  54. // point of the line are closest.
  55. s1 = segExtent;
  56. s0 = -(a01 * s1 + b0);
  57. }
  58. }
  59. else
  60. {
  61. // The endpoint e0 of the segment and an interior point
  62. // of the line are closest.
  63. s1 = -segExtent;
  64. s0 = -(a01 * s1 + b0);
  65. }
  66. }
  67. else
  68. {
  69. // The line and segment are parallel. Choose the closest pair
  70. // so that one point is at segment origin.
  71. s1 = (Real)0;
  72. s0 = -b0;
  73. }
  74. result.parameter[0] = s0;
  75. result.parameter[1] = s1;
  76. result.closestPoint[0] = line.origin + s0 * line.direction;
  77. result.closestPoint[1] = segCenter + s1 * segDirection;
  78. diff = result.closestPoint[0] - result.closestPoint[1];
  79. result.sqrDistance = Dot(diff, diff);
  80. result.distance = std::sqrt(result.sqrDistance);
  81. return result;
  82. }
  83. };
  84. // Template aliases for convenience.
  85. template <int N, typename Real>
  86. using DCPLineSegment = DCPQuery<Real, Line<N, Real>, Segment<N, Real>>;
  87. template <typename Real>
  88. using DCPLine2Segment2 = DCPLineSegment<2, Real>;
  89. template <typename Real>
  90. using DCPLine3Segment3 = DCPLineSegment<3, Real>;
  91. }