From 621eddb769fca399b8dd0ee06a4605ffe31747d6 Mon Sep 17 00:00:00 2001 From: "erwin.coumans" Date: Tue, 23 Feb 2010 09:52:39 +0000 Subject: [PATCH] make btHashMap data protected (not private) for easier serialization move btTriangleInfoMap.h into its own header, into the BulletCollision/CollisionShapes folder add btTriangleInfoMap pointer to btBvhTriangleMeshShape, so we don't need to use the 'userpointer' anymore preparation for serialization of btTriangleInfoMap. See also http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=4603&start=30 --- Demos/Benchmarks/BenchmarkDemo.cpp | 22 ++-- .../CollisionInterfaceDemo.cpp | 2 +- src/BulletCollision/CMakeLists.txt | 1 + .../btInternalEdgeUtility.cpp | 7 +- .../CollisionDispatch/btInternalEdgeUtility.h | 53 +------- .../btBvhTriangleMeshShape.cpp | 2 + .../CollisionShapes/btBvhTriangleMeshShape.h | 24 +++- .../CollisionShapes/btTriangleInfoMap.h | 124 ++++++++++++++++++ .../btGeneric6DofConstraint.h | 8 +- src/LinearMath/btHashMap.h | 1 + 10 files changed, 171 insertions(+), 73 deletions(-) create mode 100644 src/BulletCollision/CollisionShapes/btTriangleInfoMap.h diff --git a/Demos/Benchmarks/BenchmarkDemo.cpp b/Demos/Benchmarks/BenchmarkDemo.cpp index a6df02973..9d3703933 100644 --- a/Demos/Benchmarks/BenchmarkDemo.cpp +++ b/Demos/Benchmarks/BenchmarkDemo.cpp @@ -464,11 +464,11 @@ void BenchmarkDemo::createWall(const btVector3& offsetPosition,int stackSize,con btVector3 localInertia(0,0,0); blockShape->calculateLocalInertia(mass,localInertia); -// float diffX = boxSize[0] * 1.0f; - float diffY = boxSize[1] * 1.0f; - float diffZ = boxSize[2] * 1.0f; +// btScalar diffX = boxSize[0] * 1.0f; + btScalar diffY = boxSize[1] * 1.0f; + btScalar diffZ = boxSize[2] * 1.0f; - float offset = -stackSize * (diffZ * 2.0f) * 0.5f; + btScalar offset = -stackSize * (diffZ * 2.0f) * 0.5f; btVector3 pos(0.0f, diffY, 0.0f); btTransform trans; @@ -491,24 +491,24 @@ void BenchmarkDemo::createWall(const btVector3& offsetPosition,int stackSize,con void BenchmarkDemo::createPyramid(const btVector3& offsetPosition,int stackSize,const btVector3& boxSize) { - float space = 0.000f; + btScalar space = 0.000f; btVector3 pos(0.0f, boxSize[1], 0.0f); btBoxShape* blockShape = new btBoxShape(btVector3(boxSize[0]-COLLISION_RADIUS,boxSize[1]-COLLISION_RADIUS,boxSize[2]-COLLISION_RADIUS)); btTransform trans; trans.setIdentity(); - float mass = 1.f; + btScalar mass = 1.f; btVector3 localInertia(0,0,0); blockShape->calculateLocalInertia(mass,localInertia); - float diffX = boxSize[0]; - float diffY = boxSize[1]; - float diffZ = boxSize[2]; + btScalar diffX = boxSize[0]; + btScalar diffY = boxSize[1]; + btScalar diffZ = boxSize[2]; - float offsetX = -stackSize * (diffX * 2.0f + space) * 0.5f; - float offsetZ = -stackSize * (diffZ * 2.0f + space) * 0.5f; + btScalar offsetX = -stackSize * (diffX * 2.0f + space) * 0.5f; + btScalar offsetZ = -stackSize * (diffZ * 2.0f + space) * 0.5f; while(stackSize) { for(int j=0;jgetUserPointer()) + if (trimeshShape->getTriangleInfoMap()) return; - trimeshShape->setUserPointer(triangleInfoMap); + trimeshShape->setTriangleInfoMap(triangleInfoMap); btStridingMeshInterface* meshInterface = trimeshShape->getMeshInterface(); const btVector3& meshScaling = meshInterface->getScaling(); @@ -456,7 +456,8 @@ void btAdjustInternalEdgeContacts(btManifoldPoint& cp, const btCollisionObject* if (colObj0->getCollisionShape()->getShapeType() != TRIANGLE_SHAPE_PROXYTYPE) return; - btTriangleInfoMap* triangleInfoMapPtr = (btTriangleInfoMap*) colObj0->getRootCollisionShape()->getUserPointer(); + btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)colObj0->getRootCollisionShape(); + btTriangleInfoMap* triangleInfoMapPtr = (btTriangleInfoMap*) trimesh->getTriangleInfoMap(); if (!triangleInfoMapPtr) return; diff --git a/src/BulletCollision/CollisionDispatch/btInternalEdgeUtility.h b/src/BulletCollision/CollisionDispatch/btInternalEdgeUtility.h index 1e796a965..6319344ba 100644 --- a/src/BulletCollision/CollisionDispatch/btInternalEdgeUtility.h +++ b/src/BulletCollision/CollisionDispatch/btInternalEdgeUtility.h @@ -5,6 +5,8 @@ #include "LinearMath/btHashMap.h" #include "LinearMath/btVector3.h" +#include "BulletCollision/CollisionShapes/btTriangleInfoMap.h" + ///The btInternalEdgeUtility helps to avoid or reduce artifacts due to wrong collision normals caused by internal edges. ///See also http://code.google.com/p/bullet/issues/detail?id=27 @@ -13,58 +15,7 @@ class btCollisionObject; class btManifoldPoint; class btIDebugDraw; -///for btTriangleInfo m_flags -#define TRI_INFO_V0V1_CONVEX 1 -#define TRI_INFO_V1V2_CONVEX 2 -#define TRI_INFO_V2V0_CONVEX 4 -#define TRI_INFO_V0V1_SWAP_NORMALB 8 -#define TRI_INFO_V1V2_SWAP_NORMALB 16 -#define TRI_INFO_V2V0_SWAP_NORMALB 32 - - -///The btTriangleInfo structure stores information to adjust collision normals to avoid collisions against internal edges -///it can be generated using -struct btTriangleInfo -{ - btTriangleInfo() - { - m_edgeV0V1Angle = SIMD_2_PI; - m_edgeV1V2Angle = SIMD_2_PI; - m_edgeV2V0Angle = SIMD_2_PI; - m_flags=0; - } - - int m_flags; - - btScalar m_edgeV0V1Angle; - btScalar m_edgeV1V2Angle; - btScalar m_edgeV2V0Angle; - -}; - -typedef btHashMap btInternalTriangleInfoMap; - - -///The btTriangleInfoMap stores edge angle information for some triangles. You can compute this information yourself or using btGenerateInternalEdgeInfo. -struct btTriangleInfoMap : public btInternalTriangleInfoMap -{ - btScalar m_convexEpsilon;///used to determine if an edge or contact normal is convex, using the dot product - btScalar m_planarEpsilon; ///used to determine if a triangle edge is planar with zero angle - btScalar m_equalVertexThreshold; ///used to compute connectivity: if the distance between two vertices is smaller than m_equalVertexThreshold, they are considered to be 'shared' - btScalar m_edgeDistanceThreshold; ///used to determine edge contacts: if the closest distance between a contact point and an edge is smaller than this distance threshold it is considered to "hit the edge" - btScalar m_zeroAreaThreshold; ///used to determine if a triangle is degenerate (length squared of cross product of 2 triangle edges < threshold) - - - btTriangleInfoMap() - { - m_convexEpsilon = 0.00f; - m_planarEpsilon = 0.0001f; - m_equalVertexThreshold = btScalar(0.0001)*btScalar(0.0001); - m_edgeDistanceThreshold = btScalar(0.1); - m_zeroAreaThreshold = btScalar(0.0001)*btScalar(0.0001); - } -}; enum btInternalEdgeAdjustFlags { diff --git a/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp b/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp index 0670c22d3..1d97a17fe 100644 --- a/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp +++ b/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp @@ -24,6 +24,7 @@ subject to the following restrictions: btBvhTriangleMeshShape::btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression, bool buildBvh) :btTriangleMeshShape(meshInterface), m_bvh(0), +m_triangleInfoMap(0), m_useQuantizedAabbCompression(useQuantizedAabbCompression), m_ownsBvh(false) { @@ -43,6 +44,7 @@ m_ownsBvh(false) btBvhTriangleMeshShape::btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression,const btVector3& bvhAabbMin,const btVector3& bvhAabbMax,bool buildBvh) :btTriangleMeshShape(meshInterface), m_bvh(0), +m_triangleInfoMap(0), m_useQuantizedAabbCompression(useQuantizedAabbCompression), m_ownsBvh(false) { diff --git a/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h b/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h index 688ab3b74..c172c4f01 100644 --- a/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h +++ b/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h @@ -19,7 +19,7 @@ subject to the following restrictions: #include "btTriangleMeshShape.h" #include "btOptimizedBvh.h" #include "LinearMath/btAlignedAllocator.h" - +#include "btTriangleInfoMap.h" ///The btBvhTriangleMeshShape is a static-triangle mesh shape with several optimizations, such as bounding volume hierarchy and cache friendly traversal for PlayStation 3 Cell SPU. It is recommended to enable useQuantizedAabbCompression for better memory usage. ///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. @@ -29,6 +29,8 @@ ATTRIBUTE_ALIGNED16(class) btBvhTriangleMeshShape : public btTriangleMeshShape { btOptimizedBvh* m_bvh; + btTriangleInfoMap* m_triangleInfoMap; + bool m_useQuantizedAabbCompression; bool m_ownsBvh; bool m_pad[11];////need padding due to alignment @@ -37,7 +39,7 @@ public: BT_DECLARE_ALIGNED_ALLOCATOR(); - btBvhTriangleMeshShape() : btTriangleMeshShape(0),m_bvh(0),m_ownsBvh(false) {m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;}; + btBvhTriangleMeshShape() : btTriangleMeshShape(0),m_bvh(0),m_triangleInfoMap(0),m_ownsBvh(false) {m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;}; btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression, bool buildBvh = true); ///optionally pass in a larger bvh aabb, used for quantization. This allows for deformations within this aabb @@ -73,7 +75,6 @@ public: return m_bvh; } - void setOptimizedBvh(btOptimizedBvh* bvh, const btVector3& localScaling=btVector3(1,1,1)); void buildOptimizedBvh(); @@ -83,6 +84,21 @@ public: return m_useQuantizedAabbCompression; } + void setTriangleInfoMap(btTriangleInfoMap* triangleInfoMap) + { + m_triangleInfoMap = triangleInfoMap; + } + + const btTriangleInfoMap* getTriangleInfoMap() const + { + return m_triangleInfoMap; + } + + btTriangleInfoMap* getTriangleInfoMap() + { + return m_triangleInfoMap; + } + virtual int calculateSerializeBufferSize() const; ///fills the dataBuffer and returns the struct name (and 0 on failure) @@ -100,6 +116,8 @@ struct btTriangleMeshShapeData btQuantizedBvhFloatData *m_quantizedFloatBvh; btQuantizedBvhDoubleData *m_quantizedDoubleBvh; + btTriangleInfoMapData *m_triangleInfoMap; + float m_collisionMargin; char m_pad3[4]; diff --git a/src/BulletCollision/CollisionShapes/btTriangleInfoMap.h b/src/BulletCollision/CollisionShapes/btTriangleInfoMap.h new file mode 100644 index 000000000..e24e20dbe --- /dev/null +++ b/src/BulletCollision/CollisionShapes/btTriangleInfoMap.h @@ -0,0 +1,124 @@ +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2010 Erwin Coumans http://bulletphysics.org + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +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. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef _BT_TRIANGLE_INFO_MAP_H +#define _BT_TRIANGLE_INFO_MAP_H + + +#include "LinearMath/btHashMap.h" +#include "LinearMath/btSerializer.h" + + +///for btTriangleInfo m_flags +#define TRI_INFO_V0V1_CONVEX 1 +#define TRI_INFO_V1V2_CONVEX 2 +#define TRI_INFO_V2V0_CONVEX 4 + +#define TRI_INFO_V0V1_SWAP_NORMALB 8 +#define TRI_INFO_V1V2_SWAP_NORMALB 16 +#define TRI_INFO_V2V0_SWAP_NORMALB 32 + + +///The btTriangleInfo structure stores information to adjust collision normals to avoid collisions against internal edges +///it can be generated using +struct btTriangleInfo +{ + btTriangleInfo() + { + m_edgeV0V1Angle = SIMD_2_PI; + m_edgeV1V2Angle = SIMD_2_PI; + m_edgeV2V0Angle = SIMD_2_PI; + m_flags=0; + } + + int m_flags; + + btScalar m_edgeV0V1Angle; + btScalar m_edgeV1V2Angle; + btScalar m_edgeV2V0Angle; + +}; + +typedef btHashMap btInternalTriangleInfoMap; + + +///The btTriangleInfoMap stores edge angle information for some triangles. You can compute this information yourself or using btGenerateInternalEdgeInfo. +struct btTriangleInfoMap : public btInternalTriangleInfoMap +{ + btScalar m_convexEpsilon;///used to determine if an edge or contact normal is convex, using the dot product + btScalar m_planarEpsilon; ///used to determine if a triangle edge is planar with zero angle + btScalar m_equalVertexThreshold; ///used to compute connectivity: if the distance between two vertices is smaller than m_equalVertexThreshold, they are considered to be 'shared' + btScalar m_edgeDistanceThreshold; ///used to determine edge contacts: if the closest distance between a contact point and an edge is smaller than this distance threshold it is considered to "hit the edge" + btScalar m_zeroAreaThreshold; ///used to determine if a triangle is degenerate (length squared of cross product of 2 triangle edges < threshold) + + + btTriangleInfoMap() + { + m_convexEpsilon = 0.00f; + m_planarEpsilon = 0.0001f; + m_equalVertexThreshold = btScalar(0.0001)*btScalar(0.0001); + m_edgeDistanceThreshold = btScalar(0.1); + m_zeroAreaThreshold = btScalar(0.0001)*btScalar(0.0001); + } + + virtual int calculateSerializeBufferSize() const; + + ///fills the dataBuffer and returns the struct name (and 0 on failure) + virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const; + +}; + +struct btTriangleInfoData +{ + int m_flags; + float m_edgeV0V1Angle; + float m_edgeV1V2Angle; + float m_edgeV2V0Angle; +}; + +struct btTriangleInfoMapData +{ + float m_convexEpsilon; + float m_planarEpsilon; + float m_equalVertexThreshold; + float m_edgeDistanceThreshold; + float m_zeroAreaThreshold; + + int m_hashTableSize; + int *m_hashTablePtr; + int m_nextSize; + int *m_nextPtr; + + int m_numValues; + btTriangleInfoData *m_valueArrayPtr; + int m_numKeys; + int *m_keyArrayPtr; + +}; + +SIMD_FORCE_INLINE int btTriangleInfoMap::calculateSerializeBufferSize() const +{ + return sizeof(btTriangleInfoMapData); +} + + ///fills the dataBuffer and returns the struct name (and 0 on failure) +SIMD_FORCE_INLINE const char* btTriangleInfoMap::serialize(void* dataBuffer, btSerializer* serializer) const +{ + ///todo + return 0; +} + + +#endif //_BT_TRIANGLE_INFO_MAP_H diff --git a/src/BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h b/src/BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h index dae33ba36..2653d26dc 100644 --- a/src/BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h +++ b/src/BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h @@ -569,10 +569,10 @@ SIMD_FORCE_INLINE const char* btGeneric6DofConstraint::serialize(void* dataBuffe int i; for (i=0;i<3;i++) { - dof->m_angularLowerLimit.m_floats[i] = m_angularLimits[i].m_loLimit; - dof->m_angularUpperLimit.m_floats[i] = m_angularLimits[i].m_hiLimit; - dof->m_linearLowerLimit.m_floats[i] = m_linearLimits.m_lowerLimit[i]; - dof->m_linearUpperLimit.m_floats[i] = m_linearLimits.m_upperLimit[i]; + dof->m_angularLowerLimit.m_floats[i] = float(m_angularLimits[i].m_loLimit); + dof->m_angularUpperLimit.m_floats[i] = float(m_angularLimits[i].m_hiLimit); + dof->m_linearLowerLimit.m_floats[i] = float(m_linearLimits.m_lowerLimit[i]); + dof->m_linearUpperLimit.m_floats[i] = float(m_linearLimits.m_upperLimit[i]); } dof->m_useLinearReferenceFrameA = m_useLinearReferenceFrameA? 1 : 0; diff --git a/src/LinearMath/btHashMap.h b/src/LinearMath/btHashMap.h index 726b206ee..7679c25b5 100644 --- a/src/LinearMath/btHashMap.h +++ b/src/LinearMath/btHashMap.h @@ -199,6 +199,7 @@ template class btHashMap { +protected: btAlignedObjectArray m_hashTable; btAlignedObjectArray m_next;