Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add long double precision support #4580

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
#MESSAGE("CMAKE_CXX_FLAGS_DEBUG="+${CMAKE_CXX_FLAGS_DEBUG})

OPTION(USE_DOUBLE_PRECISION "Use double precision" OFF)
OPTION(USE_LONG_DOUBLE_PRECISION "Use long double precision" OFF)
SET(CLAMP_VELOCITIES "0" CACHE STRING "Clamp rigid bodies' velocity to this value, if larger than zero. Useful to prevent floating point errors or in general runaway velocities in complex scenarios")
OPTION(USE_GRAPHICAL_BENCHMARK "Use Graphical Benchmark" ON)
OPTION(BUILD_SHARED_LIBS "Use shared libraries" OFF)
Expand Down Expand Up @@ -240,6 +241,11 @@ ADD_DEFINITIONS( -DBT_USE_DOUBLE_PRECISION)
SET( BULLET_DOUBLE_DEF "-DBT_USE_DOUBLE_PRECISION")
ENDIF (USE_DOUBLE_PRECISION)

IF (USE_LONG_DOUBLE_PRECISION)
ADD_DEFINITIONS( -DBT_USE_LONG_DOUBLE_PRECISION)
SET( BULLET_DOUBLE_DEF "-DBT_USE_LONG_DOUBLE_PRECISION")
ENDIF (USE_LONG_DOUBLE_PRECISION)

IF (NOT USE_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD)
ADD_DEFINITIONS(-DSKIP_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD)
ENDIF ()
Expand Down
63 changes: 62 additions & 1 deletion Extras/Serialize/BulletWorldImporter/btWorldImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,16 @@ btCollisionShape* btWorldImporter::convertCollisionShape(btCollisionShapeData* s
int i;
for (i = 0; i < numPoints; i++)
{
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
if (convexData->m_unscaledPointsLongDoublePtr)
tmpPoints[i].deSerialize(convexData->m_unscaledPointsLongDoublePtr[i]);
if (convexData->m_unscaledPointsDoublePtr)
tmpPoints[i].deSerializeDouble(convexData->m_unscaledPointsDoublePtr[i]);
if (convexData->m_unscaledPointsFloatPtr)
tmpPoints[i].deSerializeFloat(convexData->m_unscaledPointsFloatPtr[i]);
#elif defined(BT_USE_DOUBLE_PRECISION)
if (convexData->m_unscaledPointsLongDoublePtr)
tmpPoints[i].deSerializeLongDouble(convexData->m_unscaledPointsLongDoublePtr[i]);
if (convexData->m_unscaledPointsDoublePtr)
tmpPoints[i].deSerialize(convexData->m_unscaledPointsDoublePtr[i]);
if (convexData->m_unscaledPointsFloatPtr)
Expand All @@ -356,6 +365,8 @@ btCollisionShape* btWorldImporter::convertCollisionShape(btCollisionShapeData* s
tmpPoints[i].deSerialize(convexData->m_unscaledPointsFloatPtr[i]);
if (convexData->m_unscaledPointsDoublePtr)
tmpPoints[i].deSerializeDouble(convexData->m_unscaledPointsDoublePtr[i]);
if (convexData->m_unscaledPointsLongDoublePtr)
tmpPoints[i].deSerializeLongDouble(convexData->m_unscaledPointsLongDoublePtr[i]);
#endif //BT_USE_DOUBLE_PRECISION
}
btConvexHullShape* hullShape = createConvexHullShape();
Expand Down Expand Up @@ -2082,3 +2093,53 @@ void btWorldImporter::convertRigidBodyDouble(btRigidBodyDoubleData* colObjData)
printf("error: no shape found\n");
}
}

void btWorldImporter::convertRigidBodyLongDouble(btRigidBodyLongDoubleData* colObjData)
{
btScalar mass = btScalar(colObjData->m_inverseMass ? 1.f / colObjData->m_inverseMass : 0.f);
btVector3 localInertia;
localInertia.setZero();
btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionObjectData.m_collisionShape);
if (shapePtr && *shapePtr)
{
btTransform startTransform;
colObjData->m_collisionObjectData.m_worldTransform.m_origin.m_floats[3] = 0.f;
startTransform.deSerializeLongDouble(colObjData->m_collisionObjectData.m_worldTransform);

// startTransform.setBasis(btMatrix3x3::getIdentity());
btCollisionShape* shape = (btCollisionShape*)*shapePtr;
if (shape->isNonMoving())
{
mass = 0.f;
}
if (mass)
{
shape->calculateLocalInertia(mass, localInertia);
}
bool isDynamic = mass != 0.f;
btRigidBody* body = createRigidBody(isDynamic, mass, startTransform, shape, colObjData->m_collisionObjectData.m_name);
body->setFriction(btScalar(colObjData->m_collisionObjectData.m_friction));
body->setRestitution(btScalar(colObjData->m_collisionObjectData.m_restitution));
btVector3 linearFactor, angularFactor;
linearFactor.deSerializeLongDouble(colObjData->m_linearFactor);
angularFactor.deSerializeLongDouble(colObjData->m_angularFactor);
body->setLinearFactor(linearFactor);
body->setAngularFactor(angularFactor);

#ifdef USE_INTERNAL_EDGE_UTILITY
if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
btBvhTriangleMeshShape* trimesh = (btBvhTriangleMeshShape*)shape;
if (trimesh->getTriangleInfoMap())
{
body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
}
}
#endif //USE_INTERNAL_EDGE_UTILITY
m_bodyMap.insert(colObjData, body);
}
else
{
printf("error: no shape found\n");
}
}
9 changes: 8 additions & 1 deletion Extras/Serialize/BulletWorldImporter/btWorldImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ struct btContactSolverInfo;
struct btTypedConstraintData;
struct btTypedConstraintFloatData;
struct btTypedConstraintDoubleData;
struct btTypedConstraintLongDoubleData;

struct btRigidBodyLongDoubleData;
struct btRigidBodyDoubleData;
struct btRigidBodyFloatData;

#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
#define btRigidBodyData btRigidBodyLongDoubleData
#elif defined(BT_USE_DOUBLE_PRECISION)
#define btRigidBodyData btRigidBodyDoubleData
#else
#define btRigidBodyData btRigidBodyFloatData
Expand Down Expand Up @@ -87,6 +91,7 @@ class btWorldImporter

btAlignedObjectArray<btVector3FloatData*> m_floatVertexArrays;
btAlignedObjectArray<btVector3DoubleData*> m_doubleVertexArrays;
btAlignedObjectArray<btVector3LongDoubleData*> m_longDoubleVertexArrays;

btHashMap<btHashPtr, btOptimizedBvh*> m_bvhMap;
btHashMap<btHashPtr, btTriangleInfoMap*> m_timMap;
Expand All @@ -110,8 +115,10 @@ class btWorldImporter
void convertConstraintBackwardsCompatible281(btTypedConstraintData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion);
void convertConstraintFloat(btTypedConstraintFloatData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion);
void convertConstraintDouble(btTypedConstraintDoubleData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion);
void convertConstraintLongDouble(btTypedConstraintLongDoubleData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion);
void convertRigidBodyFloat(btRigidBodyFloatData* colObjData);
void convertRigidBodyDouble(btRigidBodyDoubleData* colObjData);
void convertRigidBodyLongDouble(btRigidBodyLongDoubleData* colObjData);

public:
btWorldImporter(btDynamicsWorld* world);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,9 @@ void btBulletXmlWorldImporter::auto_serialize_root_level_children(XMLNode* pPare

for (int i = 0; i < m_rigidBodyData.size(); i++)
{
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
convertRigidBodyLongDouble(m_rigidBodyData[i]);
#elif defined(BT_USE_DOUBLE_PRECISION)
convertRigidBodyDouble(m_rigidBodyData[i]);
#else
convertRigidBodyFloat(m_rigidBodyData[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class XMLNode;

struct btConvexInternalShapeData;
struct btCollisionShapeData;
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
struct btRigidBodyLongDoubleData;
struct btTypedConstraintLongDoubleData;
#define btRigidBodyData btRigidBodyLongDoubleData
#define btTypedConstraintData2 btTypedConstraintLongDoubleData
#elif defined(BT_USE_DOUBLE_PRECISION)
struct btRigidBodyDoubleData;
struct btTypedConstraintDoubleData;
#define btRigidBodyData btRigidBodyDoubleData
Expand Down
4 changes: 3 additions & 1 deletion examples/Collision/CollisionSdkC_Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
int unused; \
} * name

#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
typedef long double plReal;
#elif defined(BT_USE_DOUBLE_PRECISION)
typedef double plReal;
#else
typedef float plReal;
Expand Down
4 changes: 3 additions & 1 deletion examples/SharedMemory/PhysicsServerCommandProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ struct CommandLogger

void writeHeader(unsigned char* buffer) const
{
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
memcpy(buffer, "BT3CMDld", 8);
#elif defined(BT_USE_DOUBLE_PRECISION)
memcpy(buffer, "BT3CMDd", 7);
#else
memcpy(buffer, "BT3CMDf", 7);
Expand Down
4 changes: 3 additions & 1 deletion examples/SharedMemory/physx/PhysXServerCommandProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,9 @@ bool PhysXServerCommandProcessor::processLoadURDFCommand(const struct SharedMemo
mbd->m_links = mbd->m_numLinks ? (btMultiBodyLinkData *)ser.getUniquePointer((void *)articulation) : 0;

// Fill padding with zeros to appease msan.
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
memset(mbd->m_padding, 0, sizeof(mbd->m_padding));
#elif defined(BT_USE_DOUBLE_PRECISION)
memset(mbd->m_padding, 0, sizeof(mbd->m_padding));
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,12 @@ int b3VoronoiSimplexSolver::pointOutsideOfPlane(const b3Vector3& p, const b3Vect
b3Scalar signd = (d - a).dot(normal); // [AD AB AC]

#ifdef B3_CATCH_DEGENERATE_TETRAHEDRON
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
if (signd * signd < (b3Scalar(1e-8) * b3Scalar(1e-8)))
{
return -1;
}
#elif defined(BT_USE_DOUBLE_PRECISION)
if (signd * signd < (b3Scalar(1e-8) * b3Scalar(1e-8)))
{
return -1;
Expand Down
33 changes: 32 additions & 1 deletion src/BulletCollision/BroadphaseCollision/btQuantizedBvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class btSerializer;
#include "LinearMath/btVector3.h"
#include "LinearMath/btAlignedAllocator.h"

#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
#define btQuantizedBvhData btQuantizedBvhLongDoubleData
#define btOptimizedBvhNodeData btOptimizedBvhNodeLongDoubleData
#define btQuantizedBvhDataName "btQuantizedBvhLongDoubleData"
#elif defined(BT_USE_DOUBLE_PRECISION)
#define btQuantizedBvhData btQuantizedBvhDoubleData
#define btOptimizedBvhNodeData btOptimizedBvhNodeDoubleData
#define btQuantizedBvhDataName "btQuantizedBvhDoubleData"
Expand Down Expand Up @@ -494,6 +498,16 @@ struct btOptimizedBvhNodeDoubleData
char m_pad[4];
};

struct btOptimizedBvhNodeLongDoubleData
{
btVector3LongDoubleData m_aabbMinOrg;
btVector3LongDoubleData m_aabbMaxOrg;
int m_escapeIndex;
int m_subPart;
int m_triangleIndex;
char m_pad[4];
};


struct btQuantizedBvhNodeData
{
Expand Down Expand Up @@ -535,6 +549,23 @@ struct btQuantizedBvhDoubleData
int m_numSubtreeHeaders;
btBvhSubtreeInfoData *m_subTreeInfoPtr;
};

struct btQuantizedBvhLongDoubleData
{
btVector3LongDoubleData m_bvhAabbMin;
btVector3LongDoubleData m_bvhAabbMax;
btVector3LongDoubleData m_bvhQuantization;
int m_curNodeIndex;
int m_useQuantization;
int m_numContiguousLeafNodes;
int m_numQuantizedContiguousNodes;
btOptimizedBvhNodeLongDoubleData *m_contiguousNodesPtr;
btQuantizedBvhNodeData *m_quantizedContiguousNodesPtr;

int m_traversalMode;
int m_numSubtreeHeaders;
btBvhSubtreeInfoData *m_subTreeInfoPtr;
};
// clang-format on

SIMD_FORCE_INLINE int btQuantizedBvh::calculateSerializeBufferSizeNew() const
Expand Down
39 changes: 38 additions & 1 deletion src/BulletCollision/CollisionDispatch/btCollisionObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ struct btCollisionShapeData;

typedef btAlignedObjectArray<class btCollisionObject*> btCollisionObjectArray;

#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
#define btCollisionObjectData btCollisionObjectLongDoubleData
#define btCollisionObjectDataName "btCollisionObjectLongDoubleData"
#elif defined(BT_USE_DOUBLE_PRECISION)
#define btCollisionObjectData btCollisionObjectDoubleData
#define btCollisionObjectDataName "btCollisionObjectDoubleData"
#else
Expand Down Expand Up @@ -612,6 +615,40 @@ btCollisionObject
// clang-format off

///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
struct btCollisionObjectLongDoubleData
{
void *m_broadphaseHandle;
void *m_collisionShape;
btCollisionShapeData *m_rootCollisionShape;
char *m_name;

btTransformLongDoubleData m_worldTransform;
btTransformLongDoubleData m_interpolationWorldTransform;
btVector3LongDoubleData m_interpolationLinearVelocity;
btVector3LongDoubleData m_interpolationAngularVelocity;
btVector3LongDoubleData m_anisotropicFriction;
long double m_contactProcessingThreshold;
long double m_deactivationTime;
long double m_friction;
long double m_rollingFriction;
long double m_contactDamping;
long double m_contactStiffness;
long double m_restitution;
long double m_hitFraction;
long double m_ccdSweptSphereRadius;
long double m_ccdMotionThreshold;
int m_hasAnisotropicFriction;
int m_collisionFlags;
int m_islandTag1;
int m_companionId;
int m_activationState1;
int m_internalType;
int m_checkCollideWith;
int m_collisionFilterGroup;
int m_collisionFilterMask;
int m_uniqueId;//m_uniqueId is introduced for paircache. could get rid of this, by calculating the address offset etc.
};

struct btCollisionObjectDoubleData
{
void *m_broadphaseHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,16 @@ btCollisionShape* btCollisionWorldImporter::convertCollisionShape(btCollisionSha
int i;
for (i = 0; i < numPoints; i++)
{
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
if (convexData->m_unscaledPointsDoublePtr)
tmpPoints[i].deSerializeDouble(convexData->m_unscaledPointsDoublePtr[i]);
if (convexData->m_unscaledPointsLongDoublePtr)
tmpPoints[i].deSerialize(convexData->m_unscaledPointsLongDoublePtr[i]);
if (convexData->m_unscaledPointsFloatPtr)
tmpPoints[i].deSerializeFloat(convexData->m_unscaledPointsFloatPtr[i]);
#elif defined(BT_USE_DOUBLE_PRECISION)
if (convexData->m_unscaledPointsLongDoublePtr)
tmpPoints[i].deSerializeLongDouble(convexData->m_unscaledPointsLongDoublePtr[i]);
if (convexData->m_unscaledPointsDoublePtr)
tmpPoints[i].deSerialize(convexData->m_unscaledPointsDoublePtr[i]);
if (convexData->m_unscaledPointsFloatPtr)
Expand All @@ -456,6 +465,8 @@ btCollisionShape* btCollisionWorldImporter::convertCollisionShape(btCollisionSha
tmpPoints[i].deSerialize(convexData->m_unscaledPointsFloatPtr[i]);
if (convexData->m_unscaledPointsDoublePtr)
tmpPoints[i].deSerializeDouble(convexData->m_unscaledPointsDoublePtr[i]);
if (convexData->m_unscaledPointsLongDoublePtr)
tmpPoints[i].deSerializeLongDouble(convexData->m_unscaledPointsLongDoublePtr[i]);
#endif //BT_USE_DOUBLE_PRECISION
}
btConvexHullShape* hullShape = createConvexHullShape();
Expand Down
16 changes: 14 additions & 2 deletions src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,22 +382,34 @@ const char* btBvhTriangleMeshShape::serialize(void* dataBuffer, btSerializer* se
void* chunk = serializer->findPointer(m_bvh);
if (chunk)
{
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
trimeshData->m_quantizedLongDoubleBvh = (btQuantizedBvhData*)chunk;
trimeshData->m_quantizedFloatBvh = 0;
trimeshData->m_quantizedDoubleBvh = 0;
#elif defined(BT_USE_DOUBLE_PRECISION)
trimeshData->m_quantizedDoubleBvh = (btQuantizedBvhData*)chunk;
trimeshData->m_quantizedFloatBvh = 0;
trimeshData->m_quantizedLongDoubleBvh = 0;
#else
trimeshData->m_quantizedFloatBvh = (btQuantizedBvhData*)chunk;
trimeshData->m_quantizedDoubleBvh = 0;
trimeshData->m_quantizedLongDoubleBvh = 0;
#endif //BT_USE_DOUBLE_PRECISION
}
else
{
#ifdef BT_USE_DOUBLE_PRECISION
#ifdef BT_USE_LONG_DOUBLE_PRECISION
trimeshData->m_quantizedLongDoubleBvh = (btQuantizedBvhData*)serializer->getUniquePointer(m_bvh);
trimeshData->m_quantizedFloatBvh = 0;
trimeshData->m_quantizedDoubleBvh = 0;
#elif defined(BT_USE_DOUBLE_PRECISION)
trimeshData->m_quantizedDoubleBvh = (btQuantizedBvhData*)serializer->getUniquePointer(m_bvh);
trimeshData->m_quantizedFloatBvh = 0;
trimeshData->m_quantizedLongDoubleBvh = 0;
#else
trimeshData->m_quantizedFloatBvh = (btQuantizedBvhData*)serializer->getUniquePointer(m_bvh);
trimeshData->m_quantizedDoubleBvh = 0;
trimeshData->m_quantizedLongDoubleBvh = 0;
#endif //BT_USE_DOUBLE_PRECISION

int sz = m_bvh->calculateSerializeBufferSizeNew();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ struct btTriangleMeshShapeData

btQuantizedBvhFloatData *m_quantizedFloatBvh;
btQuantizedBvhDoubleData *m_quantizedDoubleBvh;
btQuantizedBvhLongDoubleData *m_quantizedLongDoubleBvh;

btTriangleInfoMapData *m_triangleInfoMap;

Expand Down
1 change: 1 addition & 0 deletions src/BulletCollision/CollisionShapes/btConcaveShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef enum PHY_ScalarType
{
PHY_FLOAT,
PHY_DOUBLE,
PHY_LONG_DOUBLE,
PHY_INTEGER,
PHY_SHORT,
PHY_FIXEDPOINT88,
Expand Down
Loading