commit 9d5cd5e29fa76144e4bcf0ea1c622c478e04c915 parent 72bb6cef18a8f51caaf33ac688f5b5f75cdc2ea5 Author: Michael Savage <mikejsavage@gmail.com> Date: Sat May 20 00:46:17 +0300 Split AABB code into aabb.h Diffstat:
aabb.h | | | 103 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
heightmap.cc | | | 47 | +---------------------------------------------- |
heightmap.h | | | 55 | +------------------------------------------------------ |
diff --git a/aabb.h b/aabb.h @@ -0,0 +1,103 @@ +#pragma once + +#include "intrinsics.h" +#include "linear_algebra.h" + +enum Quadrant { + QUADRANT_SW, + QUADRANT_SE, + QUADRANT_NW, + QUADRANT_NE, +}; + +struct AABBu32 { + v3u32 mins, maxs; + + AABBu32() { } + + explicit AABBu32( 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 ); + maxs.x = max( a.x, b.x ); + maxs.y = max( a.y, b.y ); + maxs.z = max( a.z, b.z ); + } + + AABBu32 quadrant( int q ) const; + AABBu32 clamp_z( u32 lo, u32 hi ); +}; + +struct AABB { + v3 mins, maxs; + + AABB() { } + + explicit AABB( 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 ); + maxs.y = max( a.x, b.x ); + maxs.y = max( a.y, b.y ); + maxs.y = max( a.z, b.z ); + } + + explicit AABB( const AABBu32 & 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 ); + maxs.x = checked_cast< float >( aabb.maxs.x ); + maxs.y = checked_cast< float >( aabb.maxs.y ); + maxs.z = checked_cast< float >( aabb.maxs.z ); + } + + bool contains( v3 p ) { + return p.x >= mins.x && p.y >= mins.y && p.z >= mins.z && + p.x <= maxs.x && p.y <= maxs.y && p.z <= maxs.z; + } +}; + +forceinline AABBu32 AABBu32::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 ); + } + + case QUADRANT_SE: { + v3u32 mn = mins; + v3u32 mx = maxs; + mn.x = mean( mins.x, maxs.x ); + mx.y = mean( mins.y, maxs.y ); + return AABBu32( mn, mx ); + } + + case QUADRANT_NW: { + v3u32 mn = mins; + v3u32 mx = maxs; + mn.y = mean( mins.y, maxs.y ); + mx.x = mean( mins.x, maxs.x ); + return AABBu32( 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 ); + } + } + + FATAL( "bad quadrant" ); + return AABBu32(); +} + +forceinline AABBu32 AABBu32::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 ); +} diff --git a/heightmap.cc b/heightmap.cc @@ -177,51 +177,6 @@ bool ray_vs_triangle( v3 ray_origin, v3 ray_dir, v3 p0, v3 p1, v3 p2, float * t return true; } -AABBu32 AABBu32::quadrant( int q ) const { - switch( checked_cast< Quadrant >( 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 ); - } - - case QUADRANT_SE: { - v3u32 mn = mins; - v3u32 mx = maxs; - mn.x = mean( mins.x, maxs.x ); - mx.y = mean( mins.y, maxs.y ); - return AABBu32( mn, mx ); - } - - case QUADRANT_NW: { - v3u32 mn = mins; - v3u32 mx = maxs; - mn.y = mean( mins.y, maxs.y ); - mx.x = mean( mins.x, maxs.x ); - return AABBu32( 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 ); - } - } - - FATAL( "bad quadrant" ); - return AABBu32(); -} - -AABBu32 AABBu32::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 ); -} - static size_t child_idx( size_t node_idx, size_t quadrant ) { return node_idx * 4 + quadrant + 1; } @@ -242,7 +197,7 @@ static void heightmap_build_quadtree_node( HeightmapQuadTree * qt, size_t node_i u8 lo = 255; u8 hi = 0; - for( size_t i = 0; i < 4; i++ ) { + for( int i = 0; i < 4; i++ ) { heightmap_build_quadtree_node( qt, child_idx( node_idx, i ), aabb.quadrant( i ) ); lo = min( lo, qt->nodes[ child_idx( node_idx, i ) ].min_z ); hi = max( hi, qt->nodes[ child_idx( node_idx, i ) ].max_z ); diff --git a/heightmap.h b/heightmap.h @@ -3,6 +3,7 @@ #include "intrinsics.h" #include "array.h" #include "linear_algebra.h" +#include "aabb.h" class Heightmap { public: @@ -59,58 +60,4 @@ HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm, array< Heightm // return lo0 <= hi1 && lo1 <= hi0; // } -enum Quadrant { - QUADRANT_SW, - QUADRANT_SE, - QUADRANT_NW, - QUADRANT_NE, -}; - -struct AABBu32 { - v3u32 mins, maxs; - - AABBu32() { } - - explicit AABBu32( 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 ); - maxs.x = max( a.x, b.x ); - maxs.y = max( a.y, b.y ); - maxs.z = max( a.z, b.z ); - } - - AABBu32 quadrant( int q ) const; - AABBu32 clamp_z( u32 lo, u32 hi ); -}; - -struct AABB { - v3 mins, maxs; - - AABB() { } - - explicit AABB( 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 ); - maxs.y = max( a.x, b.x ); - maxs.y = max( a.y, b.y ); - maxs.y = max( a.z, b.z ); - } - - explicit AABB( AABBu32 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 ); - maxs.x = checked_cast< float >( aabb.maxs.x ); - maxs.y = checked_cast< float >( aabb.maxs.y ); - maxs.z = checked_cast< float >( aabb.maxs.z ); - } - - bool contains( v3 p ) { - return p.x >= mins.x && p.y >= mins.y && p.z >= mins.z && - p.x <= maxs.x && p.y <= maxs.y && p.z <= maxs.z; - } -}; - bool ray_vs_quadtree( const HeightmapQuadTree * qt, v3 ray_origin, v3 ray_dir, v3 inv_dir, float * t, v3 * xnormal );