btDbvt.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/
  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. ///btDbvt implementation by Nathanael Presson
  14. #ifndef BT_DYNAMIC_BOUNDING_VOLUME_TREE_H
  15. #define BT_DYNAMIC_BOUNDING_VOLUME_TREE_H
  16. #include "bullet/LinearMath/btAlignedObjectArray.h"
  17. #include "bullet/LinearMath/btVector3.h"
  18. #include "bullet/LinearMath/btTransform.h"
  19. #include "bullet/LinearMath/btAabbUtil2.h"
  20. //
  21. // Compile time configuration
  22. //
  23. // Implementation profiles
  24. #define DBVT_IMPL_GENERIC 0 // Generic implementation
  25. #define DBVT_IMPL_SSE 1 // SSE
  26. // Template implementation of ICollide
  27. #ifdef _WIN32
  28. #if (defined (_MSC_VER) && _MSC_VER >= 1400)
  29. #define DBVT_USE_TEMPLATE 1
  30. #else
  31. #define DBVT_USE_TEMPLATE 0
  32. #endif
  33. #else
  34. #define DBVT_USE_TEMPLATE 0
  35. #endif
  36. // Use only intrinsics instead of inline asm
  37. #define DBVT_USE_INTRINSIC_SSE 1
  38. // Using memmov for collideOCL
  39. #define DBVT_USE_MEMMOVE 1
  40. // Enable benchmarking code
  41. #define DBVT_ENABLE_BENCHMARK 0
  42. // Inlining
  43. #define DBVT_INLINE SIMD_FORCE_INLINE
  44. // Specific methods implementation
  45. //SSE gives errors on a MSVC 7.1
  46. #if defined (BT_USE_SSE) //&& defined (_WIN32)
  47. #define DBVT_SELECT_IMPL DBVT_IMPL_SSE
  48. #define DBVT_MERGE_IMPL DBVT_IMPL_SSE
  49. #define DBVT_INT0_IMPL DBVT_IMPL_SSE
  50. #else
  51. #define DBVT_SELECT_IMPL DBVT_IMPL_GENERIC
  52. #define DBVT_MERGE_IMPL DBVT_IMPL_GENERIC
  53. #define DBVT_INT0_IMPL DBVT_IMPL_GENERIC
  54. #endif
  55. #if (DBVT_SELECT_IMPL==DBVT_IMPL_SSE)|| \
  56. (DBVT_MERGE_IMPL==DBVT_IMPL_SSE)|| \
  57. (DBVT_INT0_IMPL==DBVT_IMPL_SSE)
  58. #include <emmintrin.h>
  59. #endif
  60. //
  61. // Auto config and checks
  62. //
  63. #if DBVT_USE_TEMPLATE
  64. #define DBVT_VIRTUAL
  65. #define DBVT_VIRTUAL_DTOR(a)
  66. #define DBVT_PREFIX template <typename T>
  67. #define DBVT_IPOLICY T& policy
  68. #define DBVT_CHECKTYPE static const ICollide& typechecker=*(T*)1;(void)typechecker;
  69. #else
  70. #define DBVT_VIRTUAL_DTOR(a) virtual ~a() {}
  71. #define DBVT_VIRTUAL virtual
  72. #define DBVT_PREFIX
  73. #define DBVT_IPOLICY ICollide& policy
  74. #define DBVT_CHECKTYPE
  75. #endif
  76. #if DBVT_USE_MEMMOVE
  77. #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
  78. #include <memory.h>
  79. #endif
  80. #include <string.h>
  81. #endif
  82. #ifndef DBVT_USE_TEMPLATE
  83. #error "DBVT_USE_TEMPLATE undefined"
  84. #endif
  85. #ifndef DBVT_USE_MEMMOVE
  86. #error "DBVT_USE_MEMMOVE undefined"
  87. #endif
  88. #ifndef DBVT_ENABLE_BENCHMARK
  89. #error "DBVT_ENABLE_BENCHMARK undefined"
  90. #endif
  91. #ifndef DBVT_SELECT_IMPL
  92. #error "DBVT_SELECT_IMPL undefined"
  93. #endif
  94. #ifndef DBVT_MERGE_IMPL
  95. #error "DBVT_MERGE_IMPL undefined"
  96. #endif
  97. #ifndef DBVT_INT0_IMPL
  98. #error "DBVT_INT0_IMPL undefined"
  99. #endif
  100. //
  101. // Defaults volumes
  102. //
  103. /* btDbvtAabbMm */
  104. struct btDbvtAabbMm
  105. {
  106. DBVT_INLINE btVector3 Center() const { return((mi+mx)/2); }
  107. DBVT_INLINE btVector3 Lengths() const { return(mx-mi); }
  108. DBVT_INLINE btVector3 Extents() const { return((mx-mi)/2); }
  109. DBVT_INLINE const btVector3& Mins() const { return(mi); }
  110. DBVT_INLINE const btVector3& Maxs() const { return(mx); }
  111. static inline btDbvtAabbMm FromCE(const btVector3& c,const btVector3& e);
  112. static inline btDbvtAabbMm FromCR(const btVector3& c,btScalar r);
  113. static inline btDbvtAabbMm FromMM(const btVector3& mi,const btVector3& mx);
  114. static inline btDbvtAabbMm FromPoints(const btVector3* pts,int n);
  115. static inline btDbvtAabbMm FromPoints(const btVector3** ppts,int n);
  116. DBVT_INLINE void Expand(const btVector3& e);
  117. DBVT_INLINE void SignedExpand(const btVector3& e);
  118. DBVT_INLINE bool Contain(const btDbvtAabbMm& a) const;
  119. DBVT_INLINE int Classify(const btVector3& n,btScalar o,int s) const;
  120. DBVT_INLINE btScalar ProjectMinimum(const btVector3& v,unsigned signs) const;
  121. DBVT_INLINE friend bool Intersect( const btDbvtAabbMm& a,
  122. const btDbvtAabbMm& b);
  123. DBVT_INLINE friend bool Intersect( const btDbvtAabbMm& a,
  124. const btVector3& b);
  125. DBVT_INLINE friend btScalar Proximity( const btDbvtAabbMm& a,
  126. const btDbvtAabbMm& b);
  127. DBVT_INLINE friend int Select( const btDbvtAabbMm& o,
  128. const btDbvtAabbMm& a,
  129. const btDbvtAabbMm& b);
  130. DBVT_INLINE friend void Merge( const btDbvtAabbMm& a,
  131. const btDbvtAabbMm& b,
  132. btDbvtAabbMm& r);
  133. DBVT_INLINE friend bool NotEqual( const btDbvtAabbMm& a,
  134. const btDbvtAabbMm& b);
  135. DBVT_INLINE btVector3& tMins() { return(mi); }
  136. DBVT_INLINE btVector3& tMaxs() { return(mx); }
  137. private:
  138. DBVT_INLINE void AddSpan(const btVector3& d,btScalar& smi,btScalar& smx) const;
  139. private:
  140. btVector3 mi,mx;
  141. };
  142. // Types
  143. typedef btDbvtAabbMm btDbvtVolume;
  144. /* btDbvtNode */
  145. struct btDbvtNode
  146. {
  147. btDbvtVolume volume;
  148. btDbvtNode* parent;
  149. DBVT_INLINE bool isleaf() const { return(childs[1]==0); }
  150. DBVT_INLINE bool isinternal() const { return(!isleaf()); }
  151. union
  152. {
  153. btDbvtNode* childs[2];
  154. void* data;
  155. int dataAsInt;
  156. };
  157. };
  158. ///The btDbvt class implements a fast dynamic bounding volume tree based on axis aligned bounding boxes (aabb tree).
  159. ///This btDbvt is used for soft body collision detection and for the btDbvtBroadphase. It has a fast insert, remove and update of nodes.
  160. ///Unlike the btQuantizedBvh, nodes can be dynamically moved around, which allows for change in topology of the underlying data structure.
  161. struct btDbvt
  162. {
  163. /* Stack element */
  164. struct sStkNN
  165. {
  166. const btDbvtNode* a;
  167. const btDbvtNode* b;
  168. sStkNN() {}
  169. sStkNN(const btDbvtNode* na,const btDbvtNode* nb) : a(na),b(nb) {}
  170. };
  171. struct sStkNP
  172. {
  173. const btDbvtNode* node;
  174. int mask;
  175. sStkNP(const btDbvtNode* n,unsigned m) : node(n),mask(m) {}
  176. };
  177. struct sStkNPS
  178. {
  179. const btDbvtNode* node;
  180. int mask;
  181. btScalar value;
  182. sStkNPS() {}
  183. sStkNPS(const btDbvtNode* n,unsigned m,btScalar v) : node(n),mask(m),value(v) {}
  184. };
  185. struct sStkCLN
  186. {
  187. const btDbvtNode* node;
  188. btDbvtNode* parent;
  189. sStkCLN(const btDbvtNode* n,btDbvtNode* p) : node(n),parent(p) {}
  190. };
  191. // Policies/Interfaces
  192. /* ICollide */
  193. struct ICollide
  194. {
  195. DBVT_VIRTUAL_DTOR(ICollide)
  196. DBVT_VIRTUAL void Process(const btDbvtNode*,const btDbvtNode*) {}
  197. DBVT_VIRTUAL void Process(const btDbvtNode*) {}
  198. DBVT_VIRTUAL void Process(const btDbvtNode* n,btScalar) { Process(n); }
  199. DBVT_VIRTUAL bool Descent(const btDbvtNode*) { return(true); }
  200. DBVT_VIRTUAL bool AllLeaves(const btDbvtNode*) { return(true); }
  201. };
  202. /* IWriter */
  203. struct IWriter
  204. {
  205. virtual ~IWriter() {}
  206. virtual void Prepare(const btDbvtNode* root,int numnodes)=0;
  207. virtual void WriteNode(const btDbvtNode*,int index,int parent,int child0,int child1)=0;
  208. virtual void WriteLeaf(const btDbvtNode*,int index,int parent)=0;
  209. };
  210. /* IClone */
  211. struct IClone
  212. {
  213. virtual ~IClone() {}
  214. virtual void CloneLeaf(btDbvtNode*) {}
  215. };
  216. // Constants
  217. enum {
  218. SIMPLE_STACKSIZE = 64,
  219. DOUBLE_STACKSIZE = SIMPLE_STACKSIZE*2
  220. };
  221. // Fields
  222. btDbvtNode* m_root;
  223. btDbvtNode* m_free;
  224. int m_lkhd;
  225. int m_leaves;
  226. unsigned m_opath;
  227. btAlignedObjectArray<sStkNN> m_stkStack;
  228. mutable btAlignedObjectArray<const btDbvtNode*> m_rayTestStack;
  229. // Methods
  230. btDbvt();
  231. ~btDbvt();
  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. btDbvtNode* insert(const btDbvtVolume& box,void* data);
  238. void update(btDbvtNode* leaf,int lookahead=-1);
  239. void update(btDbvtNode* leaf,btDbvtVolume& volume);
  240. bool update(btDbvtNode* leaf,btDbvtVolume& volume,const btVector3& velocity,btScalar margin);
  241. bool update(btDbvtNode* leaf,btDbvtVolume& volume,const btVector3& velocity);
  242. bool update(btDbvtNode* leaf,btDbvtVolume& volume,btScalar margin);
  243. void remove(btDbvtNode* leaf);
  244. void write(IWriter* iwriter) const;
  245. void clone(btDbvt& dest,IClone* iclone=0) const;
  246. static int maxdepth(const btDbvtNode* node);
  247. static int countLeaves(const btDbvtNode* node);
  248. static void extractLeaves(const btDbvtNode* node,btAlignedObjectArray<const btDbvtNode*>& leaves);
  249. #if DBVT_ENABLE_BENCHMARK
  250. static void benchmark();
  251. #else
  252. static void benchmark(){}
  253. #endif
  254. // DBVT_IPOLICY must support ICollide policy/interface
  255. DBVT_PREFIX
  256. static void enumNodes( const btDbvtNode* root,
  257. DBVT_IPOLICY);
  258. DBVT_PREFIX
  259. static void enumLeaves( const btDbvtNode* root,
  260. DBVT_IPOLICY);
  261. DBVT_PREFIX
  262. void collideTT( const btDbvtNode* root0,
  263. const btDbvtNode* root1,
  264. DBVT_IPOLICY);
  265. DBVT_PREFIX
  266. void collideTTpersistentStack( const btDbvtNode* root0,
  267. const btDbvtNode* root1,
  268. DBVT_IPOLICY);
  269. #if 0
  270. DBVT_PREFIX
  271. void collideTT( const btDbvtNode* root0,
  272. const btDbvtNode* root1,
  273. const btTransform& xform,
  274. DBVT_IPOLICY);
  275. DBVT_PREFIX
  276. void collideTT( const btDbvtNode* root0,
  277. const btTransform& xform0,
  278. const btDbvtNode* root1,
  279. const btTransform& xform1,
  280. DBVT_IPOLICY);
  281. #endif
  282. DBVT_PREFIX
  283. void collideTV( const btDbvtNode* root,
  284. const btDbvtVolume& volume,
  285. DBVT_IPOLICY) const;
  286. ///rayTest is a re-entrant ray test, and can be called in parallel as long as the btAlignedAlloc 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. DBVT_PREFIX
  289. static void rayTest( const btDbvtNode* root,
  290. const btVector3& rayFrom,
  291. const btVector3& rayTo,
  292. 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 btDbvtBroadphase to accelerate world ray casts
  295. DBVT_PREFIX
  296. void rayTestInternal( const btDbvtNode* root,
  297. const btVector3& rayFrom,
  298. const btVector3& rayTo,
  299. const btVector3& rayDirectionInverse,
  300. unsigned int signs[3],
  301. btScalar lambda_max,
  302. const btVector3& aabbMin,
  303. const btVector3& aabbMax,
  304. DBVT_IPOLICY) const;
  305. DBVT_PREFIX
  306. static void collideKDOP(const btDbvtNode* root,
  307. const btVector3* normals,
  308. const btScalar* offsets,
  309. int count,
  310. DBVT_IPOLICY);
  311. DBVT_PREFIX
  312. static void collideOCL( const btDbvtNode* root,
  313. const btVector3* normals,
  314. const btScalar* offsets,
  315. const btVector3& sortaxis,
  316. int count,
  317. DBVT_IPOLICY,
  318. bool fullsort=true);
  319. DBVT_PREFIX
  320. static void collideTU( const btDbvtNode* root,
  321. DBVT_IPOLICY);
  322. // Helpers
  323. static DBVT_INLINE int nearest(const int* i,const btDbvt::sStkNPS* a,btScalar 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 DBVT_INLINE int allocate( btAlignedObjectArray<int>& ifree,
  334. btAlignedObjectArray<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. btDbvt(const btDbvt&) {}
  347. };
  348. //
  349. // Inline's
  350. //
  351. //
  352. inline btDbvtAabbMm btDbvtAabbMm::FromCE(const btVector3& c,const btVector3& e)
  353. {
  354. btDbvtAabbMm box;
  355. box.mi=c-e;box.mx=c+e;
  356. return(box);
  357. }
  358. //
  359. inline btDbvtAabbMm btDbvtAabbMm::FromCR(const btVector3& c,btScalar r)
  360. {
  361. return(FromCE(c,btVector3(r,r,r)));
  362. }
  363. //
  364. inline btDbvtAabbMm btDbvtAabbMm::FromMM(const btVector3& mi,const btVector3& mx)
  365. {
  366. btDbvtAabbMm box;
  367. box.mi=mi;box.mx=mx;
  368. return(box);
  369. }
  370. //
  371. inline btDbvtAabbMm btDbvtAabbMm::FromPoints(const btVector3* pts,int n)
  372. {
  373. btDbvtAabbMm 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 btDbvtAabbMm btDbvtAabbMm::FromPoints(const btVector3** ppts,int n)
  384. {
  385. btDbvtAabbMm 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. DBVT_INLINE void btDbvtAabbMm::Expand(const btVector3& e)
  396. {
  397. mi-=e;mx+=e;
  398. }
  399. //
  400. DBVT_INLINE void btDbvtAabbMm::SignedExpand(const btVector3& 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. DBVT_INLINE bool btDbvtAabbMm::Contain(const btDbvtAabbMm& 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. DBVT_INLINE int btDbvtAabbMm::Classify(const btVector3& n,btScalar o,int s) const
  418. {
  419. btVector3 pi,px;
  420. switch(s)
  421. {
  422. case (0+0+0): px=btVector3(mi.x(),mi.y(),mi.z());
  423. pi=btVector3(mx.x(),mx.y(),mx.z());break;
  424. case (1+0+0): px=btVector3(mx.x(),mi.y(),mi.z());
  425. pi=btVector3(mi.x(),mx.y(),mx.z());break;
  426. case (0+2+0): px=btVector3(mi.x(),mx.y(),mi.z());
  427. pi=btVector3(mx.x(),mi.y(),mx.z());break;
  428. case (1+2+0): px=btVector3(mx.x(),mx.y(),mi.z());
  429. pi=btVector3(mi.x(),mi.y(),mx.z());break;
  430. case (0+0+4): px=btVector3(mi.x(),mi.y(),mx.z());
  431. pi=btVector3(mx.x(),mx.y(),mi.z());break;
  432. case (1+0+4): px=btVector3(mx.x(),mi.y(),mx.z());
  433. pi=btVector3(mi.x(),mx.y(),mi.z());break;
  434. case (0+2+4): px=btVector3(mi.x(),mx.y(),mx.z());
  435. pi=btVector3(mx.x(),mi.y(),mi.z());break;
  436. case (1+2+4): px=btVector3(mx.x(),mx.y(),mx.z());
  437. pi=btVector3(mi.x(),mi.y(),mi.z());break;
  438. }
  439. if((btDot(n,px)+o)<0) return(-1);
  440. if((btDot(n,pi)+o)>=0) return(+1);
  441. return(0);
  442. }
  443. //
  444. DBVT_INLINE btScalar btDbvtAabbMm::ProjectMinimum(const btVector3& v,unsigned signs) const
  445. {
  446. const btVector3* b[]={&mx,&mi};
  447. const btVector3 p( b[(signs>>0)&1]->x(),
  448. b[(signs>>1)&1]->y(),
  449. b[(signs>>2)&1]->z());
  450. return(btDot(p,v));
  451. }
  452. //
  453. DBVT_INLINE void btDbvtAabbMm::AddSpan(const btVector3& d,btScalar& smi,btScalar& 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. DBVT_INLINE bool Intersect( const btDbvtAabbMm& a,
  465. const btDbvtAabbMm& b)
  466. {
  467. #if DBVT_INT0_IMPL == 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. DBVT_INLINE bool Intersect( const btDbvtAabbMm& a,
  487. const btVector3& 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. DBVT_INLINE btScalar Proximity( const btDbvtAabbMm& a,
  499. const btDbvtAabbMm& b)
  500. {
  501. const btVector3 d=(a.mi+a.mx)-(b.mi+b.mx);
  502. return(btFabs(d.x())+btFabs(d.y())+btFabs(d.z()));
  503. }
  504. //
  505. DBVT_INLINE int Select( const btDbvtAabbMm& o,
  506. const btDbvtAabbMm& a,
  507. const btDbvtAabbMm& b)
  508. {
  509. #if DBVT_SELECT_IMPL == DBVT_IMPL_SSE
  510. #if defined (_WIN32)
  511. static ATTRIBUTE_ALIGNED16(const unsigned __int32) mask[]={0x7fffffff,0x7fffffff,0x7fffffff,0x7fffffff};
  512. #else
  513. static ATTRIBUTE_ALIGNED16(const unsigned int) mask[]={0x7fffffff,0x7fffffff,0x7fffffff,0x00000000 /*0x7fffffff*/};
  514. #endif
  515. ///@todo: the intrinsic version is 11% slower
  516. #if DBVT_USE_INTRINSIC_SSE
  517. union btSSEUnion ///NOTE: if we use more intrinsics, move btSSEUnion 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. btSSEUnion tmp;
  540. tmp.ssereg = _mm_cmple_ss(bmi,ami);
  541. return tmp.ints[0]&1;
  542. #else
  543. 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(Proximity(o,a)<Proximity(o,b)?0:1);
  575. #endif
  576. }
  577. //
  578. DBVT_INLINE void Merge( const btDbvtAabbMm& a,
  579. const btDbvtAabbMm& b,
  580. btDbvtAabbMm& r)
  581. {
  582. #if DBVT_MERGE_IMPL==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. DBVT_INLINE bool NotEqual( const btDbvtAabbMm& a,
  601. const btDbvtAabbMm& 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. DBVT_PREFIX
  615. inline void btDbvt::enumNodes( const btDbvtNode* root,
  616. DBVT_IPOLICY)
  617. {
  618. 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. DBVT_PREFIX
  628. inline void btDbvt::enumLeaves( const btDbvtNode* root,
  629. DBVT_IPOLICY)
  630. {
  631. 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. DBVT_PREFIX
  644. inline void btDbvt::collideTT( const btDbvtNode* root0,
  645. const btDbvtNode* root1,
  646. DBVT_IPOLICY)
  647. {
  648. DBVT_CHECKTYPE
  649. if(root0&&root1)
  650. {
  651. int depth=1;
  652. int treshold=DOUBLE_STACKSIZE-4;
  653. btAlignedObjectArray<sStkNN> stkStack;
  654. stkStack.resize(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(Intersect(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. DBVT_PREFIX
  706. inline void btDbvt::collideTTpersistentStack( const btDbvtNode* root0,
  707. const btDbvtNode* root1,
  708. DBVT_IPOLICY)
  709. {
  710. DBVT_CHECKTYPE
  711. if(root0&&root1)
  712. {
  713. int depth=1;
  714. int treshold=DOUBLE_STACKSIZE-4;
  715. m_stkStack.resize(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(Intersect(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. DBVT_PREFIX
  769. inline void btDbvt::collideTT( const btDbvtNode* root0,
  770. const btDbvtNode* root1,
  771. const btTransform& xform,
  772. DBVT_IPOLICY)
  773. {
  774. DBVT_CHECKTYPE
  775. if(root0&&root1)
  776. {
  777. int depth=1;
  778. int treshold=DOUBLE_STACKSIZE-4;
  779. btAlignedObjectArray<sStkNN> stkStack;
  780. stkStack.resize(DOUBLE_STACKSIZE);
  781. stkStack[0]=sStkNN(root0,root1);
  782. do {
  783. sStkNN p=stkStack[--depth];
  784. if(Intersect(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. DBVT_PREFIX
  824. inline void btDbvt::collideTT( const btDbvtNode* root0,
  825. const btTransform& xform0,
  826. const btDbvtNode* root1,
  827. const btTransform& xform1,
  828. DBVT_IPOLICY)
  829. {
  830. const btTransform xform=xform0.inverse()*xform1;
  831. collideTT(root0,root1,xform,policy);
  832. }
  833. #endif
  834. //
  835. DBVT_PREFIX
  836. inline void btDbvt::collideTV( const btDbvtNode* root,
  837. const btDbvtVolume& vol,
  838. DBVT_IPOLICY) const
  839. {
  840. DBVT_CHECKTYPE
  841. if(root)
  842. {
  843. ATTRIBUTE_ALIGNED16(btDbvtVolume) volume(vol);
  844. btAlignedObjectArray<const btDbvtNode*> stack;
  845. stack.resize(0);
  846. stack.reserve(SIMPLE_STACKSIZE);
  847. stack.push_back(root);
  848. do {
  849. const btDbvtNode* n=stack[stack.size()-1];
  850. stack.pop_back();
  851. if(Intersect(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. DBVT_PREFIX
  867. inline void btDbvt::rayTestInternal( const btDbvtNode* root,
  868. const btVector3& rayFrom,
  869. const btVector3& rayTo,
  870. const btVector3& rayDirectionInverse,
  871. unsigned int signs[3],
  872. btScalar lambda_max,
  873. const btVector3& aabbMin,
  874. const btVector3& aabbMax,
  875. DBVT_IPOLICY) const
  876. {
  877. (void) rayTo;
  878. DBVT_CHECKTYPE
  879. if(root)
  880. {
  881. btVector3 resultNormal;
  882. int depth=1;
  883. int treshold=DOUBLE_STACKSIZE-2;
  884. btAlignedObjectArray<const btDbvtNode*>& stack = m_rayTestStack;
  885. stack.resize(DOUBLE_STACKSIZE);
  886. stack[0]=root;
  887. btVector3 bounds[2];
  888. do
  889. {
  890. const btDbvtNode* node=stack[--depth];
  891. bounds[0] = node->volume.Mins()-aabbMax;
  892. bounds[1] = node->volume.Maxs()-aabbMin;
  893. btScalar tmin=1.f,lambda_min=0.f;
  894. unsigned int result1=false;
  895. result1 = btRayAabb2(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. DBVT_PREFIX
  918. inline void btDbvt::rayTest( const btDbvtNode* root,
  919. const btVector3& rayFrom,
  920. const btVector3& rayTo,
  921. DBVT_IPOLICY)
  922. {
  923. DBVT_CHECKTYPE
  924. if(root)
  925. {
  926. btVector3 rayDir = (rayTo-rayFrom);
  927. rayDir.normalize ();
  928. ///what about division by zero? --> just set rayDirection[i] to INF/BT_LARGE_FLOAT
  929. btVector3 rayDirectionInverse;
  930. rayDirectionInverse[0] = rayDir[0] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDir[0];
  931. rayDirectionInverse[1] = rayDir[1] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDir[1];
  932. rayDirectionInverse[2] = rayDir[2] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDir[2];
  933. unsigned int signs[3] = { rayDirectionInverse[0] < 0.0, rayDirectionInverse[1] < 0.0, rayDirectionInverse[2] < 0.0};
  934. btScalar lambda_max = rayDir.dot(rayTo-rayFrom);
  935. btVector3 resultNormal;
  936. btAlignedObjectArray<const btDbvtNode*> stack;
  937. int depth=1;
  938. int treshold=DOUBLE_STACKSIZE-2;
  939. stack.resize(DOUBLE_STACKSIZE);
  940. stack[0]=root;
  941. btVector3 bounds[2];
  942. do {
  943. const btDbvtNode* node=stack[--depth];
  944. bounds[0] = node->volume.Mins();
  945. bounds[1] = node->volume.Maxs();
  946. btScalar tmin=1.f,lambda_min=0.f;
  947. unsigned int result1 = btRayAabb2(rayFrom,rayDirectionInverse,signs,bounds,tmin,lambda_min,lambda_max);
  948. #ifdef COMPARE_BTRAY_AABB2
  949. btScalar param=1.f;
  950. bool result2 = btRayAabb(rayFrom,rayTo,node->volume.Mins(),node->volume.Maxs(),param,resultNormal);
  951. btAssert(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. DBVT_PREFIX
  975. inline void btDbvt::collideKDOP(const btDbvtNode* root,
  976. const btVector3* normals,
  977. const btScalar* offsets,
  978. int count,
  979. DBVT_IPOLICY)
  980. {
  981. DBVT_CHECKTYPE
  982. if(root)
  983. {
  984. const int inside=(1<<count)-1;
  985. btAlignedObjectArray<sStkNP> stack;
  986. int signs[sizeof(unsigned)*8];
  987. btAssert(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(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. DBVT_PREFIX
  1029. inline void btDbvt::collideOCL( const btDbvtNode* root,
  1030. const btVector3* normals,
  1031. const btScalar* offsets,
  1032. const btVector3& sortaxis,
  1033. int count,
  1034. DBVT_IPOLICY,
  1035. bool fsort)
  1036. {
  1037. 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. btAlignedObjectArray<sStkNPS> stock;
  1045. btAlignedObjectArray<int> ifree;
  1046. btAlignedObjectArray<int> stack;
  1047. int signs[sizeof(unsigned)*8];
  1048. btAssert(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(SIMPLE_STACKSIZE);
  1056. stack.reserve(SIMPLE_STACKSIZE);
  1057. ifree.reserve(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 btDbvtNode* 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 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 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. DBVT_PREFIX
  1126. inline void btDbvt::collideTU( const btDbvtNode* root,
  1127. DBVT_IPOLICY)
  1128. {
  1129. DBVT_CHECKTYPE
  1130. if(root)
  1131. {
  1132. btAlignedObjectArray<const btDbvtNode*> stack;
  1133. stack.reserve(SIMPLE_STACKSIZE);
  1134. stack.push_back(root);
  1135. do {
  1136. const btDbvtNode* 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 DBVT_USE_MEMMOVE
  1152. #undef DBVT_USE_TEMPLATE
  1153. #undef DBVT_VIRTUAL_DTOR
  1154. #undef DBVT_VIRTUAL
  1155. #undef DBVT_PREFIX
  1156. #undef DBVT_IPOLICY
  1157. #undef DBVT_CHECKTYPE
  1158. #undef DBVT_IMPL_GENERIC
  1159. #undef DBVT_IMPL_SSE
  1160. #undef DBVT_USE_INTRINSIC_SSE
  1161. #undef DBVT_SELECT_IMPL
  1162. #undef DBVT_MERGE_IMPL
  1163. #undef DBVT_INT0_IMPL
  1164. #endif