OPC_RayCollider.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 ray collider.
  11. * \file OPC_RayCollider.cpp
  12. * \author Pierre Terdiman
  13. * \date June, 2, 2001
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * Contains a ray-vs-tree collider.
  19. * This class performs a stabbing query on an AABB tree, i.e. does a ray-mesh collision.
  20. *
  21. * HIGHER DISTANCE BOUND:
  22. *
  23. * If P0 and P1 are two 3D points, let's define:
  24. * - d = distance between P0 and P1
  25. * - Origin = P0
  26. * - Direction = (P1 - P0) / d = normalized direction vector
  27. * - A parameter t such as a point P on the line (P0,P1) is P = Origin + t * Direction
  28. * - t = 0 --> P = P0
  29. * - t = d --> P = P1
  30. *
  31. * Then we can define a general "ray" as:
  32. *
  33. * struct Ray
  34. * {
  35. * Point Origin;
  36. * Point Direction;
  37. * };
  38. *
  39. * But it actually maps three different things:
  40. * - a segment, when 0 <= t <= d
  41. * - a half-line, when 0 <= t < +infinity, or -infinity < t <= d
  42. * - a line, when -infinity < t < +infinity
  43. *
  44. * In Opcode, we support segment queries, which yield half-line queries by setting d = +infinity.
  45. * We don't support line-queries. If you need them, shift the origin along the ray by an appropriate margin.
  46. *
  47. * In short, the lower bound is always 0, and you can setup the higher bound "d" with RayCollider::SetMaxDist().
  48. *
  49. * Query |segment |half-line |line
  50. * --------|-------------------|---------------|----------------
  51. * Usages |-shadow feelers |-raytracing |-
  52. * |-sweep tests |-in/out tests |
  53. *
  54. * FIRST CONTACT:
  55. *
  56. * - You can setup "first contact" mode or "all contacts" mode with RayCollider::SetFirstContact().
  57. * - In "first contact" mode we return as soon as the ray hits one face. If can be useful e.g. for shadow feelers, where
  58. * you want to know whether the path to the light is free or not (a boolean answer is enough).
  59. * - In "all contacts" mode we return all faces hit by the ray.
  60. *
  61. * TEMPORAL COHERENCE:
  62. *
  63. * - You can enable or disable temporal coherence with RayCollider::SetTemporalCoherence().
  64. * - It currently only works in "first contact" mode.
  65. * - If temporal coherence is enabled, the previously hit triangle is cached during the first query. Then, next queries
  66. * start by colliding the ray against the cached triangle. If they still collide, we return immediately.
  67. *
  68. * CLOSEST HIT:
  69. *
  70. * - You can enable or disable "closest hit" with RayCollider::SetClosestHit().
  71. * - It currently only works in "all contacts" mode.
  72. * - If closest hit is enabled, faces are sorted by distance on-the-fly and the closest one only is reported.
  73. *
  74. * BACKFACE CULLING:
  75. *
  76. * - You can enable or disable backface culling with RayCollider::SetCulling().
  77. * - If culling is enabled, ray will not hit back faces (only front faces).
  78. *
  79. *
  80. *
  81. * \class RayCollider
  82. * \author Pierre Terdiman
  83. * \version 1.3
  84. * \date June, 2, 2001
  85. */
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. /**
  89. * This class describes a face hit by a ray or segment.
  90. * This is a particular class dedicated to stabbing queries.
  91. *
  92. * \class CollisionFace
  93. * \author Pierre Terdiman
  94. * \version 1.3
  95. * \date March, 20, 2001
  96. */
  97. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. /**
  100. * This class is a dedicated collection of CollisionFace.
  101. *
  102. * \class CollisionFaces
  103. * \author Pierre Terdiman
  104. * \version 1.3
  105. * \date March, 20, 2001
  106. */
  107. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  108. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. #include "Opcode.h"
  110. using namespace Opcode;
  111. #include "OPC_RayAABBOverlap.h"
  112. #include "OPC_RayTriOverlap.h"
  113. #define SET_CONTACT(prim_index, flag) \
  114. mNbIntersections++; \
  115. /* Set contact status */ \
  116. mFlags |= flag; \
  117. /* In any case the contact has been found and recorded in mStabbedFace */ \
  118. mStabbedFace.mFaceID = prim_index;
  119. #ifdef OPC_RAYHIT_CALLBACK
  120. #define HANDLE_CONTACT(prim_index, flag) \
  121. SET_CONTACT(prim_index, flag) \
  122. \
  123. if(mHitCallback) (mHitCallback)(mStabbedFace, mUserData);
  124. #define UPDATE_CACHE \
  125. if(cache && GetContactStatus()) \
  126. { \
  127. *cache = mStabbedFace.mFaceID; \
  128. }
  129. #else
  130. #define HANDLE_CONTACT(prim_index, flag) \
  131. SET_CONTACT(prim_index, flag) \
  132. \
  133. /* Now we can also record it in mStabbedFaces if available */ \
  134. if(mStabbedFaces) \
  135. { \
  136. /* If we want all faces or if that's the first one we hit */ \
  137. if(!mClosestHit || !mStabbedFaces->GetNbFaces()) \
  138. { \
  139. mStabbedFaces->AddFace(mStabbedFace); \
  140. } \
  141. else \
  142. { \
  143. /* We only keep closest hit */ \
  144. CollisionFace* Current = const_cast<CollisionFace*>(mStabbedFaces->GetFaces()); \
  145. if(Current && mStabbedFace.mDistance<Current->mDistance) \
  146. { \
  147. *Current = mStabbedFace; \
  148. } \
  149. } \
  150. }
  151. #define UPDATE_CACHE \
  152. if(cache && GetContactStatus() && mStabbedFaces) \
  153. { \
  154. const CollisionFace* Current = mStabbedFaces->GetFaces(); \
  155. if(Current) *cache = Current->mFaceID; \
  156. else *cache = INVALID_ID_OPC; \
  157. }
  158. #endif
  159. #define SEGMENT_PRIM(prim_index, flag) \
  160. /* Request vertices from the app */ \
  161. VertexPointers VP; mIMesh->GetTriangle(VP, prim_index); \
  162. \
  163. /* Perform ray-tri overlap test and return */ \
  164. if(RayTriOverlap(*VP.Vertex[0], *VP.Vertex[1], *VP.Vertex[2])) \
  165. { \
  166. /* Intersection point is valid if dist < segment's length */ \
  167. /* We know dist>0 so we can use integers */ \
  168. if(IR(mStabbedFace.mDistance)<IR(mMaxDist)) \
  169. { \
  170. HANDLE_CONTACT(prim_index, flag) \
  171. } \
  172. }
  173. #define RAY_PRIM(prim_index, flag) \
  174. /* Request vertices from the app */ \
  175. VertexPointers VP; mIMesh->GetTriangle(VP, prim_index); \
  176. \
  177. /* Perform ray-tri overlap test and return */ \
  178. if(RayTriOverlap(*VP.Vertex[0], *VP.Vertex[1], *VP.Vertex[2])) \
  179. { \
  180. HANDLE_CONTACT(prim_index, flag) \
  181. }
  182. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. /**
  184. * Constructor.
  185. */
  186. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  187. RayCollider::RayCollider() :
  188. mNbRayBVTests (0),
  189. mNbRayPrimTests (0),
  190. mNbIntersections (0),
  191. mCulling (true),
  192. #ifdef OPC_RAYHIT_CALLBACK
  193. mHitCallback (null),
  194. mUserData (0),
  195. #else
  196. mClosestHit (false),
  197. mStabbedFaces (null),
  198. #endif
  199. mMaxDist (MAX_FLOAT)
  200. {
  201. }
  202. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. /**
  204. * Destructor.
  205. */
  206. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. RayCollider::~RayCollider()
  208. {
  209. }
  210. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. /**
  212. * Validates current settings. You should call this method after all the settings and callbacks have been defined.
  213. * \return null if everything is ok, else a string describing the problem
  214. */
  215. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216. const char* RayCollider::ValidateSettings()
  217. {
  218. if(mMaxDist<0.0f) return "Higher distance bound must be positive!";
  219. if(TemporalCoherenceEnabled() && !FirstContactEnabled()) return "Temporal coherence only works with ""First contact"" mode!";
  220. #ifndef OPC_RAYHIT_CALLBACK
  221. if(mClosestHit && FirstContactEnabled()) return "Closest hit doesn't work with ""First contact"" mode!";
  222. if(TemporalCoherenceEnabled() && mClosestHit) return "Temporal coherence can't guarantee to report closest hit!";
  223. #endif
  224. if(SkipPrimitiveTests()) return "SkipPrimitiveTests not possible for RayCollider ! (not implemented)";
  225. return null;
  226. }
  227. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  228. /**
  229. * Generic stabbing query for generic OPCODE models. After the call, access the results:
  230. * - with GetContactStatus()
  231. * - in the user-provided destination array
  232. *
  233. * \param world_ray [in] stabbing ray in world space
  234. * \param model [in] Opcode model to collide with
  235. * \param world [in] model's world matrix, or null
  236. * \param cache [in] a possibly cached face index, or null
  237. * \return true if success
  238. * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
  239. */
  240. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  241. bool RayCollider::Collide(const Ray& world_ray, const Model& model, const Matrix4x4* world, udword* cache)
  242. {
  243. // Checkings
  244. if(!Setup(&model)) return false;
  245. // Init collision query
  246. if(InitQuery(world_ray, world, cache)) return true;
  247. if(!model.HasLeafNodes())
  248. {
  249. if(model.IsQuantized())
  250. {
  251. const AABBQuantizedNoLeafTree* Tree = (const AABBQuantizedNoLeafTree*)model.GetTree();
  252. // Setup dequantization coeffs
  253. mCenterCoeff = Tree->mCenterCoeff;
  254. mExtentsCoeff = Tree->mExtentsCoeff;
  255. // Perform stabbing query
  256. if(IR(mMaxDist)!=IEEE_MAX_FLOAT) _SegmentStab(Tree->GetNodes());
  257. else _RayStab(Tree->GetNodes());
  258. }
  259. else
  260. {
  261. const AABBNoLeafTree* Tree = (const AABBNoLeafTree*)model.GetTree();
  262. // Perform stabbing query
  263. if(IR(mMaxDist)!=IEEE_MAX_FLOAT) _SegmentStab(Tree->GetNodes());
  264. else _RayStab(Tree->GetNodes());
  265. }
  266. }
  267. else
  268. {
  269. if(model.IsQuantized())
  270. {
  271. const AABBQuantizedTree* Tree = (const AABBQuantizedTree*)model.GetTree();
  272. // Setup dequantization coeffs
  273. mCenterCoeff = Tree->mCenterCoeff;
  274. mExtentsCoeff = Tree->mExtentsCoeff;
  275. // Perform stabbing query
  276. if(IR(mMaxDist)!=IEEE_MAX_FLOAT) _SegmentStab(Tree->GetNodes());
  277. else _RayStab(Tree->GetNodes());
  278. }
  279. else
  280. {
  281. const AABBCollisionTree* Tree = (const AABBCollisionTree*)model.GetTree();
  282. // Perform stabbing query
  283. if(IR(mMaxDist)!=IEEE_MAX_FLOAT) _SegmentStab(Tree->GetNodes());
  284. else _RayStab(Tree->GetNodes());
  285. }
  286. }
  287. // Update cache if needed
  288. UPDATE_CACHE
  289. return true;
  290. }
  291. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  292. /**
  293. * Initializes a stabbing query :
  294. * - reset stats & contact status
  295. * - compute ray in local space
  296. * - check temporal coherence
  297. *
  298. * \param world_ray [in] stabbing ray in world space
  299. * \param world [in] object's world matrix, or null
  300. * \param face_id [in] index of previously stabbed triangle
  301. * \return TRUE if we can return immediately
  302. * \warning SCALE NOT SUPPORTED. The matrix must contain rotation & translation parts only.
  303. */
  304. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  305. BOOL RayCollider::InitQuery(const Ray& world_ray, const Matrix4x4* world, udword* face_id)
  306. {
  307. // Reset stats & contact status
  308. Collider::InitQuery();
  309. mNbRayBVTests = 0;
  310. mNbRayPrimTests = 0;
  311. mNbIntersections = 0;
  312. #ifndef OPC_RAYHIT_CALLBACK
  313. if(mStabbedFaces) mStabbedFaces->Reset();
  314. #endif
  315. // Compute ray in local space
  316. // The (Origin/Dir) form is needed for the ray-triangle test anyway (even for segment tests)
  317. if(world)
  318. {
  319. Matrix3x3 InvWorld = *world;
  320. mDir = InvWorld * world_ray.mDir;
  321. Matrix4x4 World;
  322. InvertPRMatrix(World, *world);
  323. mOrigin = world_ray.mOrig * World;
  324. }
  325. else
  326. {
  327. mDir = world_ray.mDir;
  328. mOrigin = world_ray.mOrig;
  329. }
  330. // 4) Special case: 1-triangle meshes [Opcode 1.3]
  331. if(mCurrentModel && mCurrentModel->HasSingleNode())
  332. {
  333. // We simply perform the BV-Prim overlap test each time. We assume single triangle has index 0.
  334. if(!SkipPrimitiveTests())
  335. {
  336. // Perform overlap test between the unique triangle and the ray (and set contact status if needed)
  337. SEGMENT_PRIM(udword(0), OPC_CONTACT)
  338. // Return immediately regardless of status
  339. return TRUE;
  340. }
  341. }
  342. // Check temporal coherence :
  343. // Test previously colliding primitives first
  344. if(TemporalCoherenceEnabled() && FirstContactEnabled() && face_id && *face_id!=INVALID_ID_OPC)
  345. {
  346. #ifdef OLD_CODE
  347. #ifndef OPC_RAYHIT_CALLBACK
  348. if(!mClosestHit)
  349. #endif
  350. {
  351. // Request vertices from the app
  352. VertexPointers VP;
  353. mIMesh->GetTriangle(VP, *face_id);
  354. // Perform ray-cached tri overlap test
  355. if(RayTriOverlap(*VP.Vertex[0], *VP.Vertex[1], *VP.Vertex[2]))
  356. {
  357. // Intersection point is valid if:
  358. // - distance is positive (else it can just be a face behind the orig point)
  359. // - distance is smaller than a given max distance (useful for shadow feelers)
  360. // if(mStabbedFace.mDistance>0.0f && mStabbedFace.mDistance<mMaxDist)
  361. if(IR(mStabbedFace.mDistance)<IR(mMaxDist)) // The other test is already performed in RayTriOverlap
  362. {
  363. // Set contact status
  364. mFlags |= OPC_TEMPORAL_CONTACT;
  365. mStabbedFace.mFaceID = *face_id;
  366. #ifndef OPC_RAYHIT_CALLBACK
  367. if(mStabbedFaces) mStabbedFaces->AddFace(mStabbedFace);
  368. #endif
  369. return TRUE;
  370. }
  371. }
  372. }
  373. #else
  374. // New code
  375. // We handle both Segment/ray queries with the same segment code, and a possible infinite limit
  376. SEGMENT_PRIM(*face_id, OPC_TEMPORAL_CONTACT)
  377. // Return immediately if possible
  378. if(GetContactStatus()) return TRUE;
  379. #endif
  380. }
  381. // Precompute data (moved after temporal coherence since only needed for ray-AABB)
  382. if(IR(mMaxDist)!=IEEE_MAX_FLOAT)
  383. {
  384. // For Segment-AABB overlap
  385. mData = 0.5f * mDir * mMaxDist;
  386. mData2 = mOrigin + mData;
  387. // Precompute mFDir;
  388. mFDir.x = fabsf(mData.x);
  389. mFDir.y = fabsf(mData.y);
  390. mFDir.z = fabsf(mData.z);
  391. }
  392. else
  393. {
  394. // For Ray-AABB overlap
  395. // udword x = SIR(mDir.x)-1;
  396. // udword y = SIR(mDir.y)-1;
  397. // udword z = SIR(mDir.z)-1;
  398. // mData.x = FR(x);
  399. // mData.y = FR(y);
  400. // mData.z = FR(z);
  401. // Precompute mFDir;
  402. mFDir.x = fabsf(mDir.x);
  403. mFDir.y = fabsf(mDir.y);
  404. mFDir.z = fabsf(mDir.z);
  405. }
  406. return FALSE;
  407. }
  408. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  409. /**
  410. * Stabbing query for vanilla AABB trees.
  411. * \param world_ray [in] stabbing ray in world space
  412. * \param tree [in] AABB tree
  413. * \param box_indices [out] indices of stabbed boxes
  414. * \return true if success
  415. */
  416. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  417. bool RayCollider::Collide(const Ray& world_ray, const AABBTree* tree, OPC_Container& box_indices)
  418. {
  419. // ### bad design here
  420. // This is typically called for a scene tree, full of -AABBs-, not full of triangles.
  421. // So we don't really have "primitives" to deal with. Hence it doesn't work with
  422. // "FirstContact" + "TemporalCoherence".
  423. ASSERT( !(FirstContactEnabled() && TemporalCoherenceEnabled()) );
  424. // Checkings
  425. if(!tree) return false;
  426. // Init collision query
  427. // Basically this is only called to initialize precomputed data
  428. if(InitQuery(world_ray)) return true;
  429. // Perform stabbing query
  430. if(IR(mMaxDist)!=IEEE_MAX_FLOAT) _SegmentStab(tree, box_indices);
  431. else _RayStab(tree, box_indices);
  432. return true;
  433. }
  434. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  435. /**
  436. * Recursive stabbing query for normal AABB trees.
  437. * \param node [in] current collision node
  438. */
  439. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  440. void RayCollider::_SegmentStab(const AABBCollisionNode* node)
  441. {
  442. // Perform Segment-AABB overlap test
  443. if(!SegmentAABBOverlap(node->mAABB.mCenter, node->mAABB.mExtents)) return;
  444. if(node->IsLeaf())
  445. {
  446. SEGMENT_PRIM(node->GetPrimitive(), OPC_CONTACT)
  447. }
  448. else
  449. {
  450. _SegmentStab(node->GetPos());
  451. if(ContactFound()) return;
  452. _SegmentStab(node->GetNeg());
  453. }
  454. }
  455. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  456. /**
  457. * Recursive stabbing query for quantized AABB trees.
  458. * \param node [in] current collision node
  459. */
  460. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  461. void RayCollider::_SegmentStab(const AABBQuantizedNode* node)
  462. {
  463. // Dequantize box
  464. const QuantizedAABB& Box = node->mAABB;
  465. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  466. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  467. // Perform Segment-AABB overlap test
  468. if(!SegmentAABBOverlap(Center, Extents)) return;
  469. if(node->IsLeaf())
  470. {
  471. SEGMENT_PRIM(node->GetPrimitive(), OPC_CONTACT)
  472. }
  473. else
  474. {
  475. _SegmentStab(node->GetPos());
  476. if(ContactFound()) return;
  477. _SegmentStab(node->GetNeg());
  478. }
  479. }
  480. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  481. /**
  482. * Recursive stabbing query for no-leaf AABB trees.
  483. * \param node [in] current collision node
  484. */
  485. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  486. void RayCollider::_SegmentStab(const AABBNoLeafNode* node)
  487. {
  488. // Perform Segment-AABB overlap test
  489. if(!SegmentAABBOverlap(node->mAABB.mCenter, node->mAABB.mExtents)) return;
  490. if(node->HasPosLeaf())
  491. {
  492. SEGMENT_PRIM(node->GetPosPrimitive(), OPC_CONTACT)
  493. }
  494. else _SegmentStab(node->GetPos());
  495. if(ContactFound()) return;
  496. if(node->HasNegLeaf())
  497. {
  498. SEGMENT_PRIM(node->GetNegPrimitive(), OPC_CONTACT)
  499. }
  500. else _SegmentStab(node->GetNeg());
  501. }
  502. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  503. /**
  504. * Recursive stabbing query for quantized no-leaf AABB trees.
  505. * \param node [in] current collision node
  506. */
  507. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  508. void RayCollider::_SegmentStab(const AABBQuantizedNoLeafNode* node)
  509. {
  510. // Dequantize box
  511. const QuantizedAABB& Box = node->mAABB;
  512. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  513. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  514. // Perform Segment-AABB overlap test
  515. if(!SegmentAABBOverlap(Center, Extents)) return;
  516. if(node->HasPosLeaf())
  517. {
  518. SEGMENT_PRIM(node->GetPosPrimitive(), OPC_CONTACT)
  519. }
  520. else _SegmentStab(node->GetPos());
  521. if(ContactFound()) return;
  522. if(node->HasNegLeaf())
  523. {
  524. SEGMENT_PRIM(node->GetNegPrimitive(), OPC_CONTACT)
  525. }
  526. else _SegmentStab(node->GetNeg());
  527. }
  528. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  529. /**
  530. * Recursive stabbing query for vanilla AABB trees.
  531. * \param node [in] current collision node
  532. * \param box_indices [out] indices of stabbed boxes
  533. */
  534. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  535. void RayCollider::_SegmentStab(const AABBTreeNode* node, OPC_Container& box_indices)
  536. {
  537. // Test the box against the segment
  538. Point Center, Extents;
  539. node->GetAABB()->GetCenter(Center);
  540. node->GetAABB()->GetExtents(Extents);
  541. if(!SegmentAABBOverlap(Center, Extents)) return;
  542. if(node->IsLeaf())
  543. {
  544. box_indices.Add(node->GetPrimitives(), node->GetNbPrimitives());
  545. }
  546. else
  547. {
  548. _SegmentStab(node->GetPos(), box_indices);
  549. _SegmentStab(node->GetNeg(), box_indices);
  550. }
  551. }
  552. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  553. /**
  554. * Recursive stabbing query for normal AABB trees.
  555. * \param node [in] current collision node
  556. */
  557. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  558. void RayCollider::_RayStab(const AABBCollisionNode* node)
  559. {
  560. // Perform Ray-AABB overlap test
  561. if(!RayAABBOverlap(node->mAABB.mCenter, node->mAABB.mExtents)) return;
  562. if(node->IsLeaf())
  563. {
  564. RAY_PRIM(node->GetPrimitive(), OPC_CONTACT)
  565. }
  566. else
  567. {
  568. _RayStab(node->GetPos());
  569. if(ContactFound()) return;
  570. _RayStab(node->GetNeg());
  571. }
  572. }
  573. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  574. /**
  575. * Recursive stabbing query for quantized AABB trees.
  576. * \param node [in] current collision node
  577. */
  578. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  579. void RayCollider::_RayStab(const AABBQuantizedNode* node)
  580. {
  581. // Dequantize box
  582. const QuantizedAABB& Box = node->mAABB;
  583. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  584. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  585. // Perform Ray-AABB overlap test
  586. if(!RayAABBOverlap(Center, Extents)) return;
  587. if(node->IsLeaf())
  588. {
  589. RAY_PRIM(node->GetPrimitive(), OPC_CONTACT)
  590. }
  591. else
  592. {
  593. _RayStab(node->GetPos());
  594. if(ContactFound()) return;
  595. _RayStab(node->GetNeg());
  596. }
  597. }
  598. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  599. /**
  600. * Recursive stabbing query for no-leaf AABB trees.
  601. * \param node [in] current collision node
  602. */
  603. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  604. void RayCollider::_RayStab(const AABBNoLeafNode* node)
  605. {
  606. // Perform Ray-AABB overlap test
  607. if(!RayAABBOverlap(node->mAABB.mCenter, node->mAABB.mExtents)) return;
  608. if(node->HasPosLeaf())
  609. {
  610. RAY_PRIM(node->GetPosPrimitive(), OPC_CONTACT)
  611. }
  612. else _RayStab(node->GetPos());
  613. if(ContactFound()) return;
  614. if(node->HasNegLeaf())
  615. {
  616. RAY_PRIM(node->GetNegPrimitive(), OPC_CONTACT)
  617. }
  618. else _RayStab(node->GetNeg());
  619. }
  620. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  621. /**
  622. * Recursive stabbing query for quantized no-leaf AABB trees.
  623. * \param node [in] current collision node
  624. */
  625. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  626. void RayCollider::_RayStab(const AABBQuantizedNoLeafNode* node)
  627. {
  628. // Dequantize box
  629. const QuantizedAABB& Box = node->mAABB;
  630. const Point Center(float(Box.mCenter[0]) * mCenterCoeff.x, float(Box.mCenter[1]) * mCenterCoeff.y, float(Box.mCenter[2]) * mCenterCoeff.z);
  631. const Point Extents(float(Box.mExtents[0]) * mExtentsCoeff.x, float(Box.mExtents[1]) * mExtentsCoeff.y, float(Box.mExtents[2]) * mExtentsCoeff.z);
  632. // Perform Ray-AABB overlap test
  633. if(!RayAABBOverlap(Center, Extents)) return;
  634. if(node->HasPosLeaf())
  635. {
  636. RAY_PRIM(node->GetPosPrimitive(), OPC_CONTACT)
  637. }
  638. else _RayStab(node->GetPos());
  639. if(ContactFound()) return;
  640. if(node->HasNegLeaf())
  641. {
  642. RAY_PRIM(node->GetNegPrimitive(), OPC_CONTACT)
  643. }
  644. else _RayStab(node->GetNeg());
  645. }
  646. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  647. /**
  648. * Recursive stabbing query for vanilla AABB trees.
  649. * \param node [in] current collision node
  650. * \param box_indices [out] indices of stabbed boxes
  651. */
  652. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  653. void RayCollider::_RayStab(const AABBTreeNode* node, OPC_Container& box_indices)
  654. {
  655. // Test the box against the ray
  656. Point Center, Extents;
  657. node->GetAABB()->GetCenter(Center);
  658. node->GetAABB()->GetExtents(Extents);
  659. if(!RayAABBOverlap(Center, Extents)) return;
  660. if(node->IsLeaf())
  661. {
  662. mFlags |= OPC_CONTACT;
  663. box_indices.Add(node->GetPrimitives(), node->GetNbPrimitives());
  664. }
  665. else
  666. {
  667. _RayStab(node->GetPos(), box_indices);
  668. _RayStab(node->GetNeg(), box_indices);
  669. }
  670. }