b3DynamicBvh.h 32 KB

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