IntrAlignedBox2Circle2.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/Hypersphere.h>
  11. #include <Mathematics/DistPointAlignedBox.h>
  12. #include <Mathematics/Vector2.h>
  13. // The find-intersection query is based on the document
  14. // https://www.geometrictools.com/Documentation/IntersectionMovingCircleRectangle.pdf
  15. namespace WwiseGTE
  16. {
  17. template <typename Real>
  18. class TIQuery<Real, AlignedBox2<Real>, Circle2<Real>>
  19. {
  20. public:
  21. // The intersection query considers the box and circle to be solids;
  22. // that is, the circle object includes the region inside the circular
  23. // boundary and the box object includes the region inside the
  24. // rectangular boundary. If the circle object and box object
  25. // overlap, the objects intersect.
  26. struct Result
  27. {
  28. bool intersect;
  29. };
  30. Result operator()(AlignedBox2<Real> const& box, Circle2<Real> const& circle)
  31. {
  32. DCPQuery<Real, Vector2<Real>, AlignedBox2<Real>> pbQuery;
  33. auto pbResult = pbQuery(circle.center, box);
  34. Result result;
  35. result.intersect = (pbResult.sqrDistance <= circle.radius * circle.radius);
  36. return result;
  37. }
  38. };
  39. template <typename Real>
  40. class FIQuery<Real, AlignedBox2<Real>, Circle2<Real>>
  41. {
  42. public:
  43. // Currently, only a dynamic query is supported. A static query will
  44. // need to compute the intersection set of (solid) box and circle.
  45. struct Result
  46. {
  47. // The cases are
  48. // 1. Objects initially overlapping. The contactPoint is only one
  49. // of infinitely many points in the overlap.
  50. // intersectionType = -1
  51. // contactTime = 0
  52. // contactPoint = circle.center
  53. // 2. Objects initially separated but do not intersect later. The
  54. // contactTime and contactPoint are invalid.
  55. // intersectionType = 0
  56. // contactTime = 0
  57. // contactPoint = (0,0)
  58. // 3. Objects initially separated but intersect later.
  59. // intersectionType = +1
  60. // contactTime = first time T > 0
  61. // contactPoint = corresponding first contact
  62. int intersectionType;
  63. Real contactTime;
  64. Vector2<Real> contactPoint;
  65. // TODO: To support arbitrary precision for the contactTime,
  66. // return q0, q1 and q2 where contactTime = (q0 - sqrt(q1)) / q2.
  67. // The caller can compute contactTime to desired number of digits
  68. // of precision. These are valid when intersectionType is +1 but
  69. // are set to zero (invalid) in the other cases. Do the same for
  70. // the contactPoint.
  71. };
  72. Result operator()(AlignedBox2<Real> const& box, Vector2<Real> const& boxVelocity,
  73. Circle2<Real> const& circle, Vector2<Real> const& circleVelocity)
  74. {
  75. Result result = { 0, (Real)0, { (Real)0, (Real)0 } };
  76. // Translate the circle and box so that the box center becomes
  77. // the origin. Compute the velocity of the circle relative to
  78. // the box.
  79. Vector2<Real> boxCenter = (box.max + box.min) * (Real)0.5;
  80. Vector2<Real> extent = (box.max - box.min) * (Real)0.5;
  81. Vector2<Real> C = circle.center - boxCenter;
  82. Vector2<Real> V = circleVelocity - boxVelocity;
  83. // Change signs on components, if necessary, to transform C to the
  84. // first quadrant. Adjust the velocity accordingly.
  85. Real sign[2];
  86. for (int i = 0; i < 2; ++i)
  87. {
  88. if (C[i] >= (Real)0)
  89. {
  90. sign[i] = (Real)1;
  91. }
  92. else
  93. {
  94. C[i] = -C[i];
  95. V[i] = -V[i];
  96. sign[i] = (Real)-1;
  97. }
  98. }
  99. DoQuery(extent, C, circle.radius, V, result);
  100. if (result.intersectionType != 0)
  101. {
  102. // Translate back to the original coordinate system.
  103. for (int i = 0; i < 2; ++i)
  104. {
  105. if (sign[i] < (Real)0)
  106. {
  107. result.contactPoint[i] = -result.contactPoint[i];
  108. }
  109. }
  110. result.contactPoint += boxCenter;
  111. }
  112. return result;
  113. }
  114. protected:
  115. void DoQuery(Vector2<Real> const& K, Vector2<Real> const& C,
  116. Real radius, Vector2<Real> const& V, Result& result)
  117. {
  118. Vector2<Real> delta = C - K;
  119. if (delta[1] <= radius)
  120. {
  121. if (delta[0] <= radius)
  122. {
  123. if (delta[1] <= (Real)0)
  124. {
  125. if (delta[0] <= (Real)0)
  126. {
  127. InteriorOverlap(C, result);
  128. }
  129. else
  130. {
  131. EdgeOverlap(0, 1, K, C, delta, radius, result);
  132. }
  133. }
  134. else
  135. {
  136. if (delta[0] <= (Real)0)
  137. {
  138. EdgeOverlap(1, 0, K, C, delta, radius, result);
  139. }
  140. else
  141. {
  142. if (Dot(delta, delta) <= radius * radius)
  143. {
  144. VertexOverlap(K, delta, radius, result);
  145. }
  146. else
  147. {
  148. VertexSeparated(K, delta, V, radius, result);
  149. }
  150. }
  151. }
  152. }
  153. else
  154. {
  155. EdgeUnbounded(0, 1, K, C, radius, delta, V, result);
  156. }
  157. }
  158. else
  159. {
  160. if (delta[0] <= radius)
  161. {
  162. EdgeUnbounded(1, 0, K, C, radius, delta, V, result);
  163. }
  164. else
  165. {
  166. VertexUnbounded(K, C, radius, delta, V, result);
  167. }
  168. }
  169. }
  170. private:
  171. void InteriorOverlap(Vector2<Real> const& C, Result& result)
  172. {
  173. result.intersectionType = -1;
  174. result.contactTime = (Real)0;
  175. result.contactPoint = C;
  176. }
  177. void EdgeOverlap(int i0, int i1, Vector2<Real> const& K, Vector2<Real> const& C,
  178. Vector2<Real> const& delta, Real radius, Result& result)
  179. {
  180. result.intersectionType = (delta[i0] < radius ? -1 : 1);
  181. result.contactTime = (Real)0;
  182. result.contactPoint[i0] = K[i0];
  183. result.contactPoint[i1] = C[i1];
  184. }
  185. void VertexOverlap(Vector2<Real> const& K0, Vector2<Real> const& delta,
  186. Real radius, Result& result)
  187. {
  188. Real sqrDistance = delta[0] * delta[0] + delta[1] * delta[1];
  189. Real sqrRadius = radius * radius;
  190. result.intersectionType = (sqrDistance < sqrRadius ? -1 : 1);
  191. result.contactTime = (Real)0;
  192. result.contactPoint = K0;
  193. }
  194. void VertexSeparated(Vector2<Real> const& K0, Vector2<Real> const& delta0,
  195. Vector2<Real> const& V, Real radius, Result& result)
  196. {
  197. Real q0 = -Dot(V, delta0);
  198. if (q0 > (Real)0)
  199. {
  200. Real dotVPerpD0 = Dot(V, Perp(delta0));
  201. Real q2 = Dot(V, V);
  202. Real q1 = radius * radius * q2 - dotVPerpD0 * dotVPerpD0;
  203. if (q1 >= (Real)0)
  204. {
  205. IntersectsVertex(0, 1, K0, q0, q1, q2, result);
  206. }
  207. }
  208. }
  209. void EdgeUnbounded(int i0, int i1, Vector2<Real> const& K0, Vector2<Real> const& C,
  210. Real radius, Vector2<Real> const& delta0, Vector2<Real> const& V, Result& result)
  211. {
  212. if (V[i0] < (Real)0)
  213. {
  214. Real dotVPerpD0 = V[i0] * delta0[i1] - V[i1] * delta0[i0];
  215. if (radius * V[i1] + dotVPerpD0 >= (Real)0)
  216. {
  217. Vector2<Real> K1, delta1;
  218. K1[i0] = K0[i0];
  219. K1[i1] = -K0[i1];
  220. delta1[i0] = C[i0] - K1[i0];
  221. delta1[i1] = C[i1] - K1[i1];
  222. Real dotVPerpD1 = V[i0] * delta1[i1] - V[i1] * delta1[i0];
  223. if (radius * V[i1] + dotVPerpD1 <= (Real)0)
  224. {
  225. IntersectsEdge(i0, i1, K0, C, radius, V, result);
  226. }
  227. else
  228. {
  229. Real q2 = Dot(V, V);
  230. Real q1 = radius * radius * q2 - dotVPerpD1 * dotVPerpD1;
  231. if (q1 >= (Real)0)
  232. {
  233. Real q0 = -(V[i0] * delta1[i0] + V[i1] * delta1[i1]);
  234. IntersectsVertex(i0, i1, K1, q0, q1, q2, result);
  235. }
  236. }
  237. }
  238. else
  239. {
  240. Real q2 = Dot(V, V);
  241. Real q1 = radius * radius * q2 - dotVPerpD0 * dotVPerpD0;
  242. if (q1 >= (Real)0)
  243. {
  244. Real q0 = -(V[i0] * delta0[i0] + V[i1] * delta0[i1]);
  245. IntersectsVertex(i0, i1, K0, q0, q1, q2, result);
  246. }
  247. }
  248. }
  249. }
  250. void VertexUnbounded(Vector2<Real> const& K0, Vector2<Real> const& C, Real radius,
  251. Vector2<Real> const& delta0, Vector2<Real> const& V, Result& result)
  252. {
  253. if (V[0] < (Real)0 && V[1] < (Real)0)
  254. {
  255. Real dotVPerpD0 = Dot(V, Perp(delta0));
  256. if (radius * V[0] - dotVPerpD0 <= (Real)0)
  257. {
  258. if (-radius * V[1] - dotVPerpD0 >= (Real)0)
  259. {
  260. Real q2 = Dot(V, V);
  261. Real q1 = radius * radius * q2 - dotVPerpD0 * dotVPerpD0;
  262. Real q0 = -Dot(V, delta0);
  263. IntersectsVertex(0, 1, K0, q0, q1, q2, result);
  264. }
  265. else
  266. {
  267. Vector2<Real> K1{ K0[0], -K0[1] };
  268. Vector2<Real> delta1 = C - K1;
  269. Real dotVPerpD1 = Dot(V, Perp(delta1));
  270. if (-radius * V[1] - dotVPerpD1 >= (Real)0)
  271. {
  272. IntersectsEdge(0, 1, K0, C, radius, V, result);
  273. }
  274. else
  275. {
  276. Real q2 = Dot(V, V);
  277. Real q1 = radius * radius * q2 - dotVPerpD1 * dotVPerpD1;
  278. if (q1 >= (Real)0)
  279. {
  280. Real q0 = -Dot(V, delta1);
  281. IntersectsVertex(0, 1, K1, q0, q1, q2, result);
  282. }
  283. }
  284. }
  285. }
  286. else
  287. {
  288. Vector2<Real> K2{ -K0[0], K0[1] };
  289. Vector2<Real> delta2 = C - K2;
  290. Real dotVPerpD2 = Dot(V, Perp(delta2));
  291. if (radius * V[0] - dotVPerpD2 <= (Real)0)
  292. {
  293. IntersectsEdge(1, 0, K0, C, radius, V, result);
  294. }
  295. else
  296. {
  297. Real q2 = Dot(V, V);
  298. Real q1 = radius * radius * q2 - dotVPerpD2 * dotVPerpD2;
  299. if (q1 >= (Real)0)
  300. {
  301. Real q0 = -Dot(V, delta2);
  302. IntersectsVertex(1, 0, K2, q0, q1, q2, result);
  303. }
  304. }
  305. }
  306. }
  307. }
  308. void IntersectsVertex(int i0, int i1, Vector2<Real> const& K,
  309. Real q0, Real q1, Real q2, Result& result)
  310. {
  311. result.intersectionType = +1;
  312. result.contactTime = (q0 - std::sqrt(q1)) / q2;
  313. result.contactPoint[i0] = K[i0];
  314. result.contactPoint[i1] = K[i1];
  315. }
  316. void IntersectsEdge(int i0, int i1, Vector2<Real> const& K0, Vector2<Real> const& C,
  317. Real radius, Vector2<Real> const& V, Result& result)
  318. {
  319. result.intersectionType = +1;
  320. result.contactTime = (K0[i0] + radius - C[i0]) / V[i0];
  321. result.contactPoint[i0] = K0[i0];
  322. result.contactPoint[i1] = C[i1] + result.contactTime * V[i1];
  323. }
  324. };
  325. }