IntrPlane3Triangle3.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/Hyperplane.h>
  11. #include <Mathematics/Triangle.h>
  12. #include <Mathematics/Vector3.h>
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Plane3<Real>, Triangle3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. // The number is 0 (no intersection), 1 (plane and triangle
  23. // intersect at a single point [vertex]), 2 (plane and triangle
  24. // intersect in a segment), or 3 (triangle is in the plane).
  25. // When the number is 2, the segment is either interior to the
  26. // triangle or is an edge of the triangle, the distinction stored
  27. // in 'isInterior'.
  28. int numIntersections;
  29. bool isInterior;
  30. };
  31. Result operator()(Plane3<Real> const& plane, Triangle3<Real> const& triangle)
  32. {
  33. Result result;
  34. // Determine on which side of the plane the vertices lie. The
  35. // table of possibilities is listed next with n = numNegative,
  36. // p = numPositive, and z = numZero.
  37. //
  38. // n p z intersection
  39. // ------------------------------------
  40. // 0 3 0 none
  41. // 0 2 1 vertex
  42. // 0 1 2 edge
  43. // 0 0 3 triangle in the plane
  44. // 1 2 0 segment (2 edges clipped)
  45. // 1 1 1 segment (1 edge clipped)
  46. // 1 0 2 edge
  47. // 2 1 0 segment (2 edges clipped)
  48. // 2 0 1 vertex
  49. // 3 0 0 none
  50. Real s[3];
  51. int numPositive = 0, numNegative = 0, numZero = 0;
  52. for (int i = 0; i < 3; ++i)
  53. {
  54. s[i] = Dot(plane.normal, triangle.v[i]) - plane.constant;
  55. if (s[i] > (Real)0)
  56. {
  57. ++numPositive;
  58. }
  59. else if (s[i] < (Real)0)
  60. {
  61. ++numNegative;
  62. }
  63. else
  64. {
  65. ++numZero;
  66. }
  67. }
  68. if (numZero == 0 && numPositive > 0 && numNegative > 0)
  69. {
  70. result.intersect = true;
  71. result.numIntersections = 2;
  72. result.isInterior = true;
  73. return result;
  74. }
  75. if (numZero == 1)
  76. {
  77. result.intersect = true;
  78. for (int i = 0; i < 3; ++i)
  79. {
  80. if (s[i] == (Real)0)
  81. {
  82. if (numPositive == 2 || numNegative == 2)
  83. {
  84. result.numIntersections = 1;
  85. }
  86. else
  87. {
  88. result.numIntersections = 2;
  89. result.isInterior = true;
  90. }
  91. break;
  92. }
  93. }
  94. return result;
  95. }
  96. if (numZero == 2)
  97. {
  98. result.intersect = true;
  99. result.numIntersections = 2;
  100. result.isInterior = false;
  101. return result;
  102. }
  103. if (numZero == 3)
  104. {
  105. result.intersect = true;
  106. result.numIntersections = 3;
  107. }
  108. else
  109. {
  110. result.intersect = false;
  111. result.numIntersections = 0;
  112. }
  113. return result;
  114. }
  115. };
  116. template <typename Real>
  117. class FIQuery<Real, Plane3<Real>, Triangle3<Real>>
  118. {
  119. public:
  120. struct Result
  121. {
  122. bool intersect;
  123. // The number is 0 (no intersection), 1 (plane and triangle
  124. // intersect at a single point [vertex]), 2 (plane and triangle
  125. // intersect in a segment), or 3 (triangle is in the plane).
  126. // When the number is 2, the segment is either interior to the
  127. // triangle or is an edge of the triangle, the distinction stored
  128. // in 'isInterior'.
  129. int numIntersections;
  130. bool isInterior;
  131. Vector3<Real> point[3];
  132. };
  133. Result operator()(Plane3<Real> const& plane, Triangle3<Real> const& triangle)
  134. {
  135. Result result;
  136. // Determine on which side of the plane the vertices lie. The
  137. // table of possibilities is listed next with n = numNegative,
  138. // p = numPositive, and z = numZero.
  139. //
  140. // n p z intersection
  141. // ------------------------------------
  142. // 0 3 0 none
  143. // 0 2 1 vertex
  144. // 0 1 2 edge
  145. // 0 0 3 triangle in the plane
  146. // 1 2 0 segment (2 edges clipped)
  147. // 1 1 1 segment (1 edge clipped)
  148. // 1 0 2 edge
  149. // 2 1 0 segment (2 edges clipped)
  150. // 2 0 1 vertex
  151. // 3 0 0 none
  152. Real s[3];
  153. int numPositive = 0, numNegative = 0, numZero = 0;
  154. for (int i = 0; i < 3; ++i)
  155. {
  156. s[i] = Dot(plane.normal, triangle.v[i]) - plane.constant;
  157. if (s[i] > (Real)0)
  158. {
  159. ++numPositive;
  160. }
  161. else if (s[i] < (Real)0)
  162. {
  163. ++numNegative;
  164. }
  165. else
  166. {
  167. ++numZero;
  168. }
  169. }
  170. if (numZero == 0 && numPositive > 0 && numNegative > 0)
  171. {
  172. result.intersect = true;
  173. result.numIntersections = 2;
  174. result.isInterior = true;
  175. Real sign = (Real)3 - numPositive * (Real)2;
  176. for (int i0 = 0; i0 < 3; ++i0)
  177. {
  178. if (sign * s[i0] > (Real)0)
  179. {
  180. int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
  181. Real t1 = s[i1] / (s[i1] - s[i0]);
  182. Real t2 = s[i2] / (s[i2] - s[i0]);
  183. result.point[0] = triangle.v[i1] + t1 *
  184. (triangle.v[i0] - triangle.v[i1]);
  185. result.point[1] = triangle.v[i2] + t2 *
  186. (triangle.v[i0] - triangle.v[i2]);
  187. break;
  188. }
  189. }
  190. return result;
  191. }
  192. if (numZero == 1)
  193. {
  194. result.intersect = true;
  195. for (int i0 = 0; i0 < 3; ++i0)
  196. {
  197. if (s[i0] == (Real)0)
  198. {
  199. int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
  200. result.point[0] = triangle.v[i0];
  201. if (numPositive == 2 || numNegative == 2)
  202. {
  203. result.numIntersections = 1;
  204. }
  205. else
  206. {
  207. result.numIntersections = 2;
  208. result.isInterior = true;
  209. Real t = s[i1] / (s[i1] - s[i2]);
  210. result.point[1] = triangle.v[i1] + t *
  211. (triangle.v[i2] - triangle.v[i1]);
  212. }
  213. break;
  214. }
  215. }
  216. return result;
  217. }
  218. if (numZero == 2)
  219. {
  220. result.intersect = true;
  221. result.numIntersections = 2;
  222. result.isInterior = false;
  223. for (int i0 = 0; i0 < 3; ++i0)
  224. {
  225. if (s[i0] != (Real)0)
  226. {
  227. int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
  228. result.point[0] = triangle.v[i1];
  229. result.point[1] = triangle.v[i2];
  230. break;
  231. }
  232. }
  233. return result;
  234. }
  235. if (numZero == 3)
  236. {
  237. result.intersect = true;
  238. result.numIntersections = 3;
  239. result.point[0] = triangle.v[0];
  240. result.point[1] = triangle.v[1];
  241. result.point[2] = triangle.v[2];
  242. }
  243. else
  244. {
  245. result.intersect = false;
  246. result.numIntersections = 0;
  247. }
  248. return result;
  249. }
  250. };
  251. }