1#ifndef BT_BOX_COLLISION_H_INCLUDED
2#define BT_BOX_COLLISION_H_INCLUDED
30#define BT_SWAP_NUMBERS(a, b) \
37#define BT_MAX(a, b) (a < b ? b : a)
38#define BT_MIN(a, b) (a > b ? b : a)
40#define BT_GREATER(x, y) btFabs(x) > (y)
42#define BT_MAX3(a, b, c) BT_MAX(a, BT_MAX(b, c))
43#define BT_MIN3(a, b, c) BT_MIN(a, BT_MIN(b, c))
112#define TEST_CROSS_EDGE_BOX_MCR(edge, absolute_edge, pointa, pointb, _extend, i_dir_0, i_dir_1, i_comp_0, i_comp_1) \
114 const btScalar dir0 = -edge[i_dir_0]; \
115 const btScalar dir1 = edge[i_dir_1]; \
116 btScalar pmin = pointa[i_comp_0] * dir0 + pointa[i_comp_1] * dir1; \
117 btScalar pmax = pointb[i_comp_0] * dir0 + pointb[i_comp_1] * dir1; \
120 BT_SWAP_NUMBERS(pmin, pmax); \
122 const btScalar abs_dir0 = absolute_edge[i_dir_0]; \
123 const btScalar abs_dir1 = absolute_edge[i_dir_1]; \
124 const btScalar rad = _extend[i_comp_0] * abs_dir0 + _extend[i_comp_1] * abs_dir1; \
125 if (pmin > rad || -rad > pmax) return false; \
128#define TEST_CROSS_EDGE_BOX_X_AXIS_MCR(edge, absolute_edge, pointa, pointb, _extend) \
130 TEST_CROSS_EDGE_BOX_MCR(edge, absolute_edge, pointa, pointb, _extend, 2, 1, 1, 2); \
133#define TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(edge, absolute_edge, pointa, pointb, _extend) \
135 TEST_CROSS_EDGE_BOX_MCR(edge, absolute_edge, pointa, pointb, _extend, 0, 2, 2, 0); \
138#define TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(edge, absolute_edge, pointa, pointb, _extend) \
140 TEST_CROSS_EDGE_BOX_MCR(edge, absolute_edge, pointa, pointb, _extend, 1, 0, 0, 1); \
147 return vec3[0] * mat[0][colindex] +
vec3[1] * mat[1][colindex] +
vec3[2] * mat[2][colindex];
168 for (i = 0; i < 3; i++)
170 for (j = 0; j < 3; j++)
185 temp_trans = temp_trans * trans1;
187 m_T1to0 = temp_trans.getOrigin();
188 m_R1to0 = temp_trans.getBasis();
196 m_R1to0 = trans0.getBasis().inverse();
197 m_T1to0 =
m_R1to0 * (-trans0.getOrigin());
199 m_T1to0 +=
m_R1to0 * trans1.getOrigin();
211#define BOX_PLANE_EPSILON 0.000001f
229 m_min[0] =
BT_MIN3(V1[0], V2[0], V3[0]);
230 m_min[1] =
BT_MIN3(V1[1], V2[1], V3[1]);
231 m_min[2] =
BT_MIN3(V1[2], V2[2], V3[2]);
243 m_min[0] =
BT_MIN3(V1[0], V2[0], V3[0]);
244 m_min[1] =
BT_MIN3(V1[1], V2[1], V3[1]);
245 m_min[2] =
BT_MIN3(V1[2], V2[2], V3[2]);
295 m_min[0] = other.m_min[0] - margin;
296 m_min[1] = other.m_min[1] - margin;
297 m_min[2] = other.m_min[2] - margin;
299 m_max[0] = other.m_max[0] + margin;
300 m_max[1] = other.m_max[1] + margin;
301 m_max[2] = other.m_max[2] + margin;
304 template <
typename CLASS_POINT>
306 const CLASS_POINT &V1,
307 const CLASS_POINT &V2,
308 const CLASS_POINT &V3)
310 m_min[0] =
BT_MIN3(V1[0], V2[0], V3[0]);
311 m_min[1] =
BT_MIN3(V1[1], V2[1], V3[1]);
312 m_min[2] =
BT_MIN3(V1[2], V2[2], V3[2]);
319 template <
typename CLASS_POINT>
321 const CLASS_POINT &V1,
322 const CLASS_POINT &V2,
323 const CLASS_POINT &V3,
btScalar margin)
325 m_min[0] =
BT_MIN3(V1[0], V2[0], V3[0]);
326 m_min[1] =
BT_MIN3(V1[1], V2[1], V3[1]);
327 m_min[2] =
BT_MIN3(V1[2], V2[2], V3[2]);
344 btVector3 center = (
m_max + m_min) * 0.5f;
345 btVector3 extends =
m_max - center;
347 center = trans(center);
349 btVector3 textends = extends.dot3(trans.getBasis().getRow(0).absolute(),
350 trans.getBasis().getRow(1).absolute(),
351 trans.getBasis().getRow(2).absolute());
353 m_min = center - textends;
354 m_max = center + textends;
360 btVector3 center = (
m_max + m_min) * 0.5f;
361 btVector3 extends =
m_max - center;
363 center = trans.transform(center);
365 btVector3 textends = extends.dot3(trans.m_R1to0.getRow(0).absolute(),
366 trans.m_R1to0.getRow(1).absolute(),
367 trans.m_R1to0.getRow(2).absolute());
369 m_min = center - textends;
370 m_max = center + textends;
376 m_min[0] =
BT_MIN(m_min[0], box.m_min[0]);
377 m_min[1] =
BT_MIN(m_min[1], box.m_min[1]);
378 m_min[2] =
BT_MIN(m_min[2], box.m_min[2]);
386 template <
typename CLASS_POINT>
389 m_min[0] =
BT_MIN(m_min[0], point[0]);
390 m_min[1] =
BT_MIN(m_min[1], point[1]);
391 m_min[2] =
BT_MIN(m_min[2], point[2]);
401 center = (
m_max + m_min) * 0.5f;
402 extend =
m_max - center;
408 intersection.m_min[0] =
BT_MAX(other.m_min[0], m_min[0]);
409 intersection.m_min[1] =
BT_MAX(other.m_min[1], m_min[1]);
410 intersection.m_min[2] =
BT_MAX(other.m_min[2], m_min[2]);
412 intersection.m_max[0] =
BT_MIN(other.m_max[0],
m_max[0]);
413 intersection.m_max[1] =
BT_MIN(other.m_max[1],
m_max[1]);
414 intersection.m_max[2] =
BT_MIN(other.m_max[2],
m_max[2]);
419 if (m_min[0] > other.m_max[0] ||
420 m_max[0] < other.m_min[0] ||
421 m_min[1] > other.m_max[1] ||
422 m_max[1] < other.m_min[1] ||
423 m_min[2] > other.m_max[2] ||
424 m_max[2] < other.m_min[2])
438 btVector3 extents, center;
442 btScalar Dx = vorigin[0] - center[0];
443 if (
BT_GREATER(Dx, extents[0]) && Dx * vdir[0] >= 0.0f)
return false;
444 btScalar Dy = vorigin[1] - center[1];
445 if (
BT_GREATER(Dy, extents[1]) && Dy * vdir[1] >= 0.0f)
return false;
446 btScalar Dz = vorigin[2] - center[2];
447 if (
BT_GREATER(Dz, extents[2]) && Dz * vdir[2] >= 0.0f)
return false;
449 btScalar f = vdir[1] * Dz - vdir[2] * Dy;
450 if (
btFabs(f) > extents[1] *
btFabs(vdir[2]) + extents[2] *
btFabs(vdir[1]))
return false;
451 f = vdir[2] * Dx - vdir[0] * Dz;
452 if (
btFabs(f) > extents[0] *
btFabs(vdir[2]) + extents[2] *
btFabs(vdir[0]))
return false;
453 f = vdir[0] * Dy - vdir[1] * Dx;
454 if (
btFabs(f) > extents[0] *
btFabs(vdir[1]) + extents[1] *
btFabs(vdir[0]))
return false;
460 btVector3 center = (
m_max + m_min) * 0.5f;
461 btVector3 extend =
m_max - center;
463 btScalar _fOrigin = direction.dot(center);
464 btScalar _fMaximumExtent = extend.dot(direction.absolute());
465 vmin = _fOrigin - _fMaximumExtent;
466 vmax = _fOrigin + _fMaximumExtent;
489 tbox.appy_transform(trans1_to_0);
497 tbox.appy_transform_trans_cache(trans1_to_0);
509 box.get_center_extend(cb, eb);
516 for (i = 0; i < 3; i++)
518 T[i] = transcache.m_R1to0[i].dot(cb) + transcache.m_T1to0[i] - ca[i];
519 t = transcache.m_AR[i].dot(eb) + ea[i];
523 for (i = 0; i < 3; i++)
532 int j, m, n, o, p, q, r;
533 for (i = 0; i < 3; i++)
539 for (j = 0; j < 3; j++)
543 t = T[n] * transcache.m_R1to0[m][j] - T[m] * transcache.m_R1to0[n][j];
544 t2 = ea[o] * transcache.m_AR[p][j] + ea[p] * transcache.m_AR[o][j] +
545 eb[r] * transcache.m_AR[i][q] + eb[q] * transcache.m_AR[i][r];
570 btVector3 center, extends;
573 const btVector3 v1(p1 - center);
574 const btVector3
v2(p2 - center);
575 const btVector3 v3(p3 - center);
579 btVector3 abs_diff =
diff.absolute();
588 abs_diff =
diff.absolute();
597 abs_diff =
diff.absolute();
612 if (!(t1.getOrigin() == t2.getOrigin()))
return false;
614 if (!(t1.getBasis().getRow(0) == t2.getBasis().getRow(0)))
return false;
615 if (!(t1.getBasis().getRow(1) == t2.getBasis().getRow(1)))
return false;
616 if (!(t1.getBasis().getRow(2) == t2.getBasis().getRow(2)))
return false;
ATTR_WARN_UNUSED_RESULT const BMVert * v2
BT_BOX_BOX_TRANSFORM_CACHE
Class for transforming a model1 to the space of model0.
SIMD_FORCE_INLINE void merge_point(const CLASS_POINT &point)
Merges a point.
SIMD_FORCE_INLINE void copy_with_margin(const btAABB &other, btScalar margin)
SIMD_FORCE_INLINE void calc_from_full_invert(const btTransform &trans0, const btTransform &trans1)
Calcs the full invertion of the matrices. Useful for scaling matrices.
SIMD_FORCE_INLINE void merge(const btAABB &box)
Merges a Box.
#define TEST_CROSS_EDGE_BOX_X_AXIS_MCR(edge, absolute_edge, pointa, pointb, _extend)
SIMD_FORCE_INLINE bool has_collision(const btAABB &other) const
btMatrix3x3 m_R1to0
Transforms Rotation of model1 to model 0, equal to R0' * R1.
SIMD_FORCE_INLINE bool overlapping_trans_conservative(const btAABB &box, btTransform &trans1_to_0) const
SIMD_FORCE_INLINE void calc_from_triangle(const CLASS_POINT &V1, const CLASS_POINT &V2, const CLASS_POINT &V3)
SIMD_FORCE_INLINE void appy_transform_trans_cache(const BT_BOX_BOX_TRANSFORM_CACHE &trans)
Apply a transform to an AABB.
SIMD_FORCE_INLINE void invalidate()
SIMD_FORCE_INLINE void calc_absolute_matrix()
#define TEST_CROSS_EDGE_BOX_Y_AXIS_MCR(edge, absolute_edge, pointa, pointb, _extend)
eBT_PLANE_INTERSECTION_TYPE
SIMD_FORCE_INLINE bool collide_ray(const btVector3 &vorigin, const btVector3 &vdir) const
Finds the Ray intersection parameter.
SIMD_FORCE_INLINE bool collide_plane(const btVector4 &plane) const
Simple test for planes.
SIMD_FORCE_INLINE void calc_from_homogenic(const btTransform &trans0, const btTransform &trans1)
Calc the transformation relative 1 to 0. Inverts matrics by transposing.
SIMD_FORCE_INLINE void projection_interval(const btVector3 &direction, btScalar &vmin, btScalar &vmax) const
#define TEST_CROSS_EDGE_BOX_Z_AXIS_MCR(edge, absolute_edge, pointa, pointb, _extend)
SIMD_FORCE_INLINE btScalar bt_mat3_dot_col(const btMatrix3x3 &mat, const btVector3 &vec3, int colindex)
Returns the dot product between a vec3f and the col of a matrix.
SIMD_FORCE_INLINE bool btCompareTransformsEqual(const btTransform &t1, const btTransform &t2)
Compairison of transformation objects.
SIMD_FORCE_INLINE void appy_transform(const btTransform &trans)
Apply a transform to an AABB.
SIMD_FORCE_INLINE bool collide_triangle_exact(const btVector3 &p1, const btVector3 &p2, const btVector3 &p3, const btVector4 &triangle_plane) const
test for a triangle, with edges
SIMD_FORCE_INLINE bool overlapping_trans_conservative2(const btAABB &box, const BT_BOX_BOX_TRANSFORM_CACHE &trans1_to_0) const
SIMD_FORCE_INLINE void calc_from_triangle_margin(const CLASS_POINT &V1, const CLASS_POINT &V2, const CLASS_POINT &V3, btScalar margin)
SIMD_FORCE_INLINE eBT_PLANE_INTERSECTION_TYPE plane_classify(const btVector4 &plane) const
SIMD_FORCE_INLINE bool overlapping_trans_cache(const btAABB &box, const BT_BOX_BOX_TRANSFORM_CACHE &transcache, bool fulltest) const
transcache is the transformation cache from box to this AABB
SIMD_FORCE_INLINE void increment_margin(btScalar margin)
SIMD_FORCE_INLINE void find_intersection(const btAABB &other, btAABB &intersection) const
Finds the intersecting box between this box and the other.
SIMD_FORCE_INLINE void get_center_extend(btVector3 ¢er, btVector3 &extend) const
Gets the extend and center.
#define BOX_PLANE_EPSILON
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
btMatrix3x3 m_AR
Absolute value of m_R1to0.
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
#define ATTRIBUTE_ALIGNED16(a)
SIMD_FORCE_INLINE btScalar btFabs(btScalar x)
#define SIMD_FORCE_INLINE
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)