IntrHalfspace3Triangle3.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/Triangle.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>, Triangle3<Real>>
  19. {
  20. public:
  21. struct Result
  22. {
  23. bool intersect;
  24. };
  25. Result operator()(Halfspace3<Real> const& halfspace, Triangle3<Real> const& triangle)
  26. {
  27. Result result;
  28. // Project the triangle vertices onto the normal line. The plane
  29. // of the halfspace occurs at the origin (zero) of the normal
  30. // line.
  31. Real s[3];
  32. for (int i = 0; i < 3; ++i)
  33. {
  34. s[i] = Dot(halfspace.normal, triangle.v[i]) - halfspace.constant;
  35. }
  36. // The triangle and halfspace intersect when the projection
  37. // interval maximum is nonnegative.
  38. result.intersect = (std::max(std::max(s[0], s[1]), s[2]) >= (Real)0);
  39. return result;
  40. }
  41. };
  42. template <typename Real>
  43. class FIQuery<Real, Halfspace3<Real>, Triangle3<Real>>
  44. {
  45. public:
  46. struct Result
  47. {
  48. bool intersect;
  49. // The triangle is clipped against the plane defining the
  50. // halfspace. The 'numPoints' is either 0 (no intersection),
  51. // 1 (point), 2 (segment), 3 (triangle), or 4 (quadrilateral).
  52. int numPoints;
  53. Vector3<Real> point[4];
  54. };
  55. Result operator()(Halfspace3<Real> const& halfspace, Triangle3<Real> const& triangle)
  56. {
  57. Result result;
  58. // Determine on which side of the plane the vertices lie. The
  59. // table of possibilities is listed next with n = numNegative,
  60. // p = numPositive, and z = numZero.
  61. //
  62. // n p z intersection
  63. // ---------------------------------
  64. // 0 3 0 triangle (original)
  65. // 0 2 1 triangle (original)
  66. // 0 1 2 triangle (original)
  67. // 0 0 3 triangle (original)
  68. // 1 2 0 quad (2 edges clipped)
  69. // 1 1 1 triangle (1 edge clipped)
  70. // 1 0 2 edge
  71. // 2 1 0 triangle (2 edges clipped)
  72. // 2 0 1 vertex
  73. // 3 0 0 none
  74. Real s[3];
  75. int numPositive = 0, numNegative = 0, numZero = 0;
  76. for (int i = 0; i < 3; ++i)
  77. {
  78. s[i] = Dot(halfspace.normal, triangle.v[i]) - halfspace.constant;
  79. if (s[i] > (Real)0)
  80. {
  81. ++numPositive;
  82. }
  83. else if (s[i] < (Real)0)
  84. {
  85. ++numNegative;
  86. }
  87. else
  88. {
  89. ++numZero;
  90. }
  91. }
  92. if (numNegative == 0)
  93. {
  94. // The triangle is in the halfspace.
  95. result.intersect = true;
  96. result.numPoints = 3;
  97. result.point[0] = triangle.v[0];
  98. result.point[1] = triangle.v[1];
  99. result.point[2] = triangle.v[2];
  100. }
  101. else if (numNegative == 1)
  102. {
  103. result.intersect = true;
  104. if (numPositive == 2)
  105. {
  106. // The portion of the triangle in the halfspace is a
  107. // quadrilateral.
  108. result.numPoints = 4;
  109. for (int i0 = 0; i0 < 3; ++i0)
  110. {
  111. if (s[i0] < (Real)0)
  112. {
  113. int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
  114. result.point[0] = triangle.v[i1];
  115. result.point[1] = triangle.v[i2];
  116. Real t2 = s[i2] / (s[i2] - s[i0]);
  117. Real t0 = s[i0] / (s[i0] - s[i1]);
  118. result.point[2] = triangle.v[i2] + t2 *
  119. (triangle.v[i0] - triangle.v[i2]);
  120. result.point[3] = triangle.v[i0] + t0 *
  121. (triangle.v[i1] - triangle.v[i0]);
  122. break;
  123. }
  124. }
  125. }
  126. else if (numPositive == 1)
  127. {
  128. // The portion of the triangle in the halfspace is a
  129. // triangle with one vertex on the plane.
  130. result.numPoints = 3;
  131. for (int i0 = 0; i0 < 3; ++i0)
  132. {
  133. if (s[i0] == (Real)0)
  134. {
  135. int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
  136. result.point[0] = triangle.v[i0];
  137. Real t1 = s[i1] / (s[i1] - s[i2]);
  138. Vector3<Real> p = triangle.v[i1] + t1 *
  139. (triangle.v[i2] - triangle.v[i1]);
  140. if (s[i1] > (Real)0)
  141. {
  142. result.point[1] = triangle.v[i1];
  143. result.point[2] = p;
  144. }
  145. else
  146. {
  147. result.point[1] = p;
  148. result.point[2] = triangle.v[i2];
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. else
  155. {
  156. // Only an edge of the triangle is in the halfspace.
  157. result.numPoints = 0;
  158. for (int i = 0; i < 3; ++i)
  159. {
  160. if (s[i] == (Real)0)
  161. {
  162. result.point[result.numPoints++] = triangle.v[i];
  163. }
  164. }
  165. }
  166. }
  167. else if (numNegative == 2)
  168. {
  169. result.intersect = true;
  170. if (numPositive == 1)
  171. {
  172. // The portion of the triangle in the halfspace is a
  173. // triangle.
  174. result.numPoints = 3;
  175. for (int i0 = 0; i0 < 3; ++i0)
  176. {
  177. if (s[i0] > (Real)0)
  178. {
  179. int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
  180. result.point[0] = triangle.v[i0];
  181. Real t0 = s[i0] / (s[i0] - s[i1]);
  182. Real t2 = s[i2] / (s[i2] - s[i0]);
  183. result.point[1] = triangle.v[i0] + t0 *
  184. (triangle.v[i1] - triangle.v[i0]);
  185. result.point[2] = triangle.v[i2] + t2 *
  186. (triangle.v[i0] - triangle.v[i2]);
  187. break;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. // Only a vertex of the triangle is in the halfspace.
  194. result.numPoints = 1;
  195. for (int i = 0; i < 3; ++i)
  196. {
  197. if (s[i] == (Real)0)
  198. {
  199. result.point[0] = triangle.v[i];
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. else // numNegative == 3
  206. {
  207. // The triangle is outside the halfspace. (numNegative == 3)
  208. result.intersect = false;
  209. result.numPoints = 0;
  210. }
  211. return result;
  212. }
  213. };
  214. }