IntrHalfspace3Segment3.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/FIQuery.h>
  9. #include <Mathematics/TIQuery.h>
  10. #include <Mathematics/Vector3.h>
  11. #include <Mathematics/Halfspace.h>
  12. #include <Mathematics/Segment.h>
  13. // Queries for intersection of objects with halfspaces. These are useful for
  14. // containment testing, object culling, and clipping.
  15. namespace WwiseGTE
  16. {
  17. template <typename Real>
  18. class TIQuery<Real, Halfspace3<Real>, Segment3<Real>>
  19. {
  20. public:
  21. struct Result
  22. {
  23. bool intersect;
  24. };
  25. Result operator()(Halfspace3<Real> const& halfspace, Segment3<Real> const& segment)
  26. {
  27. Result result;
  28. // Project the segment endpoints onto the normal line. The plane
  29. // of the halfspace occurs at the origin (zero) of the normal
  30. // line.
  31. Real s[2];
  32. for (int i = 0; i < 2; ++i)
  33. {
  34. s[i] = Dot(halfspace.normal, segment.p[i]) - halfspace.constant;
  35. }
  36. // The segment and halfspace intersect when the projection
  37. // interval maximum is nonnegative.
  38. result.intersect = (std::max(s[0], s[1]) >= (Real)0);
  39. return result;
  40. }
  41. };
  42. template <typename Real>
  43. class FIQuery<Real, Halfspace3<Real>, Segment3<Real>>
  44. {
  45. public:
  46. struct Result
  47. {
  48. bool intersect;
  49. // The segment is clipped against the plane defining the
  50. // halfspace. The 'numPoints' is either 0 (no intersection),
  51. // 1 (point), or 2 (segment).
  52. int numPoints;
  53. Vector3<Real> point[2];
  54. };
  55. Result operator()(Halfspace3<Real> const& halfspace, Segment3<Real> const& segment)
  56. {
  57. // Determine on which side of the plane the endpoints lie. The
  58. // table of possibilities is listed next with n = numNegative,
  59. // p = numPositive, and z = numZero.
  60. //
  61. // n p z intersection
  62. // -------------------------
  63. // 0 2 0 segment (original)
  64. // 0 1 1 segment (original)
  65. // 0 0 2 segment (original)
  66. // 1 1 0 segment (clipped)
  67. // 1 0 1 point (endpoint)
  68. // 2 0 0 none
  69. Real s[2];
  70. int numPositive = 0, numNegative = 0, numZero = 0;
  71. for (int i = 0; i < 2; ++i)
  72. {
  73. s[i] = Dot(halfspace.normal, segment.p[i]) - halfspace.constant;
  74. if (s[i] > (Real)0)
  75. {
  76. ++numPositive;
  77. }
  78. else if (s[i] < (Real)0)
  79. {
  80. ++numNegative;
  81. }
  82. else
  83. {
  84. ++numZero;
  85. }
  86. }
  87. Result result;
  88. if (numNegative == 0)
  89. {
  90. // The segment is in the halfspace.
  91. result.intersect = true;
  92. result.numPoints = 2;
  93. result.point[0] = segment.p[0];
  94. result.point[1] = segment.p[1];
  95. }
  96. else if (numNegative == 1)
  97. {
  98. result.intersect = true;
  99. result.numPoints = 1;
  100. if (numPositive == 1)
  101. {
  102. // The segment is intersected at an interior point.
  103. result.point[0] = segment.p[0] +
  104. (s[0] / (s[0] - s[1])) * (segment.p[1] - segment.p[0]);
  105. }
  106. else // numZero = 1
  107. {
  108. // One segment endpoint is on the plane.
  109. if (s[0] == (Real)0)
  110. {
  111. result.point[0] = segment.p[0];
  112. }
  113. else
  114. {
  115. result.point[0] = segment.p[1];
  116. }
  117. }
  118. }
  119. else // numNegative == 2
  120. {
  121. // The segment is outside the halfspace. (numNegative == 2)
  122. result.intersect = false;
  123. result.numPoints = 0;
  124. }
  125. return result;
  126. }
  127. };
  128. }