commit c8072885e0efdb9c074a4457d4bf170e8c6212ba parent 879a3b9194eb9f9a0a83177c526dd09b090362da Author: Michael Savage <mikejsavage@gmail.com> Date: Sat Sep 16 19:49:27 +0300 Rename AABB to MinMax, add new centre/extents AABB Diffstat:
aabb.h | | | 53 | ++++++++++++++++++++++++++++++++--------------------- |
heightmap.cc | | | 14 | +++++++------- |
diff --git a/aabb.h b/aabb.h @@ -11,12 +11,12 @@ enum Quadrant { QUADRANT_NE, }; -struct AABBu32 { +struct MinMaxu32 { v3u32 mins, maxs; - AABBu32() { } + MinMaxu32() { } - explicit AABBu32( v3u32 a, v3u32 b ) { + explicit MinMaxu32( v3u32 a, v3u32 b ) { mins.x = min( a.x, b.x ); mins.y = min( a.y, b.y ); mins.z = min( a.z, b.z ); @@ -25,16 +25,16 @@ struct AABBu32 { maxs.z = max( a.z, b.z ); } - AABBu32 quadrant( int q ) const; - AABBu32 clamp_z( u32 lo, u32 hi ); + MinMaxu32 quadrant( int q ) const; + MinMaxu32 clamp_z( u32 lo, u32 hi ); }; -struct AABB { +struct MinMax { v3 mins, maxs; - AABB() { } + MinMax() { } - explicit AABB( v3 a, v3 b ) { + explicit MinMax( v3 a, v3 b ) { mins.x = min( a.x, b.x ); mins.y = min( a.y, b.y ); mins.y = min( a.z, b.z ); @@ -43,7 +43,7 @@ struct AABB { maxs.y = max( a.z, b.z ); } - explicit AABB( const AABBu32 & aabb ) { + explicit MinMax( const MinMaxu32 & aabb ) { mins.x = checked_cast< float >( aabb.mins.x ); mins.y = checked_cast< float >( aabb.mins.y ); mins.z = checked_cast< float >( aabb.mins.z / 256.0f ); // TODO: huge hack @@ -58,13 +58,24 @@ struct AABB { } }; -forceinline AABBu32 AABBu32::quadrant( int q ) const { +struct AABB { + v3 centre, extents; + + AABB() { } + + explicit AABB( v3 c, v3 e ) { + centre = c; + extents = e; + } +}; + +forceinline MinMaxu32 MinMaxu32::quadrant( int q ) const { switch( q ) { case QUADRANT_SW: { v3u32 mx = maxs; mx.x = mean( mins.x, maxs.x ); mx.y = mean( mins.y, maxs.y ); - return AABBu32( mins, mx ); + return MinMaxu32( mins, mx ); } case QUADRANT_SE: { @@ -72,7 +83,7 @@ forceinline AABBu32 AABBu32::quadrant( int q ) const { v3u32 mx = maxs; mn.x = mean( mins.x, maxs.x ); mx.y = mean( mins.y, maxs.y ); - return AABBu32( mn, mx ); + return MinMaxu32( mn, mx ); } case QUADRANT_NW: { @@ -80,39 +91,39 @@ forceinline AABBu32 AABBu32::quadrant( int q ) const { v3u32 mx = maxs; mn.y = mean( mins.y, maxs.y ); mx.x = mean( mins.x, maxs.x ); - return AABBu32( mn, mx ); + return MinMaxu32( mn, mx ); } case QUADRANT_NE: { v3u32 mn = mins; mn.x = mean( mins.x, maxs.x ); mn.y = mean( mins.y, maxs.y ); - return AABBu32( mn, maxs ); + return MinMaxu32( mn, maxs ); } } FATAL( "bad quadrant" ); - return AABBu32(); + return MinMaxu32(); } -forceinline AABBu32 AABBu32::clamp_z( u32 lo, u32 hi ) { +forceinline MinMaxu32 MinMaxu32::clamp_z( u32 lo, u32 hi ) { v3u32 mn = mins; v3u32 mx = maxs; mn.z = max( mn.z, lo ); mx.z = min( mx.z, hi ); - return AABBu32( mn, mx ); + return MinMaxu32( mn, mx ); } -inline void format( FormatBuffer * fb, const AABB & aabb, const FormatOpts & opts ) { - format( fb, "AABB(" ); +inline void format( FormatBuffer * fb, const MinMax & aabb, const FormatOpts & opts ) { + format( fb, "MinMax(" ); format( fb, aabb.mins, opts ); format( fb, ", " ); format( fb, aabb.maxs, opts ); format( fb, ")" ); } -inline void format( FormatBuffer * fb, const AABBu32 & aabb, const FormatOpts & opts ) { - format( fb, "AABBu32(" ); +inline void format( FormatBuffer * fb, const MinMaxu32 & aabb, const FormatOpts & opts ) { + format( fb, "MinMaxu32(" ); format( fb, aabb.mins, opts ); format( fb, ", " ); format( fb, aabb.maxs, opts ); diff --git a/heightmap.cc b/heightmap.cc @@ -69,7 +69,7 @@ float Heightmap::bilerp_height( float x, float y ) const { // // -bool ray_vs_aabb( AABB aabb, v3 start, v3 inv_dir, float * t ) { +bool ray_vs_aabb( MinMax aabb, v3 start, v3 inv_dir, float * t ) { if( aabb.contains( start ) ) { *t = 0.0f; return true; @@ -187,7 +187,7 @@ static size_t child_idx( size_t node_idx, size_t quadrant ) { return node_idx * 4 + quadrant + 1; } -static void heightmap_build_quadtree_node( HeightmapQuadTree * qt, size_t node_idx, AABBu32 aabb ) { +static void heightmap_build_quadtree_node( HeightmapQuadTree * qt, size_t node_idx, MinMaxu32 aabb ) { qt->nodes[ node_idx ].min_z = U16_MAX; qt->nodes[ node_idx ].max_z = 0; @@ -229,7 +229,7 @@ HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm, array< Heightm qt.dim = hm->width - 1; qt.nodes = nodes; - AABBu32 aabb( v3u32( 0, 0, 0 ), v3u32( qt.dim, qt.dim, U16_MAX ) ); + MinMaxu32 aabb( v3u32( 0, 0, 0 ), v3u32( qt.dim, qt.dim, U16_MAX ) ); heightmap_build_quadtree_node( &qt, 0, aabb ); return qt; @@ -263,7 +263,7 @@ static void sort4( int * indices, float * elems ) { } static bool ray_vs_quadtree_node( - const HeightmapQuadTree * qt, size_t node_idx, AABBu32 aabb, + const HeightmapQuadTree * qt, size_t node_idx, MinMaxu32 aabb, const Ray3 & ray, float * t, v3 * xnormal ) { if( aabb.maxs.x - aabb.mins.x == 2 || aabb.maxs.y - aabb.mins.y == 2 ) { @@ -308,13 +308,13 @@ static bool ray_vs_quadtree_node( return hit; } - AABBu32 aabbs[ 4 ]; + MinMaxu32 aabbs[ 4 ]; float ts[ 4 ]; for( int i = 0; i < 4; i++ ) { HeightmapQuadTreeNode child = qt->nodes[ child_idx( node_idx, i ) ]; aabbs[ i ] = aabb.quadrant( i ).clamp_z( child.min_z, child.max_z ); ts[ i ] = FLT_MAX; - ray_vs_aabb( AABB( aabbs[ i ] ), ray.origin, ray.inv_dir, &ts[ i ] ); + ray_vs_aabb( MinMax( aabbs[ i ] ), ray.origin, ray.inv_dir, &ts[ i ] ); } int sorted[ 4 ]; @@ -336,7 +336,7 @@ static bool ray_vs_quadtree_node( bool ray_vs_quadtree( const HeightmapQuadTree * qt, const Ray3 & ray, float * t, v3 * xnormal ) { v3u32 mins = v3u32( 0, 0, qt->nodes[ 0 ].min_z ); v3u32 maxs = v3u32( qt->dim, qt->dim, qt->nodes[ 0 ].max_z ); - AABBu32 aabb( mins, maxs ); + MinMaxu32 aabb( mins, maxs ); return ray_vs_quadtree_node( qt, 0, aabb, ray, t, xnormal ); }