From 5d591a90dbd6dce167f55547e2b705621e06240a Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Fri, 30 Oct 2026 16:52:11 -0700 Subject: [PATCH] neighbor normal for heightfield --- src/height_field.c | 48 +++++++++++++ src/mesh.c | 12 ++++ src/shape.h | 13 ++++ test/test_height_field.c | 143 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+) diff --git a/src/height_field.c b/src/height_field.c index 37ff65f..4186f6b 100644 --- a/src/height_field.c +++ b/src/height_field.c @@ -585,6 +585,54 @@ int b3GetHeightFieldMaterial( const b3HeightFieldData* heightField, int triangle return b3GetHeightFieldMaterialIndices( heightField )[cellIndex]; } +bool b3GetHeightFieldNeighborNormal( const b3HeightFieldData* heightField, int triangleIndex, int edgeIndex, b3Vec3* normal ) +{ + B3_ASSERT( 0 <= triangleIndex && triangleIndex < b3GetHeightFieldTriangleCount( heightField ) ); + B3_ASSERT( 0 <= edgeIndex && edgeIndex < 3 ); + + if ( heightField->clockwise ) + { + // Reflected + edgeIndex = 2 - edgeIndex; + } + + int cellColumnCount = heightField->columnCount - 1; + int cellRowCount = heightField->rowCount - 1; + int cellIndex = triangleIndex >> 1; + int row = cellIndex / cellColumnCount; + int column = cellIndex - row * cellColumnCount; + bool odd = ( triangleIndex & 1 ) == 1; + + // Get the row and column for the neighbor cell. + if ( edgeIndex == 0 ) + { + column += odd ? 1 : -1; + } + else if ( edgeIndex == 2 ) + { + row += odd ? 1 : -1; + } + + if ( row < 0 || row >= cellRowCount || column < 0 || column >= cellColumnCount ) + { + return false; + } + + int neighborCell = row * cellColumnCount + column; + + const uint8_t* materialIndices = b3GetHeightFieldMaterialIndices( heightField ); + if ( materialIndices[neighborCell] == B3_HEIGHT_FIELD_HOLE ) + { + return false; + } + + b3Triangle neighbor = b3GetHeightFieldTriangle( heightField, 2 * neighborCell + ( odd ? 0 : 1 ) ); + b3Vec3 e1 = b3Sub( neighbor.vertices[1], neighbor.vertices[0] ); + b3Vec3 e2 = b3Sub( neighbor.vertices[2], neighbor.vertices[0] ); + *normal = b3Normalize( b3Cross( e1, e2 ) ); + return true; +} + b3AABB b3ComputeHeightFieldAABB( const b3HeightFieldData* shape, b3Transform transform ) { return b3AABB_Transform( transform, shape->aabb ); diff --git a/src/mesh.c b/src/mesh.c index 48c8aa5..5329946 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -1092,6 +1092,18 @@ bool b3GetMeshNeighborNormal( const b3Mesh* mesh, int triangleIndex, int edgeInd b3CosSin cs = b3ComputeCosSin( (float)quantized * ( B3_PI / B3_EDGE_ANGLE_STEPS ) ); b3Vec3 unscaled = b3Add( b3MulSV( cs.cosine, n ), b3MulSV( cs.sine, outward ) ); + b3Vec3 absScale = b3Abs( scale ); + if ( absScale.y == absScale.x && absScale.z == absScale.x ) + { + // Shortcut for uniform scale. Common case. + *normal = (b3Vec3){ + scale.x < 0.0f ? -unscaled.x : unscaled.x, + scale.y < 0.0f ? -unscaled.y : unscaled.y, + scale.z < 0.0f ? -unscaled.z : unscaled.z, + }; + return true; + } + // Handle non-uniform scale. b3Vec3 invScale = { 1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z }; *normal = b3Normalize( b3Mul( invScale, unscaled ) ); diff --git a/src/shape.h b/src/shape.h index 997c353..3415fbb 100644 --- a/src/shape.h +++ b/src/shape.h @@ -129,6 +129,8 @@ float b3ComputeHullProjectedArea( const b3HullData* hull, b3Vec3 direction ); b3Triangle b3GetHeightFieldTriangle( const b3HeightFieldData* heightField, int triangleIndex ); int b3GetHeightFieldMaterial( const b3HeightFieldData* heightField, int triangleIndex ); +bool b3GetHeightFieldNeighborNormal( const b3HeightFieldData* heightField, int triangleIndex, int edgeIndex, b3Vec3* normal ); + static inline int b3GetHeightFieldTriangleCount( const b3HeightFieldData* heightField ) { int cellCount = ( heightField->rowCount - 1 ) * ( heightField->columnCount - 1 ); @@ -185,6 +187,17 @@ static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleInde bool b3GetMeshNeighborNormal( const b3Mesh* mesh, int triangleIndex, int edgeIndex, b3Vec3* normal ); +static inline bool b3GetNeighborNormal( const b3Shape* shape, int triangleIndex, int edgeIndex, b3Vec3* normal ) +{ + if ( shape->type == b3_meshShape ) + { + return b3GetMeshNeighborNormal( &shape->mesh, triangleIndex, edgeIndex, normal ); + } + + B3_ASSERT( shape->type == b3_heightShape ); + return b3GetHeightFieldNeighborNormal( shape->heightField, triangleIndex, edgeIndex, normal ); +} + bool b3IsValidMesh( const b3MeshData* meshData ); static inline bool b3ShouldShapesCollide( b3Filter filterA, b3Filter filterB ) diff --git a/test/test_height_field.c b/test/test_height_field.c index 78ed368..55621e9 100644 --- a/test/test_height_field.c +++ b/test/test_height_field.c @@ -11,6 +11,7 @@ #include "box3d/math_functions.h" #include +#include #include static int HeightFieldCreate( void ) @@ -673,6 +674,146 @@ static int RayCastClockwiseWinding( void ) return 0; } +#define BUMPY_COUNT_X 9 +#define BUMPY_COUNT_Z 7 + +// A bumpy field with holes, so cells have every mix of interior, border and hole neighbors +static b3HeightFieldData* MakeBumpyField( bool clockwise ) +{ + float heights[BUMPY_COUNT_X * BUMPY_COUNT_Z]; + for ( int i = 0; i < BUMPY_COUNT_Z; ++i ) + { + for ( int j = 0; j < BUMPY_COUNT_X; ++j ) + { + heights[i * BUMPY_COUNT_X + j] = sinf( 0.9f * (float)i ) * cosf( 0.7f * (float)j ) + 0.1f * (float)( ( i * j ) % 3 ); + } + } + + uint8_t materials[( BUMPY_COUNT_X - 1 ) * ( BUMPY_COUNT_Z - 1 )] = { 0 }; + materials[9] = B3_HEIGHT_FIELD_HOLE; + materials[20] = B3_HEIGHT_FIELD_HOLE; + materials[47] = B3_HEIGHT_FIELD_HOLE; + + b3HeightFieldDef def = { 0 }; + def.heights = heights; + def.materialIndices = materials; + def.scale = (b3Vec3){ 1.5f, 0.8f, 0.5f }; + def.countX = BUMPY_COUNT_X; + def.countZ = BUMPY_COUNT_Z; + def.globalMinimumHeight = -2.0f; + def.globalMaximumHeight = 2.0f; + def.clockwiseWinding = clockwise; + return b3CreateHeightField( &def ); +} + +static bool IsHole( const b3HeightFieldData* hf, int triangleIndex ) +{ + return b3GetHeightFieldMaterial( hf, triangleIndex ) == B3_HEIGHT_FIELD_HOLE; +} + +static b3Vec3 TriangleNormal( b3Triangle triangle ) +{ + b3Vec3 e1 = b3Sub( triangle.vertices[1], triangle.vertices[0] ); + b3Vec3 e2 = b3Sub( triangle.vertices[2], triangle.vertices[0] ); + return b3Normalize( b3Cross( e1, e2 ) ); +} + +// Neighbors found by brute force over shared grid points must match the grid walk +static int HeightFieldNeighborNormals( void ) +{ + for ( int pass = 0; pass < 2; ++pass ) + { + b3HeightFieldData* hf = MakeBumpyField( pass == 1 ); + int triangleCount = b3GetHeightFieldTriangleCount( hf ); + int interiorCount = 0; + int boundaryCount = 0; + + for ( int i = 0; i < triangleCount; ++i ) + { + if ( IsHole( hf, i ) ) + { + continue; + } + + b3Triangle triangle = b3GetHeightFieldTriangle( hf, i ); + int indices[3] = { triangle.i1, triangle.i2, triangle.i3 }; + + for ( int k = 0; k < 3; ++k ) + { + int a = indices[k]; + int b = indices[( k + 1 ) % 3]; + + int neighbor = B3_NULL_INDEX; + for ( int j = 0; j < triangleCount && neighbor == B3_NULL_INDEX; ++j ) + { + if ( j == i || IsHole( hf, j ) ) + { + continue; + } + + b3Triangle other = b3GetHeightFieldTriangle( hf, j ); + int otherIndices[3] = { other.i1, other.i2, other.i3 }; + for ( int m = 0; m < 3; ++m ) + { + if ( otherIndices[m] == b && otherIndices[( m + 1 ) % 3] == a ) + { + neighbor = j; + } + } + } + + b3Vec3 normal; + bool found = b3GetHeightFieldNeighborNormal( hf, i, k, &normal ); + ENSURE( found == ( neighbor != B3_NULL_INDEX ) ); + + if ( found == false ) + { + boundaryCount += 1; + continue; + } + + interiorCount += 1; + b3Vec3 expected = TriangleNormal( b3GetHeightFieldTriangle( hf, neighbor ) ); + ENSURE( b3Dot( normal, expected ) > 0.99999f ); + } + } + + ENSURE( interiorCount > 0 ); + ENSURE( boundaryCount > 0 ); + b3DestroyHeightField( hf ); + } + + return 0; +} + +// The shape level lookup dispatches to the mesh and height field versions +static int NeighborNormalDispatch( void ) +{ + b3HeightFieldData* hf = MakeBumpyField( false ); + b3Shape heightShape = { 0 }; + heightShape.type = b3_heightShape; + heightShape.heightField = hf; + + b3Vec3 normal1, normal2; + ENSURE( b3GetNeighborNormal( &heightShape, 1, 1, &normal1 ) ); + ENSURE( b3GetHeightFieldNeighborNormal( hf, 1, 1, &normal2 ) ); + ENSURE( normal1.x == normal2.x && normal1.y == normal2.y && normal1.z == normal2.z ); + b3DestroyHeightField( hf ); + + b3MeshData* data = b3CreateWaveMesh( 4, 4, 1.0f, 0.5f, 0.2f, 0.15f ); + b3Shape meshShape = { 0 }; + meshShape.type = b3_meshShape; + meshShape.mesh = (b3Mesh){ data, { 2.0f, 0.5f, -1.0f } }; + + bool found1 = b3GetNeighborNormal( &meshShape, 5, 1, &normal1 ); + bool found2 = b3GetMeshNeighborNormal( &meshShape.mesh, 5, 1, &normal2 ); + ENSURE( found1 && found2 ); + ENSURE( normal1.x == normal2.x && normal1.y == normal2.y && normal1.z == normal2.z ); + b3DestroyMesh( data ); + + return 0; +} + int HeightFieldTest( void ) { RUN_SUBTEST( HeightFieldCreate ); @@ -688,6 +829,8 @@ int HeightFieldTest( void ) RUN_SUBTEST( ShapeCastClockwiseWinding ); RUN_SUBTEST( RayCastBackside ); RUN_SUBTEST( RayCastClockwiseWinding ); + RUN_SUBTEST( HeightFieldNeighborNormals ); + RUN_SUBTEST( NeighborNormalDispatch ); return 0; }