1 /* 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org 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 #ifndef BT_BVH_TRIANGLE_MESH_SHAPE_H 17 #define BT_BVH_TRIANGLE_MESH_SHAPE_H 18 19 #include "btTriangleMeshShape.h" 20 #include "btOptimizedBvh.h" 21 #include "LinearMath/btAlignedAllocator.h" 22 #include "btTriangleInfoMap.h" 23 24 ///The btBvhTriangleMeshShape is a static-triangle mesh shape, it can only be used for fixed/non-moving objects. 25 ///If you required moving concave triangle meshes, it is recommended to perform convex decomposition 26 ///using HACD, see Bullet/Demos/ConvexDecompositionDemo. 27 ///Alternatively, you can use btGimpactMeshShape for moving concave triangle meshes. 28 ///btBvhTriangleMeshShape has several optimizations, such as bounding volume hierarchy and 29 ///cache friendly traversal for PlayStation 3 Cell SPU. 30 ///It is recommended to enable useQuantizedAabbCompression for better memory usage. 31 ///It takes a triangle mesh as input, for example a btTriangleMesh or btTriangleIndexVertexArray. The btBvhTriangleMeshShape class allows for triangle mesh deformations by a refit or partialRefit method. 32 ///Instead of building the bounding volume hierarchy acceleration structure, it is also possible to serialize (save) and deserialize (load) the structure from disk. 33 ///See Demos\ConcaveDemo\ConcavePhysicsDemo.cpp for an example. 34 ATTRIBUTE_ALIGNED16(class) btBvhTriangleMeshShape : public btTriangleMeshShape 35 { 36 37 btOptimizedBvh* m_bvh; 38 btTriangleInfoMap* m_triangleInfoMap; 39 40 bool m_useQuantizedAabbCompression; 41 bool m_ownsBvh; 42 bool m_pad[11];////need padding due to alignment 43 44 public: 45 46 BT_DECLARE_ALIGNED_ALLOCATOR(); 47 48 49 btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression, bool buildBvh = true); 50 51 ///optionally pass in a larger bvh aabb, used for quantization. This allows for deformations within this aabb 52 btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression,const btVector3& bvhAabbMin,const btVector3& bvhAabbMax, bool buildBvh = true); 53 54 virtual ~btBvhTriangleMeshShape(); 55 56 bool getOwnsBvh () const 57 { 58 return m_ownsBvh; 59 } 60 61 62 63 void performRaycast (btTriangleCallback* callback, const btVector3& raySource, const btVector3& rayTarget); 64 void performConvexcast (btTriangleCallback* callback, const btVector3& boxSource, const btVector3& boxTarget, const btVector3& boxMin, const btVector3& boxMax); 65 66 virtual void processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const; 67 68 void refitTree(const btVector3& aabbMin,const btVector3& aabbMax); 69 70 ///for a fast incremental refit of parts of the tree. Note: the entire AABB of the tree will become more conservative, it never shrinks 71 void partialRefitTree(const btVector3& aabbMin,const btVector3& aabbMax); 72 73 //debugging 74 virtual const char* getName()const {return "BVHTRIANGLEMESH";} 75 76 77 virtual void setLocalScaling(const btVector3& scaling); 78 79 btOptimizedBvh* getOptimizedBvh() 80 { 81 return m_bvh; 82 } 83 84 void setOptimizedBvh(btOptimizedBvh* bvh, const btVector3& localScaling=btVector3(1,1,1)); 85 86 void buildOptimizedBvh(); 87 88 bool usesQuantizedAabbCompression() const 89 { 90 return m_useQuantizedAabbCompression; 91 } 92 93 void setTriangleInfoMap(btTriangleInfoMap* triangleInfoMap) 94 { 95 m_triangleInfoMap = triangleInfoMap; 96 } 97 98 const btTriangleInfoMap* getTriangleInfoMap() const 99 { 100 return m_triangleInfoMap; 101 } 102 103 btTriangleInfoMap* getTriangleInfoMap() 104 { 105 return m_triangleInfoMap; 106 } 107 108 virtual int calculateSerializeBufferSize() const; 109 110 ///fills the dataBuffer and returns the struct name (and 0 on failure) 111 virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const; 112 113 virtual void serializeSingleBvh(btSerializer* serializer) const; 114 115 virtual void serializeSingleTriangleInfoMap(btSerializer* serializer) const; 116 117 }; 118 119 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 120 struct btTriangleMeshShapeData 121 { 122 btCollisionShapeData m_collisionShapeData; 123 124 btStridingMeshInterfaceData m_meshInterface; 125 126 btQuantizedBvhFloatData *m_quantizedFloatBvh; 127 btQuantizedBvhDoubleData *m_quantizedDoubleBvh; 128 129 btTriangleInfoMapData *m_triangleInfoMap; 130 131 float m_collisionMargin; 132 133 char m_pad3[4]; 134 135 }; 136 137 138 SIMD_FORCE_INLINE int btBvhTriangleMeshShape::calculateSerializeBufferSize() const 139 { 140 return sizeof(btTriangleMeshShapeData); 141 } 142 143 144 145 #endif //BT_BVH_TRIANGLE_MESH_SHAPE_H 146