Skip to content

Commit

Permalink
for issue #80
Browse files Browse the repository at this point in the history
  • Loading branch information
NevilClavain committed Jul 28, 2023
1 parent 6534eb6 commit 4455748
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 110 deletions.
6 changes: 4 additions & 2 deletions lua_core/src/luaclass_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@


using namespace DrawSpace;
using namespace DrawSpace::Commons;

using namespace DrawSpace::Core;
using namespace DrawSpace::Utils;

Expand Down Expand Up @@ -663,7 +665,7 @@ int LuaClass_Globals::LUA_ctos(lua_State* p_L)

Vector c(x, y, z, 1.0);
Vector s;
Utils::Maths::CartesiantoSpherical(c, s);
Maths::CartesiantoSpherical(c, s);

dsreal longit{ Maths::RadToDeg(s[1]) };
dsreal latit{ Maths::RadToDeg(s[2]) };
Expand All @@ -688,7 +690,7 @@ int LuaClass_Globals::LUA_stoc(lua_State* p_L)

Vector s(r, t, p, 1.0);
Vector c;
Utils::Maths::SphericaltoCartesian(s, c);
Maths::SphericaltoCartesian(s, c);

lua_pushnumber(p_L, c[0]);
lua_pushnumber(p_L, c[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "quaternion.h"

using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Utils;
using namespace DrawSpace::Aspect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "maths.h"

using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Aspect;
using namespace DrawSpace::Utils;
Expand Down
2 changes: 1 addition & 1 deletion lua_extensions/planets_luaext/src/lod_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::Utils::Maths::Clamp(0.0, 1.0, (3.0 - m_relative_alt) / 3.0) };
const auto alignment_factor_limit { 0.25 * DrawSpace::Commons::Maths::Clamp(0.0, 1.0, (3.0 - m_relative_alt) / 3.0) };
if( m_enable_cdlod )
{
for( long i = 0; i < 6; i++ )
Expand Down
5 changes: 3 additions & 2 deletions lua_extensions/planets_luaext/src/lod_drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@


using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Utils;
using namespace DrawSpace::Aspect;
Expand Down Expand Up @@ -298,7 +299,7 @@ void FaceDrawingNode::draw_single_patch( Patch* p_patch, dsreal p_ray, dsreal p_
Vector landplace_normale( 0.0, 0.0, 1.0, 1.0 );

Vector landplace_normale_orient;
Utils::Maths::VectorPlanetOrientation( p_patch->GetOrientation(), landplace_normale, landplace_normale_orient );
Maths::VectorPlanetOrientation( p_patch->GetOrientation(), landplace_normale, landplace_normale_orient );

Vector landplace_normale_t;

Expand Down Expand Up @@ -1242,7 +1243,7 @@ void Drawing::create_landplace_meshe( long p_patch_resol, int p_orientation, Dra
Vector v( xcurr, ycurr, 0.0, 1.0 );


Utils::Maths::VectorPlanetOrientation( p_orientation, v, v_orient );
Maths::VectorPlanetOrientation( p_orientation, v, v_orient );


vertex.x = v_orient[0];
Expand Down
1 change: 1 addition & 0 deletions lua_extensions/planets_luaext/src/lod_face.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "exceptions.h"

using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Utils;
using namespace LOD;
Expand Down
1 change: 1 addition & 0 deletions lua_extensions/planets_luaext/src/lod_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "maths.h"

using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Utils;
using namespace DrawSpace::Aspect;
Expand Down
5 changes: 3 additions & 2 deletions lua_extensions/planets_luaext/src/lod_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@


using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Utils;
using namespace LOD;
Expand Down Expand Up @@ -191,11 +192,11 @@ m_layer_index( p_layer_index )
const auto xpole_center{ poles_coords_rand_source(rand_engine) };
const auto ypole_center{ poles_coords_rand_source(rand_engine) };

std::uniform_real_distribution<dsreal> theta_rand_source(0.0, Utils::Maths::pi / 2.0);
std::uniform_real_distribution<dsreal> theta_rand_source(0.0, Maths::pi / 2.0);
std::uniform_real_distribution<dsreal> r_rand_source(min_pole_ray, max_pole_ray);
std::uniform_int_distribution<int> xy_signs_rand_source(1, 2);

std::uniform_real_distribution<dsreal> orientation_source(0.0, Utils::Maths::pi);
std::uniform_real_distribution<dsreal> orientation_source(0.0, Maths::pi);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1892,8 +1892,8 @@ void PlanetsRenderingAspectImpl::manage_camerapoints(void)
Maths::CartesiantoSpherical(locale_camera_pos_from_planet, spherical);

camera.second.locale_camera_long_lat[0] = spherical[0];
camera.second.locale_camera_long_lat[1] = Utils::Maths::RadToDeg( spherical[1] );
camera.second.locale_camera_long_lat[2] = Utils::Maths::RadToDeg( spherical[2] );
camera.second.locale_camera_long_lat[1] = Maths::RadToDeg( spherical[1] );
camera.second.locale_camera_long_lat[2] = Maths::RadToDeg( spherical[2] );

camera.second.global_camera_pos_from_planet = camera_pos_from_planet;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "transformaspect.h"

using namespace DrawSpace;
using namespace DrawSpace::Commons;
using namespace DrawSpace::Core;
using namespace DrawSpace::Aspect;
using namespace DrawSpace::Utils;
Expand Down
198 changes: 100 additions & 98 deletions src/maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,113 +30,115 @@

namespace DrawSpace
{
namespace Utils
{
class Maths
{
public:
namespace Commons
{
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 constexpr dsreal pi{ 3.1415927 };

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 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 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 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;
}
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] );
}
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 void CubeToSphere( Vector& p_in, Vector& p_out )
{
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] = 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 VectorPlanetOrientation( int p_orientation, Vector& p_in, Vector& p_out )
{
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];
}
}
};
}
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];
}
}
};
}
}
7 changes: 4 additions & 3 deletions src/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace DrawSpace
{
namespace Utils
{
using namespace DrawSpace::Commons;
class Quaternion
{
protected:
Expand Down Expand Up @@ -81,14 +82,14 @@ class Quaternion

dsreal dot{ forward * forwardVector };

if (Utils::Maths::Abs(dot - (-1.0)) < 0.000001)
if (Maths::Abs(dot - (-1.0)) < 0.000001)
{
m_quat[0] = 0.0;
m_quat[1] = 1.0;
m_quat[2] = 0.0;
m_quat[3] = Utils::Maths::pi;
m_quat[3] = Maths::pi;
}
else if (Utils::Maths::Abs(dot - (1.0)) < 0.000001)
else if (Maths::Abs(dot - (1.0)) < 0.000001)
{
Identity();
}
Expand Down

0 comments on commit 4455748

Please sign in to comment.