IntrAlignedBox3OrientedBox3.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/AlignedBox.h>
  11. #include <Mathematics/OrientedBox.h>
  12. #include <Mathematics/Vector3.h>
  13. // The queries consider the box to be a solid.
  14. //
  15. // The test-intersection query uses the method of separating axes.
  16. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  17. // The set of potential separating directions includes the 3 face normals of
  18. // box0, the 3 face normals of box1, and 9 directions, each of which is the
  19. // cross product of an edge of box0 and and an edge of box1.
  20. //
  21. // The separating axes involving cross products of edges has numerical
  22. // robustness problems when the two edges are nearly parallel. The cross
  23. // product of the edges is nearly the zero vector, so normalization of the
  24. // cross product may produce unit-length directions that are not close to the
  25. // true direction. Such a pair of edges occurs when a box0 face normal N0 and
  26. // a box1 face normal N1 are nearly parallel. In this case, you may skip the
  27. // edge-edge directions, which is equivalent to projecting the boxes onto the
  28. // plane with normal N0 and applying a 2D separating axis test. The ability
  29. // to do so involves choosing a small nonnegative epsilon . It is used to
  30. // determine whether two face normals, one from each box, are nearly parallel:
  31. // |Dot(N0,N1)| >= 1 - epsilon. If the input is negative, it is clamped to
  32. // zero.
  33. //
  34. // The pair of integers 'separating', say, (i0,i1), identify the axis that
  35. // reported separation; there may be more than one but only one is
  36. // reported. If the separating axis is a face normal N[i0] of the aligned
  37. // box0 in dimension i0, then (i0,-1) is returned. If the axis is a face
  38. // normal box1.Axis[i1], then (-1,i1) is returned. If the axis is a cross
  39. // product of edges, Cross(N[i0],box1.Axis[i1]), then (i0,i1) is returned.
  40. namespace WwiseGTE
  41. {
  42. template <typename Real>
  43. class TIQuery<Real, AlignedBox3<Real>, OrientedBox3<Real>>
  44. {
  45. public:
  46. struct Result
  47. {
  48. // The 'epsilon' value must be nonnegative.
  49. Result(Real inEpsilon = (Real)0)
  50. :
  51. epsilon(inEpsilon >= (Real)0 ? inEpsilon : (Real)0)
  52. {
  53. }
  54. bool intersect;
  55. Real epsilon;
  56. int separating[2];
  57. };
  58. Result operator()(AlignedBox3<Real> const& box0, OrientedBox3<Real> const& box1)
  59. {
  60. Result result;
  61. // Get the centered form of the aligned box. The axes are
  62. // implicitly A0[0] = (1,0,0), A0[1] = (0,1,0) and
  63. // A0[2] = (0,0,1).
  64. Vector3<Real> C0, E0;
  65. box0.GetCenteredForm(C0, E0);
  66. // Convenience variables.
  67. Vector3<Real> const& C1 = box1.center;
  68. Vector3<Real> const* A1 = &box1.axis[0];
  69. Vector3<Real> const& E1 = box1.extent;
  70. Real const cutoff = (Real)1 - result.epsilon;
  71. bool existsParallelPair = false;
  72. // Compute the difference of box centers.
  73. Vector3<Real> D = C1 - C0;
  74. // dot01[i][j] = Dot(A0[i],A1[j]) = A1[j][i]
  75. Real dot01[3][3];
  76. // |dot01[i][j]|
  77. Real absDot01[3][3];
  78. // interval radii and distance between centers
  79. Real r0, r1, r;
  80. // r0 + r1
  81. Real r01;
  82. // Test for separation on the axis C0 + t*A0[0].
  83. for (int i = 0; i < 3; ++i)
  84. {
  85. dot01[0][i] = A1[i][0];
  86. absDot01[0][i] = std::fabs(A1[i][0]);
  87. if (absDot01[0][i] >= cutoff)
  88. {
  89. existsParallelPair = true;
  90. }
  91. }
  92. r = std::fabs(D[0]);
  93. r1 = E1[0] * absDot01[0][0] + E1[1] * absDot01[0][1] + E1[2] * absDot01[0][2];
  94. r01 = E0[0] + r1;
  95. if (r > r01)
  96. {
  97. result.intersect = false;
  98. result.separating[0] = 0;
  99. result.separating[1] = -1;
  100. return result;
  101. }
  102. // Test for separation on the axis C0 + t*A0[1].
  103. for (int i = 0; i < 3; ++i)
  104. {
  105. dot01[1][i] = A1[i][1];
  106. absDot01[1][i] = std::fabs(A1[i][1]);
  107. if (absDot01[1][i] >= cutoff)
  108. {
  109. existsParallelPair = true;
  110. }
  111. }
  112. r = std::fabs(D[1]);
  113. r1 = E1[0] * absDot01[1][0] + E1[1] * absDot01[1][1] + E1[2] * absDot01[1][2];
  114. r01 = E0[1] + r1;
  115. if (r > r01)
  116. {
  117. result.intersect = false;
  118. result.separating[0] = 1;
  119. result.separating[1] = -1;
  120. return result;
  121. }
  122. // Test for separation on the axis C0 + t*A0[2].
  123. for (int i = 0; i < 3; ++i)
  124. {
  125. dot01[2][i] = A1[i][2];
  126. absDot01[2][i] = std::fabs(A1[i][2]);
  127. if (absDot01[2][i] >= cutoff)
  128. {
  129. existsParallelPair = true;
  130. }
  131. }
  132. r = std::fabs(D[2]);
  133. r1 = E1[0] * absDot01[2][0] + E1[1] * absDot01[2][1] + E1[2] * absDot01[2][2];
  134. r01 = E0[2] + r1;
  135. if (r > r01)
  136. {
  137. result.intersect = false;
  138. result.separating[0] = 2;
  139. result.separating[1] = -1;
  140. return result;
  141. }
  142. // Test for separation on the axis C0 + t*A1[0].
  143. r = std::fabs(Dot(D, A1[0]));
  144. r0 = E0[0] * absDot01[0][0] + E0[1] * absDot01[1][0] + E0[2] * absDot01[2][0];
  145. r01 = r0 + E1[0];
  146. if (r > r01)
  147. {
  148. result.intersect = false;
  149. result.separating[0] = -1;
  150. result.separating[1] = 0;
  151. return result;
  152. }
  153. // Test for separation on the axis C0 + t*A1[1].
  154. r = std::fabs(Dot(D, A1[1]));
  155. r0 = E0[0] * absDot01[0][1] + E0[1] * absDot01[1][1] + E0[2] * absDot01[2][1];
  156. r01 = r0 + E1[1];
  157. if (r > r01)
  158. {
  159. result.intersect = false;
  160. result.separating[0] = -1;
  161. result.separating[1] = 1;
  162. return result;
  163. }
  164. // Test for separation on the axis C0 + t*A1[2].
  165. r = std::fabs(Dot(D, A1[2]));
  166. r0 = E0[0] * absDot01[0][2] + E0[1] * absDot01[1][2] + E0[2] * absDot01[2][2];
  167. r01 = r0 + E1[2];
  168. if (r > r01)
  169. {
  170. result.intersect = false;
  171. result.separating[0] = -1;
  172. result.separating[1] = 2;
  173. return result;
  174. }
  175. // At least one pair of box axes was parallel, so the separation is
  176. // effectively in 2D. The edge-edge axes do not need to be tested.
  177. if (existsParallelPair)
  178. {
  179. result.intersect = true;
  180. return result;
  181. }
  182. // Test for separation on the axis C0 + t*A0[0]xA1[0].
  183. r = std::fabs(D[2] * dot01[1][0] - D[1] * dot01[2][0]);
  184. r0 = E0[1] * absDot01[2][0] + E0[2] * absDot01[1][0];
  185. r1 = E1[1] * absDot01[0][2] + E1[2] * absDot01[0][1];
  186. r01 = r0 + r1;
  187. if (r > r01)
  188. {
  189. result.intersect = false;
  190. result.separating[0] = 0;
  191. result.separating[1] = 0;
  192. return result;
  193. }
  194. // Test for separation on the axis C0 + t*A0[0]xA1[1].
  195. r = std::fabs(D[2] * dot01[1][1] - D[1] * dot01[2][1]);
  196. r0 = E0[1] * absDot01[2][1] + E0[2] * absDot01[1][1];
  197. r1 = E1[0] * absDot01[0][2] + E1[2] * absDot01[0][0];
  198. r01 = r0 + r1;
  199. if (r > r01)
  200. {
  201. result.intersect = false;
  202. result.separating[0] = 0;
  203. result.separating[1] = 1;
  204. return result;
  205. }
  206. // Test for separation on the axis C0 + t*A0[0]xA1[2].
  207. r = std::fabs(D[2] * dot01[1][2] - D[1] * dot01[2][2]);
  208. r0 = E0[1] * absDot01[2][2] + E0[2] * absDot01[1][2];
  209. r1 = E1[0] * absDot01[0][1] + E1[1] * absDot01[0][0];
  210. r01 = r0 + r1;
  211. if (r > r01)
  212. {
  213. result.intersect = false;
  214. result.separating[0] = 0;
  215. result.separating[1] = 2;
  216. return result;
  217. }
  218. // Test for separation on the axis C0 + t*A0[1]xA1[0].
  219. r = std::fabs(D[0] * dot01[2][0] - D[2] * dot01[0][0]);
  220. r0 = E0[0] * absDot01[2][0] + E0[2] * absDot01[0][0];
  221. r1 = E1[1] * absDot01[1][2] + E1[2] * absDot01[1][1];
  222. r01 = r0 + r1;
  223. if (r > r01)
  224. {
  225. result.intersect = false;
  226. result.separating[0] = 1;
  227. result.separating[1] = 0;
  228. return result;
  229. }
  230. // Test for separation on the axis C0 + t*A0[1]xA1[1].
  231. r = std::fabs(D[0] * dot01[2][1] - D[2] * dot01[0][1]);
  232. r0 = E0[0] * absDot01[2][1] + E0[2] * absDot01[0][1];
  233. r1 = E1[0] * absDot01[1][2] + E1[2] * absDot01[1][0];
  234. r01 = r0 + r1;
  235. if (r > r01)
  236. {
  237. result.intersect = false;
  238. result.separating[0] = 1;
  239. result.separating[1] = 1;
  240. return result;
  241. }
  242. // Test for separation on the axis C0 + t*A0[1]xA1[2].
  243. r = std::fabs(D[0] * dot01[2][2] - D[2] * dot01[0][2]);
  244. r0 = E0[0] * absDot01[2][2] + E0[2] * absDot01[0][2];
  245. r1 = E1[0] * absDot01[1][1] + E1[1] * absDot01[1][0];
  246. r01 = r0 + r1;
  247. if (r > r01)
  248. {
  249. result.intersect = false;
  250. result.separating[0] = 1;
  251. result.separating[1] = 2;
  252. return result;
  253. }
  254. // Test for separation on the axis C0 + t*A0[2]xA1[0].
  255. r = std::fabs(D[1] * dot01[0][0] - D[0] * dot01[1][0]);
  256. r0 = E0[0] * absDot01[1][0] + E0[1] * absDot01[0][0];
  257. r1 = E1[1] * absDot01[2][2] + E1[2] * absDot01[2][1];
  258. r01 = r0 + r1;
  259. if (r > r01)
  260. {
  261. result.intersect = false;
  262. result.separating[0] = 2;
  263. result.separating[1] = 0;
  264. return result;
  265. }
  266. // Test for separation on the axis C0 + t*A0[2]xA1[1].
  267. r = std::fabs(D[1] * dot01[0][1] - D[0] * dot01[1][1]);
  268. r0 = E0[0] * absDot01[1][1] + E0[1] * absDot01[0][1];
  269. r1 = E1[0] * absDot01[2][2] + E1[2] * absDot01[2][0];
  270. r01 = r0 + r1;
  271. if (r > r01)
  272. {
  273. result.intersect = false;
  274. result.separating[0] = 2;
  275. result.separating[1] = 1;
  276. return result;
  277. }
  278. // Test for separation on the axis C0 + t*A0[2]xA1[2].
  279. r = std::fabs(D[1] * dot01[0][2] - D[0] * dot01[1][2]);
  280. r0 = E0[0] * absDot01[1][2] + E0[1] * absDot01[0][2];
  281. r1 = E1[0] * absDot01[2][1] + E1[1] * absDot01[2][0];
  282. r01 = r0 + r1;
  283. if (r > r01)
  284. {
  285. result.intersect = false;
  286. result.separating[0] = 2;
  287. result.separating[1] = 2;
  288. return result;
  289. }
  290. result.intersect = true;
  291. return result;
  292. }
  293. };
  294. }