16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-18 00:49:19 +02:00

Use the sq function to force constexpr expressions for numeric literals and enhance readability

This commit is contained in:
docentYT
2026-04-28 13:23:14 +02:00
parent 6be1b0886d
commit ca839652bf
15 changed files with 64 additions and 59 deletions

View File

@@ -569,7 +569,7 @@ void driver_mode::update_camera(double const Deltatime)
Camera.Reset(); // likwidacja obrotów - patrzy horyzontalnie na południe Camera.Reset(); // likwidacja obrotów - patrzy horyzontalnie na południe
if (Camera.m_owner == nullptr) if (Camera.m_owner == nullptr)
{ {
if (controlled && glm::length2(controlled->GetPosition() - Camera.Pos) < 1500 * 1500) // length2 is better than length for comparing because it does not require sqrt function if (controlled && glm::length2(controlled->GetPosition() - Camera.Pos) < sq(1500)) // length2 is better than length for comparing because it does not require sqrt function
{ {
// gdy bliżej niż 1.5km // gdy bliżej niż 1.5km
Camera.LookAt = controlled->GetPosition() + 0.4 * controlled->VectorUp() * controlled->MoverParameters->Dim.H; Camera.LookAt = controlled->GetPosition() + 0.4 * controlled->VectorUp() * controlled->MoverParameters->Dim.H;
@@ -583,7 +583,7 @@ void driver_mode::update_camera(double const Deltatime)
if (d && pDynamicNearest) if (d && pDynamicNearest)
{ {
// jeśli jakiś jest znaleziony wcześniej // jeśli jakiś jest znaleziony wcześniej
if (100.0 * glm::length2(d->GetPosition() - Camera.Pos) > glm::length2(pDynamicNearest->GetPosition() - Camera.Pos)) // length2 is better than length for comparing because it does not require sqrt function if (sq(10.0) * glm::length2(d->GetPosition() - Camera.Pos) > glm::length2(pDynamicNearest->GetPosition() - Camera.Pos)) // length2 is better than length for comparing because it does not require sqrt function
{ {
d = pDynamicNearest; // jeśli najbliższy nie jest 10 razy bliżej niż d = pDynamicNearest; // jeśli najbliższy nie jest 10 razy bliżej niż
} }

View File

@@ -150,7 +150,7 @@ openal_source::sync_with( sound_properties const &State ) {
is_multipart ? is_multipart ?
EU07_SOUND_CUTOFFRANGE : // we keep multi-part sounds around longer, to minimize restarts as the sounds get out and back in range EU07_SOUND_CUTOFFRANGE : // we keep multi-part sounds around longer, to minimize restarts as the sounds get out and back in range
sound_range * 7.5f ); sound_range * 7.5f );
if( glm::length2( sound_distance ) > std::min( ( cutoffrange * cutoffrange ), ( EU07_SOUND_CUTOFFRANGE * EU07_SOUND_CUTOFFRANGE ) ) ) { if( glm::length2( sound_distance ) > std::min( sq(cutoffrange), sq(EU07_SOUND_CUTOFFRANGE) ) ) {
stop(); stop();
sync = sync_state::bad_distance; // flag sync failure for the controller sync = sync_state::bad_distance; // flag sync failure for the controller
return; return;
@@ -406,7 +406,7 @@ openal_renderer::update( double const Deltatime ) {
cameramove = glm::dvec3{ 0.0 }; cameramove = glm::dvec3{ 0.0 };
} }
// ... from camera jump to another location // ... from camera jump to another location
if( glm::length2( cameramove ) > 100.0 * 100.0) { // length2 is better than length for comparing because it does not require sqrt function if( glm::length2( cameramove ) > sq(100.0)) { // length2 is better than length for comparing because it does not require sqrt function
cameramove = glm::dvec3{ 0.0 }; cameramove = glm::dvec3{ 0.0 };
} }
m_listenervelocity = limit_velocity( cameramove / Deltatime ); m_listenervelocity = limit_velocity( cameramove / Deltatime );

View File

@@ -363,7 +363,7 @@ sound_source::play( int const Flags ) {
if( m_range != -1 ) { if( m_range != -1 ) {
auto const cutoffrange { std::abs( m_range * 5 ) }; auto const cutoffrange { std::abs( m_range * 5 ) };
if( glm::length2( location() - Global.pCamera.Pos ) > std::min( 2750.f * 2750.f, cutoffrange * cutoffrange ) ) { if( glm::length2( location() - Global.pCamera.Pos ) > std::min( sq(2750.f), sq(cutoffrange) ) ) {
// while we drop sounds from beyond sensible and/or audible range // while we drop sounds from beyond sensible and/or audible range
// we act as if it was activated normally, meaning no need to include the opening bookend in subsequent calls // we act as if it was activated normally, meaning no need to include the opening bookend in subsequent calls
m_playbeginning = false; m_playbeginning = false;

View File

@@ -136,7 +136,7 @@ void cMoon::move() {
// mean anomaly // mean anomaly
m_body.mnanom = clamp_circular( 115.3654 + 13.0649929509 * daynumber ); // M, degrees m_body.mnanom = clamp_circular( 115.3654 + 13.0649929509 * daynumber ); // M, degrees
// eccentricity // eccentricity
double const e = 0.054900; double constexpr e = 0.054900;
// eccentric anomaly // eccentric anomaly
double E0 = m_body.mnanom + radtodeg * e * std::sin( degtorad * m_body.mnanom ) * ( 1.0 + e * std::cos( degtorad * m_body.mnanom ) ); double E0 = m_body.mnanom + radtodeg * e * std::sin( degtorad * m_body.mnanom ) * ( 1.0 + e * std::cos( degtorad * m_body.mnanom ) );
double E1 = E0 - ( E0 - radtodeg * e * std::sin( degtorad * E0 ) - m_body.mnanom ) / ( 1.0 - e * std::cos( degtorad * E0 ) ); double E1 = E0 - ( E0 - radtodeg * e * std::sin( degtorad * E0 ) - m_body.mnanom ) / ( 1.0 - e * std::cos( degtorad * E0 ) );
@@ -147,7 +147,7 @@ void cMoon::move() {
double const E = E1; double const E = E1;
// lunar orbit plane rectangular coordinates // lunar orbit plane rectangular coordinates
double const xv = mndistance * ( std::cos( degtorad * E ) - e ); double const xv = mndistance * ( std::cos( degtorad * E ) - e );
double const yv = mndistance * std::sin( degtorad * E ) * std::sqrt( 1.0 - e*e ); double const yv = mndistance * std::sin( degtorad * E ) * std::sqrt( 1.0 - sq(e) );
// distance // distance
m_body.distance = std::sqrt( xv*xv + yv*yv ); // r m_body.distance = std::sqrt( xv*xv + yv*yv ); // r
// true anomaly // true anomaly

View File

@@ -97,7 +97,7 @@ void TAnimContainer::UpdateModel() {
{ {
auto dif = vTranslateTo - vTranslation; // wektor w kierunku docelowym auto dif = vTranslateTo - vTranslation; // wektor w kierunku docelowym
double l2 = glm::length2(dif); // długość wektora potrzebnego przemieszczenia double l2 = glm::length2(dif); // długość wektora potrzebnego przemieszczenia
if (l2 >= 0.0001) if (l2 >= sq(0.01))
{ // jeśli do przemieszczenia jest ponad 1cm { // jeśli do przemieszczenia jest ponad 1cm
auto s = glm::normalize(dif); // jednostkowy wektor kierunku // Długość wektora nie jest równa 0, sprawdzane wcześniej więc wektor normalny będzie zawsze prawidłowy. auto s = glm::normalize(dif); // jednostkowy wektor kierunku // Długość wektora nie jest równa 0, sprawdzane wcześniej więc wektor normalny będzie zawsze prawidłowy.
s = s * s = s *
@@ -113,7 +113,7 @@ void TAnimContainer::UpdateModel() {
{ // koniec animowania { // koniec animowania
vTranslation = vTranslateTo; vTranslation = vTranslateTo;
fTranslateSpeed = 0.0; // wyłączenie przeliczania wektora fTranslateSpeed = 0.0; // wyłączenie przeliczania wektora
if (glm::length2(vTranslation) <= 0.0001) // jeśli jest w punkcie początkowym if (glm::length2(vTranslation) <= sq(0.01)) // jeśli jest w punkcie początkowym
iAnim &= ~2; // wyłączyć zmianę pozycji submodelu iAnim &= ~2; // wyłączyć zmianę pozycji submodelu
if( evDone ) { if( evDone ) {
// wykonanie eventu informującego o zakończeniu // wykonanie eventu informującego o zakończeniu

View File

@@ -631,8 +631,8 @@ std::pair<int, int> TSubModel::Load(cParser &parser, bool dynamic)
{ {
// jeśli pierwszy trójkąt będzie zdegenerowany, to zostanie usunięty i nie ma co sprawdzać // jeśli pierwszy trójkąt będzie zdegenerowany, to zostanie usunięty i nie ma co sprawdzać
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if ((glm::length2((vertex)->position - (vertex - 1)->position) > 1000.0 * 1000.0) || (glm::length2((vertex - 1)->position - (vertex - 2)->position) > 1000.0 * 1000.0) || if ((glm::length2((vertex)->position - (vertex - 1)->position) > sq(1000.0)) || (glm::length2((vertex - 1)->position - (vertex - 2)->position) > sq(1000.0)) ||
(glm::length2((vertex - 2)->position - (vertex)->position) > 1000.0 * 1000.0)) (glm::length2((vertex - 2)->position - (vertex)->position) > sq(1000.0)))
{ {
// jeżeli są dalej niż 2km od siebie //Ra 15-01: // jeżeli są dalej niż 2km od siebie //Ra 15-01:
// obiekt wstawiany nie powinien być większy niż 300m (trójkąty terenu w E3D mogą mieć 1.5km) // obiekt wstawiany nie powinien być większy niż 300m (trójkąty terenu w E3D mogą mieć 1.5km)

View File

@@ -26,8 +26,8 @@ http://mozilla.org/MPL/2.0/.
//#define EU07_DEBUG_OPENGL //#define EU07_DEBUG_OPENGL
int const EU07_PICKBUFFERSIZE{ 1024 }; // size of (square) textures bound with the pick framebuffer int constexpr EU07_PICKBUFFERSIZE{ 1024 }; // size of (square) textures bound with the pick framebuffer
int const EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction int constexpr EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction
void GLAPIENTRY void GLAPIENTRY
ErrorCallback( GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam ) { ErrorCallback( GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam ) {
@@ -1293,7 +1293,7 @@ bool opengl33_renderer::Render_reflections(viewport_config &vp)
auto const timestamp{ Timer::GetRenderTime() }; auto const timestamp{ Timer::GetRenderTime() };
if( ( timestamp - m_environmentupdatetime < Global.reflectiontune.update_interval ) if( ( timestamp - m_environmentupdatetime < Global.reflectiontune.update_interval )
&& ( glm::length2( m_renderpass.pass_camera.position() - m_environmentupdatelocation ) < 1000.0 * 1000.0 ) ) // length2 is better than length for comparing because it does not require sqrt function && ( glm::length2( m_renderpass.pass_camera.position() - m_environmentupdatelocation ) < sq(1000.0)) ) // length2 is better than length for comparing because it does not require sqrt function
{ {
// run update every 5+ mins of simulation time, or at least 1km from the last location // run update every 5+ mins of simulation time, or at least 1km from the last location
return false; return false;
@@ -2690,7 +2690,7 @@ void opengl33_renderer::Render(scene::shape_node const &Shape, bool const Ignore
// reflection mode draws simplified version of the shapes, by artificially increasing view range // reflection mode draws simplified version of the shapes, by artificially increasing view range
distancesquared = distancesquared =
// TBD, TODO: bind offset value with setting variable? // TBD, TODO: bind offset value with setting variable?
( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ) sq(EU07_REFLECTIONFIDELITYOFFSET)
+ glm::length2( ( data.area.center - m_renderpass.pass_camera.position() ) ); + glm::length2( ( data.area.center - m_renderpass.pass_camera.position() ) );
/* /*
// TBD: take into account distance multipliers? // TBD: take into account distance multipliers?
@@ -2764,7 +2764,7 @@ void opengl33_renderer::Render(TAnimModel *Instance)
return; return;
} }
// TBD, TODO: bind offset value with setting variable? // TBD, TODO: bind offset value with setting variable?
distancesquared += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); distancesquared += sq(EU07_REFLECTIONFIDELITYOFFSET);
break; break;
} }
default: default:
@@ -2779,7 +2779,7 @@ void opengl33_renderer::Render(TAnimModel *Instance)
} }
// crude way to reject early items too far to affect the output (mostly relevant for shadow passes) // crude way to reject early items too far to affect the output (mostly relevant for shadow passes)
auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; auto const drawdistancethreshold{ m_renderpass.draw_range + 250 };
if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { if( distancesquared > sq(drawdistancethreshold) ) {
return; return;
} }
// second stage visibility cull, reject modelstoo far away to be noticeable // second stage visibility cull, reject modelstoo far away to be noticeable
@@ -2857,7 +2857,7 @@ bool opengl33_renderer::Render(TDynamicObject *Dynamic)
} }
// TBD, TODO: bind offset value with setting variable? // TBD, TODO: bind offset value with setting variable?
// NOTE: combined 'squared' distance doesn't equal actual squared (distance + offset) but, eh // NOTE: combined 'squared' distance doesn't equal actual squared (distance + offset) but, eh
squaredistance += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); squaredistance += sq(EU07_REFLECTIONFIDELITYOFFSET);
break; break;
} }
default: default:
@@ -2869,7 +2869,7 @@ bool opengl33_renderer::Render(TDynamicObject *Dynamic)
} }
// crude way to reject early items too far to affect the output (mostly relevant for shadow and reflection passes) // crude way to reject early items too far to affect the output (mostly relevant for shadow and reflection passes)
auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; auto const drawdistancethreshold{ m_renderpass.draw_range + 250 };
if( squaredistance > drawdistancethreshold * drawdistancethreshold ) { if( squaredistance > sq(drawdistancethreshold) ) {
return false; return false;
} }
// second stage visibility cull, reject vehicles too far away to be noticeable // second stage visibility cull, reject vehicles too far away to be noticeable
@@ -3779,7 +3779,7 @@ void opengl33_renderer::Render_Alpha(TAnimModel *Instance)
} }
// crude way to reject early items too far to affect the output (mostly relevant for shadow passes) // crude way to reject early items too far to affect the output (mostly relevant for shadow passes)
auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; auto const drawdistancethreshold{ m_renderpass.draw_range + 250 };
if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { if( distancesquared > sq(drawdistancethreshold) ) {
return; return;
} }
// second stage visibility cull, reject modelstoo far away to be noticeable // second stage visibility cull, reject modelstoo far away to be noticeable
@@ -4619,7 +4619,7 @@ void opengl33_renderer::Update_Lights(light_array &Lights)
break; break;
} }
auto const lightoffset = glm::vec3{scenelight.position - camera}; auto const lightoffset = glm::vec3{scenelight.position - camera};
if (glm::length2(lightoffset) > 1000.f * 1000.f) { if (glm::length2(lightoffset) > sq(1000.f)) {
// we don't care about lights past arbitrary limit of 1 km. // we don't care about lights past arbitrary limit of 1 km.
// but there could still be weaker lights which are closer, so keep looking // but there could still be weaker lights which are closer, so keep looking
continue; continue;

View File

@@ -28,9 +28,9 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/screenshot.h" #include "rendering/screenshot.h"
#include <imgui/imgui_impl_opengl2.h> #include <imgui/imgui_impl_opengl2.h>
int const EU07_PICKBUFFERSIZE { 1024 }; // size of (square) textures bound with the pick framebuffer int constexpr EU07_PICKBUFFERSIZE { 1024 }; // size of (square) textures bound with the pick framebuffer
int const EU07_ENVIRONMENTBUFFERSIZE { 256 }; // size of (square) environmental cube map texture int constexpr EU07_ENVIRONMENTBUFFERSIZE { 256 }; // size of (square) environmental cube map texture
int const EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction int constexpr EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction
float const EU07_OPACITYDEFAULT { 0.5f }; float const EU07_OPACITYDEFAULT { 0.5f };
@@ -892,7 +892,7 @@ opengl_renderer::Render_reflections() {
auto const timestamp { Timer::GetRenderTime() }; auto const timestamp { Timer::GetRenderTime() };
if( ( timestamp - m_environmentupdatetime < Global.reflectiontune.update_interval ) if( ( timestamp - m_environmentupdatetime < Global.reflectiontune.update_interval )
&& ( glm::length2( m_renderpass.camera.position() - m_environmentupdatelocation ) < 1000.0 * 1000.0 ) ) { && ( glm::length2( m_renderpass.camera.position() - m_environmentupdatelocation ) < sq(1000.0)) ) {
// run update every 5+ mins of simulation time, or at least 1km from the last location // run update every 5+ mins of simulation time, or at least 1km from the last location
return false; return false;
} }
@@ -2284,7 +2284,7 @@ opengl_renderer::Render( scene::shape_node const &Shape, bool const Ignorerange
// reflection mode draws simplified version of the shapes, by artificially increasing view range // reflection mode draws simplified version of the shapes, by artificially increasing view range
distancesquared = distancesquared =
// TBD, TODO: bind offset value with setting variable? // TBD, TODO: bind offset value with setting variable?
( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ) sq(EU07_REFLECTIONFIDELITYOFFSET)
// TBD: take into account distance multipliers? // TBD: take into account distance multipliers?
+ glm::length2( ( data.area.center - m_renderpass.camera.position() ) ) /* / Global.fDistanceFactor */; + glm::length2( ( data.area.center - m_renderpass.camera.position() ) ) /* / Global.fDistanceFactor */;
break; break;
@@ -2360,7 +2360,7 @@ opengl_renderer::Render( TAnimModel *Instance ) {
return; return;
} }
// TBD, TODO: bind offset value with setting variable? // TBD, TODO: bind offset value with setting variable?
distancesquared += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); distancesquared += sq(EU07_REFLECTIONFIDELITYOFFSET);
break; break;
} }
default: { default: {
@@ -2374,7 +2374,7 @@ opengl_renderer::Render( TAnimModel *Instance ) {
} }
// crude way to reject early items too far to affect the output (mostly relevant for shadow passes) // crude way to reject early items too far to affect the output (mostly relevant for shadow passes)
auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; auto const drawdistancethreshold{ m_renderpass.draw_range + 250 };
if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { if( distancesquared > sq(drawdistancethreshold) ) {
return; return;
} }
// second stage visibility cull, reject modelstoo far away to be noticeable // second stage visibility cull, reject modelstoo far away to be noticeable
@@ -2426,14 +2426,14 @@ opengl_renderer::Render( TDynamicObject *Dynamic ) {
squaredistance = glm::length2( glm::vec3{ glm::dvec3{ Dynamic->vPosition - Global.pCamera.Pos } } / Global.ZoomFactor ); squaredistance = glm::length2( glm::vec3{ glm::dvec3{ Dynamic->vPosition - Global.pCamera.Pos } } / Global.ZoomFactor );
if( false == FreeFlyModeFlag ) { if( false == FreeFlyModeFlag ) {
// filter out small details if we're in vehicle cab // filter out small details if we're in vehicle cab
squaredistance = std::max( 100.f * 100.f, squaredistance ); squaredistance = std::max( sq(100.f), squaredistance );
} }
break; break;
} }
case rendermode::cabshadows: { case rendermode::cabshadows: {
squaredistance = glm::length2( glm::vec3{ glm::dvec3{ Dynamic->vPosition - Global.pCamera.Pos } } / Global.ZoomFactor ); squaredistance = glm::length2( glm::vec3{ glm::dvec3{ Dynamic->vPosition - Global.pCamera.Pos } } / Global.ZoomFactor );
// filter out small details // filter out small details
squaredistance = std::max( 100.f * 100.f, squaredistance ); squaredistance = std::max( sq(100.f), squaredistance );
break; break;
} }
case rendermode::reflections: { case rendermode::reflections: {
@@ -2441,7 +2441,7 @@ opengl_renderer::Render( TDynamicObject *Dynamic ) {
// it also ignores zoom settings and distance multipliers // it also ignores zoom settings and distance multipliers
squaredistance = squaredistance =
std::max( std::max(
100.f * 100.f, sq(100.f),
// TBD: take into account distance multipliers? // TBD: take into account distance multipliers?
glm::length2( glm::vec3{ originoffset } ) /* / Global.fDistanceFactor */ ); glm::length2( glm::vec3{ originoffset } ) /* / Global.fDistanceFactor */ );
// NOTE: arbitrary draw range limit // NOTE: arbitrary draw range limit
@@ -2450,7 +2450,7 @@ opengl_renderer::Render( TDynamicObject *Dynamic ) {
} }
// TBD, TODO: bind offset value with setting variable? // TBD, TODO: bind offset value with setting variable?
// NOTE: combined 'squared' distance doesn't equal actual squared (distance + offset) but, eh // NOTE: combined 'squared' distance doesn't equal actual squared (distance + offset) but, eh
squaredistance += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); squaredistance += sq(EU07_REFLECTIONFIDELITYOFFSET);
break; break;
} }
default: { default: {
@@ -2461,7 +2461,7 @@ opengl_renderer::Render( TDynamicObject *Dynamic ) {
} }
// crude way to reject early items too far to affect the output (mostly relevant for shadow and reflection passes) // crude way to reject early items too far to affect the output (mostly relevant for shadow and reflection passes)
auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; auto const drawdistancethreshold{ m_renderpass.draw_range + 250 };
if( squaredistance > drawdistancethreshold * drawdistancethreshold ) { if( squaredistance > sq(drawdistancethreshold) ) {
return false; return false;
} }
// second stage visibility cull, reject vehicles too far away to be noticeable // second stage visibility cull, reject vehicles too far away to be noticeable
@@ -3519,7 +3519,7 @@ opengl_renderer::Render_Alpha( TAnimModel *Instance ) {
} }
// crude way to reject early items too far to affect the output (mostly relevant for shadow passes) // crude way to reject early items too far to affect the output (mostly relevant for shadow passes)
auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; auto const drawdistancethreshold{ m_renderpass.draw_range + 250 };
if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { if( distancesquared > sq(drawdistancethreshold) ) {
return; return;
} }
// second stage visibility cull, reject modelstoo far away to be noticeable // second stage visibility cull, reject modelstoo far away to be noticeable
@@ -4342,7 +4342,7 @@ opengl_renderer::Update_Lights( light_array &Lights ) {
} }
auto const lightoffset = glm::vec3{ scenelight.position - camera }; auto const lightoffset = glm::vec3{ scenelight.position - camera };
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if( glm::length2( lightoffset ) > 1000.f * 1000.f ) { if( glm::length2( lightoffset ) > sq(1000.f)) {
// we don't care about lights past arbitrary limit of 1 km. // we don't care about lights past arbitrary limit of 1 km.
// but there could still be weaker lights which are closer, so keep looking // but there could still be weaker lights which are closer, so keep looking
continue; continue;

View File

@@ -71,7 +71,7 @@ basic_precipitation::update() {
} }
// ... from camera jump to another location // ... from camera jump to another location
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if( glm::length2( cameramove ) > 100.0 * 100.0 ) if( glm::length2( cameramove ) > sq(100.0) )
{ {
cameramove = glm::dvec3{ 0.0 }; cameramove = glm::dvec3{ 0.0 };
} }

View File

@@ -285,7 +285,7 @@ basic_cell::insert( shape_node Shape ) {
&& ( shapedata.rangesquared_max == targetshapedata.rangesquared_max ) && ( shapedata.rangesquared_max == targetshapedata.rangesquared_max )
// ...and located close to each other (within arbitrary limit of 25m) // ...and located close to each other (within arbitrary limit of 25m)
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
&& ( glm::length2( shapedata.area.center - targetshapedata.area.center ) < 25.0 * 25.0 ) ) { && ( glm::length2( shapedata.area.center - targetshapedata.area.center ) < sq(25.0) ) ) {
if( true == targetshape.merge( Shape ) ) { if( true == targetshape.merge( Shape ) ) {
// if the shape was merged there's nothing left to do // if the shape was merged there's nothing left to do
@@ -312,7 +312,7 @@ basic_cell::insert( lines_node Lines ) {
&& ( linesdata.rangesquared_max == targetlinesdata.rangesquared_max ) && ( linesdata.rangesquared_max == targetlinesdata.rangesquared_max )
// ...and located close to each other (within arbitrary limit of 10m) // ...and located close to each other (within arbitrary limit of 10m)
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
&& ( glm::length2( linesdata.area.center - targetlinesdata.area.center ) < 10.0 * 10.0) ) { && ( glm::length2( linesdata.area.center - targetlinesdata.area.center ) < sq(10.0) ) ) {
if( true == targetlines.merge( Lines ) ) { if( true == targetlines.merge( Lines ) ) {
// if the shape was merged there's nothing left to do // if the shape was merged there's nothing left to do
@@ -701,7 +701,7 @@ basic_section::update_traction( TDynamicObject *Vehicle, int const Pantographind
for( auto &cell : m_cells ) { for( auto &cell : m_cells ) {
// we reject early cells which aren't within our area of interest // we reject early cells which aren't within our area of interest
if( glm::length2( cell.area().center - pantographposition ) < ( ( cell.area().radius + radius ) * ( cell.area().radius + radius ) ) ) { if( glm::length2( cell.area().center - pantographposition ) < sq(cell.area().radius + radius) ) {
cell.update_traction( Vehicle, Pantographindex ); cell.update_traction( Vehicle, Pantographindex );
} }
} }
@@ -713,7 +713,7 @@ basic_section::update_events( glm::dvec3 const &Location, float const Radius ) {
for( auto &cell : m_cells ) { for( auto &cell : m_cells ) {
if( glm::length2( cell.area().center - Location ) < ( ( cell.area().radius + Radius ) * ( cell.area().radius + Radius ) ) ) { if( glm::length2( cell.area().center - Location ) < sq(cell.area().radius + Radius) ) {
// we reject cells which aren't within our area of interest // we reject cells which aren't within our area of interest
cell.update_events(); cell.update_events();
} }
@@ -726,7 +726,7 @@ basic_section::update_sounds( glm::dvec3 const &Location, float const Radius ) {
for( auto &cell : m_cells ) { for( auto &cell : m_cells ) {
if( glm::length2( cell.area().center - Location ) < ( ( cell.area().radius + Radius ) * ( cell.area().radius + Radius ) ) ) { if( glm::length2( cell.area().center - Location ) < sq(cell.area().radius + Radius) ) {
// we reject cells which aren't within our area of interest // we reject cells which aren't within our area of interest
cell.update_sounds(); cell.update_sounds();
} }
@@ -739,7 +739,7 @@ basic_section::radio_stop( glm::dvec3 const &Location, float const Radius ) {
for( auto &cell : m_cells ) { for( auto &cell : m_cells ) {
if( glm::length2( cell.area().center - Location ) < ( ( cell.area().radius + Radius ) * ( cell.area().radius + Radius ) ) ) { if( glm::length2( cell.area().center - Location ) < sq(cell.area().radius + Radius) ) {
// we reject cells which aren't within our area of interest // we reject cells which aren't within our area of interest
cell.radio_stop(); cell.radio_stop();
} }
@@ -854,7 +854,7 @@ basic_section::find( glm::dvec3 const &Point, float const Radius, bool const Onl
for( auto &cell : m_cells ) { for( auto &cell : m_cells ) {
// we reject early cells which aren't within our area of interest // we reject early cells which aren't within our area of interest
if( glm::length2( cell.area().center - Point ) > ( ( cell.area().radius + Radius ) * ( cell.area().radius + Radius ) ) ) { if( glm::length2( cell.area().center - Point ) > sq(cell.area().radius + Radius) ) {
continue; continue;
} }
std::tie( vehiclefound, distancefound ) = cell.find( Point, Radius, Onlycontrolled, Findbycoupler ); std::tie( vehiclefound, distancefound ) = cell.find( Point, Radius, Onlycontrolled, Findbycoupler );
@@ -900,7 +900,7 @@ basic_section::find( glm::dvec3 const &Point, TTraction const *Other, int const
for( auto &cell : m_cells ) { for( auto &cell : m_cells ) {
// we reject early cells which aren't within our area of interest // we reject early cells which aren't within our area of interest
if( glm::length2( cell.area().center - Point ) > ( ( cell.area().radius + radius ) * ( cell.area().radius + radius ) ) ) { if( glm::length2( cell.area().center - Point ) > sq(cell.area().radius + radius) ) {
continue; continue;
} }
std::tie( tractionfound, endpointfound, distancefound ) = cell.find( Point, Other, Currentdirection ); std::tie( tractionfound, endpointfound, distancefound ) = cell.find( Point, Other, Currentdirection );
@@ -1477,7 +1477,7 @@ basic_region::sections( glm::dvec3 const &Point, float const Radius ) {
auto *section { m_sections[ row * EU07_REGIONSIDESECTIONCOUNT + column ] }; auto *section { m_sections[ row * EU07_REGIONSIDESECTIONCOUNT + column ] };
if( ( section != nullptr ) if( ( section != nullptr )
&& ( glm::length2( section->area().center - Point ) <= ( ( section->area().radius + padding + Radius ) * ( section->area().radius + padding + Radius ) ) ) ) { && ( glm::length2( section->area().center - Point ) <= sq( section->area().radius + padding + Radius ) ) ) {
m_scratchpad.sections.emplace_back( section ); m_scratchpad.sections.emplace_back( section );
} }

View File

@@ -29,9 +29,9 @@ class opengl33_renderer;
namespace scene { namespace scene {
int const EU07_CELLSIZE = 250; int constexpr EU07_CELLSIZE = 250;
int const EU07_SECTIONSIZE = 1000; int constexpr EU07_SECTIONSIZE = 1000;
int const EU07_REGIONSIDESECTIONCOUNT = 500; // number of sections along a side of square region int constexpr EU07_REGIONSIDESECTIONCOUNT = 500; // number of sections along a side of square region
struct scratch_data { struct scratch_data {
@@ -428,7 +428,7 @@ public:
//private: //private:
// types // types
using section_array = std::array<basic_section *, EU07_REGIONSIDESECTIONCOUNT * EU07_REGIONSIDESECTIONCOUNT>; using section_array = std::array<basic_section *, sq(EU07_REGIONSIDESECTIONCOUNT)>;
struct region_scratchpad { struct region_scratchpad {

View File

@@ -29,6 +29,11 @@ template <typename T> T sign(T x)
#define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem #define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem
#define RadToDeg(r) ((180.0 / M_PI) * (r)) #define RadToDeg(r) ((180.0 / M_PI) * (r))
template <typename T> constexpr T sq(T v)
{
return v * v;
}
namespace paths namespace paths
{ {
inline constexpr const char *scenery = "scenery/"; inline constexpr const char *scenery = "scenery/";

View File

@@ -1413,7 +1413,7 @@ TDynamicObject * TDynamicObject::ABuFindNearestObject(glm::vec3 pos, TTrack *Tra
if( CouplNr == -2 ) { if( CouplNr == -2 ) {
// wektor [kamera-obiekt] - poszukiwanie obiektu // wektor [kamera-obiekt] - poszukiwanie obiektu
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if (glm::length2(glm::dvec3(pos) - dynamic->vPosition) < 10.0 * 10.0) if (glm::length2(glm::dvec3(pos) - dynamic->vPosition) < sq(10.0))
{ {
// 10 metrów // 10 metrów
return dynamic; return dynamic;
@@ -1422,13 +1422,13 @@ TDynamicObject * TDynamicObject::ABuFindNearestObject(glm::vec3 pos, TTrack *Tra
else { else {
// jeśli (CouplNr) inne niz -2, szukamy sprzęgu // jeśli (CouplNr) inne niz -2, szukamy sprzęgu
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if (glm::length2(glm::dvec3(pos) - dynamic->vCoulpler[0]) < 5.0 * 5.0) { if (glm::length2(glm::dvec3(pos) - dynamic->vCoulpler[0]) < sq(5.0)) {
// 5 metrów // 5 metrów
CouplNr = 0; CouplNr = 0;
return dynamic; return dynamic;
} }
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if (glm::length2(glm::dvec3(pos) - dynamic->vCoulpler[1]) < 5.0 * 5.0) { if (glm::length2(glm::dvec3(pos) - dynamic->vCoulpler[1]) < sq(5.0)) {
// 5 metrów // 5 metrów
CouplNr = 1; CouplNr = 1;
return dynamic; return dynamic;

View File

@@ -9724,7 +9724,7 @@ TTrain::radio_message( sound_source *Message, int const Channel ) {
auto const soundrange { Message->range() }; auto const soundrange { Message->range() };
if( ( soundrange > 0 ) if( ( soundrange > 0 )
&& ( glm::length2( Message->location() - glm::dvec3 { DynamicObject->GetPosition() } ) > ( soundrange * soundrange ) ) ) { && ( glm::length2( Message->location() - glm::dvec3 { DynamicObject->GetPosition() } ) > sq(soundrange) ) ) {
// skip message playback if the receiver is outside of the emitter's range // skip message playback if the receiver is outside of the emitter's range
return; return;
} }

View File

@@ -560,8 +560,8 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin)
} }
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if( (glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1) < 0.02 * 0.02) if( (glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1) < sq(0.02))
|| (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + cp1) < 0.02 * 0.02) ) { || (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + cp1) < sq(0.02)) ) {
// "prostowanie" prostych z kontrolnymi, dokładność 2cm // "prostowanie" prostych z kontrolnymi, dokładność 2cm
cp1 = cp2 = glm::dvec3(0, 0, 0); cp1 = cp2 = glm::dvec3(0, 0, 0);
} }
@@ -657,8 +657,8 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin)
if( eType != tt_Cross ) { if( eType != tt_Cross ) {
// dla skrzyżowań muszą być podane kontrolne // dla skrzyżowań muszą być podane kontrolne
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if( (glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1 ) < 0.02 * 0.02) if( glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1 ) < sq(0.02)
|| (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + cp1 ) < 0.02 * 0.02) ) { || glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + cp1 ) < sq(0.02) ) {
// "prostowanie" prostych z kontrolnymi, dokładność 2cm // "prostowanie" prostych z kontrolnymi, dokładność 2cm
cp1 = cp2 = glm::dvec3(0, 0, 0); cp1 = cp2 = glm::dvec3(0, 0, 0);
} }
@@ -722,8 +722,8 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin)
if( eType != tt_Cross ) { if( eType != tt_Cross ) {
// dla skrzyżowań muszą być podane kontrolne // dla skrzyżowań muszą być podane kontrolne
// length2 is better than length for comparing because it does not require sqrt function // length2 is better than length for comparing because it does not require sqrt function
if( (glm::length2(( p3 + p3 + p4 ) / 3.0 - p3 - cp3) < 0.02 * 0.02) if( (glm::length2(( p3 + p3 + p4 ) / 3.0 - p3 - cp3) < sq(0.02))
|| (glm::length2(( p3 + p4 + p4 ) / 3.0 - p4 + cp3) < 0.02 * 0.02) ) { || (glm::length2(( p3 + p4 + p4 ) / 3.0 - p4 + cp3) < sq(0.02)) ) {
// "prostowanie" prostych z kontrolnymi, dokładność 2cm // "prostowanie" prostych z kontrolnymi, dokładność 2cm
cp3 = cp4 = glm::dvec3(0, 0, 0); cp3 = cp4 = glm::dvec3(0, 0, 0);
} }