IntrIntervals.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 <array>
  11. // The intervals are of the form [t0,t1], [t0,+infinity) or (-infinity,t1].
  12. // Degenerate intervals are allowed (t0 = t1). The queries do not perform
  13. // validation on the input intervals to test whether t0 <= t1.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class TIQuery<Real, std::array<Real, 2>, std::array<Real, 2>>
  18. {
  19. public:
  20. // The query tests overlap, whether a single point or an entire
  21. // interval.
  22. struct Result
  23. {
  24. bool intersect;
  25. // Dynamic queries (intervals moving with constant speeds). If
  26. // 'intersect' is true, the contact times are valid and
  27. // 0 <= firstTime <= lastTime, firstTime <= maxTime
  28. // If 'intersect' is false, there are two cases reported. If the
  29. // intervals will intersect at firstTime > maxTime, the contact
  30. // times are reported just as when 'intersect' is true. However,
  31. // if the intervals will not intersect, then firstTime and
  32. // lastTime are both set to zero (invalid because 'intersect' is
  33. // false).
  34. Real firstTime, lastTime;
  35. };
  36. // Static query. The firstTime and lastTime values are invalid
  37. // regardless of the value of 'intersect'.
  38. Result operator()(std::array<Real, 2> const& interval0, std::array<Real, 2> const& interval1)
  39. {
  40. Result result;
  41. result.intersect = (interval0[0] <= interval1[1] && interval0[1] >= interval1[0]);
  42. result.firstTime = (Real)0;
  43. result.lastTime = (Real)0;
  44. return result;
  45. }
  46. // Static queries where at least one interval is semiinfinite. The
  47. // two types of semiinfinite intervals are [a,+infinity), which I call
  48. // a positive-infinite interval, and (-infinity,a], which I call a
  49. // negative-infinite interval.
  50. Result operator()(std::array<Real, 2> const& finite, Real const& a, bool isPositiveInfinite)
  51. {
  52. Result result;
  53. result.firstTime = (Real)0;
  54. result.lastTime = (Real)0;
  55. if (isPositiveInfinite)
  56. {
  57. result.intersect = (finite[1] >= a);
  58. }
  59. else // is negative-infinite
  60. {
  61. result.intersect = (finite[0] <= a);
  62. }
  63. return result;
  64. }
  65. Result operator()(Real const& a0, bool isPositiveInfinite0,
  66. Real const& a1, bool isPositiveInfinite1)
  67. {
  68. Result result;
  69. result.firstTime = (Real)0;
  70. result.lastTime = (Real)0;
  71. if (isPositiveInfinite0)
  72. {
  73. if (isPositiveInfinite1)
  74. {
  75. result.intersect = true;
  76. }
  77. else // interval1 is negative-infinite
  78. {
  79. result.intersect = (a0 <= a1);
  80. }
  81. }
  82. else // interval0 is negative-infinite
  83. {
  84. if (isPositiveInfinite1)
  85. {
  86. result.intersect = (a0 >= a1);
  87. }
  88. else // interval1 is negative-infinite
  89. {
  90. result.intersect = true;
  91. }
  92. }
  93. return result;
  94. }
  95. // Dynamic query. Current time is 0, maxTime > 0 is required.
  96. Result operator()(Real maxTime, std::array<Real, 2> const& interval0,
  97. Real speed0, std::array<Real, 2> const& interval1, Real speed1)
  98. {
  99. Result result;
  100. if (interval0[1] < interval1[0])
  101. {
  102. // interval0 initially to the left of interval1.
  103. Real diffSpeed = speed0 - speed1;
  104. if (diffSpeed > (Real)0)
  105. {
  106. // The intervals must move towards each other. 'intersect'
  107. // is true when the intervals will intersect by maxTime.
  108. Real diffPos = interval1[0] - interval0[1];
  109. Real invDiffSpeed = (Real)1 / diffSpeed;
  110. result.intersect = (diffPos <= maxTime * diffSpeed);
  111. result.firstTime = diffPos * invDiffSpeed;
  112. result.lastTime = (interval1[1] - interval0[0]) * invDiffSpeed;
  113. return result;
  114. }
  115. }
  116. else if (interval0[0] > interval1[1])
  117. {
  118. // interval0 initially to the right of interval1.
  119. Real diffSpeed = speed1 - speed0;
  120. if (diffSpeed > (Real)0)
  121. {
  122. // The intervals must move towards each other. 'intersect'
  123. // is true when the intervals will intersect by maxTime.
  124. Real diffPos = interval0[0] - interval1[1];
  125. Real invDiffSpeed = (Real)1 / diffSpeed;
  126. result.intersect = (diffPos <= maxTime * diffSpeed);
  127. result.firstTime = diffPos * invDiffSpeed;
  128. result.lastTime = (interval0[1] - interval1[0]) * invDiffSpeed;
  129. return result;
  130. }
  131. }
  132. else
  133. {
  134. // The intervals are initially intersecting.
  135. result.intersect = true;
  136. result.firstTime = (Real)0;
  137. if (speed1 > speed0)
  138. {
  139. result.lastTime = (interval0[1] - interval1[0]) / (speed1 - speed0);
  140. }
  141. else if (speed1 < speed0)
  142. {
  143. result.lastTime = (interval1[1] - interval0[0]) / (speed0 - speed1);
  144. }
  145. else
  146. {
  147. result.lastTime = std::numeric_limits<Real>::max();
  148. }
  149. return result;
  150. }
  151. result.intersect = false;
  152. result.firstTime = (Real)0;
  153. result.lastTime = (Real)0;
  154. return result;
  155. }
  156. };
  157. template <typename Real>
  158. class FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>>
  159. {
  160. public:
  161. // The query finds overlap, whether a single point or an entire
  162. // interval.
  163. struct Result
  164. {
  165. Result()
  166. :
  167. intersect(false),
  168. numIntersections(0),
  169. overlap{ (Real)0, (Real)0 },
  170. type(isEmpty),
  171. firstTime((Real)0),
  172. lastTime((Real)0)
  173. {
  174. }
  175. bool intersect;
  176. // Static queries (no motion of intervals over time). The number
  177. // of number of intersections is 0 (no overlap), 1 (intervals are
  178. // just touching), or 2 (intervals overlap in an interval). If
  179. // 'intersect' is false, numIntersections is 0 and 'overlap' is
  180. // set to [0,0]. If 'intersect' is true, numIntersections is
  181. // 1 or 2. When 1, 'overlap' is set to [x,x], which is degenerate
  182. // and represents the single intersection point x. When 2,
  183. // 'overlap' is the interval of intersection.
  184. int numIntersections;
  185. std::array<Real, 2> overlap;
  186. // No intersection.
  187. static int const isEmpty = 0;
  188. // Intervals touch at an endpoint, [t0,t0].
  189. static int const isPoint = 1;
  190. // Finite-length interval of intersection, [t0,t1].
  191. static int const isFinite = 2;
  192. // Smiinfinite interval of intersection, [t0,+infinity). The
  193. // result.overlap[0] is t0 and result.overlap[1] is +1 as a
  194. // message that the right endpoint is +infinity (you still need
  195. // the result.type to know this interpretation).
  196. static int const isPositiveInfinite = 3;
  197. // Semiinfinite interval of intersection, (-infinity,t1]. The
  198. // result.overlap[0] is =1 as a message that the left endpoint is
  199. // -infinity (you still need the result.type to know this
  200. // interpretation). The result.overlap[1] is t1.
  201. static int const isNegativeInfinite = 4;
  202. // The dynamic queries all set the type to isDynamicQuery because
  203. // the queries look for time of first and last contact.
  204. static int const isDynamicQuery = 5;
  205. // The type is one of isEmpty, isPoint, isFinite,
  206. // isPositiveInfinite, isNegativeInfinite or isDynamicQuery.
  207. int type;
  208. // Dynamic queries (intervals moving with constant speeds). If
  209. // 'intersect' is true, the contact times are valid and
  210. // 0 <= firstTime <= lastTime, firstTime <= maxTime
  211. // If 'intersect' is false, there are two cases reported. If the
  212. // intervals will intersect at firstTime > maxTime, the contact
  213. // times are reported just as when 'intersect' is true. However,
  214. // if the intervals will not intersect, then firstTime and
  215. // lastTime are both set to zero (invalid because 'intersect' is
  216. // false).
  217. Real firstTime, lastTime;
  218. };
  219. // Static query.
  220. Result operator()(std::array<Real, 2> const& interval0, std::array<Real, 2> const& interval1)
  221. {
  222. Result result;
  223. if (interval0[1] < interval1[0] || interval0[0] > interval1[1])
  224. {
  225. result.numIntersections = 0;
  226. result.overlap[0] = (Real)0;
  227. result.overlap[1] = (Real)0;
  228. result.type = Result::isEmpty;
  229. }
  230. else if (interval0[1] > interval1[0])
  231. {
  232. if (interval0[0] < interval1[1])
  233. {
  234. result.overlap[0] = (interval0[0] < interval1[0] ? interval1[0] : interval0[0]);
  235. result.overlap[1] = (interval0[1] > interval1[1] ? interval1[1] : interval0[1]);
  236. if (result.overlap[0] < result.overlap[1])
  237. {
  238. result.numIntersections = 2;
  239. result.type = Result::isFinite;
  240. }
  241. else
  242. {
  243. result.numIntersections = 1;
  244. result.type = Result::isPoint;
  245. }
  246. }
  247. else // interval0[0] == interval1[1]
  248. {
  249. result.numIntersections = 1;
  250. result.overlap[0] = interval0[0];
  251. result.overlap[1] = result.overlap[0];
  252. result.type = Result::isPoint;
  253. }
  254. }
  255. else // interval0[1] == interval1[0]
  256. {
  257. result.numIntersections = 1;
  258. result.overlap[0] = interval0[1];
  259. result.overlap[1] = result.overlap[0];
  260. result.type = Result::isPoint;
  261. }
  262. result.intersect = (result.numIntersections > 0);
  263. return result;
  264. }
  265. // Static queries where at least one interval is semiinfinite. The
  266. // two types of semiinfinite intervals are [a,+infinity), which I call
  267. // a positive-infinite interval, and (-infinity,a], which I call a
  268. // negative-infinite interval.
  269. Result operator()(std::array<Real, 2> const& finite, Real const& a, bool isPositiveInfinite)
  270. {
  271. Result result;
  272. if (isPositiveInfinite)
  273. {
  274. if (finite[1] > a)
  275. {
  276. result.overlap[0] = std::max(finite[0], a);
  277. result.overlap[1] = finite[1];
  278. if (result.overlap[0] < result.overlap[1])
  279. {
  280. result.numIntersections = 2;
  281. result.type = Result::isFinite;
  282. }
  283. else
  284. {
  285. result.numIntersections = 1;
  286. result.type = Result::isPoint;
  287. }
  288. }
  289. else if (finite[1] == a)
  290. {
  291. result.numIntersections = 1;
  292. result.overlap[0] = a;
  293. result.overlap[1] = result.overlap[0];
  294. result.type = Result::isPoint;
  295. }
  296. else
  297. {
  298. result.numIntersections = 0;
  299. result.overlap[0] = (Real)0;
  300. result.overlap[1] = (Real)0;
  301. result.type = Result::isEmpty;
  302. }
  303. }
  304. else // is negative-infinite
  305. {
  306. if (finite[0] < a)
  307. {
  308. result.overlap[0] = finite[0];
  309. result.overlap[1] = std::min(finite[1], a);
  310. if (result.overlap[0] < result.overlap[1])
  311. {
  312. result.numIntersections = 2;
  313. result.type = Result::isFinite;
  314. }
  315. else
  316. {
  317. result.numIntersections = 1;
  318. result.type = Result::isPoint;
  319. }
  320. }
  321. else if (finite[0] == a)
  322. {
  323. result.numIntersections = 1;
  324. result.overlap[0] = a;
  325. result.overlap[1] = result.overlap[0];
  326. result.type = Result::isPoint;
  327. }
  328. else
  329. {
  330. result.numIntersections = 0;
  331. result.overlap[0] = (Real)0;
  332. result.overlap[1] = (Real)0;
  333. result.type = Result::isEmpty;
  334. }
  335. }
  336. result.intersect = (result.numIntersections > 0);
  337. return result;
  338. }
  339. Result operator()(Real const& a0, bool isPositiveInfinite0,
  340. Real const& a1, bool isPositiveInfinite1)
  341. {
  342. Result result;
  343. if (isPositiveInfinite0)
  344. {
  345. if (isPositiveInfinite1)
  346. {
  347. // overlap[1] is +infinity, but set it to +1 because Real
  348. // might not have a representation for +infinity. The
  349. // type indicates the interval is positive-infinite, so
  350. // the +1 is a reminder that overlap[1] is +infinity.
  351. result.numIntersections = 1;
  352. result.overlap[0] = std::max(a0, a1);
  353. result.overlap[1] = (Real)+1;
  354. result.type = Result::isPositiveInfinite;
  355. }
  356. else // interval1 is negative-infinite
  357. {
  358. if (a0 > a1)
  359. {
  360. result.numIntersections = 0;
  361. result.overlap[0] = (Real)0;
  362. result.overlap[1] = (Real)0;
  363. result.type = Result::isEmpty;
  364. }
  365. else if (a0 < a1)
  366. {
  367. result.numIntersections = 2;
  368. result.overlap[0] = a0;
  369. result.overlap[1] = a1;
  370. result.type = Result::isFinite;
  371. }
  372. else // a0 == a1
  373. {
  374. result.numIntersections = 1;
  375. result.overlap[0] = a0;
  376. result.overlap[1] = result.overlap[0];
  377. result.type = Result::isPoint;
  378. }
  379. }
  380. }
  381. else // interval0 is negative-infinite
  382. {
  383. if (isPositiveInfinite1)
  384. {
  385. if (a0 < a1)
  386. {
  387. result.numIntersections = 0;
  388. result.overlap[0] = (Real)0;
  389. result.overlap[1] = (Real)0;
  390. result.type = Result::isEmpty;
  391. }
  392. else if (a0 > a1)
  393. {
  394. result.numIntersections = 2;
  395. result.overlap[0] = a1;
  396. result.overlap[1] = a0;
  397. result.type = Result::isFinite;
  398. }
  399. else
  400. {
  401. result.numIntersections = 1;
  402. result.overlap[0] = a1;
  403. result.overlap[1] = result.overlap[0];
  404. result.type = Result::isPoint;
  405. }
  406. result.intersect = (a0 >= a1);
  407. }
  408. else // interval1 is negative-infinite
  409. {
  410. // overlap[0] is -infinity, but set it to -1 because Real
  411. // might not have a representation for -infinity. The
  412. // type indicates the interval is negative-infinite, so
  413. // the -1 is a reminder that overlap[0] is -infinity.
  414. result.numIntersections = 1;
  415. result.overlap[0] = (Real)-1;
  416. result.overlap[1] = std::min(a0, a1);
  417. result.type = Result::isNegativeInfinite;
  418. }
  419. }
  420. result.intersect = (result.numIntersections > 0);
  421. return result;
  422. }
  423. // Dynamic query. Current time is 0, maxTime > 0 is required.
  424. Result operator()(Real maxTime, std::array<Real, 2> const& interval0,
  425. Real speed0, std::array<Real, 2> const& interval1, Real speed1)
  426. {
  427. Result result;
  428. result.type = Result::isDynamicQuery;
  429. if (interval0[1] < interval1[0])
  430. {
  431. // interval0 initially to the left of interval1.
  432. Real diffSpeed = speed0 - speed1;
  433. if (diffSpeed > (Real)0)
  434. {
  435. // The intervals must move towards each other. 'intersect'
  436. // is true when the intervals will intersect by maxTime.
  437. Real diffPos = interval1[0] - interval0[1];
  438. Real invDiffSpeed = (Real)1 / diffSpeed;
  439. result.intersect = (diffPos <= maxTime * diffSpeed);
  440. result.numIntersections = 1;
  441. result.firstTime = diffPos * invDiffSpeed;
  442. result.lastTime = (interval1[1] - interval0[0]) * invDiffSpeed;
  443. result.overlap[0] = interval0[0] + result.firstTime * speed0;
  444. result.overlap[1] = result.overlap[0];
  445. return result;
  446. }
  447. }
  448. else if (interval0[0] > interval1[1])
  449. {
  450. // interval0 initially to the right of interval1.
  451. Real diffSpeed = speed1 - speed0;
  452. if (diffSpeed > (Real)0)
  453. {
  454. // The intervals must move towards each other. 'intersect'
  455. // is true when the intervals will intersect by maxTime.
  456. Real diffPos = interval0[0] - interval1[1];
  457. Real invDiffSpeed = (Real)1 / diffSpeed;
  458. result.intersect = (diffPos <= maxTime * diffSpeed);
  459. result.numIntersections = 1;
  460. result.firstTime = diffPos * invDiffSpeed;
  461. result.lastTime = (interval0[1] - interval1[0]) * invDiffSpeed;
  462. result.overlap[0] = interval1[1] + result.firstTime * speed1;
  463. result.overlap[1] = result.overlap[0];
  464. return result;
  465. }
  466. }
  467. else
  468. {
  469. // The intervals are initially intersecting.
  470. result.intersect = true;
  471. result.firstTime = (Real)0;
  472. if (speed1 > speed0)
  473. {
  474. result.lastTime = (interval0[1] - interval1[0]) / (speed1 - speed0);
  475. }
  476. else if (speed1 < speed0)
  477. {
  478. result.lastTime = (interval1[1] - interval0[0]) / (speed0 - speed1);
  479. }
  480. else
  481. {
  482. result.lastTime = std::numeric_limits<Real>::max();
  483. }
  484. if (interval0[1] > interval1[0])
  485. {
  486. if (interval0[0] < interval1[1])
  487. {
  488. result.numIntersections = 2;
  489. result.overlap[0] = (interval0[0] < interval1[0] ? interval1[0] : interval0[0]);
  490. result.overlap[1] = (interval0[1] > interval1[1] ? interval1[1] : interval0[1]);
  491. }
  492. else // interval0[0] == interval1[1]
  493. {
  494. result.numIntersections = 1;
  495. result.overlap[0] = interval0[0];
  496. result.overlap[1] = result.overlap[0];
  497. }
  498. }
  499. else // interval0[1] == interval1[0]
  500. {
  501. result.numIntersections = 1;
  502. result.overlap[0] = interval0[1];
  503. result.overlap[1] = result.overlap[0];
  504. }
  505. return result;
  506. }
  507. result.intersect = false;
  508. result.numIntersections = 0;
  509. result.overlap[0] = (Real)0;
  510. result.overlap[1] = (Real)0;
  511. result.firstTime = (Real)0;
  512. result.lastTime = (Real)0;
  513. return result;
  514. }
  515. };
  516. // Template aliases for convenience.
  517. template <typename Real>
  518. using TIIntervalInterval = TIQuery<Real, std::array<Real, 2>, std::array<Real, 2>>;
  519. template <typename Real>
  520. using FIIntervalInterval = FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>>;
  521. }