OPC_TreeCollider.cpp 38 KB

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