From b2747eb2b83ff1b2df3a19dd05e4304910c5fe83 Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Mon, 2 Nov 2026 14:05:15 -0800 Subject: [PATCH] faster mesh edge identification fix for cylinder tunneling in soak --- benchmark/ghost_baseline.csv | 10 ---- benchmark/ghost_baseline_hf.csv | 10 ---- benchmark/ghost_scan.c | 4 +- benchmark/soak_baseline.csv | 14 ++--- samples/sample_manifold.cpp | 24 -------- shared/ghost_collisions.c | 15 +---- shared/ghost_collisions.h | 1 - src/mesh.c | 101 ++++++++++++++++++-------------- src/mesh_contact.c | 50 ++++++++-------- 9 files changed, 93 insertions(+), 136 deletions(-) diff --git a/benchmark/ghost_baseline.csv b/benchmark/ghost_baseline.csv index 783182a..e143a9d 100644 --- a/benchmark/ghost_baseline.csv +++ b/benchmark/ghost_baseline.csv @@ -59,16 +59,6 @@ tjunction,0,1,capsule,0,2 tjunction,0,1,upright,0,2 tjunction,0,1,sphere,0,2 tjunction,0,1,cylinder,0,2 -seam,0,0,box,0,2 -seam,0,0,capsule,0,2 -seam,0,0,upright,0,2 -seam,0,0,sphere,0,2 -seam,0,0,cylinder,2,2 -seam,0,1,box,2,2 -seam,0,1,capsule,0,2 -seam,0,1,upright,0,2 -seam,0,1,sphere,0,2 -seam,0,1,cylinder,0,2 peel,0,0,box,0,2 peel,0,0,capsule,0,2 peel,0,0,upright,0,2 diff --git a/benchmark/ghost_baseline_hf.csv b/benchmark/ghost_baseline_hf.csv index 5ce9643..0309718 100644 --- a/benchmark/ghost_baseline_hf.csv +++ b/benchmark/ghost_baseline_hf.csv @@ -59,16 +59,6 @@ tjunction,0,1,capsule,0,2 tjunction,0,1,upright,0,2 tjunction,0,1,sphere,0,2 tjunction,0,1,cylinder,0,2 -seam,0,0,box,0,2 -seam,0,0,capsule,0,2 -seam,0,0,upright,6,2 -seam,0,0,sphere,0,2 -seam,0,0,cylinder,3,2 -seam,0,1,box,2,1 -seam,0,1,capsule,0,2 -seam,0,1,upright,0,2 -seam,0,1,sphere,0,2 -seam,0,1,cylinder,0,2 peel,0,0,box,0,2 peel,0,0,capsule,0,2 peel,0,0,upright,0,2 diff --git a/benchmark/ghost_scan.c b/benchmark/ghost_scan.c index 7429ec2..784a9d3 100644 --- a/benchmark/ghost_scan.c +++ b/benchmark/ghost_scan.c @@ -18,7 +18,7 @@ #include // Command line and result file keys -static const char* s_sceneKeys[e_ghostSceneCount] = { "flat", "ramp", "cliff", "crest", "bumps", "tjunction", "seam", "peel" }; +static const char* s_sceneKeys[e_ghostSceneCount] = { "flat", "ramp", "cliff", "crest", "bumps", "tjunction", "peel" }; static const char* s_bodyKeys[e_ghostBodyCount] = { "box", "capsule", "upright", "sphere", "cylinder" }; #define MAX_CONFIGS 256 @@ -270,7 +270,7 @@ int main( int argc, char** argv ) { printf( "Usage: ghost_scan [options]\n" "Runs the Ghost Collisions sample headless and counts ghost manifolds per scene and body.\n" - " -s, --scene= flat, ramp, cliff, crest, bumps, tjunction, seam, peel (default all)\n" + " -s, --scene= flat, ramp, cliff, crest, bumps, tjunction, peel (default all)\n" " -b, --body= box, capsule, upright, sphere, cylinder (default all)\n" " -m, --mirror=<0|1|all> mirrored mesh (default all)\n" " -p, --preset=<0-3|all> PEEL preset (default all)\n" diff --git a/benchmark/soak_baseline.csv b/benchmark/soak_baseline.csv index 10100f4..fb55bb8 100644 --- a/benchmark/soak_baseline.csv +++ b/benchmark/soak_baseline.csv @@ -1,9 +1,9 @@ filter,base,generations,shape,tunnels,awake,depth4,depth8,depth16,depth32 -all,12345,1000,box,0,0,1536,214,2,0 -all,12345,1000,capsule,0,0,14474,3477,28,0 -all,12345,1000,cylinder,0,0,11200,2338,36,0 +all,12345,1000,box,0,0,1544,220,4,0 +all,12345,1000,capsule,0,0,14505,3487,28,0 +all,12345,1000,cylinder,0,0,11228,2355,37,0 all,12345,1000,sphere,0,0,56,0,0,0 -all,12345,16000,box,0,0,23701,3164,43,0 -all,12345,16000,capsule,0,0,231138,54408,513,0 -all,12345,16000,cylinder,0,0,179066,37366,473,0 -all,12345,16000,sphere,0,0,989,0,0,0 +all,12345,16000,box,0,0,23648,3205,46,0 +all,12345,16000,capsule,0,0,231483,54547,515,0 +all,12345,16000,cylinder,0,0,179103,37448,485,0 +all,12345,16000,sphere,0,0,987,0,0,0 diff --git a/samples/sample_manifold.cpp b/samples/sample_manifold.cpp index 66ccfbc..7bc1c6d 100644 --- a/samples/sample_manifold.cpp +++ b/samples/sample_manifold.cpp @@ -2615,20 +2615,6 @@ static void BuildGhostTJunction( std::vector& meshes ) meshes.push_back( mesh ); } -// The flat pair with each triangle in its own mesh -static void BuildGhostSeam( std::vector& meshes ) -{ - GhostMesh left; - left.vertices = { { 0.0f, 0.0f, -2.0f }, { 0.0f, 0.0f, 2.0f }, { -2.0f, 0.0f, 0.0f } }; - AddGhostTriangle( left, 0, 1, 2 ); - meshes.push_back( left ); - - GhostMesh right; - right.vertices = { { 0.0f, 0.0f, -2.0f }, { 0.0f, 0.0f, 2.0f }, { 2.0f, 0.0f, 0.0f } }; - AddGhostTriangle( right, 1, 0, 2 ); - meshes.push_back( right ); -} - // s&box units are inches. Beam top at y = 0 for x > 0, chamfer dropping 1 inch over 1.5 inch for x // < 0, 8 inch cells along z with the chamfer quads split like the s&box sample. A side depth in inches // adds a vertical face below the chamfer, so the chamfer bottom edge is convex instead of a boundary. @@ -2751,16 +2737,6 @@ static const GhostScene s_hullGhostScenes[] = { { 0.0f, 45.0f, 0.0f }, true, }, - { - "Mesh seam", - "the flat corner case with each triangle in its own mesh", - "none yet, each mesh only sees its own triangles (ghost plan step 4)", - BuildGhostSeam, - { 0.5f, 0.5f, 0.5f }, - { -0.708f, 0.499f, 0.0f }, - { 0.0f, 45.0f, 0.0f }, - true, - }, { "Chamfer", "none without handling, SAT picks the box bottom for the chamfer triangles, 0.07 inch deep", diff --git a/shared/ghost_collisions.c b/shared/ghost_collisions.c index ad84279..3207139 100644 --- a/shared/ghost_collisions.c +++ b/shared/ghost_collisions.c @@ -29,7 +29,7 @@ static const PeelPreset s_peelPresets[GHOST_PEEL_PRESET_COUNT] = { }; const char* g_ghostSceneNames[e_ghostSceneCount] = { - "Flat", "Concave", "Cliff", "Crest", "Bumps", "T-Junction", "Seam", "Internal", + "Flat", "Concave", "Cliff", "Crest", "Bumps", "T-Junction", "Internal", }; const char* g_ghostBodyNames[e_ghostBodyCount] = { "Box", "H Capsule", "V Capsule", "Sphere", "Cylinder" }; @@ -377,11 +377,6 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) GetCellCount( 2.0f * groundHalfWidth, 0.5f * cellSize ), false ); AddHeightField( worldId, data, 0.0f, h, GetCellCount( h, cellSize ), groundHalfWidth, cellCountZ, false ); } - else if ( settings->scene == e_ghostSeam ) - { - AddHeightField( worldId, data, -h, 0.0f, GetCellCount( h, cellSize ), groundHalfWidth, cellCountZ, false ); - AddHeightField( worldId, data, 0.0f, h, GetCellCount( h, cellSize ), groundHalfWidth, cellCountZ, false ); - } else if ( settings->scene == e_ghostInternal ) { // One grid line per profile row, as in the mesh @@ -408,14 +403,6 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) AddStrip( &builder, right, 2, cellSize, cellSize, mirror ); AddMesh( data, &builder, false ); } - else if ( settings->scene == e_ghostSeam ) - { - // Two mesh shapes meeting at x = 0, so the seam spans two contacts - AddStrip( &builder, left, 2, cellSize, cellSize, mirror ); - AddMesh( data, &builder, false ); - AddStrip( &builder, right, 2, cellSize, cellSize, mirror ); - AddMesh( data, &builder, false ); - } else if ( settings->scene == e_ghostInternal ) { // The mesh has no friction and the ground plane under it does, as in PEEL diff --git a/shared/ghost_collisions.h b/shared/ghost_collisions.h index 4161257..70b8696 100644 --- a/shared/ghost_collisions.h +++ b/shared/ghost_collisions.h @@ -27,7 +27,6 @@ typedef enum GhostScene e_ghostCrest, e_ghostBumps, e_ghostTJunction, - e_ghostSeam, e_ghostInternal, e_ghostSceneCount } GhostScene; diff --git a/src/mesh.c b/src/mesh.c index 5439d8e..5b439cb 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -1144,34 +1144,6 @@ int b3GetMeshVertexFlag( const b3Mesh* mesh, int triangleIndex, int vertexIndex return vertexFlags[indices[vertexIndex]]; } -#define NAME b3EdgeMap -#define KEY_TY uint64_t -#define VAL_TY int -#define HASH_FN vt_hash_integer -#define CMPR_FN vt_cmpr_integer -#define MALLOC_FN b3Alloc -#define FREE_FN b3Free -#include "verstable.h" - -#if 0 -// todo this is for testing other hash tables -struct b3TestHash -{ - size_t operator()( uint64_t key ) const - { - key ^= key >> 23; - key *= 0x2127599bf4325c37ull; - key ^= key >> 47; - return (size_t)key; - } -}; -#endif - -// Results for MeshCreationBenchmark -// eastl::hash_map : 4.9703 ms and 5.1445ms with FastHash function -// std::unordered_map : 4.8755 ms and 4.4780ms with FastHash function -// verstable : 3.3968 ms with default FastHash -// no edge identification : 1.7396 ms static void b3IdentifyEdges( b3MeshData* mesh ) { b3MeshTriangle* triangles = b3GetMeshTrianglesWrite( mesh ); @@ -1233,27 +1205,61 @@ static void b3IdentifyEdges( b3MeshData* mesh ) normals[i] = b3Normalize( n ); } - b3EdgeMap map; - b3EdgeMap_init( &map ); - b3EdgeMap_reserve( &map, edgeCount ); + // Counting sort for edges. Faster than a hashtable. + int vertexCount = mesh->vertexCount; + int* edgeStarts = B3_ALLOC( int, ( vertexCount + 1 ) ); + int* sortedEdges = B3_ALLOC( int, edgeCount ); + int* stamps = B3_ALLOC( int, vertexCount ); + int* firstEdges = B3_ALLOC( int, vertexCount ); - uint64_t key = (uint64_t)edges[0].vertex1 << 32 | (uint64_t)edges[0].vertex2; - b3EdgeMap_insert( &map, key, 0 ); + for ( int i = 0; i <= vertexCount; ++i ) + { + edgeStarts[i] = 0; + } + + for ( int i = 0; i < vertexCount; ++i ) + { + stamps[i] = B3_NULL_INDEX; + } + + for ( int i = 0; i < edgeCount; ++i ) + { + B3_ASSERT( 0 <= edges[i].vertex1 && edges[i].vertex2 < vertexCount ); + edgeStarts[edges[i].vertex1 + 1] += 1; + } + + for ( int i = 0; i < vertexCount; ++i ) + { + edgeStarts[i + 1] += edgeStarts[i]; + } + + for ( int i = 0; i < edgeCount; ++i ) + { + int vertex1 = edges[i].vertex1; + sortedEdges[edgeStarts[vertex1]] = i; + edgeStarts[vertex1] += 1; + } // Find unique edges and assign adjacency - for ( int i = 1; i < edgeCount; ++i ) + int begin = 0; + for ( int vertex1 = 0; vertex1 < vertexCount; ++vertex1 ) { - b3MeshEdge* edge = edges + i; - key = (uint64_t)edge->vertex1 << 32 | (uint64_t)edge->vertex2; - b3EdgeMap_itr itr = b3EdgeMap_get( &map, key ); + int end = edgeStarts[vertex1]; + for ( int j = begin; j < end; ++j ) + { + int i = sortedEdges[j]; + b3MeshEdge* edge = edges + i; + int vertex2 = edge->vertex2; - if ( b3EdgeMap_is_end( itr ) ) - { - b3EdgeMap_insert( &map, key, i ); - } - else - { - int otherIndex = itr.data->val; + // Establish twin. + if ( stamps[vertex2] != vertex1 ) + { + stamps[vertex2] = vertex1; + firstEdges[vertex2] = i; + continue; + } + + int otherIndex = firstEdges[vertex2]; B3_ASSERT( otherIndex < i ); b3MeshEdge* base = edges + otherIndex; @@ -1265,9 +1271,14 @@ static void b3IdentifyEdges( b3MeshData* mesh ) base->triangleCount += 1; } + + begin = end; } - b3EdgeMap_cleanup( &map ); + B3_FREE( firstEdges, int, vertexCount ); + B3_FREE( stamps, int, vertexCount ); + B3_FREE( sortedEdges, int, edgeCount ); + B3_FREE( edgeStarts, int, ( vertexCount + 1 ) ); for ( int i = 0; i < edgeCount; ++i ) { diff --git a/src/mesh_contact.c b/src/mesh_contact.c index df66957..6801d1b 100644 --- a/src/mesh_contact.c +++ b/src/mesh_contact.c @@ -621,28 +621,17 @@ static b3NeighborResult b3TestHullFaceNeighbors( const b3Shape* shape, int trian b3TriangleFeature feature = b3_featureNone; if ( nearCount == 2 ) { - int vertexIndex; if ( nearEdges[0] == 0 && nearEdges[1] == 1 ) { feature = b3_featureVertex2; - vertexIndex = 1; } else if ( nearEdges[0] == 1 ) { feature = b3_featureVertex3; - vertexIndex = 2; } else { feature = b3_featureVertex1; - vertexIndex = 0; - } - - // Test vertex. - if ( b3AdmitsVertexNormal( shape, triangleIndex, vertices, vertexIndex, normal ) ) - { - *nearFeature = feature; - return b3_neighborAdmit; } } @@ -766,10 +755,7 @@ static bool b3ShouldBlockNormal( const b3Vec3* vertices, b3BlockTarget target, b { b3Vec3 triangleNormal = target.normal; float distanceTolerance = target.distanceTolerance; - if ( b3AbsFloat( b3Dot( triangleNormal, b3Sub( point, vertices[0] ) ) ) > distanceTolerance ) - { - return false; - } + B3_ASSERT( b3AbsFloat( b3Dot( triangleNormal, b3Sub( point, vertices[0] ) ) ) <= distanceTolerance ); if ( b3Dot( normal, triangleNormal ) < -0.01f ) { @@ -864,12 +850,14 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape const b3Vec3* meshVertices = NULL; b3Vec3 meshScale = b3Vec3_one; bool meshFlipped = false; + bool unitScale = true; if ( shapeA->type == b3_meshShape ) { meshTriangles = b3GetMeshTriangles( shapeA->mesh.data ); meshVertices = b3GetMeshVertices( shapeA->mesh.data ); meshScale = shapeA->mesh.scale; meshFlipped = meshScale.x * meshScale.y * meshScale.z < 0.0f; + unitScale = meshScale.x == 1.0f && meshScale.y == 1.0f && meshScale.z == 1.0f; } b3FloatW4 cxx = b3SplatW4( relativeMatrix.cx.x ); @@ -907,13 +895,21 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape if ( meshVertices != NULL ) { b3MeshTriangle meshTriangle = meshTriangles[triangleIndex]; - lanes[0][lane] = b3Mul( meshScale, meshVertices[meshTriangle.index1] ); - lanes[1][lane] = - b3Mul( meshScale, - meshVertices[meshFlipped ? meshTriangle.index3 : meshTriangle.index2] ); - lanes[2][lane] = - b3Mul( meshScale, - meshVertices[meshFlipped ? meshTriangle.index2 : meshTriangle.index3] ); + b3Vec3 v1 = meshVertices[meshTriangle.index1]; + b3Vec3 v2 = meshVertices[meshFlipped ? meshTriangle.index3 : meshTriangle.index2]; + b3Vec3 v3 = meshVertices[meshFlipped ? meshTriangle.index2 : meshTriangle.index3]; + + // todo comment + if ( unitScale == false ) + { + v1 = b3Mul( meshScale, v1 ); + v2 = b3Mul( meshScale, v2 ); + v3 = b3Mul( meshScale, v3 ); + } + + lanes[0][lane] = v1; + lanes[1][lane] = v2; + lanes[2][lane] = v3; } else { @@ -1205,7 +1201,15 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape continue; } - if ( b3ShouldBlockNormal( survivorVertices + 3 * j, blockTargets[j], source->point, normal ) ) + // todo comment + const b3Vec3* targetVertices = survivorVertices + 3 * j; + float planeDistance = b3Dot( blockTargets[j].normal, b3Sub( source->point, targetVertices[0] ) ); + if ( b3AbsFloat( planeDistance ) > blockTargets[j].distanceTolerance ) + { + continue; + } + + if ( b3ShouldBlockNormal( targetVertices, blockTargets[j], source->point, normal ) ) { // todo explain this better float alignment = b3Dot( normal, blockTargets[j].normal );