btBoxCollision.h 19 KB

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