Name cache (#53)

Removed B3_BODY_NAME_LENGTH and B3_SHAPE_NAME_LENGTH.
Now using a name cache instead to save memory and make recordings more
universal.
This is a recording version bump.
This commit is contained in:
Erin Catto 2026-07-06 17:33:45 -07:00 • committed by GitHub
commit 52f1a254ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 759 additions and 824 deletions

View file

@ -277,8 +277,8 @@ static int PointInShapeTest( void )
// Shared assertions for a surface hit. The normal points outward toward the ray,
// the point sits on the surface, and the point lies on the ray at the reported fraction.
static int CheckCastHit( b3CastOutput out, b3Vec3 origin, b3Vec3 translation, b3Vec3 point, b3Vec3 normal,
float fraction, float tol )
static int CheckCastHit( b3CastOutput out, b3Vec3 origin, b3Vec3 translation, b3Vec3 point, b3Vec3 normal, float fraction,
float tol )
{
ENSURE( out.hit );
ENSURE_SMALL( out.fraction - fraction, tol );
@ -313,22 +313,22 @@ static int RayCastSphereHitTest( void )
{
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastSphere( &s, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 0.0f, 0.0f },
( b3Vec3 ){ -1.0f, 0.0f, 0.0f }, 3.0f / 8.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ -1.0f, 0.0f, 0.0f }, (b3Vec3){ -1.0f, 0.0f, 0.0f },
3.0f / 8.0f, 1e-5f ) )
return 1;
}
{
b3RayCastInput input = { { 0.0f, 4.0f, 0.0f }, { 0.0f, -8.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastSphere( &s, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 1.0f, 0.0f },
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 3.0f / 8.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 0.0f, 1.0f, 0.0f }, (b3Vec3){ 0.0f, 1.0f, 0.0f },
3.0f / 8.0f, 1e-5f ) )
return 1;
}
{
b3RayCastInput input = { { 0.0f, 0.0f, -4.0f }, { 0.0f, 0.0f, 8.0f }, 1.0f };
b3CastOutput out = b3RayCastSphere( &s, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 0.0f, -1.0f },
( b3Vec3 ){ 0.0f, 0.0f, -1.0f }, 3.0f / 8.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 0.0f, 0.0f, -1.0f }, (b3Vec3){ 0.0f, 0.0f, -1.0f },
3.0f / 8.0f, 1e-5f ) )
return 1;
}
@ -337,8 +337,8 @@ static int RayCastSphereHitTest( void )
b3Sphere s2 = { { 5.0f, 0.0f, 0.0f }, 2.0f };
b3RayCastInput input = { { 0.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastSphere( &s2, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 3.0f, 0.0f, 0.0f },
( b3Vec3 ){ -1.0f, 0.0f, 0.0f }, 0.3f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 3.0f, 0.0f, 0.0f }, (b3Vec3){ -1.0f, 0.0f, 0.0f },
0.3f, 1e-5f ) )
return 1;
}
@ -347,8 +347,8 @@ static int RayCastSphereHitTest( void )
float k = 0.70710678f;
b3RayCastInput input = { { -3.0f, -3.0f, 0.0f }, { 6.0f, 6.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastSphere( &s, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -k, -k, 0.0f },
( b3Vec3 ){ -k, -k, 0.0f }, 0.382149f, 1e-4f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ -k, -k, 0.0f }, (b3Vec3){ -k, -k, 0.0f }, 0.382149f,
1e-4f ) )
return 1;
}
@ -447,24 +447,24 @@ static int RayCastCapsuleSideTest( void )
{
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 1.0f, 0.0f },
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 1.0f / 3.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 0.0f, 1.0f, 0.0f }, (b3Vec3){ 0.0f, 1.0f, 0.0f },
1.0f / 3.0f, 1e-5f ) )
return 1;
}
// Same from +z to exercise the other transverse direction.
{
b3RayCastInput input = { { 0.0f, 0.0f, 3.0f }, { 0.0f, 0.0f, -6.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 0.0f, 1.0f },
( b3Vec3 ){ 0.0f, 0.0f, 1.0f }, 1.0f / 3.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 0.0f, 0.0f, 1.0f }, (b3Vec3){ 0.0f, 0.0f, 1.0f },
1.0f / 3.0f, 1e-5f ) )
return 1;
}
// Side hit nearer the c1 end.
{
b3RayCastInput input = { { -1.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 1.0f, 0.0f },
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 1.0f / 3.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ -1.0f, 1.0f, 0.0f }, (b3Vec3){ 0.0f, 1.0f, 0.0f },
1.0f / 3.0f, 1e-5f ) )
return 1;
}
return 0;
@ -477,8 +477,8 @@ static int RayCastCapsuleObliqueTest( void )
// dot(axis, rayAxis) != 0.
b3RayCastInput input = { { -3.0f, 3.0f, 0.0f }, { 4.0f, -4.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
return CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 1.0f, 0.0f },
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 0.5f, 1e-4f );
return CheckCastHit( out, input.origin, input.translation, (b3Vec3){ -1.0f, 1.0f, 0.0f }, (b3Vec3){ 0.0f, 1.0f, 0.0f }, 0.5f,
1e-4f );
}
static int RayCastCapsuleCapTest( void )
@ -489,24 +489,24 @@ static int RayCastCapsuleCapTest( void )
{
b3RayCastInput input = { { 5.0f, 0.0f, 0.0f }, { -8.0f, 0.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 3.0f, 0.0f, 0.0f },
( b3Vec3 ){ 1.0f, 0.0f, 0.0f }, 1.0f / 4.0f, 1e-5f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 3.0f, 0.0f, 0.0f }, (b3Vec3){ 1.0f, 0.0f, 0.0f },
1.0f / 4.0f, 1e-5f ) )
return 1;
}
// Off-axis ray through the c2 cap center, approaching from outside the cylinder.
{
b3RayCastInput input = { { 4.0f, 2.0f, 0.0f }, { -4.0f, -4.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 2.0f + k, k, 0.0f },
( b3Vec3 ){ k, k, 0.0f }, 0.323223f, 1e-4f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ 2.0f + k, k, 0.0f }, (b3Vec3){ k, k, 0.0f }, 0.323223f,
1e-4f ) )
return 1;
}
// Mirror through the c1 cap center.
{
b3RayCastInput input = { { -4.0f, 2.0f, 0.0f }, { 4.0f, -4.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -2.0f - k, k, 0.0f },
( b3Vec3 ){ -k, k, 0.0f }, 0.323223f, 1e-4f ) )
if ( CheckCastHit( out, input.origin, input.translation, (b3Vec3){ -2.0f - k, k, 0.0f }, (b3Vec3){ -k, k, 0.0f },
0.323223f, 1e-4f ) )
return 1;
}
return 0;
@ -581,8 +581,8 @@ static int RayCastCapsuleDegenerateTest( void )
b3Capsule c = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, 1.0f };
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
b3CastOutput out = b3RayCastCapsule( &c, &input );
return CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 0.0f, 0.0f },
( b3Vec3 ){ -1.0f, 0.0f, 0.0f }, 3.0f / 8.0f, 1e-5f );
return CheckCastHit( out, input.origin, input.translation, (b3Vec3){ -1.0f, 0.0f, 0.0f }, (b3Vec3){ -1.0f, 0.0f, 0.0f },
3.0f / 8.0f, 1e-5f );
}
static int RayCastCapsuleClipTest( void )
@ -774,7 +774,7 @@ static int RayCastFarOriginTest( void )
{
for ( int ib = 0; ib < ARRAY_COUNT( offsets ); ++ib )
{
b3Vec3 u = b3Normalize( ( b3Vec3 ){ offsets[ia], offsets[ib], 1.0f } );
b3Vec3 u = b3Normalize( (b3Vec3){ offsets[ia], offsets[ib], 1.0f } );
b3Vec3 origin = b3MulAdd( h, d, u );
b3Vec3 translation = b3MulSV( -2.0f * d, u );
b3RayCastInput input = { origin, translation, 1.0f };
@ -854,19 +854,20 @@ static int RayCastShapeTest( void )
return 0;
}
// The shape stores its name inline, capped at B3_SHAPE_NAME_LENGTH with a reserved null. The stored value
// is the source truncated to the cap, so derive the expectation from the cap and stay valid at any length.
static int CheckShapeName( b3ShapeId shapeId, const char* expected )
{
const char* got = b3Shape_GetName( shapeId );
ENSURE( got != NULL );
ENSURE( got != NULL || expected == NULL );
if ( got == NULL )
{
return 0;
}
int srcLen = expected != NULL ? (int)strlen( expected ) : 0;
int expectLen = srcLen < B3_SHAPE_NAME_LENGTH ? srcLen : B3_SHAPE_NAME_LENGTH;
ENSURE( (int)strlen( got ) == expectLen );
if ( expectLen > 0 )
ENSURE( (int)strlen( got ) == srcLen );
if ( srcLen > 0 )
{
ENSURE( strncmp( got, expected, (size_t)expectLen ) == 0 );
ENSURE( strncmp( got, expected, (size_t)srcLen ) == 0 );
}
return 0;
}
@ -888,7 +889,7 @@ static int ShapeNameTest( void )
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
b3ShapeId shapeId = b3CreateSphereShape( bodyId, &shapeDef, &s );
if ( CheckShapeName( shapeId, "" ) )
if ( CheckShapeName( shapeId, NULL ) )
return 1;
// Name carried on the def.
@ -909,7 +910,7 @@ static int ShapeNameTest( void )
// NULL clears the name.
b3Shape_SetName( shapeId, NULL );
if ( CheckShapeName( shapeId, "" ) )
if ( CheckShapeName( shapeId, NULL ) )
return 1;
b3DestroyWorld( worldId );