faster mesh edge identification
fix for cylinder tunneling in soak
This commit is contained in:
parent
1170814dee
commit
b2747eb2b8
9 changed files with 93 additions and 136 deletions
101
src/mesh.c
101
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 )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
|
|
|||
Loading…
Reference in a new issue