IntrOrientedBox3OrientedBox3.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/TIQuery.h>
  9. #include <Mathematics/OrientedBox.h>
  10. #include <Mathematics/Vector3.h>
  11. // The queries consider the box to be a solid.
  12. //
  13. // The test-intersection query uses the method of separating axes.
  14. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  15. // The set of potential separating directions includes the 3 face normals of
  16. // box0, the 3 face normals of box1, and 9 directions, each of which is the
  17. // cross product of an edge of box0 and and an edge of box1.
  18. //
  19. // The separating axes involving cross products of edges has numerical
  20. // robustness problems when the two edges are nearly parallel. The cross
  21. // product of the edges is nearly the zero vector, so normalization of the
  22. // cross product may produce unit-length directions that are not close to the
  23. // true direction. Such a pair of edges occurs when a box0 face normal N0 and
  24. // a box1 face normal N1 are nearly parallel. In this case, you may skip the
  25. // edge-edge directions, which is equivalent to projecting the boxes onto the
  26. // plane with normal N0 and applying a 2D separating axis test. The ability
  27. // to do so involves choosing a small nonnegative epsilon. It is used to
  28. // determine whether two face normals, one from each box, are nearly parallel:
  29. // |Dot(N0,N1)| >= 1 - epsilon. If the input is negative, it is clamped to
  30. // zero.
  31. //
  32. // The pair of integers 'separating', say, (i0,i1), identify the axis that
  33. // reported separation; there may be more than one but only one is
  34. // reported. If the separating axis is a face normal N[i0] of the aligned
  35. // box0 in dimension i0, then (i0,-1) is returned. If the axis is a face
  36. // normal box1.Axis[i1], then (-1,i1) is returned. If the axis is a cross
  37. // product of edges, Cross(N[i0],box1.Axis[i1]), then (i0,i1) is returned.
  38. namespace WwiseGTE
  39. {
  40. template <typename Real>
  41. class TIQuery<Real, OrientedBox3<Real>, OrientedBox3<Real>>
  42. {
  43. public:
  44. struct Result
  45. {
  46. // The 'epsilon' value must be nonnegative.
  47. Result(Real inEpsilon = (Real)0)
  48. :
  49. epsilon(inEpsilon >= (Real)0 ? inEpsilon : (Real)0)
  50. {
  51. }
  52. bool intersect;
  53. Real epsilon;
  54. int separating[2];
  55. };
  56. Result operator()(OrientedBox3<Real> const& box0, OrientedBox3<Real> const& box1)
  57. {
  58. Result result;
  59. // Convenience variables.
  60. Vector3<Real> const& C0 = box0.center;
  61. Vector3<Real> const* A0 = &box0.axis[0];
  62. Vector3<Real> const& E0 = box0.extent;
  63. Vector3<Real> const& C1 = box1.center;
  64. Vector3<Real> const* A1 = &box1.axis[0];
  65. Vector3<Real> const& E1 = box1.extent;
  66. Real const cutoff = (Real)1 - result.epsilon;
  67. bool existsParallelPair = false;
  68. // Compute difference of box centers.
  69. Vector3<Real> D = C1 - C0;
  70. // dot01[i][j] = Dot(A0[i],A1[j]) = A1[j][i]
  71. Real dot01[3][3];
  72. // |dot01[i][j]|
  73. Real absDot01[3][3];
  74. // Dot(D, A0[i])
  75. Real dotDA0[3];
  76. // interval radii and distance between centers
  77. Real r0, r1, r;
  78. // r0 + r1
  79. Real r01;
  80. // Test for separation on the axis C0 + t*A0[0].
  81. for (int i = 0; i < 3; ++i)
  82. {
  83. dot01[0][i] = Dot(A0[0], A1[i]);
  84. absDot01[0][i] = std::fabs(dot01[0][i]);
  85. if (absDot01[0][i] > cutoff)
  86. {
  87. existsParallelPair = true;
  88. }
  89. }
  90. dotDA0[0] = Dot(D, A0[0]);
  91. r = std::fabs(dotDA0[0]);
  92. r1 = E1[0] * absDot01[0][0] + E1[1] * absDot01[0][1] + E1[2] * absDot01[0][2];
  93. r01 = E0[0] + r1;
  94. if (r > r01)
  95. {
  96. result.intersect = false;
  97. result.separating[0] = 0;
  98. result.separating[1] = -1;
  99. return result;
  100. }
  101. // Test for separation on the axis C0 + t*A0[1].
  102. for (int i = 0; i < 3; ++i)
  103. {
  104. dot01[1][i] = Dot(A0[1], A1[i]);
  105. absDot01[1][i] = std::fabs(dot01[1][i]);
  106. if (absDot01[1][i] > cutoff)
  107. {
  108. existsParallelPair = true;
  109. }
  110. }
  111. dotDA0[1] = Dot(D, A0[1]);
  112. r = std::fabs(dotDA0[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] = Dot(A0[2], A1[i]);
  126. absDot01[2][i] = std::fabs(dot01[2][i]);
  127. if (absDot01[2][i] > cutoff)
  128. {
  129. existsParallelPair = true;
  130. }
  131. }
  132. dotDA0[2] = Dot(D, A0[2]);
  133. r = std::fabs(dotDA0[2]);
  134. r1 = E1[0] * absDot01[2][0] + E1[1] * absDot01[2][1] + E1[2] * absDot01[2][2];
  135. r01 = E0[2] + r1;
  136. if (r > r01)
  137. {
  138. result.intersect = false;
  139. result.separating[0] = 2;
  140. result.separating[1] = -1;
  141. return result;
  142. }
  143. // Test for separation on the axis C0 + t*A1[0].
  144. r = std::fabs(Dot(D, A1[0]));
  145. r0 = E0[0] * absDot01[0][0] + E0[1] * absDot01[1][0] + E0[2] * absDot01[2][0];
  146. r01 = r0 + E1[0];
  147. if (r > r01)
  148. {
  149. result.intersect = false;
  150. result.separating[0] = -1;
  151. result.separating[1] = 0;
  152. return result;
  153. }
  154. // Test for separation on the axis C0 + t*A1[1].
  155. r = std::fabs(Dot(D, A1[1]));
  156. r0 = E0[0] * absDot01[0][1] + E0[1] * absDot01[1][1] + E0[2] * absDot01[2][1];
  157. r01 = r0 + E1[1];
  158. if (r > r01)
  159. {
  160. result.intersect = false;
  161. result.separating[0] = -1;
  162. result.separating[1] = 1;
  163. return result;
  164. }
  165. // Test for separation on the axis C0 + t*A1[2].
  166. r = std::fabs(Dot(D, A1[2]));
  167. r0 = E0[0] * absDot01[0][2] + E0[1] * absDot01[1][2] + E0[2] * absDot01[2][2];
  168. r01 = r0 + E1[2];
  169. if (r > r01)
  170. {
  171. result.intersect = false;
  172. result.separating[0] = -1;
  173. result.separating[1] = 2;
  174. return result;
  175. }
  176. // At least one pair of box axes was parallel, so the separation is
  177. // effectively in 2D. The edge-edge axes do not need to be tested.
  178. if (existsParallelPair)
  179. {
  180. return true;
  181. }
  182. // Test for separation on the axis C0 + t*A0[0]xA1[0].
  183. r = std::fabs(dotDA0[2] * dot01[1][0] - dotDA0[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(dotDA0[2] * dot01[1][1] - dotDA0[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(dotDA0[2] * dot01[1][2] - dotDA0[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(dotDA0[0] * dot01[2][0] - dotDA0[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(dotDA0[0] * dot01[2][1] - dotDA0[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(dotDA0[0] * dot01[2][2] - dotDA0[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(dotDA0[1] * dot01[0][0] - dotDA0[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(dotDA0[1] * dot01[0][1] - dotDA0[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(dotDA0[1] * dot01[0][2] - dotDA0[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. }