OPC_TreeCollider.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains code for a tree collider.
  11. * \file OPC_TreeCollider.cpp
  12. * \author Pierre Terdiman
  13. * \date March, 20, 2001
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * Contains an AABB tree collider.
  19. * This class performs a collision test between two AABB trees.
  20. *
  21. * \class AABBTreeCollider
  22. * \author Pierre Terdiman
  23. * \version 1.3
  24. * \date March, 20, 2001
  25. */
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. #include "Opcode.h"
  29. using namespace Opcode;
  30. #include "OPC_BoxBoxOverlap.h"
  31. #include "OPC_TriBoxOverlap.h"
  32. #include "OPC_TriTriOverlap.h"
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. /**
  35. * Constructor.
  36. */
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. AABBTreeCollider::AABBTreeCollider() :
  39. mNbBVBVTests (0),
  40. mNbPrimPrimTests (0),
  41. mNbBVPrimTests (0),
  42. mFullBoxBoxTest (true),
  43. mFullPrimBoxTest (true),
  44. mIMesh0 (null),
  45. mIMesh1 (null)
  46. {
  47. }
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. /**
  50. * Destructor.
  51. */
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. AABBTreeCollider::~AABBTreeCollider()
  54. {
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. /**
  58. * Validates current settings. You should call this method after all the settings and callbacks have been defined.
  59. * \return null if everything is ok, else a string describing the problem
  60. */
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. const char* AABBTreeCollider::ValidateSettings()
  63. {
  64. if(TemporalCoherenceEnabled() && !FirstContactEnabled()) return "Temporal coherence only works with ""First contact"" mode!";
  65. return null;
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. /**
  69. * Generic collision query for generic OPCODE models. After the call, access the results with:
  70. * - GetContactStatus()
  71. * - GetNbPairs()
  72. * - GetPairs()
  73. *
  74. * \param cache [in] collision cache for model pointers and a colliding pair of primitives
  75. * \param world0 [in] world matrix for first object
  76. * \param world1 [in] world matrix for second object
  77. * \return true if success
  78. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  79. */
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. bool AABBTreeCollider::Collide(BVTCache& cache, const Matrix4x4* world0, const Matrix4x4* world1)
  82. {
  83. // Checkings
  84. if(!cache.Model0 || !cache.Model1) return false;
  85. if(cache.Model0->HasLeafNodes()!=cache.Model1->HasLeafNodes()) return false;
  86. if(cache.Model0->IsQuantized()!=cache.Model1->IsQuantized()) return false;
  87. /*
  88. Rules:
  89. - perform hull test
  90. - when hulls collide, disable hull test
  91. - if meshes overlap, reset countdown
  92. - if countdown reaches 0, enable hull test
  93. */
  94. #ifdef __MESHMERIZER_H__
  95. // Handle hulls
  96. if(cache.HullTest)
  97. {
  98. if(cache.Model0->GetHull() && cache.Model1->GetHull())
  99. {
  100. struct Local
  101. {
  102. static Point* SVCallback(const Point& sv, udword& previndex, udword user_data)
  103. {
  104. CollisionHull* Hull = (CollisionHull*)user_data;
  105. previndex = Hull->ComputeSupportingVertex(sv, previndex);
  106. return (Point*)&Hull->GetVerts()[previndex];
  107. }
  108. };
  109. bool Collide;
  110. if(0)
  111. {
  112. static GJKEngine GJK;
  113. static bool GJKInitDone=false;
  114. if(!GJKInitDone)
  115. {
  116. GJK.Enable(GJK_BACKUP_PROCEDURE);
  117. GJK.Enable(GJK_DEGENERATE);
  118. GJK.Enable(GJK_HILLCLIMBING);
  119. GJKInitDone = true;
  120. }
  121. GJK.SetCallbackObj0(Local::SVCallback);
  122. GJK.SetCallbackObj1(Local::SVCallback);
  123. GJK.SetUserData0(udword(cache.Model0->GetHull()));
  124. GJK.SetUserData1(udword(cache.Model1->GetHull()));
  125. Collide = GJK.Collide(*world0, *world1, &cache.SepVector);
  126. }
  127. else
  128. {
  129. static SVEngine SVE;
  130. SVE.SetCallbackObj0(Local::SVCallback);
  131. SVE.SetCallbackObj1(Local::SVCallback);
  132. SVE.SetUserData0(udword(cache.Model0->GetHull()));
  133. SVE.SetUserData1(udword(cache.Model1->GetHull()));
  134. Collide = SVE.Collide(*world0, *world1, &cache.SepVector);
  135. }
  136. if(!Collide)
  137. {
  138. // Reset stats & contact status
  139. mFlags &= ~OPC_CONTACT;
  140. mNbBVBVTests = 0;
  141. mNbPrimPrimTests = 0;
  142. mNbBVPrimTests = 0;
  143. mPairs.Reset();
  144. return true;
  145. }
  146. }
  147. }
  148. // Here, hulls collide
  149. cache.HullTest = false;
  150. #endif // __MESHMERIZER_H__
  151. // Checkings
  152. if(!Setup(cache.Model0->GetMeshInterface(), cache.Model1->GetMeshInterface())) return false;
  153. // Simple double-dispatch
  154. bool Status;
  155. if(!cache.Model0->HasLeafNodes())
  156. {
  157. if(cache.Model0->IsQuantized())
  158. {
  159. const AABBQuantizedNoLeafTree* T0 = (const AABBQuantizedNoLeafTree*)cache.Model0->GetTree();
  160. const AABBQuantizedNoLeafTree* T1 = (const AABBQuantizedNoLeafTree*)cache.Model1->GetTree();
  161. Status = Collide(T0, T1, world0, world1, &cache);
  162. }
  163. else
  164. {
  165. const AABBNoLeafTree* T0 = (const AABBNoLeafTree*)cache.Model0->GetTree();
  166. const AABBNoLeafTree* T1 = (const AABBNoLeafTree*)cache.Model1->GetTree();
  167. Status = Collide(T0, T1, world0, world1, &cache);
  168. }
  169. }
  170. else
  171. {
  172. if(cache.Model0->IsQuantized())
  173. {
  174. const AABBQuantizedTree* T0 = (const AABBQuantizedTree*)cache.Model0->GetTree();
  175. const AABBQuantizedTree* T1 = (const AABBQuantizedTree*)cache.Model1->GetTree();
  176. Status = Collide(T0, T1, world0, world1, &cache);
  177. }
  178. else
  179. {
  180. const AABBCollisionTree* T0 = (const AABBCollisionTree*)cache.Model0->GetTree();
  181. const AABBCollisionTree* T1 = (const AABBCollisionTree*)cache.Model1->GetTree();
  182. Status = Collide(T0, T1, world0, world1, &cache);
  183. }
  184. }
  185. #ifdef __MESHMERIZER_H__
  186. if(Status)
  187. {
  188. // Reset counter as long as overlap occurs
  189. if(GetContactStatus()) cache.ResetCountDown();
  190. // Enable hull test again when counter reaches zero
  191. cache.CountDown--;
  192. if(!cache.CountDown)
  193. {
  194. cache.ResetCountDown();
  195. cache.HullTest = true;
  196. }
  197. }
  198. #endif
  199. return Status;
  200. }
  201. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  202. /**
  203. * Initializes a collision query :
  204. * - reset stats & contact status
  205. * - setup matrices
  206. *
  207. * \param world0 [in] world matrix for first object
  208. * \param world1 [in] world matrix for second object
  209. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  210. */
  211. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. void AABBTreeCollider::InitQuery(const Matrix4x4* world0, const Matrix4x4* world1)
  213. {
  214. // Reset stats & contact status
  215. Collider::InitQuery();
  216. mNbBVBVTests = 0;
  217. mNbPrimPrimTests = 0;
  218. mNbBVPrimTests = 0;
  219. mPairs.Reset();
  220. // Setup matrices
  221. Matrix4x4 InvWorld0, InvWorld1;
  222. if(world0) InvertPRMatrix(InvWorld0, *world0);
  223. else InvWorld0.Identity();
  224. if(world1) InvertPRMatrix(InvWorld1, *world1);
  225. else InvWorld1.Identity();
  226. Matrix4x4 World0to1 = world0 ? (*world0 * InvWorld1) : InvWorld1;
  227. Matrix4x4 World1to0 = world1 ? (*world1 * InvWorld0) : InvWorld0;
  228. mR0to1 = World0to1; World0to1.GetTrans(mT0to1);
  229. mR1to0 = World1to0; World1to0.GetTrans(mT1to0);
  230. // Precompute absolute 1-to-0 rotation matrix
  231. for(udword i=0;i<3;i++)
  232. {
  233. for(udword j=0;j<3;j++)
  234. {
  235. // Epsilon value prevents floating-point inaccuracies (strategy borrowed from RAPID)
  236. mAR.m[i][j] = 1e-6f + fabsf(mR1to0.m[i][j]);
  237. }
  238. }
  239. }
  240. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  241. /**
  242. * Takes advantage of temporal coherence.
  243. * \param cache [in] cache for a pair of previously colliding primitives
  244. * \return true if we can return immediately
  245. * \warning only works for "First Contact" mode
  246. */
  247. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  248. bool AABBTreeCollider::CheckTemporalCoherence(Pair* cache)
  249. {
  250. // Checkings
  251. if(!cache) return false;
  252. // Test previously colliding primitives first
  253. if(TemporalCoherenceEnabled() && FirstContactEnabled())
  254. {
  255. PrimTest(cache->id0, cache->id1);
  256. if(GetContactStatus()) return true;
  257. }
  258. return false;
  259. }
  260. #define UPDATE_CACHE \
  261. if(cache && GetContactStatus()) \
  262. { \
  263. cache->id0 = mPairs.GetEntry(0); \
  264. cache->id1 = mPairs.GetEntry(1); \
  265. }
  266. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  267. /**
  268. * Collision query for normal AABB trees.
  269. * \param tree0 [in] AABB tree from first object
  270. * \param tree1 [in] AABB tree from second object
  271. * \param world0 [in] world matrix for first object
  272. * \param world1 [in] world matrix for second object
  273. * \param cache [in/out] cache for a pair of previously colliding primitives
  274. * \return true if success
  275. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  276. */
  277. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  278. bool AABBTreeCollider::Collide(const AABBCollisionTree* tree0, const AABBCollisionTree* tree1, const Matrix4x4* world0, const Matrix4x4* world1, Pair* cache)
  279. {
  280. // Init collision query
  281. InitQuery(world0, world1);
  282. // Check previous state
  283. if(CheckTemporalCoherence(cache)) return true;
  284. // Perform collision query
  285. _Collide(tree0->GetNodes(), tree1->GetNodes());
  286. UPDATE_CACHE
  287. return true;
  288. }
  289. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. /**
  291. * Collision query for no-leaf AABB trees.
  292. * \param tree0 [in] AABB tree from first object
  293. * \param tree1 [in] AABB tree from second object
  294. * \param world0 [in] world matrix for first object
  295. * \param world1 [in] world matrix for second object
  296. * \param cache [in/out] cache for a pair of previously colliding primitives
  297. * \return true if success
  298. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  299. */
  300. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  301. bool AABBTreeCollider::Collide(const AABBNoLeafTree* tree0, const AABBNoLeafTree* tree1, const Matrix4x4* world0, const Matrix4x4* world1, Pair* cache)
  302. {
  303. // Init collision query
  304. InitQuery(world0, world1);
  305. // Check previous state
  306. if(CheckTemporalCoherence(cache)) return true;
  307. // Perform collision query
  308. _Collide(tree0->GetNodes(), tree1->GetNodes());
  309. UPDATE_CACHE
  310. return true;
  311. }
  312. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  313. /**
  314. * Collision query for quantized AABB trees.
  315. * \param tree0 [in] AABB tree from first object
  316. * \param tree1 [in] AABB tree from second object
  317. * \param world0 [in] world matrix for first object
  318. * \param world1 [in] world matrix for second object
  319. * \param cache [in/out] cache for a pair of previously colliding primitives
  320. * \return true if success
  321. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  322. */
  323. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  324. bool AABBTreeCollider::Collide(const AABBQuantizedTree* tree0, const AABBQuantizedTree* tree1, const Matrix4x4* world0, const Matrix4x4* world1, Pair* cache)
  325. {
  326. // Init collision query
  327. InitQuery(world0, world1);
  328. // Check previous state
  329. if(CheckTemporalCoherence(cache)) return true;
  330. // Setup dequantization coeffs
  331. mCenterCoeff0 = tree0->mCenterCoeff;
  332. mExtentsCoeff0 = tree0->mExtentsCoeff;
  333. mCenterCoeff1 = tree1->mCenterCoeff;
  334. mExtentsCoeff1 = tree1->mExtentsCoeff;
  335. // Dequantize box A
  336. const AABBQuantizedNode* N0 = tree0->GetNodes();
  337. const Point a(float(N0->mAABB.mExtents[0]) * mExtentsCoeff0.x, float(N0->mAABB.mExtents[1]) * mExtentsCoeff0.y, float(N0->mAABB.mExtents[2]) * mExtentsCoeff0.z);
  338. const Point Pa(float(N0->mAABB.mCenter[0]) * mCenterCoeff0.x, float(N0->mAABB.mCenter[1]) * mCenterCoeff0.y, float(N0->mAABB.mCenter[2]) * mCenterCoeff0.z);
  339. // Dequantize box B
  340. const AABBQuantizedNode* N1 = tree1->GetNodes();
  341. const Point b(float(N1->mAABB.mExtents[0]) * mExtentsCoeff1.x, float(N1->mAABB.mExtents[1]) * mExtentsCoeff1.y, float(N1->mAABB.mExtents[2]) * mExtentsCoeff1.z);
  342. const Point Pb(float(N1->mAABB.mCenter[0]) * mCenterCoeff1.x, float(N1->mAABB.mCenter[1]) * mCenterCoeff1.y, float(N1->mAABB.mCenter[2]) * mCenterCoeff1.z);
  343. // Perform collision query
  344. _Collide(N0, N1, a, Pa, b, Pb);
  345. UPDATE_CACHE
  346. return true;
  347. }
  348. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  349. /**
  350. * Collision query for quantized no-leaf AABB trees.
  351. * \param tree0 [in] AABB tree from first object
  352. * \param tree1 [in] AABB tree from second object
  353. * \param world0 [in] world matrix for first object
  354. * \param world1 [in] world matrix for second object
  355. * \param cache [in/out] cache for a pair of previously colliding primitives
  356. * \return true if success
  357. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  358. */
  359. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  360. bool AABBTreeCollider::Collide(const AABBQuantizedNoLeafTree* tree0, const AABBQuantizedNoLeafTree* tree1, const Matrix4x4* world0, const Matrix4x4* world1, Pair* cache)
  361. {
  362. // Init collision query
  363. InitQuery(world0, world1);
  364. // Check previous state
  365. if(CheckTemporalCoherence(cache)) return true;
  366. // Setup dequantization coeffs
  367. mCenterCoeff0 = tree0->mCenterCoeff;
  368. mExtentsCoeff0 = tree0->mExtentsCoeff;
  369. mCenterCoeff1 = tree1->mCenterCoeff;
  370. mExtentsCoeff1 = tree1->mExtentsCoeff;
  371. // Perform collision query
  372. _Collide(tree0->GetNodes(), tree1->GetNodes());
  373. UPDATE_CACHE
  374. return true;
  375. }
  376. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  377. // Standard trees
  378. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  379. // The normal AABB tree can use 2 different descent rules (with different performances)
  380. //#define ORIGINAL_CODE //!< UNC-like descent rules
  381. #define ALTERNATIVE_CODE //!< Alternative descent rules
  382. #ifdef ORIGINAL_CODE
  383. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  384. /**
  385. * Recursive collision query for normal AABB trees.
  386. * \param b0 [in] collision node from first tree
  387. * \param b1 [in] collision node from second tree
  388. */
  389. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  390. void AABBTreeCollider::_Collide(const AABBCollisionNode* b0, const AABBCollisionNode* b1)
  391. {
  392. // Perform BV-BV overlap test
  393. if(!BoxBoxOverlap(b0->mAABB.mExtents, b0->mAABB.mCenter, b1->mAABB.mExtents, b1->mAABB.mCenter)) return;
  394. if(b0->IsLeaf() && b1->IsLeaf()) { PrimTest(b0->GetPrimitive(), b1->GetPrimitive()); return; }
  395. if(b1->IsLeaf() || (!b0->IsLeaf() && (b0->GetSize() > b1->GetSize())))
  396. {
  397. _Collide(b0->GetNeg(), b1);
  398. if(ContactFound()) return;
  399. _Collide(b0->GetPos(), b1);
  400. }
  401. else
  402. {
  403. _Collide(b0, b1->GetNeg());
  404. if(ContactFound()) return;
  405. _Collide(b0, b1->GetPos());
  406. }
  407. }
  408. #endif
  409. #ifdef ALTERNATIVE_CODE
  410. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  411. /**
  412. * Recursive collision query for normal AABB trees.
  413. * \param b0 [in] collision node from first tree
  414. * \param b1 [in] collision node from second tree
  415. */
  416. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  417. void AABBTreeCollider::_Collide(const AABBCollisionNode* b0, const AABBCollisionNode* b1)
  418. {
  419. // Perform BV-BV overlap test
  420. if(!BoxBoxOverlap(b0->mAABB.mExtents, b0->mAABB.mCenter, b1->mAABB.mExtents, b1->mAABB.mCenter))
  421. {
  422. return;
  423. }
  424. if(b0->IsLeaf())
  425. {
  426. if(b1->IsLeaf())
  427. {
  428. PrimTest(b0->GetPrimitive(), b1->GetPrimitive());
  429. }
  430. else
  431. {
  432. _Collide(b0, b1->GetNeg());
  433. if(ContactFound()) return;
  434. _Collide(b0, b1->GetPos());
  435. }
  436. }
  437. else if(b1->IsLeaf())
  438. {
  439. _Collide(b0->GetNeg(), b1);
  440. if(ContactFound()) return;
  441. _Collide(b0->GetPos(), b1);
  442. }
  443. else
  444. {
  445. _Collide(b0->GetNeg(), b1->GetNeg());
  446. if(ContactFound()) return;
  447. _Collide(b0->GetNeg(), b1->GetPos());
  448. if(ContactFound()) return;
  449. _Collide(b0->GetPos(), b1->GetNeg());
  450. if(ContactFound()) return;
  451. _Collide(b0->GetPos(), b1->GetPos());
  452. }
  453. }
  454. #endif
  455. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  456. // No-leaf trees
  457. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  458. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  459. /**
  460. * Leaf-leaf test for two primitive indices.
  461. * \param id0 [in] index from first leaf-triangle
  462. * \param id1 [in] index from second leaf-triangle
  463. */
  464. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  465. void AABBTreeCollider::PrimTest(udword id0, udword id1)
  466. {
  467. // Request vertices from the app
  468. VertexPointers VP0;
  469. VertexPointers VP1;
  470. mIMesh0->GetTriangle(VP0, id0);
  471. mIMesh1->GetTriangle(VP1, id1);
  472. // Transform from space 1 to space 0
  473. Point u0,u1,u2;
  474. TransformPoint(u0, *VP1.Vertex[0], mR1to0, mT1to0);
  475. TransformPoint(u1, *VP1.Vertex[1], mR1to0, mT1to0);
  476. TransformPoint(u2, *VP1.Vertex[2], mR1to0, mT1to0);
  477. // Perform triangle-triangle overlap test
  478. if(TriTriOverlap(*VP0.Vertex[0], *VP0.Vertex[1], *VP0.Vertex[2], u0, u1, u2))
  479. {
  480. // Keep track of colliding pairs
  481. mPairs.Add(id0).Add(id1);
  482. // Set contact status
  483. mFlags |= OPC_CONTACT;
  484. }
  485. }
  486. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  487. /**
  488. * Leaf-leaf test for a previously fetched triangle from tree A (in B's space) and a new leaf from B.
  489. * \param id1 [in] leaf-triangle index from tree B
  490. */
  491. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  492. inline_ void AABBTreeCollider::PrimTestTriIndex(udword id1)
  493. {
  494. // Request vertices from the app
  495. VertexPointers VP;
  496. mIMesh1->GetTriangle(VP, id1);
  497. // Perform triangle-triangle overlap test
  498. if(TriTriOverlap(mLeafVerts[0], mLeafVerts[1], mLeafVerts[2], *VP.Vertex[0], *VP.Vertex[1], *VP.Vertex[2]))
  499. {
  500. // Keep track of colliding pairs
  501. mPairs.Add(mLeafIndex).Add(id1);
  502. // Set contact status
  503. mFlags |= OPC_CONTACT;
  504. }
  505. }
  506. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  507. /**
  508. * Leaf-leaf test for a previously fetched triangle from tree B (in A's space) and a new leaf from A.
  509. * \param id0 [in] leaf-triangle index from tree A
  510. */
  511. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  512. inline_ void AABBTreeCollider::PrimTestIndexTri(udword id0)
  513. {
  514. // Request vertices from the app
  515. VertexPointers VP;
  516. mIMesh0->GetTriangle(VP, id0);
  517. // Perform triangle-triangle overlap test
  518. if(TriTriOverlap(mLeafVerts[0], mLeafVerts[1], mLeafVerts[2], *VP.Vertex[0], *VP.Vertex[1], *VP.Vertex[2]))
  519. {
  520. // Keep track of colliding pairs
  521. mPairs.Add(id0).Add(mLeafIndex);
  522. // Set contact status
  523. mFlags |= OPC_CONTACT;
  524. }
  525. }
  526. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  527. /**
  528. * Recursive collision of a leaf node from A and a branch from B.
  529. * \param b [in] collision node from second tree
  530. */
  531. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  532. void AABBTreeCollider::_CollideTriBox(const AABBNoLeafNode* b)
  533. {
  534. // Perform triangle-box overlap test
  535. if(!TriBoxOverlap(b->mAABB.mCenter, b->mAABB.mExtents)) return;
  536. // Keep same triangle, deal with first child
  537. if(b->HasPosLeaf()) PrimTestTriIndex(b->GetPosPrimitive());
  538. else _CollideTriBox(b->GetPos());
  539. if(ContactFound()) return;
  540. // Keep same triangle, deal with second child
  541. if(b->HasNegLeaf()) PrimTestTriIndex(b->GetNegPrimitive());
  542. else _CollideTriBox(b->GetNeg());
  543. }
  544. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  545. /**
  546. * Recursive collision of a leaf node from B and a branch from A.
  547. * \param b [in] collision node from first tree
  548. */
  549. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  550. void AABBTreeCollider::_CollideBoxTri(const AABBNoLeafNode* b)
  551. {
  552. // Perform triangle-box overlap test
  553. if(!TriBoxOverlap(b->mAABB.mCenter, b->mAABB.mExtents)) return;
  554. // Keep same triangle, deal with first child
  555. if(b->HasPosLeaf()) PrimTestIndexTri(b->GetPosPrimitive());
  556. else _CollideBoxTri(b->GetPos());
  557. if(ContactFound()) return;
  558. // Keep same triangle, deal with second child
  559. if(b->HasNegLeaf()) PrimTestIndexTri(b->GetNegPrimitive());
  560. else _CollideBoxTri(b->GetNeg());
  561. }
  562. //! Request triangle vertices from the app and transform them
  563. #define FETCH_LEAF(prim_index, imesh, rot, trans) \
  564. mLeafIndex = prim_index; \
  565. /* Request vertices from the app */ \
  566. VertexPointers VP; imesh->GetTriangle(VP, prim_index); \
  567. /* Transform them in a common space */ \
  568. TransformPoint(mLeafVerts[0], *VP.Vertex[0], rot, trans); \
  569. TransformPoint(mLeafVerts[1], *VP.Vertex[1], rot, trans); \
  570. TransformPoint(mLeafVerts[2], *VP.Vertex[2], rot, trans);
  571. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  572. /**
  573. * Recursive collision query for no-leaf AABB trees.
  574. * \param a [in] collision node from first tree
  575. * \param b [in] collision node from second tree
  576. */
  577. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  578. void AABBTreeCollider::_Collide(const AABBNoLeafNode* a, const AABBNoLeafNode* b)
  579. {
  580. // Perform BV-BV overlap test
  581. if(!BoxBoxOverlap(a->mAABB.mExtents, a->mAABB.mCenter, b->mAABB.mExtents, b->mAABB.mCenter)) return;
  582. // Catch leaf status
  583. BOOL BHasPosLeaf = b->HasPosLeaf();
  584. BOOL BHasNegLeaf = b->HasNegLeaf();
  585. if(a->HasPosLeaf())
  586. {
  587. FETCH_LEAF(a->GetPosPrimitive(), mIMesh0, mR0to1, mT0to1)
  588. if(BHasPosLeaf) PrimTestTriIndex(b->GetPosPrimitive());
  589. else _CollideTriBox(b->GetPos());
  590. if(ContactFound()) return;
  591. if(BHasNegLeaf) PrimTestTriIndex(b->GetNegPrimitive());
  592. else _CollideTriBox(b->GetNeg());
  593. }
  594. else
  595. {
  596. if(BHasPosLeaf)
  597. {
  598. FETCH_LEAF(b->GetPosPrimitive(), mIMesh1, mR1to0, mT1to0)
  599. _CollideBoxTri(a->GetPos());
  600. }
  601. else _Collide(a->GetPos(), b->GetPos());
  602. if(ContactFound()) return;
  603. if(BHasNegLeaf)
  604. {
  605. FETCH_LEAF(b->GetNegPrimitive(), mIMesh1, mR1to0, mT1to0)
  606. _CollideBoxTri(a->GetPos());
  607. }
  608. else _Collide(a->GetPos(), b->GetNeg());
  609. }
  610. if(ContactFound()) return;
  611. if(a->HasNegLeaf())
  612. {
  613. FETCH_LEAF(a->GetNegPrimitive(), mIMesh0, mR0to1, mT0to1)
  614. if(BHasPosLeaf) PrimTestTriIndex(b->GetPosPrimitive());
  615. else _CollideTriBox(b->GetPos());
  616. if(ContactFound()) return;
  617. if(BHasNegLeaf) PrimTestTriIndex(b->GetNegPrimitive());
  618. else _CollideTriBox(b->GetNeg());
  619. }
  620. else
  621. {
  622. if(BHasPosLeaf)
  623. {
  624. // ### That leaf has possibly already been fetched
  625. FETCH_LEAF(b->GetPosPrimitive(), mIMesh1, mR1to0, mT1to0)
  626. _CollideBoxTri(a->GetNeg());
  627. }
  628. else _Collide(a->GetNeg(), b->GetPos());
  629. if(ContactFound()) return;
  630. if(BHasNegLeaf)
  631. {
  632. // ### That leaf has possibly already been fetched
  633. FETCH_LEAF(b->GetNegPrimitive(), mIMesh1, mR1to0, mT1to0)
  634. _CollideBoxTri(a->GetNeg());
  635. }
  636. else _Collide(a->GetNeg(), b->GetNeg());
  637. }
  638. }
  639. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  640. // Quantized trees
  641. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  642. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  643. /**
  644. * Recursive collision query for quantized AABB trees.
  645. * \param b0 [in] collision node from first tree
  646. * \param b1 [in] collision node from second tree
  647. * \param a [in] extent from box A
  648. * \param Pa [in] center from box A
  649. * \param b [in] extent from box B
  650. * \param Pb [in] center from box B
  651. */
  652. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  653. void AABBTreeCollider::_Collide(const AABBQuantizedNode* b0, const AABBQuantizedNode* b1, const Point& a, const Point& Pa, const Point& b, const Point& Pb)
  654. {
  655. // Perform BV-BV overlap test
  656. if(!BoxBoxOverlap(a, Pa, b, Pb)) return;
  657. if(b0->IsLeaf() && b1->IsLeaf()) { PrimTest(b0->GetPrimitive(), b1->GetPrimitive()); return; }
  658. if(b1->IsLeaf() || (!b0->IsLeaf() && (b0->GetSize() > b1->GetSize())))
  659. {
  660. // Dequantize box
  661. const QuantizedAABB* Box = &b0->GetNeg()->mAABB;
  662. const Point negPa(float(Box->mCenter[0]) * mCenterCoeff0.x, float(Box->mCenter[1]) * mCenterCoeff0.y, float(Box->mCenter[2]) * mCenterCoeff0.z);
  663. const Point nega(float(Box->mExtents[0]) * mExtentsCoeff0.x, float(Box->mExtents[1]) * mExtentsCoeff0.y, float(Box->mExtents[2]) * mExtentsCoeff0.z);
  664. _Collide(b0->GetNeg(), b1, nega, negPa, b, Pb);
  665. if(ContactFound()) return;
  666. // Dequantize box
  667. Box = &b0->GetPos()->mAABB;
  668. const Point posPa(float(Box->mCenter[0]) * mCenterCoeff0.x, float(Box->mCenter[1]) * mCenterCoeff0.y, float(Box->mCenter[2]) * mCenterCoeff0.z);
  669. const Point posa(float(Box->mExtents[0]) * mExtentsCoeff0.x, float(Box->mExtents[1]) * mExtentsCoeff0.y, float(Box->mExtents[2]) * mExtentsCoeff0.z);
  670. _Collide(b0->GetPos(), b1, posa, posPa, b, Pb);
  671. }
  672. else
  673. {
  674. // Dequantize box
  675. const QuantizedAABB* Box = &b1->GetNeg()->mAABB;
  676. const Point negPb(float(Box->mCenter[0]) * mCenterCoeff1.x, float(Box->mCenter[1]) * mCenterCoeff1.y, float(Box->mCenter[2]) * mCenterCoeff1.z);
  677. const Point negb(float(Box->mExtents[0]) * mExtentsCoeff1.x, float(Box->mExtents[1]) * mExtentsCoeff1.y, float(Box->mExtents[2]) * mExtentsCoeff1.z);
  678. _Collide(b0, b1->GetNeg(), a, Pa, negb, negPb);
  679. if(ContactFound()) return;
  680. // Dequantize box
  681. Box = &b1->GetPos()->mAABB;
  682. const Point posPb(float(Box->mCenter[0]) * mCenterCoeff1.x, float(Box->mCenter[1]) * mCenterCoeff1.y, float(Box->mCenter[2]) * mCenterCoeff1.z);
  683. const Point posb(float(Box->mExtents[0]) * mExtentsCoeff1.x, float(Box->mExtents[1]) * mExtentsCoeff1.y, float(Box->mExtents[2]) * mExtentsCoeff1.z);
  684. _Collide(b0, b1->GetPos(), a, Pa, posb, posPb);
  685. }
  686. }
  687. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  688. // Quantized no-leaf trees
  689. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  690. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  691. /**
  692. * Recursive collision of a leaf node from A and a quantized branch from B.
  693. * \param leaf [in] leaf triangle from first tree
  694. * \param b [in] collision node from second tree
  695. */
  696. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  697. void AABBTreeCollider::_CollideTriBox(const AABBQuantizedNoLeafNode* b)
  698. {
  699. // Dequantize box
  700. const QuantizedAABB* bb = &b->mAABB;
  701. const Point Pb(float(bb->mCenter[0]) * mCenterCoeff1.x, float(bb->mCenter[1]) * mCenterCoeff1.y, float(bb->mCenter[2]) * mCenterCoeff1.z);
  702. const Point eb(float(bb->mExtents[0]) * mExtentsCoeff1.x, float(bb->mExtents[1]) * mExtentsCoeff1.y, float(bb->mExtents[2]) * mExtentsCoeff1.z);
  703. // Perform triangle-box overlap test
  704. if(!TriBoxOverlap(Pb, eb)) return;
  705. if(b->HasPosLeaf()) PrimTestTriIndex(b->GetPosPrimitive());
  706. else _CollideTriBox(b->GetPos());
  707. if(ContactFound()) return;
  708. if(b->HasNegLeaf()) PrimTestTriIndex(b->GetNegPrimitive());
  709. else _CollideTriBox(b->GetNeg());
  710. }
  711. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  712. /**
  713. * Recursive collision of a leaf node from B and a quantized branch from A.
  714. * \param b [in] collision node from first tree
  715. * \param leaf [in] leaf triangle from second tree
  716. */
  717. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  718. void AABBTreeCollider::_CollideBoxTri(const AABBQuantizedNoLeafNode* b)
  719. {
  720. // Dequantize box
  721. const QuantizedAABB* bb = &b->mAABB;
  722. const Point Pa(float(bb->mCenter[0]) * mCenterCoeff0.x, float(bb->mCenter[1]) * mCenterCoeff0.y, float(bb->mCenter[2]) * mCenterCoeff0.z);
  723. const Point ea(float(bb->mExtents[0]) * mExtentsCoeff0.x, float(bb->mExtents[1]) * mExtentsCoeff0.y, float(bb->mExtents[2]) * mExtentsCoeff0.z);
  724. // Perform triangle-box overlap test
  725. if(!TriBoxOverlap(Pa, ea)) return;
  726. if(b->HasPosLeaf()) PrimTestIndexTri(b->GetPosPrimitive());
  727. else _CollideBoxTri(b->GetPos());
  728. if(ContactFound()) return;
  729. if(b->HasNegLeaf()) PrimTestIndexTri(b->GetNegPrimitive());
  730. else _CollideBoxTri(b->GetNeg());
  731. }
  732. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  733. /**
  734. * Recursive collision query for quantized no-leaf AABB trees.
  735. * \param a [in] collision node from first tree
  736. * \param b [in] collision node from second tree
  737. */
  738. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  739. void AABBTreeCollider::_Collide(const AABBQuantizedNoLeafNode* a, const AABBQuantizedNoLeafNode* b)
  740. {
  741. // Dequantize box A
  742. const QuantizedAABB* ab = &a->mAABB;
  743. const Point Pa(float(ab->mCenter[0]) * mCenterCoeff0.x, float(ab->mCenter[1]) * mCenterCoeff0.y, float(ab->mCenter[2]) * mCenterCoeff0.z);
  744. const Point ea(float(ab->mExtents[0]) * mExtentsCoeff0.x, float(ab->mExtents[1]) * mExtentsCoeff0.y, float(ab->mExtents[2]) * mExtentsCoeff0.z);
  745. // Dequantize box B
  746. const QuantizedAABB* bb = &b->mAABB;
  747. const Point Pb(float(bb->mCenter[0]) * mCenterCoeff1.x, float(bb->mCenter[1]) * mCenterCoeff1.y, float(bb->mCenter[2]) * mCenterCoeff1.z);
  748. const Point eb(float(bb->mExtents[0]) * mExtentsCoeff1.x, float(bb->mExtents[1]) * mExtentsCoeff1.y, float(bb->mExtents[2]) * mExtentsCoeff1.z);
  749. // Perform BV-BV overlap test
  750. if(!BoxBoxOverlap(ea, Pa, eb, Pb)) return;
  751. // Catch leaf status
  752. BOOL BHasPosLeaf = b->HasPosLeaf();
  753. BOOL BHasNegLeaf = b->HasNegLeaf();
  754. if(a->HasPosLeaf())
  755. {
  756. FETCH_LEAF(a->GetPosPrimitive(), mIMesh0, mR0to1, mT0to1)
  757. if(BHasPosLeaf) PrimTestTriIndex(b->GetPosPrimitive());
  758. else _CollideTriBox(b->GetPos());
  759. if(ContactFound()) return;
  760. if(BHasNegLeaf) PrimTestTriIndex(b->GetNegPrimitive());
  761. else _CollideTriBox(b->GetNeg());
  762. }
  763. else
  764. {
  765. if(BHasPosLeaf)
  766. {
  767. FETCH_LEAF(b->GetPosPrimitive(), mIMesh1, mR1to0, mT1to0)
  768. _CollideBoxTri(a->GetPos());
  769. }
  770. else _Collide(a->GetPos(), b->GetPos());
  771. if(ContactFound()) return;
  772. if(BHasNegLeaf)
  773. {
  774. FETCH_LEAF(b->GetNegPrimitive(), mIMesh1, mR1to0, mT1to0)
  775. _CollideBoxTri(a->GetPos());
  776. }
  777. else _Collide(a->GetPos(), b->GetNeg());
  778. }
  779. if(ContactFound()) return;
  780. if(a->HasNegLeaf())
  781. {
  782. FETCH_LEAF(a->GetNegPrimitive(), mIMesh0, mR0to1, mT0to1)
  783. if(BHasPosLeaf) PrimTestTriIndex(b->GetPosPrimitive());
  784. else _CollideTriBox(b->GetPos());
  785. if(ContactFound()) return;
  786. if(BHasNegLeaf) PrimTestTriIndex(b->GetNegPrimitive());
  787. else _CollideTriBox(b->GetNeg());
  788. }
  789. else
  790. {
  791. if(BHasPosLeaf)
  792. {
  793. // ### That leaf has possibly already been fetched
  794. FETCH_LEAF(b->GetPosPrimitive(), mIMesh1, mR1to0, mT1to0)
  795. _CollideBoxTri(a->GetNeg());
  796. }
  797. else _Collide(a->GetNeg(), b->GetPos());
  798. if(ContactFound()) return;
  799. if(BHasNegLeaf)
  800. {
  801. // ### That leaf has possibly already been fetched
  802. FETCH_LEAF(b->GetNegPrimitive(), mIMesh1, mR1to0, mT1to0)
  803. _CollideBoxTri(a->GetNeg());
  804. }
  805. else _Collide(a->GetNeg(), b->GetNeg());
  806. }
  807. }