From ec5c4ad276788000a66bbe9c2136a08bf1a6d1aa Mon Sep 17 00:00:00 2001 From: Tijmen Verhoef Date: Sat, 7 Dec 2024 15:57:10 +0100 Subject: [PATCH 1/3] hid vector types behind ifndef guard --- tiny_bvh.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tiny_bvh.h b/tiny_bvh.h index fcfa252..f63d0e8 100644 --- a/tiny_bvh.h +++ b/tiny_bvh.h @@ -90,6 +90,7 @@ THE SOFTWARE. // Features #define DOUBLE_PRECISION_SUPPORT +//#define TINYBVH_USE_CUSTOM_VECTOR_TYPES // CWBVH triangle format: doesn't seem to help on GPU? // #define CWBVH_COMPRESSED_TRIS @@ -179,6 +180,8 @@ namespace tinybvh { #pragma warning ( disable: 4201 /* nameless struct / union */ ) #endif +#ifndef TINYBVH_USE_CUSTOM_VECTOR_TYPES + struct bvhvec3; struct ALIGNED( 16 ) bvhvec4 { @@ -239,17 +242,19 @@ struct bvhuint2 unsigned x, y; }; +#ifdef TINYBVH_IMPLEMENTATION +bvhvec4::bvhvec4( const bvhvec3& a ) { x = a.x; y = a.y; z = a.z; w = 0; } +bvhvec4::bvhvec4( const bvhvec3& a, float b ) { x = a.x; y = a.y; z = a.z; w = b; } +#endif + +#endif // TINYBVH_USE_CUSTOM_VECTOR_TYPES + struct bvhaabb { bvhvec3 minBounds; unsigned dummy1; bvhvec3 maxBounds; unsigned dummy2; }; -#ifdef TINYBVH_IMPLEMENTATION -bvhvec4::bvhvec4( const bvhvec3& a ) { x = a.x; y = a.y; z = a.z; w = 0; } -bvhvec4::bvhvec4( const bvhvec3& a, float b ) { x = a.x; y = a.y; z = a.z; w = b; } -#endif - #ifdef _MSC_VER #pragma warning ( pop ) #endif @@ -323,6 +328,7 @@ static bvhvec3 normalize( const bvhvec3& a ) #ifdef DOUBLE_PRECISION_SUPPORT +#ifndef TINYBVH_USE_CUSTOM_VECTOR_TYPES // Double-precision math struct bvhdbl3 { @@ -334,6 +340,7 @@ struct bvhdbl3 double& operator [] ( const int i ) { return cell[i]; } union { struct { double x, y, z; }; double cell[3]; }; }; +#endif // TINYBVH_USE_CUSTOM_VECTOR_TYPES static inline bvhdbl3 tinybvh_min( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( tinybvh_min( a.x, b.x ), tinybvh_min( a.y, b.y ), tinybvh_min( a.z, b.z ) ); } static inline bvhdbl3 tinybvh_max( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( tinybvh_max( a.x, b.x ), tinybvh_max( a.y, b.y ), tinybvh_max( a.z, b.z ) ); } From a632680ddd2108dee61f483aa8255341771adf8a Mon Sep 17 00:00:00 2001 From: Tijmen Verhoef Date: Sat, 7 Dec 2024 16:09:50 +0100 Subject: [PATCH 2/3] also added guard to vector operators --- tiny_bvh.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tiny_bvh.h b/tiny_bvh.h index f63d0e8..1862a5e 100644 --- a/tiny_bvh.h +++ b/tiny_bvh.h @@ -283,6 +283,8 @@ template inline static void tinybvh_swap( T& a, T& b ) { T t = a; a = // Operator overloads. // Only a minimal set is provided. +#ifndef TINYBVH_USE_CUSTOM_VECTOR_TYPES + inline bvhvec2 operator-( const bvhvec2& a ) { return bvhvec2( -a.x, -a.y ); } inline bvhvec3 operator-( const bvhvec3& a ) { return bvhvec3( -a.x, -a.y, -a.z ); } inline bvhvec4 operator-( const bvhvec4& a ) { return bvhvec4( -a.x, -a.y, -a.z, -a.w ); } @@ -309,6 +311,8 @@ inline bvhvec3 operator/( float b, const bvhvec3& a ) { return bvhvec3( b / a.x, inline bvhvec4 operator/( float b, const bvhvec4& a ) { return bvhvec4( b / a.x, b / a.y, b / a.z, b / a.w ); } inline bvhvec3 operator*=( bvhvec3& a, const float b ) { return bvhvec3( a.x * b, a.y * b, a.z * b ); } +#endif // TINYBVH_USE_CUSTOM_VECTOR_TYPES + // Vector math: cross and dot. static inline bvhvec3 cross( const bvhvec3& a, const bvhvec3& b ) { @@ -327,9 +331,10 @@ static bvhvec3 normalize( const bvhvec3& a ) } #ifdef DOUBLE_PRECISION_SUPPORT +// Double-precision math #ifndef TINYBVH_USE_CUSTOM_VECTOR_TYPES -// Double-precision math + struct bvhdbl3 { bvhdbl3() = default; @@ -340,10 +345,14 @@ struct bvhdbl3 double& operator [] ( const int i ) { return cell[i]; } union { struct { double x, y, z; }; double cell[3]; }; }; + #endif // TINYBVH_USE_CUSTOM_VECTOR_TYPES static inline bvhdbl3 tinybvh_min( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( tinybvh_min( a.x, b.x ), tinybvh_min( a.y, b.y ), tinybvh_min( a.z, b.z ) ); } static inline bvhdbl3 tinybvh_max( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( tinybvh_max( a.x, b.x ), tinybvh_max( a.y, b.y ), tinybvh_max( a.z, b.z ) ); } + +#ifndef TINYBVH_USE_CUSTOM_VECTOR_TYPES + inline bvhdbl3 operator-( const bvhdbl3& a ) { return bvhdbl3( -a.x, -a.y, -a.z ); } inline bvhdbl3 operator+( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( a.x + b.x, a.y + b.y, a.z + b.z ); } inline bvhdbl3 operator-( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( a.x - b.x, a.y - b.y, a.z - b.z ); } @@ -354,6 +363,8 @@ inline bvhdbl3 operator*( float b, const bvhdbl3& a ) { return bvhdbl3( b * a.x, inline bvhdbl3 operator/( float b, const bvhdbl3& a ) { return bvhdbl3( b / a.x, b / a.y, b / a.z ); } inline bvhdbl3 operator*=( bvhdbl3& a, const float b ) { return bvhdbl3( a.x * b, a.y * b, a.z * b ); } +#endif // TINYBVH_USE_CUSTOM_VECTOR_TYPES + static inline bvhdbl3 cross( const bvhdbl3& a, const bvhdbl3& b ) { return bvhdbl3( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x ); From a997fb3d359e0c6515a7011a2fb0e026d6284583 Mon Sep 17 00:00:00 2001 From: Tijmen Verhoef Date: Sat, 7 Dec 2024 16:13:38 +0100 Subject: [PATCH 3/3] documented custom vector type usage --- tiny_bvh.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tiny_bvh.h b/tiny_bvh.h index 1862a5e..48001b8 100644 --- a/tiny_bvh.h +++ b/tiny_bvh.h @@ -50,6 +50,24 @@ THE SOFTWARE. // #include "tiny_bvh.h" // +// +// tinybvh can use custom vector types by defining TINYBVH_USE_CUSTOM_VECTOR_TYPES once before inclusion. +// To define custom vector types create a tinybvh namespace with the appropriate using directives, e.g.: +// namespace tinybvh +// { +// using bvhint2 = math::int2; +// using bvhint3 = math::int3; +// using bvhuint2 = math::uint2; +// using bvhvec2 = math::float2; +// using bvhvec3 = math::float3; +// using bvhvec4 = math::float4; +// using bvhdbl3 = math::double3; +// } +// +// #define TINYBVH_USE_CUSTOM_VECTOR_TYPES +// #include +// + // How to use: // See tiny_bvh_test.cpp for basic usage. In short: // instantiate a BVH: tinybvh::BVH bvh;