From 3ae7ad5d1468c63fc79124bea6ced33d24ebc4fa Mon Sep 17 00:00:00 2001 From: NevilClavain Date: Fri, 28 Jul 2023 20:14:27 +0200 Subject: [PATCH] for issue #80 --- drawspacecore_project/CMakeLists.txt | 6 +- .../planets_luaext/src/lod_body.cpp | 2 +- src/maths.h | 185 +++++++++--------- src/quaternion.h | 1 - 4 files changed, 96 insertions(+), 98 deletions(-) diff --git a/drawspacecore_project/CMakeLists.txt b/drawspacecore_project/CMakeLists.txt index 55bdb137..2fcd95b8 100644 --- a/drawspacecore_project/CMakeLists.txt +++ b/drawspacecore_project/CMakeLists.txt @@ -45,6 +45,11 @@ ${CMAKE_SOURCE_DIR}/src/ds_types.h ${CMAKE_SOURCE_DIR}/src/exceptions.h ${CMAKE_SOURCE_DIR}/src/primitives.h ${CMAKE_SOURCE_DIR}/src/singleton.h +${CMAKE_SOURCE_DIR}/src/maths.h +) + +source_group(maths FILES +${CMAKE_SOURCE_DIR}/src/maths.h ) source_group(aspect FILES @@ -212,7 +217,6 @@ ${CMAKE_SOURCE_DIR}/src/filesystem.cpp ${CMAKE_SOURCE_DIR}/src/filesystem.h ${CMAKE_SOURCE_DIR}/src/jsonparser.cpp ${CMAKE_SOURCE_DIR}/src/jsonparser.h -${CMAKE_SOURCE_DIR}/src/maths.h ${CMAKE_SOURCE_DIR}/src/matrix.cpp ${CMAKE_SOURCE_DIR}/src/matrix.h ${CMAKE_SOURCE_DIR}/src/md5.h diff --git a/lua_extensions/planets_luaext/src/lod_body.cpp b/lua_extensions/planets_luaext/src/lod_body.cpp index 867af9dd..7e80c44b 100644 --- a/lua_extensions/planets_luaext/src/lod_body.cpp +++ b/lua_extensions/planets_luaext/src/lod_body.cpp @@ -98,7 +98,7 @@ void Body::Compute( void ) ///////////////////////////////////////// // alignment_factor_limit augmente au fur et a mesure qu'on approche l'altitude zero - const auto alignment_factor_limit { 0.25 * DrawSpace::Commons::Maths::Clamp(0.0, 1.0, (3.0 - m_relative_alt) / 3.0) }; + const auto alignment_factor_limit { 0.25 * DrawSpace::Maths::Clamp(0.0, 1.0, (3.0 - m_relative_alt) / 3.0) }; if( m_enable_cdlod ) { for( long i = 0; i < 6; i++ ) diff --git a/src/maths.h b/src/maths.h index eaca4363..1e167b7a 100644 --- a/src/maths.h +++ b/src/maths.h @@ -30,115 +30,110 @@ namespace DrawSpace { - namespace Commons + using namespace Utils; + + namespace Maths { - using namespace Utils; + static constexpr dsreal pi{ 3.1415927 }; - class Maths - { - public: + static double Square( dsreal a ) { return a * a; }; + static int Floor( dsreal a ) { return ((int)a - (a < 0 && a != (int)a)); }; + static int Ceiling( dsreal a ) { return ((int)a + (a > 0 && a != (int)a)); }; + + static double Min( dsreal a, dsreal b ) { return (a < b ? a : b); }; + static double Max( dsreal a, dsreal b ) { return (a > b ? a : b); }; + static double Abs( dsreal a ) { return (a < 0 ? -a : a); }; + static double Clamp( dsreal a, dsreal b, dsreal x ) { return (x < a ? a : (x > b ? b : x)); }; + static double Lerp( dsreal a, dsreal b, dsreal x ) { return a + x * (b - a); }; + static double Cubic( dsreal a ) { return a * a * (3 - 2*a); }; + static double Pulse( dsreal a, dsreal b, dsreal x ) { return (double)((x >= a) - (x >= b)); }; + static double Gamma( dsreal a, dsreal g ) { return pow(a, 1/g); }; + static double Expose( dsreal l, dsreal k ) { return (1 - exp(-l * k)); }; + static double DegToRad( dsreal ang ) { return ( ( ang * pi ) / 180.0 ); }; + static double RadToDeg( dsreal ang ) { return ( ( ang * 180.0 ) / pi ); }; - static constexpr dsreal pi{ 3.1415927 }; - static double Square( dsreal a ) { return a * a; }; - static int Floor( dsreal a ) { return ((int)a - (a < 0 && a != (int)a)); }; - static int Ceiling( dsreal a ) { return ((int)a + (a > 0 && a != (int)a)); }; + static void SphericaltoCartesian( const Vector& p_in, Vector& p_out ) + { + p_out[2] = ( p_in[0] * cos( p_in[2] ) * cos( p_in[1] ) ); + p_out[0] = ( p_in[0] * cos( p_in[2] ) * sin( p_in[1] ) ); + p_out[1] = ( p_in[0] * sin( p_in[2] ) ); + } + + static void CartesiantoSpherical( Vector& p_in, Vector& p_out ) + { + // cas particulier + if( p_in[1] > 0.0 && 0.0 == p_in[0] && 0.0 == p_in[2] ) + { + p_out[0] = p_in[1]; + p_out[2] = pi / 2.0; + p_out[1] = 0.0; + return; + } + else if( p_in[1] < 0.0 && 0.0 == p_in[0] && 0.0 == p_in[2] ) + { + p_out[0] = -p_in[1]; + p_out[2] = -pi / 2.0; + p_out[1] = 0.0; + return; + } + + p_out[0] = p_in.Length(); + p_out[2] = asin( p_in[1] / p_out[0] ); + p_out[1] = atan2( p_in[0], p_in[2] ); + } + + static void CubeToSphere( Vector& p_in, Vector& p_out ) + { + float xtemp = p_in[0]; + float ytemp = p_in[1]; + float ztemp = p_in[2]; - static double Min( dsreal a, dsreal b ) { return (a < b ? a : b); }; - static double Max( dsreal a, dsreal b ) { return (a > b ? a : b); }; - static double Abs( dsreal a ) { return (a < 0 ? -a : a); }; - static double Clamp( dsreal a, dsreal b, dsreal x ) { return (x < a ? a : (x > b ? b : x)); }; - static double Lerp( dsreal a, dsreal b, dsreal x ) { return a + x * (b - a); }; - static double Cubic( dsreal a ) { return a * a * (3 - 2*a); }; - static double Pulse( dsreal a, dsreal b, dsreal x ) { return (double)((x >= a) - (x >= b)); }; - static double Gamma( dsreal a, dsreal g ) { return pow(a, 1/g); }; - static double Expose( dsreal l, dsreal k ) { return (1 - exp(-l * k)); }; - static double DegToRad( dsreal ang ) { return ( ( ang * pi ) / 180.0 ); }; - static double RadToDeg( dsreal ang ) { return ( ( ang * 180.0 ) / pi ); }; + p_out[0] = xtemp * sqrt( 1.0 - ytemp * ytemp * 0.5 - ztemp * ztemp * 0.5 + ytemp * ytemp * ztemp * ztemp / 3.0 ); + p_out[1] = ytemp * sqrt( 1.0 - ztemp * ztemp * 0.5 - xtemp * xtemp * 0.5 + xtemp * xtemp * ztemp * ztemp / 3.0 ); + p_out[2] = ztemp * sqrt( 1.0 - xtemp * xtemp * 0.5 - ytemp * ytemp * 0.5 + xtemp * xtemp * ytemp * ytemp / 3.0 ); + } + static void VectorPlanetOrientation( int p_orientation, Vector& p_in, Vector& p_out ) + { + Vector res; - static void SphericaltoCartesian( const Vector& p_in, Vector& p_out ) + if( 0 == p_orientation ) // front { - p_out[2] = ( p_in[0] * cos( p_in[2] ) * cos( p_in[1] ) ); - p_out[0] = ( p_in[0] * cos( p_in[2] ) * sin( p_in[1] ) ); - p_out[1] = ( p_in[0] * sin( p_in[2] ) ); + p_out[0] = p_in[0]; + p_out[1] = p_in[1]; + p_out[2] = p_in[2]; } - - static void CartesiantoSpherical( Vector& p_in, Vector& p_out ) + else if( 1 == p_orientation ) // rear { - // cas particulier - if( p_in[1] > 0.0 && 0.0 == p_in[0] && 0.0 == p_in[2] ) - { - p_out[0] = p_in[1]; - p_out[2] = pi / 2.0; - p_out[1] = 0.0; - return; - } - else if( p_in[1] < 0.0 && 0.0 == p_in[0] && 0.0 == p_in[2] ) - { - p_out[0] = -p_in[1]; - p_out[2] = -pi / 2.0; - p_out[1] = 0.0; - return; - } - - p_out[0] = p_in.Length(); - p_out[2] = asin( p_in[1] / p_out[0] ); - p_out[1] = atan2( p_in[0], p_in[2] ); + p_out[0] = -p_in[0]; + p_out[1] = p_in[1]; + p_out[2] = -p_in[2]; } - - static void CubeToSphere( Vector& p_in, Vector& p_out ) + else if( 2 == p_orientation ) // left { - float xtemp = p_in[0]; - float ytemp = p_in[1]; - float ztemp = p_in[2]; - - p_out[0] = xtemp * sqrt( 1.0 - ytemp * ytemp * 0.5 - ztemp * ztemp * 0.5 + ytemp * ytemp * ztemp * ztemp / 3.0 ); - p_out[1] = ytemp * sqrt( 1.0 - ztemp * ztemp * 0.5 - xtemp * xtemp * 0.5 + xtemp * xtemp * ztemp * ztemp / 3.0 ); - p_out[2] = ztemp * sqrt( 1.0 - xtemp * xtemp * 0.5 - ytemp * ytemp * 0.5 + xtemp * xtemp * ytemp * ytemp / 3.0 ); + p_out[0] = -p_in[2]; + p_out[1] = p_in[1]; + p_out[2] = p_in[0]; } - - static void VectorPlanetOrientation( int p_orientation, Vector& p_in, Vector& p_out ) + else if( 3 == p_orientation ) // right { - Vector res; - - if( 0 == p_orientation ) // front - { - p_out[0] = p_in[0]; - p_out[1] = p_in[1]; - p_out[2] = p_in[2]; - } - else if( 1 == p_orientation ) // rear - { - p_out[0] = -p_in[0]; - p_out[1] = p_in[1]; - p_out[2] = -p_in[2]; - } - else if( 2 == p_orientation ) // left - { - p_out[0] = -p_in[2]; - p_out[1] = p_in[1]; - p_out[2] = p_in[0]; - } - else if( 3 == p_orientation ) // right - { - p_out[0] = p_in[2]; - p_out[1] = p_in[1]; - p_out[2] = -p_in[0]; - } - else if( 4 == p_orientation ) // top - { - p_out[0] = p_in[0]; - p_out[1] = p_in[2]; - p_out[2] = -p_in[1]; - } - else //if( 5 == p_orientation ) // bottom - { - p_out[0] = p_in[0]; - p_out[1] = -p_in[2]; - p_out[2] = p_in[1]; - } + p_out[0] = p_in[2]; + p_out[1] = p_in[1]; + p_out[2] = -p_in[0]; + } + else if( 4 == p_orientation ) // top + { + p_out[0] = p_in[0]; + p_out[1] = p_in[2]; + p_out[2] = -p_in[1]; + } + else //if( 5 == p_orientation ) // bottom + { + p_out[0] = p_in[0]; + p_out[1] = -p_in[2]; + p_out[2] = p_in[1]; } - }; + } } } diff --git a/src/quaternion.h b/src/quaternion.h index 29a9eda8..902856b5 100644 --- a/src/quaternion.h +++ b/src/quaternion.h @@ -32,7 +32,6 @@ namespace DrawSpace { namespace Utils { -using namespace DrawSpace::Commons; class Quaternion { protected: