[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 4 | |
---|
| 5 | This software is provided 'as-is', without any express or implied warranty. |
---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
| 7 | Permission is granted to anyone to use this software for any purpose, |
---|
| 8 | including commercial applications, and to alter it and redistribute it freely, |
---|
| 9 | subject to the following restrictions: |
---|
| 10 | |
---|
| 11 | 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. |
---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
| 13 | 3. This notice may not be removed or altered from any source distribution. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #include "btTriangleMeshShape.h" |
---|
| 17 | #include "LinearMath/btVector3.h" |
---|
| 18 | #include "LinearMath/btQuaternion.h" |
---|
| 19 | #include "btStridingMeshInterface.h" |
---|
| 20 | #include "LinearMath/btAabbUtil2.h" |
---|
| 21 | #include "BulletCollision/CollisionShapes/btCollisionMargin.h" |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | btTriangleMeshShape::btTriangleMeshShape(btStridingMeshInterface* meshInterface) |
---|
| 25 | : btConcaveShape (), m_meshInterface(meshInterface) |
---|
| 26 | { |
---|
| 27 | m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE; |
---|
| 28 | if(meshInterface->hasPremadeAabb()) |
---|
| 29 | { |
---|
| 30 | meshInterface->getPremadeAabb(&m_localAabbMin, &m_localAabbMax); |
---|
| 31 | } |
---|
| 32 | else |
---|
| 33 | { |
---|
| 34 | recalcLocalAabb(); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | btTriangleMeshShape::~btTriangleMeshShape() |
---|
| 40 | { |
---|
| 41 | |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | void btTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const |
---|
| 48 | { |
---|
| 49 | |
---|
| 50 | btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin); |
---|
| 51 | localHalfExtents += btVector3(getMargin(),getMargin(),getMargin()); |
---|
| 52 | btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin); |
---|
| 53 | |
---|
| 54 | btMatrix3x3 abs_b = trans.getBasis().absolute(); |
---|
| 55 | |
---|
[2430] | 56 | btVector3 center = trans(localCenter); |
---|
[1963] | 57 | |
---|
| 58 | btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents), |
---|
| 59 | abs_b[1].dot(localHalfExtents), |
---|
| 60 | abs_b[2].dot(localHalfExtents)); |
---|
| 61 | aabbMin = center - extent; |
---|
| 62 | aabbMax = center + extent; |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | void btTriangleMeshShape::recalcLocalAabb() |
---|
| 68 | { |
---|
| 69 | for (int i=0;i<3;i++) |
---|
| 70 | { |
---|
| 71 | btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
| 72 | vec[i] = btScalar(1.); |
---|
| 73 | btVector3 tmp = localGetSupportingVertex(vec); |
---|
| 74 | m_localAabbMax[i] = tmp[i]+m_collisionMargin; |
---|
| 75 | vec[i] = btScalar(-1.); |
---|
| 76 | tmp = localGetSupportingVertex(vec); |
---|
| 77 | m_localAabbMin[i] = tmp[i]-m_collisionMargin; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | class SupportVertexCallback : public btTriangleCallback |
---|
| 84 | { |
---|
| 85 | |
---|
| 86 | btVector3 m_supportVertexLocal; |
---|
| 87 | public: |
---|
| 88 | |
---|
| 89 | btTransform m_worldTrans; |
---|
| 90 | btScalar m_maxDot; |
---|
| 91 | btVector3 m_supportVecLocal; |
---|
| 92 | |
---|
| 93 | SupportVertexCallback(const btVector3& supportVecWorld,const btTransform& trans) |
---|
| 94 | : m_supportVertexLocal(btScalar(0.),btScalar(0.),btScalar(0.)), m_worldTrans(trans) ,m_maxDot(btScalar(-1e30)) |
---|
| 95 | |
---|
| 96 | { |
---|
| 97 | m_supportVecLocal = supportVecWorld * m_worldTrans.getBasis(); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | virtual void processTriangle( btVector3* triangle,int partId, int triangleIndex) |
---|
| 101 | { |
---|
| 102 | (void)partId; |
---|
| 103 | (void)triangleIndex; |
---|
| 104 | for (int i=0;i<3;i++) |
---|
| 105 | { |
---|
| 106 | btScalar dot = m_supportVecLocal.dot(triangle[i]); |
---|
| 107 | if (dot > m_maxDot) |
---|
| 108 | { |
---|
| 109 | m_maxDot = dot; |
---|
| 110 | m_supportVertexLocal = triangle[i]; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | btVector3 GetSupportVertexWorldSpace() |
---|
| 116 | { |
---|
| 117 | return m_worldTrans(m_supportVertexLocal); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | btVector3 GetSupportVertexLocal() |
---|
| 121 | { |
---|
| 122 | return m_supportVertexLocal; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | void btTriangleMeshShape::setLocalScaling(const btVector3& scaling) |
---|
| 129 | { |
---|
| 130 | m_meshInterface->setScaling(scaling); |
---|
| 131 | recalcLocalAabb(); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | const btVector3& btTriangleMeshShape::getLocalScaling() const |
---|
| 135 | { |
---|
| 136 | return m_meshInterface->getScaling(); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | //#define DEBUG_TRIANGLE_MESH |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | void btTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const |
---|
| 149 | { |
---|
| 150 | struct FilteredCallback : public btInternalTriangleIndexCallback |
---|
| 151 | { |
---|
| 152 | btTriangleCallback* m_callback; |
---|
| 153 | btVector3 m_aabbMin; |
---|
| 154 | btVector3 m_aabbMax; |
---|
| 155 | |
---|
| 156 | FilteredCallback(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) |
---|
| 157 | :m_callback(callback), |
---|
| 158 | m_aabbMin(aabbMin), |
---|
| 159 | m_aabbMax(aabbMax) |
---|
| 160 | { |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex) |
---|
| 164 | { |
---|
| 165 | if (TestTriangleAgainstAabb2(&triangle[0],m_aabbMin,m_aabbMax)) |
---|
| 166 | { |
---|
| 167 | //check aabb in triangle-space, before doing this |
---|
| 168 | m_callback->processTriangle(triangle,partId,triangleIndex); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | }; |
---|
| 174 | |
---|
| 175 | FilteredCallback filterCallback(callback,aabbMin,aabbMax); |
---|
| 176 | |
---|
| 177 | m_meshInterface->InternalProcessAllTriangles(&filterCallback,aabbMin,aabbMax); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | void btTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
| 185 | { |
---|
| 186 | (void)mass; |
---|
| 187 | //moving concave objects not supported |
---|
| 188 | btAssert(0); |
---|
| 189 | inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | btVector3 btTriangleMeshShape::localGetSupportingVertex(const btVector3& vec) const |
---|
| 194 | { |
---|
| 195 | btVector3 supportVertex; |
---|
| 196 | |
---|
| 197 | btTransform ident; |
---|
| 198 | ident.setIdentity(); |
---|
| 199 | |
---|
| 200 | SupportVertexCallback supportCallback(vec,ident); |
---|
| 201 | |
---|
| 202 | btVector3 aabbMax(btScalar(1e30),btScalar(1e30),btScalar(1e30)); |
---|
| 203 | |
---|
| 204 | processAllTriangles(&supportCallback,-aabbMax,aabbMax); |
---|
| 205 | |
---|
| 206 | supportVertex = supportCallback.GetSupportVertexLocal(); |
---|
| 207 | |
---|
| 208 | return supportVertex; |
---|
| 209 | } |
---|