btBoxCollision.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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_min[3] = 0.f;
  199. m_max[0] = BT_MAX3(V1[0], V2[0], V3[0]);
  200. m_max[1] = BT_MAX3(V1[1], V2[1], V3[1]);
  201. m_max[2] = BT_MAX3(V1[2], V2[2], V3[2]);
  202. m_max[3] = 0.f;
  203. }
  204. btAABB(const btVector3 &V1,
  205. const btVector3 &V2,
  206. const btVector3 &V3,
  207. btScalar margin)
  208. {
  209. m_min[0] = BT_MIN3(V1[0], V2[0], V3[0]);
  210. m_min[1] = BT_MIN3(V1[1], V2[1], V3[1]);
  211. m_min[2] = BT_MIN3(V1[2], V2[2], V3[2]);
  212. m_min[3] = 0.f;
  213. m_max[0] = BT_MAX3(V1[0], V2[0], V3[0]);
  214. m_max[1] = BT_MAX3(V1[1], V2[1], V3[1]);
  215. m_max[2] = BT_MAX3(V1[2], V2[2], V3[2]);
  216. m_max[3] = 0.f;
  217. m_min[0] -= margin;
  218. m_min[1] -= margin;
  219. m_min[2] -= margin;
  220. m_max[0] += margin;
  221. m_max[1] += margin;
  222. m_max[2] += margin;
  223. }
  224. btAABB(const btAABB &other) : m_min(other.m_min), m_max(other.m_max)
  225. {
  226. }
  227. btAABB(const btAABB &other, btScalar margin) : m_min(other.m_min), m_max(other.m_max)
  228. {
  229. m_min[0] -= margin;
  230. m_min[1] -= margin;
  231. m_min[2] -= margin;
  232. m_max[0] += margin;
  233. m_max[1] += margin;
  234. m_max[2] += margin;
  235. }
  236. SIMD_FORCE_INLINE void invalidate()
  237. {
  238. m_min[0] = SIMD_INFINITY;
  239. m_min[1] = SIMD_INFINITY;
  240. m_min[2] = SIMD_INFINITY;
  241. m_min[3] = 0.f;
  242. m_max[0] = -SIMD_INFINITY;
  243. m_max[1] = -SIMD_INFINITY;
  244. m_max[2] = -SIMD_INFINITY;
  245. m_max[3] = 0.f;
  246. }
  247. SIMD_FORCE_INLINE void increment_margin(btScalar margin)
  248. {
  249. m_min[0] -= margin;
  250. m_min[1] -= margin;
  251. m_min[2] -= margin;
  252. m_max[0] += margin;
  253. m_max[1] += margin;
  254. m_max[2] += margin;
  255. }
  256. SIMD_FORCE_INLINE void copy_with_margin(const btAABB &other, btScalar margin)
  257. {
  258. m_min[0] = other.m_min[0] - margin;
  259. m_min[1] = other.m_min[1] - margin;
  260. m_min[2] = other.m_min[2] - margin;
  261. m_min[3] = 0.f;
  262. m_max[0] = other.m_max[0] + margin;
  263. m_max[1] = other.m_max[1] + margin;
  264. m_max[2] = other.m_max[2] + margin;
  265. m_max[3] = 0.f;
  266. }
  267. template <typename CLASS_POINT>
  268. SIMD_FORCE_INLINE void calc_from_triangle(
  269. const CLASS_POINT &V1,
  270. const CLASS_POINT &V2,
  271. const CLASS_POINT &V3)
  272. {
  273. m_min[0] = BT_MIN3(V1[0], V2[0], V3[0]);
  274. m_min[1] = BT_MIN3(V1[1], V2[1], V3[1]);
  275. m_min[2] = BT_MIN3(V1[2], V2[2], V3[2]);
  276. m_min[3] = 0.f;
  277. m_max[0] = BT_MAX3(V1[0], V2[0], V3[0]);
  278. m_max[1] = BT_MAX3(V1[1], V2[1], V3[1]);
  279. m_max[2] = BT_MAX3(V1[2], V2[2], V3[2]);
  280. m_max[3] = 0.f;
  281. }
  282. template <typename CLASS_POINT>
  283. SIMD_FORCE_INLINE void calc_from_triangle_margin(
  284. const CLASS_POINT &V1,
  285. const CLASS_POINT &V2,
  286. const CLASS_POINT &V3, btScalar margin)
  287. {
  288. m_min[0] = BT_MIN3(V1[0], V2[0], V3[0]);
  289. m_min[1] = BT_MIN3(V1[1], V2[1], V3[1]);
  290. m_min[2] = BT_MIN3(V1[2], V2[2], V3[2]);
  291. m_min[3] = 0.f;
  292. m_max[0] = BT_MAX3(V1[0], V2[0], V3[0]);
  293. m_max[1] = BT_MAX3(V1[1], V2[1], V3[1]);
  294. m_max[2] = BT_MAX3(V1[2], V2[2], V3[2]);
  295. m_max[3] = 0.f;
  296. m_min[0] -= margin;
  297. m_min[1] -= margin;
  298. m_min[2] -= margin;
  299. m_max[0] += margin;
  300. m_max[1] += margin;
  301. m_max[2] += margin;
  302. }
  303. //! Apply a transform to an AABB
  304. SIMD_FORCE_INLINE void appy_transform(const btTransform &trans)
  305. {
  306. btVector3 center = (m_max + m_min) * 0.5f;
  307. btVector3 extends = m_max - center;
  308. // Compute new center
  309. center = trans(center);
  310. btVector3 textends = extends.dot3(trans.getBasis().getRow(0).absolute(),
  311. trans.getBasis().getRow(1).absolute(),
  312. trans.getBasis().getRow(2).absolute());
  313. m_min = center - textends;
  314. m_max = center + textends;
  315. }
  316. //! Apply a transform to an AABB
  317. SIMD_FORCE_INLINE void appy_transform_trans_cache(const BT_BOX_BOX_TRANSFORM_CACHE &trans)
  318. {
  319. btVector3 center = (m_max + m_min) * 0.5f;
  320. btVector3 extends = m_max - center;
  321. // Compute new center
  322. center = trans.transform(center);
  323. btVector3 textends = extends.dot3(trans.m_R1to0.getRow(0).absolute(),
  324. trans.m_R1to0.getRow(1).absolute(),
  325. trans.m_R1to0.getRow(2).absolute());
  326. m_min = center - textends;
  327. m_max = center + textends;
  328. }
  329. //! Merges a Box
  330. SIMD_FORCE_INLINE void merge(const btAABB &box)
  331. {
  332. m_min[0] = BT_MIN(m_min[0], box.m_min[0]);
  333. m_min[1] = BT_MIN(m_min[1], box.m_min[1]);
  334. m_min[2] = BT_MIN(m_min[2], box.m_min[2]);
  335. m_max[0] = BT_MAX(m_max[0], box.m_max[0]);
  336. m_max[1] = BT_MAX(m_max[1], box.m_max[1]);
  337. m_max[2] = BT_MAX(m_max[2], box.m_max[2]);
  338. }
  339. //! Merges a point
  340. template <typename CLASS_POINT>
  341. SIMD_FORCE_INLINE void merge_point(const CLASS_POINT &point)
  342. {
  343. m_min[0] = BT_MIN(m_min[0], point[0]);
  344. m_min[1] = BT_MIN(m_min[1], point[1]);
  345. m_min[2] = BT_MIN(m_min[2], point[2]);
  346. m_max[0] = BT_MAX(m_max[0], point[0]);
  347. m_max[1] = BT_MAX(m_max[1], point[1]);
  348. m_max[2] = BT_MAX(m_max[2], point[2]);
  349. }
  350. //! Gets the extend and center
  351. SIMD_FORCE_INLINE void get_center_extend(btVector3 & center, btVector3 & extend) const
  352. {
  353. center = (m_max + m_min) * 0.5f;
  354. extend = m_max - center;
  355. }
  356. //! Finds the intersecting box between this box and the other.
  357. SIMD_FORCE_INLINE void find_intersection(const btAABB &other, btAABB &intersection) const
  358. {
  359. intersection.m_min[0] = BT_MAX(other.m_min[0], m_min[0]);
  360. intersection.m_min[1] = BT_MAX(other.m_min[1], m_min[1]);
  361. intersection.m_min[2] = BT_MAX(other.m_min[2], m_min[2]);
  362. intersection.m_max[0] = BT_MIN(other.m_max[0], m_max[0]);
  363. intersection.m_max[1] = BT_MIN(other.m_max[1], m_max[1]);
  364. intersection.m_max[2] = BT_MIN(other.m_max[2], m_max[2]);
  365. }
  366. SIMD_FORCE_INLINE bool has_collision(const btAABB &other) const
  367. {
  368. if (m_min[0] > other.m_max[0] ||
  369. m_max[0] < other.m_min[0] ||
  370. m_min[1] > other.m_max[1] ||
  371. m_max[1] < other.m_min[1] ||
  372. m_min[2] > other.m_max[2] ||
  373. m_max[2] < other.m_min[2])
  374. {
  375. return false;
  376. }
  377. return true;
  378. }
  379. /*! \brief Finds the Ray intersection parameter.
  380. \param aabb Aligned box
  381. \param vorigin A vec3f with the origin of the ray
  382. \param vdir A vec3f with the direction of the ray
  383. */
  384. SIMD_FORCE_INLINE bool collide_ray(const btVector3 &vorigin, const btVector3 &vdir) const
  385. {
  386. btVector3 extents, center;
  387. this->get_center_extend(center, extents);
  388. ;
  389. btScalar Dx = vorigin[0] - center[0];
  390. if (BT_GREATER(Dx, extents[0]) && Dx * vdir[0] >= 0.0f) return false;
  391. btScalar Dy = vorigin[1] - center[1];
  392. if (BT_GREATER(Dy, extents[1]) && Dy * vdir[1] >= 0.0f) return false;
  393. btScalar Dz = vorigin[2] - center[2];
  394. if (BT_GREATER(Dz, extents[2]) && Dz * vdir[2] >= 0.0f) return false;
  395. btScalar f = vdir[1] * Dz - vdir[2] * Dy;
  396. if (btFabs(f) > extents[1] * btFabs(vdir[2]) + extents[2] * btFabs(vdir[1])) return false;
  397. f = vdir[2] * Dx - vdir[0] * Dz;
  398. if (btFabs(f) > extents[0] * btFabs(vdir[2]) + extents[2] * btFabs(vdir[0])) return false;
  399. f = vdir[0] * Dy - vdir[1] * Dx;
  400. if (btFabs(f) > extents[0] * btFabs(vdir[1]) + extents[1] * btFabs(vdir[0])) return false;
  401. return true;
  402. }
  403. SIMD_FORCE_INLINE void projection_interval(const btVector3 &direction, btScalar &vmin, btScalar &vmax) const
  404. {
  405. btVector3 center = (m_max + m_min) * 0.5f;
  406. btVector3 extend = m_max - center;
  407. btScalar _fOrigin = direction.dot(center);
  408. btScalar _fMaximumExtent = extend.dot(direction.absolute());
  409. vmin = _fOrigin - _fMaximumExtent;
  410. vmax = _fOrigin + _fMaximumExtent;
  411. }
  412. SIMD_FORCE_INLINE eBT_PLANE_INTERSECTION_TYPE plane_classify(const btVector4 &plane) const
  413. {
  414. btScalar _fmin, _fmax;
  415. this->projection_interval(plane, _fmin, _fmax);
  416. if (plane[3] > _fmax + BOX_PLANE_EPSILON)
  417. {
  418. return BT_CONST_BACK_PLANE; // 0
  419. }
  420. if (plane[3] + BOX_PLANE_EPSILON >= _fmin)
  421. {
  422. return BT_CONST_COLLIDE_PLANE; //1
  423. }
  424. return BT_CONST_FRONT_PLANE; //2
  425. }
  426. SIMD_FORCE_INLINE bool overlapping_trans_conservative(const btAABB &box, btTransform &trans1_to_0) const
  427. {
  428. btAABB tbox = box;
  429. tbox.appy_transform(trans1_to_0);
  430. return has_collision(tbox);
  431. }
  432. SIMD_FORCE_INLINE bool overlapping_trans_conservative2(const btAABB &box,
  433. const BT_BOX_BOX_TRANSFORM_CACHE &trans1_to_0) const
  434. {
  435. btAABB tbox = box;
  436. tbox.appy_transform_trans_cache(trans1_to_0);
  437. return has_collision(tbox);
  438. }
  439. //! transcache is the transformation cache from box to this AABB
  440. SIMD_FORCE_INLINE bool overlapping_trans_cache(
  441. const btAABB &box, const BT_BOX_BOX_TRANSFORM_CACHE &transcache, bool fulltest) const
  442. {
  443. //Taken from OPCODE
  444. btVector3 ea, eb; //extends
  445. btVector3 ca, cb; //extends
  446. get_center_extend(ca, ea);
  447. box.get_center_extend(cb, eb);
  448. btVector3 T;
  449. btScalar t, t2;
  450. int i;
  451. // Class I : A's basis vectors
  452. for (i = 0; i < 3; i++)
  453. {
  454. T[i] = transcache.m_R1to0[i].dot(cb) + transcache.m_T1to0[i] - ca[i];
  455. t = transcache.m_AR[i].dot(eb) + ea[i];
  456. if (BT_GREATER(T[i], t)) return false;
  457. }
  458. // Class II : B's basis vectors
  459. for (i = 0; i < 3; i++)
  460. {
  461. t = bt_mat3_dot_col(transcache.m_R1to0, T, i);
  462. t2 = bt_mat3_dot_col(transcache.m_AR, ea, i) + eb[i];
  463. if (BT_GREATER(t, t2)) return false;
  464. }
  465. // Class III : 9 cross products
  466. if (fulltest)
  467. {
  468. int j, m, n, o, p, q, r;
  469. for (i = 0; i < 3; i++)
  470. {
  471. m = (i + 1) % 3;
  472. n = (i + 2) % 3;
  473. o = i == 0 ? 1 : 0;
  474. p = i == 2 ? 1 : 2;
  475. for (j = 0; j < 3; j++)
  476. {
  477. q = j == 2 ? 1 : 2;
  478. r = j == 0 ? 1 : 0;
  479. t = T[n] * transcache.m_R1to0[m][j] - T[m] * transcache.m_R1to0[n][j];
  480. t2 = ea[o] * transcache.m_AR[p][j] + ea[p] * transcache.m_AR[o][j] +
  481. eb[r] * transcache.m_AR[i][q] + eb[q] * transcache.m_AR[i][r];
  482. if (BT_GREATER(t, t2)) return false;
  483. }
  484. }
  485. }
  486. return true;
  487. }
  488. //! Simple test for planes.
  489. SIMD_FORCE_INLINE bool collide_plane(
  490. const btVector4 &plane) const
  491. {
  492. eBT_PLANE_INTERSECTION_TYPE classify = plane_classify(plane);
  493. return (classify == BT_CONST_COLLIDE_PLANE);
  494. }
  495. //! test for a triangle, with edges
  496. SIMD_FORCE_INLINE bool collide_triangle_exact(
  497. const btVector3 &p1,
  498. const btVector3 &p2,
  499. const btVector3 &p3,
  500. const btVector4 &triangle_plane) const
  501. {
  502. if (!collide_plane(triangle_plane)) return false;
  503. btVector3 center, extends;
  504. this->get_center_extend(center, extends);
  505. const btVector3 v1(p1 - center);
  506. const btVector3 v2(p2 - center);
  507. const btVector3 v3(p3 - center);
  508. //First axis
  509. btVector3 diff(v2 - v1);
  510. btVector3 abs_diff = diff.absolute();
  511. //Test With X axis
  512. TEST_CROSS_EDGE_BOX_X_AXIS_MCR(diff, abs_diff, v1, v3, extends);
  513. //Test With Y axis
  514. TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(diff, abs_diff, v1, v3, extends);
  515. //Test With Z axis
  516. TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(diff, abs_diff, v1, v3, extends);
  517. diff = v3 - v2;
  518. abs_diff = diff.absolute();
  519. //Test With X axis
  520. TEST_CROSS_EDGE_BOX_X_AXIS_MCR(diff, abs_diff, v2, v1, extends);
  521. //Test With Y axis
  522. TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(diff, abs_diff, v2, v1, extends);
  523. //Test With Z axis
  524. TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(diff, abs_diff, v2, v1, extends);
  525. diff = v1 - v3;
  526. abs_diff = diff.absolute();
  527. //Test With X axis
  528. TEST_CROSS_EDGE_BOX_X_AXIS_MCR(diff, abs_diff, v3, v2, extends);
  529. //Test With Y axis
  530. TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(diff, abs_diff, v3, v2, extends);
  531. //Test With Z axis
  532. TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(diff, abs_diff, v3, v2, extends);
  533. return true;
  534. }
  535. };
  536. //! Compairison of transformation objects
  537. SIMD_FORCE_INLINE bool btCompareTransformsEqual(const btTransform &t1, const btTransform &t2)
  538. {
  539. if (!(t1.getOrigin() == t2.getOrigin())) return false;
  540. if (!(t1.getBasis().getRow(0) == t2.getBasis().getRow(0))) return false;
  541. if (!(t1.getBasis().getRow(1) == t2.getBasis().getRow(1))) return false;
  542. if (!(t1.getBasis().getRow(2) == t2.getBasis().getRow(2))) return false;
  543. return true;
  544. }
  545. #endif // GIM_BOX_COLLISION_H_INCLUDED