btBoxCollision.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. #ifndef BT_BOX_COLLISION_H_INCLUDED
  2. #define BT_BOX_COLLISION_H_INCLUDED
  3. /*! \file gim_box_collision.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. This source file is part of GIMPACT Library.
  8. For the latest info, see http://gimpact.sourceforge.net/
  9. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  10. email: projectileman@yahoo.com
  11. This software is provided 'as-is', without any express or implied warranty.
  12. In no event will the authors be held liable for any damages arising from the use of this software.
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute it freely,
  15. subject to the following restrictions:
  16. 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.
  17. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #include "LinearMath/btTransform.h"
  21. ///Swap numbers
  22. #define BT_SWAP_NUMBERS(a,b){ \
  23. a = a+b; \
  24. b = a-b; \
  25. a = a-b; \
  26. }\
  27. #define BT_MAX(a,b) (a<b?b:a)
  28. #define BT_MIN(a,b) (a>b?b:a)
  29. #define BT_GREATER(x, y) btFabs(x) > (y)
  30. #define BT_MAX3(a,b,c) BT_MAX(a,BT_MAX(b,c))
  31. #define BT_MIN3(a,b,c) BT_MIN(a,BT_MIN(b,c))
  32. enum eBT_PLANE_INTERSECTION_TYPE
  33. {
  34. BT_CONST_BACK_PLANE = 0,
  35. BT_CONST_COLLIDE_PLANE,
  36. BT_CONST_FRONT_PLANE
  37. };
  38. //SIMD_FORCE_INLINE bool test_cross_edge_box(
  39. // const btVector3 & edge,
  40. // const btVector3 & absolute_edge,
  41. // const btVector3 & pointa,
  42. // const btVector3 & pointb, const btVector3 & extend,
  43. // int dir_index0,
  44. // int dir_index1
  45. // int component_index0,
  46. // int component_index1)
  47. //{
  48. // // dir coords are -z and y
  49. //
  50. // const btScalar dir0 = -edge[dir_index0];
  51. // const btScalar dir1 = edge[dir_index1];
  52. // btScalar pmin = pointa[component_index0]*dir0 + pointa[component_index1]*dir1;
  53. // btScalar pmax = pointb[component_index0]*dir0 + pointb[component_index1]*dir1;
  54. // //find minmax
  55. // if(pmin>pmax)
  56. // {
  57. // BT_SWAP_NUMBERS(pmin,pmax);
  58. // }
  59. // //find extends
  60. // const btScalar rad = extend[component_index0] * absolute_edge[dir_index0] +
  61. // extend[component_index1] * absolute_edge[dir_index1];
  62. //
  63. // if(pmin>rad || -rad>pmax) return false;
  64. // return true;
  65. //}
  66. //
  67. //SIMD_FORCE_INLINE bool test_cross_edge_box_X_axis(
  68. // const btVector3 & edge,
  69. // const btVector3 & absolute_edge,
  70. // const btVector3 & pointa,
  71. // const btVector3 & pointb, btVector3 & extend)
  72. //{
  73. //
  74. // return test_cross_edge_box(edge,absolute_edge,pointa,pointb,extend,2,1,1,2);
  75. //}
  76. //
  77. //
  78. //SIMD_FORCE_INLINE bool test_cross_edge_box_Y_axis(
  79. // const btVector3 & edge,
  80. // const btVector3 & absolute_edge,
  81. // const btVector3 & pointa,
  82. // const btVector3 & pointb, btVector3 & extend)
  83. //{
  84. //
  85. // return test_cross_edge_box(edge,absolute_edge,pointa,pointb,extend,0,2,2,0);
  86. //}
  87. //
  88. //SIMD_FORCE_INLINE bool test_cross_edge_box_Z_axis(
  89. // const btVector3 & edge,
  90. // const btVector3 & absolute_edge,
  91. // const btVector3 & pointa,
  92. // const btVector3 & pointb, btVector3 & extend)
  93. //{
  94. //
  95. // return test_cross_edge_box(edge,absolute_edge,pointa,pointb,extend,1,0,0,1);
  96. //}
  97. #define TEST_CROSS_EDGE_BOX_MCR(edge,absolute_edge,pointa,pointb,_extend,i_dir_0,i_dir_1,i_comp_0,i_comp_1)\
  98. {\
  99. const btScalar dir0 = -edge[i_dir_0];\
  100. const btScalar dir1 = edge[i_dir_1];\
  101. btScalar pmin = pointa[i_comp_0]*dir0 + pointa[i_comp_1]*dir1;\
  102. btScalar pmax = pointb[i_comp_0]*dir0 + pointb[i_comp_1]*dir1;\
  103. if(pmin>pmax)\
  104. {\
  105. BT_SWAP_NUMBERS(pmin,pmax); \
  106. }\
  107. const btScalar abs_dir0 = absolute_edge[i_dir_0];\
  108. const btScalar abs_dir1 = absolute_edge[i_dir_1];\
  109. const btScalar rad = _extend[i_comp_0] * abs_dir0 + _extend[i_comp_1] * abs_dir1;\
  110. if(pmin>rad || -rad>pmax) return false;\
  111. }\
  112. #define TEST_CROSS_EDGE_BOX_X_AXIS_MCR(edge,absolute_edge,pointa,pointb,_extend)\
  113. {\
  114. TEST_CROSS_EDGE_BOX_MCR(edge,absolute_edge,pointa,pointb,_extend,2,1,1,2);\
  115. }\
  116. #define TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(edge,absolute_edge,pointa,pointb,_extend)\
  117. {\
  118. TEST_CROSS_EDGE_BOX_MCR(edge,absolute_edge,pointa,pointb,_extend,0,2,2,0);\
  119. }\
  120. #define TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(edge,absolute_edge,pointa,pointb,_extend)\
  121. {\
  122. TEST_CROSS_EDGE_BOX_MCR(edge,absolute_edge,pointa,pointb,_extend,1,0,0,1);\
  123. }\
  124. //! Returns the dot product between a vec3f and the col of a matrix
  125. SIMD_FORCE_INLINE btScalar bt_mat3_dot_col(
  126. const btMatrix3x3 & mat, const btVector3 & vec3, int colindex)
  127. {
  128. return vec3[0]*mat[0][colindex] + vec3[1]*mat[1][colindex] + vec3[2]*mat[2][colindex];
  129. }
  130. //! Class for transforming a model1 to the space of model0
  131. ATTRIBUTE_ALIGNED16 (class) BT_BOX_BOX_TRANSFORM_CACHE
  132. {
  133. public:
  134. btVector3 m_T1to0;//!< Transforms translation of model1 to model 0
  135. btMatrix3x3 m_R1to0;//!< Transforms Rotation of model1 to model 0, equal to R0' * R1
  136. btMatrix3x3 m_AR;//!< Absolute value of m_R1to0
  137. SIMD_FORCE_INLINE void calc_absolute_matrix()
  138. {
  139. // static const btVector3 vepsi(1e-6f,1e-6f,1e-6f);
  140. // m_AR[0] = vepsi + m_R1to0[0].absolute();
  141. // m_AR[1] = vepsi + m_R1to0[1].absolute();
  142. // m_AR[2] = vepsi + m_R1to0[2].absolute();
  143. int i,j;
  144. for(i=0;i<3;i++)
  145. {
  146. for(j=0;j<3;j++ )
  147. {
  148. m_AR[i][j] = 1e-6f + btFabs(m_R1to0[i][j]);
  149. }
  150. }
  151. }
  152. BT_BOX_BOX_TRANSFORM_CACHE()
  153. {
  154. }
  155. //! Calc the transformation relative 1 to 0. Inverts matrics by transposing
  156. SIMD_FORCE_INLINE void calc_from_homogenic(const btTransform & trans0,const btTransform & trans1)
  157. {
  158. btTransform temp_trans = trans0.inverse();
  159. temp_trans = temp_trans * trans1;
  160. m_T1to0 = temp_trans.getOrigin();
  161. m_R1to0 = temp_trans.getBasis();
  162. calc_absolute_matrix();
  163. }
  164. //! Calcs the full invertion of the matrices. Useful for scaling matrices
  165. SIMD_FORCE_INLINE void calc_from_full_invert(const btTransform & trans0,const btTransform & trans1)
  166. {
  167. m_R1to0 = trans0.getBasis().inverse();
  168. m_T1to0 = m_R1to0 * (-trans0.getOrigin());
  169. m_T1to0 += m_R1to0*trans1.getOrigin();
  170. m_R1to0 *= trans1.getBasis();
  171. calc_absolute_matrix();
  172. }
  173. SIMD_FORCE_INLINE btVector3 transform(const btVector3 & point) const
  174. {
  175. return point.dot3( m_R1to0[0], m_R1to0[1], m_R1to0[2] ) + m_T1to0;
  176. }
  177. };
  178. #define BOX_PLANE_EPSILON 0.000001f
  179. //! Axis aligned box
  180. ATTRIBUTE_ALIGNED16 (class) btAABB
  181. {
  182. public:
  183. btVector3 m_min;
  184. btVector3 m_max;
  185. btAABB()
  186. {}
  187. btAABB(const btVector3 & V1,
  188. const btVector3 & V2,
  189. const btVector3 & V3)
  190. {
  191. m_min[0] = BT_MIN3(V1[0],V2[0],V3[0]);
  192. m_min[1] = BT_MIN3(V1[1],V2[1],V3[1]);
  193. m_min[2] = BT_MIN3(V1[2],V2[2],V3[2]);
  194. m_max[0] = BT_MAX3(V1[0],V2[0],V3[0]);
  195. m_max[1] = BT_MAX3(V1[1],V2[1],V3[1]);
  196. m_max[2] = BT_MAX3(V1[2],V2[2],V3[2]);
  197. }
  198. btAABB(const btVector3 & V1,
  199. const btVector3 & V2,
  200. const btVector3 & V3,
  201. btScalar margin)
  202. {
  203. m_min[0] = BT_MIN3(V1[0],V2[0],V3[0]);
  204. m_min[1] = BT_MIN3(V1[1],V2[1],V3[1]);
  205. m_min[2] = BT_MIN3(V1[2],V2[2],V3[2]);
  206. m_max[0] = BT_MAX3(V1[0],V2[0],V3[0]);
  207. m_max[1] = BT_MAX3(V1[1],V2[1],V3[1]);
  208. m_max[2] = BT_MAX3(V1[2],V2[2],V3[2]);
  209. m_min[0] -= margin;
  210. m_min[1] -= margin;
  211. m_min[2] -= margin;
  212. m_max[0] += margin;
  213. m_max[1] += margin;
  214. m_max[2] += margin;
  215. }
  216. btAABB(const btAABB &other):
  217. m_min(other.m_min),m_max(other.m_max)
  218. {
  219. }
  220. btAABB(const btAABB &other,btScalar margin ):
  221. m_min(other.m_min),m_max(other.m_max)
  222. {
  223. m_min[0] -= margin;
  224. m_min[1] -= margin;
  225. m_min[2] -= margin;
  226. m_max[0] += margin;
  227. m_max[1] += margin;
  228. m_max[2] += margin;
  229. }
  230. SIMD_FORCE_INLINE void invalidate()
  231. {
  232. m_min[0] = SIMD_INFINITY;
  233. m_min[1] = SIMD_INFINITY;
  234. m_min[2] = SIMD_INFINITY;
  235. m_max[0] = -SIMD_INFINITY;
  236. m_max[1] = -SIMD_INFINITY;
  237. m_max[2] = -SIMD_INFINITY;
  238. }
  239. SIMD_FORCE_INLINE void increment_margin(btScalar margin)
  240. {
  241. m_min[0] -= margin;
  242. m_min[1] -= margin;
  243. m_min[2] -= margin;
  244. m_max[0] += margin;
  245. m_max[1] += margin;
  246. m_max[2] += margin;
  247. }
  248. SIMD_FORCE_INLINE void copy_with_margin(const btAABB &other, btScalar margin)
  249. {
  250. m_min[0] = other.m_min[0] - margin;
  251. m_min[1] = other.m_min[1] - margin;
  252. m_min[2] = other.m_min[2] - margin;
  253. m_max[0] = other.m_max[0] + margin;
  254. m_max[1] = other.m_max[1] + margin;
  255. m_max[2] = other.m_max[2] + margin;
  256. }
  257. template<typename CLASS_POINT>
  258. SIMD_FORCE_INLINE void calc_from_triangle(
  259. const CLASS_POINT & V1,
  260. const CLASS_POINT & V2,
  261. const CLASS_POINT & V3)
  262. {
  263. m_min[0] = BT_MIN3(V1[0],V2[0],V3[0]);
  264. m_min[1] = BT_MIN3(V1[1],V2[1],V3[1]);
  265. m_min[2] = BT_MIN3(V1[2],V2[2],V3[2]);
  266. m_max[0] = BT_MAX3(V1[0],V2[0],V3[0]);
  267. m_max[1] = BT_MAX3(V1[1],V2[1],V3[1]);
  268. m_max[2] = BT_MAX3(V1[2],V2[2],V3[2]);
  269. }
  270. template<typename CLASS_POINT>
  271. SIMD_FORCE_INLINE void calc_from_triangle_margin(
  272. const CLASS_POINT & V1,
  273. const CLASS_POINT & V2,
  274. const CLASS_POINT & V3, btScalar margin)
  275. {
  276. m_min[0] = BT_MIN3(V1[0],V2[0],V3[0]);
  277. m_min[1] = BT_MIN3(V1[1],V2[1],V3[1]);
  278. m_min[2] = BT_MIN3(V1[2],V2[2],V3[2]);
  279. m_max[0] = BT_MAX3(V1[0],V2[0],V3[0]);
  280. m_max[1] = BT_MAX3(V1[1],V2[1],V3[1]);
  281. m_max[2] = BT_MAX3(V1[2],V2[2],V3[2]);
  282. m_min[0] -= margin;
  283. m_min[1] -= margin;
  284. m_min[2] -= margin;
  285. m_max[0] += margin;
  286. m_max[1] += margin;
  287. m_max[2] += margin;
  288. }
  289. //! Apply a transform to an AABB
  290. SIMD_FORCE_INLINE void appy_transform(const btTransform & trans)
  291. {
  292. btVector3 center = (m_max+m_min)*0.5f;
  293. btVector3 extends = m_max - center;
  294. // Compute new center
  295. center = trans(center);
  296. btVector3 textends = extends.dot3(trans.getBasis().getRow(0).absolute(),
  297. trans.getBasis().getRow(1).absolute(),
  298. trans.getBasis().getRow(2).absolute());
  299. m_min = center - textends;
  300. m_max = center + textends;
  301. }
  302. //! Apply a transform to an AABB
  303. SIMD_FORCE_INLINE void appy_transform_trans_cache(const BT_BOX_BOX_TRANSFORM_CACHE & trans)
  304. {
  305. btVector3 center = (m_max+m_min)*0.5f;
  306. btVector3 extends = m_max - center;
  307. // Compute new center
  308. center = trans.transform(center);
  309. btVector3 textends = extends.dot3(trans.m_R1to0.getRow(0).absolute(),
  310. trans.m_R1to0.getRow(1).absolute(),
  311. trans.m_R1to0.getRow(2).absolute());
  312. m_min = center - textends;
  313. m_max = center + textends;
  314. }
  315. //! Merges a Box
  316. SIMD_FORCE_INLINE void merge(const btAABB & box)
  317. {
  318. m_min[0] = BT_MIN(m_min[0],box.m_min[0]);
  319. m_min[1] = BT_MIN(m_min[1],box.m_min[1]);
  320. m_min[2] = BT_MIN(m_min[2],box.m_min[2]);
  321. m_max[0] = BT_MAX(m_max[0],box.m_max[0]);
  322. m_max[1] = BT_MAX(m_max[1],box.m_max[1]);
  323. m_max[2] = BT_MAX(m_max[2],box.m_max[2]);
  324. }
  325. //! Merges a point
  326. template<typename CLASS_POINT>
  327. SIMD_FORCE_INLINE void merge_point(const CLASS_POINT & point)
  328. {
  329. m_min[0] = BT_MIN(m_min[0],point[0]);
  330. m_min[1] = BT_MIN(m_min[1],point[1]);
  331. m_min[2] = BT_MIN(m_min[2],point[2]);
  332. m_max[0] = BT_MAX(m_max[0],point[0]);
  333. m_max[1] = BT_MAX(m_max[1],point[1]);
  334. m_max[2] = BT_MAX(m_max[2],point[2]);
  335. }
  336. //! Gets the extend and center
  337. SIMD_FORCE_INLINE void get_center_extend(btVector3 & center,btVector3 & extend) const
  338. {
  339. center = (m_max+m_min)*0.5f;
  340. extend = m_max - center;
  341. }
  342. //! Finds the intersecting box between this box and the other.
  343. SIMD_FORCE_INLINE void find_intersection(const btAABB & other, btAABB & intersection) const
  344. {
  345. intersection.m_min[0] = BT_MAX(other.m_min[0],m_min[0]);
  346. intersection.m_min[1] = BT_MAX(other.m_min[1],m_min[1]);
  347. intersection.m_min[2] = BT_MAX(other.m_min[2],m_min[2]);
  348. intersection.m_max[0] = BT_MIN(other.m_max[0],m_max[0]);
  349. intersection.m_max[1] = BT_MIN(other.m_max[1],m_max[1]);
  350. intersection.m_max[2] = BT_MIN(other.m_max[2],m_max[2]);
  351. }
  352. SIMD_FORCE_INLINE bool has_collision(const btAABB & other) const
  353. {
  354. if(m_min[0] > other.m_max[0] ||
  355. m_max[0] < other.m_min[0] ||
  356. m_min[1] > other.m_max[1] ||
  357. m_max[1] < other.m_min[1] ||
  358. m_min[2] > other.m_max[2] ||
  359. m_max[2] < other.m_min[2])
  360. {
  361. return false;
  362. }
  363. return true;
  364. }
  365. /*! \brief Finds the Ray intersection parameter.
  366. \param aabb Aligned box
  367. \param vorigin A vec3f with the origin of the ray
  368. \param vdir A vec3f with the direction of the ray
  369. */
  370. SIMD_FORCE_INLINE bool collide_ray(const btVector3 & vorigin,const btVector3 & vdir) const
  371. {
  372. btVector3 extents,center;
  373. this->get_center_extend(center,extents);;
  374. btScalar Dx = vorigin[0] - center[0];
  375. if(BT_GREATER(Dx, extents[0]) && Dx*vdir[0]>=0.0f) return false;
  376. btScalar Dy = vorigin[1] - center[1];
  377. if(BT_GREATER(Dy, extents[1]) && Dy*vdir[1]>=0.0f) return false;
  378. btScalar Dz = vorigin[2] - center[2];
  379. if(BT_GREATER(Dz, extents[2]) && Dz*vdir[2]>=0.0f) return false;
  380. btScalar f = vdir[1] * Dz - vdir[2] * Dy;
  381. if(btFabs(f) > extents[1]*btFabs(vdir[2]) + extents[2]*btFabs(vdir[1])) return false;
  382. f = vdir[2] * Dx - vdir[0] * Dz;
  383. if(btFabs(f) > extents[0]*btFabs(vdir[2]) + extents[2]*btFabs(vdir[0]))return false;
  384. f = vdir[0] * Dy - vdir[1] * Dx;
  385. if(btFabs(f) > extents[0]*btFabs(vdir[1]) + extents[1]*btFabs(vdir[0]))return false;
  386. return true;
  387. }
  388. SIMD_FORCE_INLINE void projection_interval(const btVector3 & direction, btScalar &vmin, btScalar &vmax) const
  389. {
  390. btVector3 center = (m_max+m_min)*0.5f;
  391. btVector3 extend = m_max-center;
  392. btScalar _fOrigin = direction.dot(center);
  393. btScalar _fMaximumExtent = extend.dot(direction.absolute());
  394. vmin = _fOrigin - _fMaximumExtent;
  395. vmax = _fOrigin + _fMaximumExtent;
  396. }
  397. SIMD_FORCE_INLINE eBT_PLANE_INTERSECTION_TYPE plane_classify(const btVector4 &plane) const
  398. {
  399. btScalar _fmin,_fmax;
  400. this->projection_interval(plane,_fmin,_fmax);
  401. if(plane[3] > _fmax + BOX_PLANE_EPSILON)
  402. {
  403. return BT_CONST_BACK_PLANE; // 0
  404. }
  405. if(plane[3]+BOX_PLANE_EPSILON >=_fmin)
  406. {
  407. return BT_CONST_COLLIDE_PLANE; //1
  408. }
  409. return BT_CONST_FRONT_PLANE;//2
  410. }
  411. SIMD_FORCE_INLINE bool overlapping_trans_conservative(const btAABB & box, btTransform & trans1_to_0) const
  412. {
  413. btAABB tbox = box;
  414. tbox.appy_transform(trans1_to_0);
  415. return has_collision(tbox);
  416. }
  417. SIMD_FORCE_INLINE bool overlapping_trans_conservative2(const btAABB & box,
  418. const BT_BOX_BOX_TRANSFORM_CACHE & trans1_to_0) const
  419. {
  420. btAABB tbox = box;
  421. tbox.appy_transform_trans_cache(trans1_to_0);
  422. return has_collision(tbox);
  423. }
  424. //! transcache is the transformation cache from box to this AABB
  425. SIMD_FORCE_INLINE bool overlapping_trans_cache(
  426. const btAABB & box,const BT_BOX_BOX_TRANSFORM_CACHE & transcache, bool fulltest) const
  427. {
  428. //Taken from OPCODE
  429. btVector3 ea,eb;//extends
  430. btVector3 ca,cb;//extends
  431. get_center_extend(ca,ea);
  432. box.get_center_extend(cb,eb);
  433. btVector3 T;
  434. btScalar t,t2;
  435. int i;
  436. // Class I : A's basis vectors
  437. for(i=0;i<3;i++)
  438. {
  439. T[i] = transcache.m_R1to0[i].dot(cb) + transcache.m_T1to0[i] - ca[i];
  440. t = transcache.m_AR[i].dot(eb) + ea[i];
  441. if(BT_GREATER(T[i], t)) return false;
  442. }
  443. // Class II : B's basis vectors
  444. for(i=0;i<3;i++)
  445. {
  446. t = bt_mat3_dot_col(transcache.m_R1to0,T,i);
  447. t2 = bt_mat3_dot_col(transcache.m_AR,ea,i) + eb[i];
  448. if(BT_GREATER(t,t2)) return false;
  449. }
  450. // Class III : 9 cross products
  451. if(fulltest)
  452. {
  453. int j,m,n,o,p,q,r;
  454. for(i=0;i<3;i++)
  455. {
  456. m = (i+1)%3;
  457. n = (i+2)%3;
  458. o = i==0?1:0;
  459. p = i==2?1:2;
  460. for(j=0;j<3;j++)
  461. {
  462. q = j==2?1:2;
  463. r = j==0?1:0;
  464. t = T[n]*transcache.m_R1to0[m][j] - T[m]*transcache.m_R1to0[n][j];
  465. t2 = ea[o]*transcache.m_AR[p][j] + ea[p]*transcache.m_AR[o][j] +
  466. eb[r]*transcache.m_AR[i][q] + eb[q]*transcache.m_AR[i][r];
  467. if(BT_GREATER(t,t2)) return false;
  468. }
  469. }
  470. }
  471. return true;
  472. }
  473. //! Simple test for planes.
  474. SIMD_FORCE_INLINE bool collide_plane(
  475. const btVector4 & plane) const
  476. {
  477. eBT_PLANE_INTERSECTION_TYPE classify = plane_classify(plane);
  478. return (classify == BT_CONST_COLLIDE_PLANE);
  479. }
  480. //! test for a triangle, with edges
  481. SIMD_FORCE_INLINE bool collide_triangle_exact(
  482. const btVector3 & p1,
  483. const btVector3 & p2,
  484. const btVector3 & p3,
  485. const btVector4 & triangle_plane) const
  486. {
  487. if(!collide_plane(triangle_plane)) return false;
  488. btVector3 center,extends;
  489. this->get_center_extend(center,extends);
  490. const btVector3 v1(p1 - center);
  491. const btVector3 v2(p2 - center);
  492. const btVector3 v3(p3 - center);
  493. //First axis
  494. btVector3 diff(v2 - v1);
  495. btVector3 abs_diff = diff.absolute();
  496. //Test With X axis
  497. TEST_CROSS_EDGE_BOX_X_AXIS_MCR(diff,abs_diff,v1,v3,extends);
  498. //Test With Y axis
  499. TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(diff,abs_diff,v1,v3,extends);
  500. //Test With Z axis
  501. TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(diff,abs_diff,v1,v3,extends);
  502. diff = v3 - v2;
  503. abs_diff = diff.absolute();
  504. //Test With X axis
  505. TEST_CROSS_EDGE_BOX_X_AXIS_MCR(diff,abs_diff,v2,v1,extends);
  506. //Test With Y axis
  507. TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(diff,abs_diff,v2,v1,extends);
  508. //Test With Z axis
  509. TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(diff,abs_diff,v2,v1,extends);
  510. diff = v1 - v3;
  511. abs_diff = diff.absolute();
  512. //Test With X axis
  513. TEST_CROSS_EDGE_BOX_X_AXIS_MCR(diff,abs_diff,v3,v2,extends);
  514. //Test With Y axis
  515. TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(diff,abs_diff,v3,v2,extends);
  516. //Test With Z axis
  517. TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(diff,abs_diff,v3,v2,extends);
  518. return true;
  519. }
  520. };
  521. //! Compairison of transformation objects
  522. SIMD_FORCE_INLINE bool btCompareTransformsEqual(const btTransform & t1,const btTransform & t2)
  523. {
  524. if(!(t1.getOrigin() == t2.getOrigin()) ) return false;
  525. if(!(t1.getBasis().getRow(0) == t2.getBasis().getRow(0)) ) return false;
  526. if(!(t1.getBasis().getRow(1) == t2.getBasis().getRow(1)) ) return false;
  527. if(!(t1.getBasis().getRow(2) == t2.getBasis().getRow(2)) ) return false;
  528. return true;
  529. }
  530. #endif // GIM_BOX_COLLISION_H_INCLUDED