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