b3DynamicBvh.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///b3DynamicBvh implementation by Nathanael Presson
  14. #ifndef B3_DYNAMIC_BOUNDING_VOLUME_TREE_H
  15. #define B3_DYNAMIC_BOUNDING_VOLUME_TREE_H
  16. #include "Bullet3Common/b3AlignedObjectArray.h"
  17. #include "Bullet3Common/b3Vector3.h"
  18. #include "Bullet3Common/b3Transform.h"
  19. #include "Bullet3Geometry/b3AabbUtil.h"
  20. //
  21. // Compile time configuration
  22. //
  23. // Implementation profiles
  24. #define B3_DBVT_IMPL_GENERIC 0 // Generic implementation
  25. #define B3_DBVT_IMPL_SSE 1 // SSE
  26. // Template implementation of ICollide
  27. #ifdef _WIN32
  28. #if (defined(_MSC_VER) && _MSC_VER >= 1400)
  29. #define B3_DBVT_USE_TEMPLATE 1
  30. #else
  31. #define B3_DBVT_USE_TEMPLATE 0
  32. #endif
  33. #else
  34. #define B3_DBVT_USE_TEMPLATE 0
  35. #endif
  36. // Use only intrinsics instead of inline asm
  37. #define B3_DBVT_USE_INTRINSIC_SSE 1
  38. // Using memmov for collideOCL
  39. #define B3_DBVT_USE_MEMMOVE 1
  40. // Enable benchmarking code
  41. #define B3_DBVT_ENABLE_BENCHMARK 0
  42. // Inlining
  43. #define B3_DBVT_INLINE B3_FORCE_INLINE
  44. // Specific methods implementation
  45. //SSE gives errors on a MSVC 7.1
  46. #if defined(B3_USE_SSE) //&& defined (_WIN32)
  47. #define B3_DBVT_SELECT_IMPL B3_DBVT_IMPL_SSE
  48. #define B3_DBVT_MERGE_IMPL B3_DBVT_IMPL_SSE
  49. #define B3_DBVT_INT0_IMPL B3_DBVT_IMPL_SSE
  50. #else
  51. #define B3_DBVT_SELECT_IMPL B3_DBVT_IMPL_GENERIC
  52. #define B3_DBVT_MERGE_IMPL B3_DBVT_IMPL_GENERIC
  53. #define B3_DBVT_INT0_IMPL B3_DBVT_IMPL_GENERIC
  54. #endif
  55. #if (B3_DBVT_SELECT_IMPL == B3_DBVT_IMPL_SSE) || \
  56. (B3_DBVT_MERGE_IMPL == B3_DBVT_IMPL_SSE) || \
  57. (B3_DBVT_INT0_IMPL == B3_DBVT_IMPL_SSE)
  58. #include <emmintrin.h>
  59. #endif
  60. //
  61. // Auto config and checks
  62. //
  63. #if B3_DBVT_USE_TEMPLATE
  64. #define B3_DBVT_VIRTUAL
  65. #define B3_DBVT_VIRTUAL_DTOR(a)
  66. #define B3_DBVT_PREFIX template <typename T>
  67. #define B3_DBVT_IPOLICY T& policy
  68. #define B3_DBVT_CHECKTYPE \
  69. static const ICollide& typechecker = *(T*)1; \
  70. (void)typechecker;
  71. #else
  72. #define B3_DBVT_VIRTUAL_DTOR(a) \
  73. virtual ~a() {}
  74. #define B3_DBVT_VIRTUAL virtual
  75. #define B3_DBVT_PREFIX
  76. #define B3_DBVT_IPOLICY ICollide& policy
  77. #define B3_DBVT_CHECKTYPE
  78. #endif
  79. #if B3_DBVT_USE_MEMMOVE
  80. #if !defined(__CELLOS_LV2__) && !defined(__MWERKS__)
  81. #include <memory.h>
  82. #endif
  83. #include <string.h>
  84. #endif
  85. #ifndef B3_DBVT_USE_TEMPLATE
  86. #error "B3_DBVT_USE_TEMPLATE undefined"
  87. #endif
  88. #ifndef B3_DBVT_USE_MEMMOVE
  89. #error "B3_DBVT_USE_MEMMOVE undefined"
  90. #endif
  91. #ifndef B3_DBVT_ENABLE_BENCHMARK
  92. #error "B3_DBVT_ENABLE_BENCHMARK undefined"
  93. #endif
  94. #ifndef B3_DBVT_SELECT_IMPL
  95. #error "B3_DBVT_SELECT_IMPL undefined"
  96. #endif
  97. #ifndef B3_DBVT_MERGE_IMPL
  98. #error "B3_DBVT_MERGE_IMPL undefined"
  99. #endif
  100. #ifndef B3_DBVT_INT0_IMPL
  101. #error "B3_DBVT_INT0_IMPL undefined"
  102. #endif
  103. //
  104. // Defaults volumes
  105. //
  106. /* b3DbvtAabbMm */
  107. struct b3DbvtAabbMm
  108. {
  109. B3_DBVT_INLINE b3Vector3 Center() const { return ((mi + mx) / 2); }
  110. B3_DBVT_INLINE b3Vector3 Lengths() const { return (mx - mi); }
  111. B3_DBVT_INLINE b3Vector3 Extents() const { return ((mx - mi) / 2); }
  112. B3_DBVT_INLINE const b3Vector3& Mins() const { return (mi); }
  113. B3_DBVT_INLINE const b3Vector3& Maxs() const { return (mx); }
  114. static inline b3DbvtAabbMm FromCE(const b3Vector3& c, const b3Vector3& e);
  115. static inline b3DbvtAabbMm FromCR(const b3Vector3& c, b3Scalar r);
  116. static inline b3DbvtAabbMm FromMM(const b3Vector3& mi, const b3Vector3& mx);
  117. static inline b3DbvtAabbMm FromPoints(const b3Vector3* pts, int n);
  118. static inline b3DbvtAabbMm FromPoints(const b3Vector3** ppts, int n);
  119. B3_DBVT_INLINE void Expand(const b3Vector3& e);
  120. B3_DBVT_INLINE void SignedExpand(const b3Vector3& e);
  121. B3_DBVT_INLINE bool Contain(const b3DbvtAabbMm& a) const;
  122. B3_DBVT_INLINE int Classify(const b3Vector3& n, b3Scalar o, int s) const;
  123. B3_DBVT_INLINE b3Scalar ProjectMinimum(const b3Vector3& v, unsigned signs) const;
  124. B3_DBVT_INLINE friend bool b3Intersect(const b3DbvtAabbMm& a,
  125. const b3DbvtAabbMm& b);
  126. B3_DBVT_INLINE friend bool b3Intersect(const b3DbvtAabbMm& a,
  127. const b3Vector3& b);
  128. B3_DBVT_INLINE friend b3Scalar b3Proximity(const b3DbvtAabbMm& a,
  129. const b3DbvtAabbMm& b);
  130. B3_DBVT_INLINE friend int b3Select(const b3DbvtAabbMm& o,
  131. const b3DbvtAabbMm& a,
  132. const b3DbvtAabbMm& b);
  133. B3_DBVT_INLINE friend void b3Merge(const b3DbvtAabbMm& a,
  134. const b3DbvtAabbMm& b,
  135. b3DbvtAabbMm& r);
  136. B3_DBVT_INLINE friend bool b3NotEqual(const b3DbvtAabbMm& a,
  137. const b3DbvtAabbMm& b);
  138. B3_DBVT_INLINE b3Vector3& tMins() { return (mi); }
  139. B3_DBVT_INLINE b3Vector3& tMaxs() { return (mx); }
  140. private:
  141. B3_DBVT_INLINE void AddSpan(const b3Vector3& d, b3Scalar& smi, b3Scalar& smx) const;
  142. private:
  143. b3Vector3 mi, mx;
  144. };
  145. // Types
  146. typedef b3DbvtAabbMm b3DbvtVolume;
  147. /* b3DbvtNode */
  148. struct b3DbvtNode
  149. {
  150. b3DbvtVolume volume;
  151. b3DbvtNode* parent;
  152. B3_DBVT_INLINE bool isleaf() const { return (childs[1] == 0); }
  153. B3_DBVT_INLINE bool isinternal() const { return (!isleaf()); }
  154. union {
  155. b3DbvtNode* childs[2];
  156. void* data;
  157. int dataAsInt;
  158. };
  159. };
  160. ///The b3DynamicBvh class implements a fast dynamic bounding volume tree based on axis aligned bounding boxes (aabb tree).
  161. ///This b3DynamicBvh is used for soft body collision detection and for the b3DynamicBvhBroadphase. It has a fast insert, remove and update of nodes.
  162. ///Unlike the b3QuantizedBvh, nodes can be dynamically moved around, which allows for change in topology of the underlying data structure.
  163. struct b3DynamicBvh
  164. {
  165. /* Stack element */
  166. struct sStkNN
  167. {
  168. const b3DbvtNode* a;
  169. const b3DbvtNode* b;
  170. sStkNN() {}
  171. sStkNN(const b3DbvtNode* na, const b3DbvtNode* nb) : a(na), b(nb) {}
  172. };
  173. struct sStkNP
  174. {
  175. const b3DbvtNode* node;
  176. int mask;
  177. sStkNP(const b3DbvtNode* n, unsigned m) : node(n), mask(m) {}
  178. };
  179. struct sStkNPS
  180. {
  181. const b3DbvtNode* node;
  182. int mask;
  183. b3Scalar value;
  184. sStkNPS() {}
  185. sStkNPS(const b3DbvtNode* n, unsigned m, b3Scalar v) : node(n), mask(m), value(v) {}
  186. };
  187. struct sStkCLN
  188. {
  189. const b3DbvtNode* node;
  190. b3DbvtNode* parent;
  191. sStkCLN(const b3DbvtNode* n, b3DbvtNode* p) : node(n), parent(p) {}
  192. };
  193. // Policies/Interfaces
  194. /* ICollide */
  195. struct ICollide
  196. {
  197. B3_DBVT_VIRTUAL_DTOR(ICollide)
  198. B3_DBVT_VIRTUAL void Process(const b3DbvtNode*, const b3DbvtNode*) {}
  199. B3_DBVT_VIRTUAL void Process(const b3DbvtNode*) {}
  200. B3_DBVT_VIRTUAL void Process(const b3DbvtNode* n, b3Scalar) { Process(n); }
  201. B3_DBVT_VIRTUAL bool Descent(const b3DbvtNode*) { return (true); }
  202. B3_DBVT_VIRTUAL bool AllLeaves(const b3DbvtNode*) { return (true); }
  203. };
  204. /* IWriter */
  205. struct IWriter
  206. {
  207. virtual ~IWriter() {}
  208. virtual void Prepare(const b3DbvtNode* root, int numnodes) = 0;
  209. virtual void WriteNode(const b3DbvtNode*, int index, int parent, int child0, int child1) = 0;
  210. virtual void WriteLeaf(const b3DbvtNode*, int index, int parent) = 0;
  211. };
  212. /* IClone */
  213. struct IClone
  214. {
  215. virtual ~IClone() {}
  216. virtual void CloneLeaf(b3DbvtNode*) {}
  217. };
  218. // Constants
  219. enum
  220. {
  221. B3_SIMPLE_STACKSIZE = 64,
  222. B3_DOUBLE_STACKSIZE = B3_SIMPLE_STACKSIZE * 2
  223. };
  224. // Fields
  225. b3DbvtNode* m_root;
  226. b3DbvtNode* m_free;
  227. int m_lkhd;
  228. int m_leaves;
  229. unsigned m_opath;
  230. b3AlignedObjectArray<sStkNN> m_stkStack;
  231. mutable b3AlignedObjectArray<const b3DbvtNode*> m_rayTestStack;
  232. // Methods
  233. b3DynamicBvh();
  234. ~b3DynamicBvh();
  235. void clear();
  236. bool empty() const { return (0 == m_root); }
  237. void optimizeBottomUp();
  238. void optimizeTopDown(int bu_treshold = 128);
  239. void optimizeIncremental(int passes);
  240. b3DbvtNode* insert(const b3DbvtVolume& box, void* data);
  241. void update(b3DbvtNode* leaf, int lookahead = -1);
  242. void update(b3DbvtNode* leaf, b3DbvtVolume& volume);
  243. bool update(b3DbvtNode* leaf, b3DbvtVolume& volume, const b3Vector3& velocity, b3Scalar margin);
  244. bool update(b3DbvtNode* leaf, b3DbvtVolume& volume, const b3Vector3& velocity);
  245. bool update(b3DbvtNode* leaf, b3DbvtVolume& volume, b3Scalar margin);
  246. void remove(b3DbvtNode* leaf);
  247. void write(IWriter* iwriter) const;
  248. void clone(b3DynamicBvh& dest, IClone* iclone = 0) const;
  249. static int maxdepth(const b3DbvtNode* node);
  250. static int countLeaves(const b3DbvtNode* node);
  251. static void extractLeaves(const b3DbvtNode* node, b3AlignedObjectArray<const b3DbvtNode*>& leaves);
  252. #if B3_DBVT_ENABLE_BENCHMARK
  253. static void benchmark();
  254. #else
  255. static void benchmark()
  256. {
  257. }
  258. #endif
  259. // B3_DBVT_IPOLICY must support ICollide policy/interface
  260. B3_DBVT_PREFIX
  261. static void enumNodes(const b3DbvtNode* root,
  262. B3_DBVT_IPOLICY);
  263. B3_DBVT_PREFIX
  264. static void enumLeaves(const b3DbvtNode* root,
  265. B3_DBVT_IPOLICY);
  266. B3_DBVT_PREFIX
  267. void collideTT(const b3DbvtNode* root0,
  268. const b3DbvtNode* root1,
  269. B3_DBVT_IPOLICY);
  270. B3_DBVT_PREFIX
  271. void collideTTpersistentStack(const b3DbvtNode* root0,
  272. const b3DbvtNode* root1,
  273. B3_DBVT_IPOLICY);
  274. #if 0
  275. B3_DBVT_PREFIX
  276. void collideTT( const b3DbvtNode* root0,
  277. const b3DbvtNode* root1,
  278. const b3Transform& xform,
  279. B3_DBVT_IPOLICY);
  280. B3_DBVT_PREFIX
  281. void collideTT( const b3DbvtNode* root0,
  282. const b3Transform& xform0,
  283. const b3DbvtNode* root1,
  284. const b3Transform& xform1,
  285. B3_DBVT_IPOLICY);
  286. #endif
  287. B3_DBVT_PREFIX
  288. void collideTV(const b3DbvtNode* root,
  289. const b3DbvtVolume& volume,
  290. B3_DBVT_IPOLICY) const;
  291. ///rayTest is a re-entrant ray test, and can be called in parallel as long as the b3AlignedAlloc is thread-safe (uses locking etc)
  292. ///rayTest is slower than rayTestInternal, because it builds a local stack, using memory allocations, and it recomputes signs/rayDirectionInverses each time
  293. B3_DBVT_PREFIX
  294. static void rayTest(const b3DbvtNode* root,
  295. const b3Vector3& rayFrom,
  296. const b3Vector3& rayTo,
  297. B3_DBVT_IPOLICY);
  298. ///rayTestInternal is faster than rayTest, because it uses a persistent stack (to reduce dynamic memory allocations to a minimum) and it uses precomputed signs/rayInverseDirections
  299. ///rayTestInternal is used by b3DynamicBvhBroadphase to accelerate world ray casts
  300. B3_DBVT_PREFIX
  301. void rayTestInternal(const b3DbvtNode* root,
  302. const b3Vector3& rayFrom,
  303. const b3Vector3& rayTo,
  304. const b3Vector3& rayDirectionInverse,
  305. unsigned int signs[3],
  306. b3Scalar lambda_max,
  307. const b3Vector3& aabbMin,
  308. const b3Vector3& aabbMax,
  309. B3_DBVT_IPOLICY) const;
  310. B3_DBVT_PREFIX
  311. static void collideKDOP(const b3DbvtNode* root,
  312. const b3Vector3* normals,
  313. const b3Scalar* offsets,
  314. int count,
  315. B3_DBVT_IPOLICY);
  316. B3_DBVT_PREFIX
  317. static void collideOCL(const b3DbvtNode* root,
  318. const b3Vector3* normals,
  319. const b3Scalar* offsets,
  320. const b3Vector3& sortaxis,
  321. int count,
  322. B3_DBVT_IPOLICY,
  323. bool fullsort = true);
  324. B3_DBVT_PREFIX
  325. static void collideTU(const b3DbvtNode* root,
  326. B3_DBVT_IPOLICY);
  327. // Helpers
  328. static B3_DBVT_INLINE int nearest(const int* i, const b3DynamicBvh::sStkNPS* a, b3Scalar v, int l, int h)
  329. {
  330. int m = 0;
  331. while (l < h)
  332. {
  333. m = (l + h) >> 1;
  334. if (a[i[m]].value >= v)
  335. l = m + 1;
  336. else
  337. h = m;
  338. }
  339. return (h);
  340. }
  341. static B3_DBVT_INLINE int allocate(b3AlignedObjectArray<int>& ifree,
  342. b3AlignedObjectArray<sStkNPS>& stock,
  343. const sStkNPS& value)
  344. {
  345. int i;
  346. if (ifree.size() > 0)
  347. {
  348. i = ifree[ifree.size() - 1];
  349. ifree.pop_back();
  350. stock[i] = value;
  351. }
  352. else
  353. {
  354. i = stock.size();
  355. stock.push_back(value);
  356. }
  357. return (i);
  358. }
  359. //
  360. private:
  361. b3DynamicBvh(const b3DynamicBvh&) {}
  362. };
  363. //
  364. // Inline's
  365. //
  366. //
  367. inline b3DbvtAabbMm b3DbvtAabbMm::FromCE(const b3Vector3& c, const b3Vector3& e)
  368. {
  369. b3DbvtAabbMm box;
  370. box.mi = c - e;
  371. box.mx = c + e;
  372. return (box);
  373. }
  374. //
  375. inline b3DbvtAabbMm b3DbvtAabbMm::FromCR(const b3Vector3& c, b3Scalar r)
  376. {
  377. return (FromCE(c, b3MakeVector3(r, r, r)));
  378. }
  379. //
  380. inline b3DbvtAabbMm b3DbvtAabbMm::FromMM(const b3Vector3& mi, const b3Vector3& mx)
  381. {
  382. b3DbvtAabbMm box;
  383. box.mi = mi;
  384. box.mx = mx;
  385. return (box);
  386. }
  387. //
  388. inline b3DbvtAabbMm b3DbvtAabbMm::FromPoints(const b3Vector3* pts, int n)
  389. {
  390. b3DbvtAabbMm box;
  391. box.mi = box.mx = pts[0];
  392. for (int i = 1; i < n; ++i)
  393. {
  394. box.mi.setMin(pts[i]);
  395. box.mx.setMax(pts[i]);
  396. }
  397. return (box);
  398. }
  399. //
  400. inline b3DbvtAabbMm b3DbvtAabbMm::FromPoints(const b3Vector3** ppts, int n)
  401. {
  402. b3DbvtAabbMm box;
  403. box.mi = box.mx = *ppts[0];
  404. for (int i = 1; i < n; ++i)
  405. {
  406. box.mi.setMin(*ppts[i]);
  407. box.mx.setMax(*ppts[i]);
  408. }
  409. return (box);
  410. }
  411. //
  412. B3_DBVT_INLINE void b3DbvtAabbMm::Expand(const b3Vector3& e)
  413. {
  414. mi -= e;
  415. mx += e;
  416. }
  417. //
  418. B3_DBVT_INLINE void b3DbvtAabbMm::SignedExpand(const b3Vector3& e)
  419. {
  420. if (e.x > 0)
  421. mx.setX(mx.x + e[0]);
  422. else
  423. mi.setX(mi.x + e[0]);
  424. if (e.y > 0)
  425. mx.setY(mx.y + e[1]);
  426. else
  427. mi.setY(mi.y + e[1]);
  428. if (e.z > 0)
  429. mx.setZ(mx.z + e[2]);
  430. else
  431. mi.setZ(mi.z + e[2]);
  432. }
  433. //
  434. B3_DBVT_INLINE bool b3DbvtAabbMm::Contain(const b3DbvtAabbMm& a) const
  435. {
  436. return ((mi.x <= a.mi.x) &&
  437. (mi.y <= a.mi.y) &&
  438. (mi.z <= a.mi.z) &&
  439. (mx.x >= a.mx.x) &&
  440. (mx.y >= a.mx.y) &&
  441. (mx.z >= a.mx.z));
  442. }
  443. //
  444. B3_DBVT_INLINE int b3DbvtAabbMm::Classify(const b3Vector3& n, b3Scalar o, int s) const
  445. {
  446. b3Vector3 pi, px;
  447. switch (s)
  448. {
  449. case (0 + 0 + 0):
  450. px = b3MakeVector3(mi.x, mi.y, mi.z);
  451. pi = b3MakeVector3(mx.x, mx.y, mx.z);
  452. break;
  453. case (1 + 0 + 0):
  454. px = b3MakeVector3(mx.x, mi.y, mi.z);
  455. pi = b3MakeVector3(mi.x, mx.y, mx.z);
  456. break;
  457. case (0 + 2 + 0):
  458. px = b3MakeVector3(mi.x, mx.y, mi.z);
  459. pi = b3MakeVector3(mx.x, mi.y, mx.z);
  460. break;
  461. case (1 + 2 + 0):
  462. px = b3MakeVector3(mx.x, mx.y, mi.z);
  463. pi = b3MakeVector3(mi.x, mi.y, mx.z);
  464. break;
  465. case (0 + 0 + 4):
  466. px = b3MakeVector3(mi.x, mi.y, mx.z);
  467. pi = b3MakeVector3(mx.x, mx.y, mi.z);
  468. break;
  469. case (1 + 0 + 4):
  470. px = b3MakeVector3(mx.x, mi.y, mx.z);
  471. pi = b3MakeVector3(mi.x, mx.y, mi.z);
  472. break;
  473. case (0 + 2 + 4):
  474. px = b3MakeVector3(mi.x, mx.y, mx.z);
  475. pi = b3MakeVector3(mx.x, mi.y, mi.z);
  476. break;
  477. case (1 + 2 + 4):
  478. px = b3MakeVector3(mx.x, mx.y, mx.z);
  479. pi = b3MakeVector3(mi.x, mi.y, mi.z);
  480. break;
  481. }
  482. if ((b3Dot(n, px) + o) < 0) return (-1);
  483. if ((b3Dot(n, pi) + o) >= 0) return (+1);
  484. return (0);
  485. }
  486. //
  487. B3_DBVT_INLINE b3Scalar b3DbvtAabbMm::ProjectMinimum(const b3Vector3& v, unsigned signs) const
  488. {
  489. const b3Vector3* b[] = {&mx, &mi};
  490. const b3Vector3 p = b3MakeVector3(b[(signs >> 0) & 1]->x,
  491. b[(signs >> 1) & 1]->y,
  492. b[(signs >> 2) & 1]->z);
  493. return (b3Dot(p, v));
  494. }
  495. //
  496. B3_DBVT_INLINE void b3DbvtAabbMm::AddSpan(const b3Vector3& d, b3Scalar& smi, b3Scalar& smx) const
  497. {
  498. for (int i = 0; i < 3; ++i)
  499. {
  500. if (d[i] < 0)
  501. {
  502. smi += mx[i] * d[i];
  503. smx += mi[i] * d[i];
  504. }
  505. else
  506. {
  507. smi += mi[i] * d[i];
  508. smx += mx[i] * d[i];
  509. }
  510. }
  511. }
  512. //
  513. B3_DBVT_INLINE bool b3Intersect(const b3DbvtAabbMm& a,
  514. const b3DbvtAabbMm& b)
  515. {
  516. #if B3_DBVT_INT0_IMPL == B3_DBVT_IMPL_SSE
  517. const __m128 rt(_mm_or_ps(_mm_cmplt_ps(_mm_load_ps(b.mx), _mm_load_ps(a.mi)),
  518. _mm_cmplt_ps(_mm_load_ps(a.mx), _mm_load_ps(b.mi))));
  519. #if defined(_WIN32)
  520. const __int32* pu((const __int32*)&rt);
  521. #else
  522. const int* pu((const int*)&rt);
  523. #endif
  524. return ((pu[0] | pu[1] | pu[2]) == 0);
  525. #else
  526. return ((a.mi.x <= b.mx.x) &&
  527. (a.mx.x >= b.mi.x) &&
  528. (a.mi.y <= b.mx.y) &&
  529. (a.mx.y >= b.mi.y) &&
  530. (a.mi.z <= b.mx.z) &&
  531. (a.mx.z >= b.mi.z));
  532. #endif
  533. }
  534. //
  535. B3_DBVT_INLINE bool b3Intersect(const b3DbvtAabbMm& a,
  536. const b3Vector3& b)
  537. {
  538. return ((b.x >= a.mi.x) &&
  539. (b.y >= a.mi.y) &&
  540. (b.z >= a.mi.z) &&
  541. (b.x <= a.mx.x) &&
  542. (b.y <= a.mx.y) &&
  543. (b.z <= a.mx.z));
  544. }
  545. //////////////////////////////////////
  546. //
  547. B3_DBVT_INLINE b3Scalar b3Proximity(const b3DbvtAabbMm& a,
  548. const b3DbvtAabbMm& b)
  549. {
  550. const b3Vector3 d = (a.mi + a.mx) - (b.mi + b.mx);
  551. return (b3Fabs(d.x) + b3Fabs(d.y) + b3Fabs(d.z));
  552. }
  553. //
  554. B3_DBVT_INLINE int b3Select(const b3DbvtAabbMm& o,
  555. const b3DbvtAabbMm& a,
  556. const b3DbvtAabbMm& b)
  557. {
  558. #if B3_DBVT_SELECT_IMPL == B3_DBVT_IMPL_SSE
  559. #if defined(_WIN32)
  560. static B3_ATTRIBUTE_ALIGNED16(const unsigned __int32) mask[] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
  561. #else
  562. static B3_ATTRIBUTE_ALIGNED16(const unsigned int) mask[] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x00000000 /*0x7fffffff*/};
  563. #endif
  564. ///@todo: the intrinsic version is 11% slower
  565. #if B3_DBVT_USE_INTRINSIC_SSE
  566. union b3SSEUnion ///NOTE: if we use more intrinsics, move b3SSEUnion into the LinearMath directory
  567. {
  568. __m128 ssereg;
  569. float floats[4];
  570. int ints[4];
  571. };
  572. __m128 omi(_mm_load_ps(o.mi));
  573. omi = _mm_add_ps(omi, _mm_load_ps(o.mx));
  574. __m128 ami(_mm_load_ps(a.mi));
  575. ami = _mm_add_ps(ami, _mm_load_ps(a.mx));
  576. ami = _mm_sub_ps(ami, omi);
  577. ami = _mm_and_ps(ami, _mm_load_ps((const float*)mask));
  578. __m128 bmi(_mm_load_ps(b.mi));
  579. bmi = _mm_add_ps(bmi, _mm_load_ps(b.mx));
  580. bmi = _mm_sub_ps(bmi, omi);
  581. bmi = _mm_and_ps(bmi, _mm_load_ps((const float*)mask));
  582. __m128 t0(_mm_movehl_ps(ami, ami));
  583. ami = _mm_add_ps(ami, t0);
  584. ami = _mm_add_ss(ami, _mm_shuffle_ps(ami, ami, 1));
  585. __m128 t1(_mm_movehl_ps(bmi, bmi));
  586. bmi = _mm_add_ps(bmi, t1);
  587. bmi = _mm_add_ss(bmi, _mm_shuffle_ps(bmi, bmi, 1));
  588. b3SSEUnion tmp;
  589. tmp.ssereg = _mm_cmple_ss(bmi, ami);
  590. return tmp.ints[0] & 1;
  591. #else
  592. B3_ATTRIBUTE_ALIGNED16(__int32 r[1]);
  593. __asm
  594. {
  595. mov eax,o
  596. mov ecx,a
  597. mov edx,b
  598. movaps xmm0,[eax]
  599. movaps xmm5,mask
  600. addps xmm0,[eax+16]
  601. movaps xmm1,[ecx]
  602. movaps xmm2,[edx]
  603. addps xmm1,[ecx+16]
  604. addps xmm2,[edx+16]
  605. subps xmm1,xmm0
  606. subps xmm2,xmm0
  607. andps xmm1,xmm5
  608. andps xmm2,xmm5
  609. movhlps xmm3,xmm1
  610. movhlps xmm4,xmm2
  611. addps xmm1,xmm3
  612. addps xmm2,xmm4
  613. pshufd xmm3,xmm1,1
  614. pshufd xmm4,xmm2,1
  615. addss xmm1,xmm3
  616. addss xmm2,xmm4
  617. cmpless xmm2,xmm1
  618. movss r,xmm2
  619. }
  620. return (r[0] & 1);
  621. #endif
  622. #else
  623. return (b3Proximity(o, a) < b3Proximity(o, b) ? 0 : 1);
  624. #endif
  625. }
  626. //
  627. B3_DBVT_INLINE void b3Merge(const b3DbvtAabbMm& a,
  628. const b3DbvtAabbMm& b,
  629. b3DbvtAabbMm& r)
  630. {
  631. #if B3_DBVT_MERGE_IMPL == B3_DBVT_IMPL_SSE
  632. __m128 ami(_mm_load_ps(a.mi));
  633. __m128 amx(_mm_load_ps(a.mx));
  634. __m128 bmi(_mm_load_ps(b.mi));
  635. __m128 bmx(_mm_load_ps(b.mx));
  636. ami = _mm_min_ps(ami, bmi);
  637. amx = _mm_max_ps(amx, bmx);
  638. _mm_store_ps(r.mi, ami);
  639. _mm_store_ps(r.mx, amx);
  640. #else
  641. for (int i = 0; i < 3; ++i)
  642. {
  643. if (a.mi[i] < b.mi[i])
  644. r.mi[i] = a.mi[i];
  645. else
  646. r.mi[i] = b.mi[i];
  647. if (a.mx[i] > b.mx[i])
  648. r.mx[i] = a.mx[i];
  649. else
  650. r.mx[i] = b.mx[i];
  651. }
  652. #endif
  653. }
  654. //
  655. B3_DBVT_INLINE bool b3NotEqual(const b3DbvtAabbMm& a,
  656. const b3DbvtAabbMm& b)
  657. {
  658. return ((a.mi.x != b.mi.x) ||
  659. (a.mi.y != b.mi.y) ||
  660. (a.mi.z != b.mi.z) ||
  661. (a.mx.x != b.mx.x) ||
  662. (a.mx.y != b.mx.y) ||
  663. (a.mx.z != b.mx.z));
  664. }
  665. //
  666. // Inline's
  667. //
  668. //
  669. B3_DBVT_PREFIX
  670. inline void b3DynamicBvh::enumNodes(const b3DbvtNode* root,
  671. B3_DBVT_IPOLICY)
  672. {
  673. B3_DBVT_CHECKTYPE
  674. policy.Process(root);
  675. if (root->isinternal())
  676. {
  677. enumNodes(root->childs[0], policy);
  678. enumNodes(root->childs[1], policy);
  679. }
  680. }
  681. //
  682. B3_DBVT_PREFIX
  683. inline void b3DynamicBvh::enumLeaves(const b3DbvtNode* root,
  684. B3_DBVT_IPOLICY)
  685. {
  686. B3_DBVT_CHECKTYPE
  687. if (root->isinternal())
  688. {
  689. enumLeaves(root->childs[0], policy);
  690. enumLeaves(root->childs[1], policy);
  691. }
  692. else
  693. {
  694. policy.Process(root);
  695. }
  696. }
  697. //
  698. B3_DBVT_PREFIX
  699. inline void b3DynamicBvh::collideTT(const b3DbvtNode* root0,
  700. const b3DbvtNode* root1,
  701. B3_DBVT_IPOLICY)
  702. {
  703. B3_DBVT_CHECKTYPE
  704. if (root0 && root1)
  705. {
  706. int depth = 1;
  707. int treshold = B3_DOUBLE_STACKSIZE - 4;
  708. b3AlignedObjectArray<sStkNN> stkStack;
  709. stkStack.resize(B3_DOUBLE_STACKSIZE);
  710. stkStack[0] = sStkNN(root0, root1);
  711. do
  712. {
  713. sStkNN p = stkStack[--depth];
  714. if (depth > treshold)
  715. {
  716. stkStack.resize(stkStack.size() * 2);
  717. treshold = stkStack.size() - 4;
  718. }
  719. if (p.a == p.b)
  720. {
  721. if (p.a->isinternal())
  722. {
  723. stkStack[depth++] = sStkNN(p.a->childs[0], p.a->childs[0]);
  724. stkStack[depth++] = sStkNN(p.a->childs[1], p.a->childs[1]);
  725. stkStack[depth++] = sStkNN(p.a->childs[0], p.a->childs[1]);
  726. }
  727. }
  728. else if (b3Intersect(p.a->volume, p.b->volume))
  729. {
  730. if (p.a->isinternal())
  731. {
  732. if (p.b->isinternal())
  733. {
  734. stkStack[depth++] = sStkNN(p.a->childs[0], p.b->childs[0]);
  735. stkStack[depth++] = sStkNN(p.a->childs[1], p.b->childs[0]);
  736. stkStack[depth++] = sStkNN(p.a->childs[0], p.b->childs[1]);
  737. stkStack[depth++] = sStkNN(p.a->childs[1], p.b->childs[1]);
  738. }
  739. else
  740. {
  741. stkStack[depth++] = sStkNN(p.a->childs[0], p.b);
  742. stkStack[depth++] = sStkNN(p.a->childs[1], p.b);
  743. }
  744. }
  745. else
  746. {
  747. if (p.b->isinternal())
  748. {
  749. stkStack[depth++] = sStkNN(p.a, p.b->childs[0]);
  750. stkStack[depth++] = sStkNN(p.a, p.b->childs[1]);
  751. }
  752. else
  753. {
  754. policy.Process(p.a, p.b);
  755. }
  756. }
  757. }
  758. } while (depth);
  759. }
  760. }
  761. B3_DBVT_PREFIX
  762. inline void b3DynamicBvh::collideTTpersistentStack(const b3DbvtNode* root0,
  763. const b3DbvtNode* root1,
  764. B3_DBVT_IPOLICY)
  765. {
  766. B3_DBVT_CHECKTYPE
  767. if (root0 && root1)
  768. {
  769. int depth = 1;
  770. int treshold = B3_DOUBLE_STACKSIZE - 4;
  771. m_stkStack.resize(B3_DOUBLE_STACKSIZE);
  772. m_stkStack[0] = sStkNN(root0, root1);
  773. do
  774. {
  775. sStkNN p = m_stkStack[--depth];
  776. if (depth > treshold)
  777. {
  778. m_stkStack.resize(m_stkStack.size() * 2);
  779. treshold = m_stkStack.size() - 4;
  780. }
  781. if (p.a == p.b)
  782. {
  783. if (p.a->isinternal())
  784. {
  785. m_stkStack[depth++] = sStkNN(p.a->childs[0], p.a->childs[0]);
  786. m_stkStack[depth++] = sStkNN(p.a->childs[1], p.a->childs[1]);
  787. m_stkStack[depth++] = sStkNN(p.a->childs[0], p.a->childs[1]);
  788. }
  789. }
  790. else if (b3Intersect(p.a->volume, p.b->volume))
  791. {
  792. if (p.a->isinternal())
  793. {
  794. if (p.b->isinternal())
  795. {
  796. m_stkStack[depth++] = sStkNN(p.a->childs[0], p.b->childs[0]);
  797. m_stkStack[depth++] = sStkNN(p.a->childs[1], p.b->childs[0]);
  798. m_stkStack[depth++] = sStkNN(p.a->childs[0], p.b->childs[1]);
  799. m_stkStack[depth++] = sStkNN(p.a->childs[1], p.b->childs[1]);
  800. }
  801. else
  802. {
  803. m_stkStack[depth++] = sStkNN(p.a->childs[0], p.b);
  804. m_stkStack[depth++] = sStkNN(p.a->childs[1], p.b);
  805. }
  806. }
  807. else
  808. {
  809. if (p.b->isinternal())
  810. {
  811. m_stkStack[depth++] = sStkNN(p.a, p.b->childs[0]);
  812. m_stkStack[depth++] = sStkNN(p.a, p.b->childs[1]);
  813. }
  814. else
  815. {
  816. policy.Process(p.a, p.b);
  817. }
  818. }
  819. }
  820. } while (depth);
  821. }
  822. }
  823. #if 0
  824. //
  825. B3_DBVT_PREFIX
  826. inline void b3DynamicBvh::collideTT( const b3DbvtNode* root0,
  827. const b3DbvtNode* root1,
  828. const b3Transform& xform,
  829. B3_DBVT_IPOLICY)
  830. {
  831. B3_DBVT_CHECKTYPE
  832. if(root0&&root1)
  833. {
  834. int depth=1;
  835. int treshold=B3_DOUBLE_STACKSIZE-4;
  836. b3AlignedObjectArray<sStkNN> stkStack;
  837. stkStack.resize(B3_DOUBLE_STACKSIZE);
  838. stkStack[0]=sStkNN(root0,root1);
  839. do {
  840. sStkNN p=stkStack[--depth];
  841. if(b3Intersect(p.a->volume,p.b->volume,xform))
  842. {
  843. if(depth>treshold)
  844. {
  845. stkStack.resize(stkStack.size()*2);
  846. treshold=stkStack.size()-4;
  847. }
  848. if(p.a->isinternal())
  849. {
  850. if(p.b->isinternal())
  851. {
  852. stkStack[depth++]=sStkNN(p.a->childs[0],p.b->childs[0]);
  853. stkStack[depth++]=sStkNN(p.a->childs[1],p.b->childs[0]);
  854. stkStack[depth++]=sStkNN(p.a->childs[0],p.b->childs[1]);
  855. stkStack[depth++]=sStkNN(p.a->childs[1],p.b->childs[1]);
  856. }
  857. else
  858. {
  859. stkStack[depth++]=sStkNN(p.a->childs[0],p.b);
  860. stkStack[depth++]=sStkNN(p.a->childs[1],p.b);
  861. }
  862. }
  863. else
  864. {
  865. if(p.b->isinternal())
  866. {
  867. stkStack[depth++]=sStkNN(p.a,p.b->childs[0]);
  868. stkStack[depth++]=sStkNN(p.a,p.b->childs[1]);
  869. }
  870. else
  871. {
  872. policy.Process(p.a,p.b);
  873. }
  874. }
  875. }
  876. } while(depth);
  877. }
  878. }
  879. //
  880. B3_DBVT_PREFIX
  881. inline void b3DynamicBvh::collideTT( const b3DbvtNode* root0,
  882. const b3Transform& xform0,
  883. const b3DbvtNode* root1,
  884. const b3Transform& xform1,
  885. B3_DBVT_IPOLICY)
  886. {
  887. const b3Transform xform=xform0.inverse()*xform1;
  888. collideTT(root0,root1,xform,policy);
  889. }
  890. #endif
  891. //
  892. B3_DBVT_PREFIX
  893. inline void b3DynamicBvh::collideTV(const b3DbvtNode* root,
  894. const b3DbvtVolume& vol,
  895. B3_DBVT_IPOLICY) const
  896. {
  897. B3_DBVT_CHECKTYPE
  898. if (root)
  899. {
  900. B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume)
  901. volume(vol);
  902. b3AlignedObjectArray<const b3DbvtNode*> stack;
  903. stack.resize(0);
  904. stack.reserve(B3_SIMPLE_STACKSIZE);
  905. stack.push_back(root);
  906. do
  907. {
  908. const b3DbvtNode* n = stack[stack.size() - 1];
  909. stack.pop_back();
  910. if (b3Intersect(n->volume, volume))
  911. {
  912. if (n->isinternal())
  913. {
  914. stack.push_back(n->childs[0]);
  915. stack.push_back(n->childs[1]);
  916. }
  917. else
  918. {
  919. policy.Process(n);
  920. }
  921. }
  922. } while (stack.size() > 0);
  923. }
  924. }
  925. B3_DBVT_PREFIX
  926. inline void b3DynamicBvh::rayTestInternal(const b3DbvtNode* root,
  927. const b3Vector3& rayFrom,
  928. const b3Vector3& rayTo,
  929. const b3Vector3& rayDirectionInverse,
  930. unsigned int signs[3],
  931. b3Scalar lambda_max,
  932. const b3Vector3& aabbMin,
  933. const b3Vector3& aabbMax,
  934. B3_DBVT_IPOLICY) const
  935. {
  936. (void)rayTo;
  937. B3_DBVT_CHECKTYPE
  938. if (root)
  939. {
  940. int depth = 1;
  941. int treshold = B3_DOUBLE_STACKSIZE - 2;
  942. b3AlignedObjectArray<const b3DbvtNode*>& stack = m_rayTestStack;
  943. stack.resize(B3_DOUBLE_STACKSIZE);
  944. stack[0] = root;
  945. b3Vector3 bounds[2];
  946. do
  947. {
  948. const b3DbvtNode* node = stack[--depth];
  949. bounds[0] = node->volume.Mins() - aabbMax;
  950. bounds[1] = node->volume.Maxs() - aabbMin;
  951. b3Scalar tmin = 1.f, lambda_min = 0.f;
  952. unsigned int result1 = false;
  953. result1 = b3RayAabb2(rayFrom, rayDirectionInverse, signs, bounds, tmin, lambda_min, lambda_max);
  954. if (result1)
  955. {
  956. if (node->isinternal())
  957. {
  958. if (depth > treshold)
  959. {
  960. stack.resize(stack.size() * 2);
  961. treshold = stack.size() - 2;
  962. }
  963. stack[depth++] = node->childs[0];
  964. stack[depth++] = node->childs[1];
  965. }
  966. else
  967. {
  968. policy.Process(node);
  969. }
  970. }
  971. } while (depth);
  972. }
  973. }
  974. //
  975. B3_DBVT_PREFIX
  976. inline void b3DynamicBvh::rayTest(const b3DbvtNode* root,
  977. const b3Vector3& rayFrom,
  978. const b3Vector3& rayTo,
  979. B3_DBVT_IPOLICY)
  980. {
  981. B3_DBVT_CHECKTYPE
  982. if (root)
  983. {
  984. b3Vector3 rayDir = (rayTo - rayFrom);
  985. rayDir.normalize();
  986. ///what about division by zero? --> just set rayDirection[i] to INF/B3_LARGE_FLOAT
  987. b3Vector3 rayDirectionInverse;
  988. rayDirectionInverse[0] = rayDir[0] == b3Scalar(0.0) ? b3Scalar(B3_LARGE_FLOAT) : b3Scalar(1.0) / rayDir[0];
  989. rayDirectionInverse[1] = rayDir[1] == b3Scalar(0.0) ? b3Scalar(B3_LARGE_FLOAT) : b3Scalar(1.0) / rayDir[1];
  990. rayDirectionInverse[2] = rayDir[2] == b3Scalar(0.0) ? b3Scalar(B3_LARGE_FLOAT) : b3Scalar(1.0) / rayDir[2];
  991. unsigned int signs[3] = {rayDirectionInverse[0] < 0.0, rayDirectionInverse[1] < 0.0, rayDirectionInverse[2] < 0.0};
  992. b3Scalar lambda_max = rayDir.dot(rayTo - rayFrom);
  993. #ifdef COMPARE_BTRAY_AABB2
  994. b3Vector3 resultNormal;
  995. #endif //COMPARE_BTRAY_AABB2
  996. b3AlignedObjectArray<const b3DbvtNode*> stack;
  997. int depth = 1;
  998. int treshold = B3_DOUBLE_STACKSIZE - 2;
  999. stack.resize(B3_DOUBLE_STACKSIZE);
  1000. stack[0] = root;
  1001. b3Vector3 bounds[2];
  1002. do
  1003. {
  1004. const b3DbvtNode* node = stack[--depth];
  1005. bounds[0] = node->volume.Mins();
  1006. bounds[1] = node->volume.Maxs();
  1007. b3Scalar tmin = 1.f, lambda_min = 0.f;
  1008. unsigned int result1 = b3RayAabb2(rayFrom, rayDirectionInverse, signs, bounds, tmin, lambda_min, lambda_max);
  1009. #ifdef COMPARE_BTRAY_AABB2
  1010. b3Scalar param = 1.f;
  1011. bool result2 = b3RayAabb(rayFrom, rayTo, node->volume.Mins(), node->volume.Maxs(), param, resultNormal);
  1012. b3Assert(result1 == result2);
  1013. #endif //TEST_BTRAY_AABB2
  1014. if (result1)
  1015. {
  1016. if (node->isinternal())
  1017. {
  1018. if (depth > treshold)
  1019. {
  1020. stack.resize(stack.size() * 2);
  1021. treshold = stack.size() - 2;
  1022. }
  1023. stack[depth++] = node->childs[0];
  1024. stack[depth++] = node->childs[1];
  1025. }
  1026. else
  1027. {
  1028. policy.Process(node);
  1029. }
  1030. }
  1031. } while (depth);
  1032. }
  1033. }
  1034. //
  1035. B3_DBVT_PREFIX
  1036. inline void b3DynamicBvh::collideKDOP(const b3DbvtNode* root,
  1037. const b3Vector3* normals,
  1038. const b3Scalar* offsets,
  1039. int count,
  1040. B3_DBVT_IPOLICY)
  1041. {
  1042. B3_DBVT_CHECKTYPE
  1043. if (root)
  1044. {
  1045. const int inside = (1 << count) - 1;
  1046. b3AlignedObjectArray<sStkNP> stack;
  1047. int signs[sizeof(unsigned) * 8];
  1048. b3Assert(count < int(sizeof(signs) / sizeof(signs[0])));
  1049. for (int i = 0; i < count; ++i)
  1050. {
  1051. signs[i] = ((normals[i].x >= 0) ? 1 : 0) +
  1052. ((normals[i].y >= 0) ? 2 : 0) +
  1053. ((normals[i].z >= 0) ? 4 : 0);
  1054. }
  1055. stack.reserve(B3_SIMPLE_STACKSIZE);
  1056. stack.push_back(sStkNP(root, 0));
  1057. do
  1058. {
  1059. sStkNP se = stack[stack.size() - 1];
  1060. bool out = false;
  1061. stack.pop_back();
  1062. for (int i = 0, j = 1; (!out) && (i < count); ++i, j <<= 1)
  1063. {
  1064. if (0 == (se.mask & j))
  1065. {
  1066. const int side = se.node->volume.Classify(normals[i], offsets[i], signs[i]);
  1067. switch (side)
  1068. {
  1069. case -1:
  1070. out = true;
  1071. break;
  1072. case +1:
  1073. se.mask |= j;
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. if (!out)
  1079. {
  1080. if ((se.mask != inside) && (se.node->isinternal()))
  1081. {
  1082. stack.push_back(sStkNP(se.node->childs[0], se.mask));
  1083. stack.push_back(sStkNP(se.node->childs[1], se.mask));
  1084. }
  1085. else
  1086. {
  1087. if (policy.AllLeaves(se.node)) enumLeaves(se.node, policy);
  1088. }
  1089. }
  1090. } while (stack.size());
  1091. }
  1092. }
  1093. //
  1094. B3_DBVT_PREFIX
  1095. inline void b3DynamicBvh::collideOCL(const b3DbvtNode* root,
  1096. const b3Vector3* normals,
  1097. const b3Scalar* offsets,
  1098. const b3Vector3& sortaxis,
  1099. int count,
  1100. B3_DBVT_IPOLICY,
  1101. bool fsort)
  1102. {
  1103. B3_DBVT_CHECKTYPE
  1104. if (root)
  1105. {
  1106. const unsigned srtsgns = (sortaxis[0] >= 0 ? 1 : 0) +
  1107. (sortaxis[1] >= 0 ? 2 : 0) +
  1108. (sortaxis[2] >= 0 ? 4 : 0);
  1109. const int inside = (1 << count) - 1;
  1110. b3AlignedObjectArray<sStkNPS> stock;
  1111. b3AlignedObjectArray<int> ifree;
  1112. b3AlignedObjectArray<int> stack;
  1113. int signs[sizeof(unsigned) * 8];
  1114. b3Assert(count < int(sizeof(signs) / sizeof(signs[0])));
  1115. for (int i = 0; i < count; ++i)
  1116. {
  1117. signs[i] = ((normals[i].x >= 0) ? 1 : 0) +
  1118. ((normals[i].y >= 0) ? 2 : 0) +
  1119. ((normals[i].z >= 0) ? 4 : 0);
  1120. }
  1121. stock.reserve(B3_SIMPLE_STACKSIZE);
  1122. stack.reserve(B3_SIMPLE_STACKSIZE);
  1123. ifree.reserve(B3_SIMPLE_STACKSIZE);
  1124. stack.push_back(allocate(ifree, stock, sStkNPS(root, 0, root->volume.ProjectMinimum(sortaxis, srtsgns))));
  1125. do
  1126. {
  1127. const int id = stack[stack.size() - 1];
  1128. sStkNPS se = stock[id];
  1129. stack.pop_back();
  1130. ifree.push_back(id);
  1131. if (se.mask != inside)
  1132. {
  1133. bool out = false;
  1134. for (int i = 0, j = 1; (!out) && (i < count); ++i, j <<= 1)
  1135. {
  1136. if (0 == (se.mask & j))
  1137. {
  1138. const int side = se.node->volume.Classify(normals[i], offsets[i], signs[i]);
  1139. switch (side)
  1140. {
  1141. case -1:
  1142. out = true;
  1143. break;
  1144. case +1:
  1145. se.mask |= j;
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. if (out) continue;
  1151. }
  1152. if (policy.Descent(se.node))
  1153. {
  1154. if (se.node->isinternal())
  1155. {
  1156. const b3DbvtNode* pns[] = {se.node->childs[0], se.node->childs[1]};
  1157. sStkNPS nes[] = {sStkNPS(pns[0], se.mask, pns[0]->volume.ProjectMinimum(sortaxis, srtsgns)),
  1158. sStkNPS(pns[1], se.mask, pns[1]->volume.ProjectMinimum(sortaxis, srtsgns))};
  1159. const int q = nes[0].value < nes[1].value ? 1 : 0;
  1160. int j = stack.size();
  1161. if (fsort && (j > 0))
  1162. {
  1163. /* Insert 0 */
  1164. j = nearest(&stack[0], &stock[0], nes[q].value, 0, stack.size());
  1165. stack.push_back(0);
  1166. #if B3_DBVT_USE_MEMMOVE
  1167. memmove(&stack[j + 1], &stack[j], sizeof(int) * (stack.size() - j - 1));
  1168. #else
  1169. for (int k = stack.size() - 1; k > j; --k) stack[k] = stack[k - 1];
  1170. #endif
  1171. stack[j] = allocate(ifree, stock, nes[q]);
  1172. /* Insert 1 */
  1173. j = nearest(&stack[0], &stock[0], nes[1 - q].value, j, stack.size());
  1174. stack.push_back(0);
  1175. #if B3_DBVT_USE_MEMMOVE
  1176. memmove(&stack[j + 1], &stack[j], sizeof(int) * (stack.size() - j - 1));
  1177. #else
  1178. for (int k = stack.size() - 1; k > j; --k) stack[k] = stack[k - 1];
  1179. #endif
  1180. stack[j] = allocate(ifree, stock, nes[1 - q]);
  1181. }
  1182. else
  1183. {
  1184. stack.push_back(allocate(ifree, stock, nes[q]));
  1185. stack.push_back(allocate(ifree, stock, nes[1 - q]));
  1186. }
  1187. }
  1188. else
  1189. {
  1190. policy.Process(se.node, se.value);
  1191. }
  1192. }
  1193. } while (stack.size());
  1194. }
  1195. }
  1196. //
  1197. B3_DBVT_PREFIX
  1198. inline void b3DynamicBvh::collideTU(const b3DbvtNode* root,
  1199. B3_DBVT_IPOLICY)
  1200. {
  1201. B3_DBVT_CHECKTYPE
  1202. if (root)
  1203. {
  1204. b3AlignedObjectArray<const b3DbvtNode*> stack;
  1205. stack.reserve(B3_SIMPLE_STACKSIZE);
  1206. stack.push_back(root);
  1207. do
  1208. {
  1209. const b3DbvtNode* n = stack[stack.size() - 1];
  1210. stack.pop_back();
  1211. if (policy.Descent(n))
  1212. {
  1213. if (n->isinternal())
  1214. {
  1215. stack.push_back(n->childs[0]);
  1216. stack.push_back(n->childs[1]);
  1217. }
  1218. else
  1219. {
  1220. policy.Process(n);
  1221. }
  1222. }
  1223. } while (stack.size() > 0);
  1224. }
  1225. }
  1226. //
  1227. // PP Cleanup
  1228. //
  1229. #undef B3_DBVT_USE_MEMMOVE
  1230. #undef B3_DBVT_USE_TEMPLATE
  1231. #undef B3_DBVT_VIRTUAL_DTOR
  1232. #undef B3_DBVT_VIRTUAL
  1233. #undef B3_DBVT_PREFIX
  1234. #undef B3_DBVT_IPOLICY
  1235. #undef B3_DBVT_CHECKTYPE
  1236. #undef B3_DBVT_IMPL_GENERIC
  1237. #undef B3_DBVT_IMPL_SSE
  1238. #undef B3_DBVT_USE_INTRINSIC_SSE
  1239. #undef B3_DBVT_SELECT_IMPL
  1240. #undef B3_DBVT_MERGE_IMPL
  1241. #undef B3_DBVT_INT0_IMPL
  1242. #endif