From ca839652bf85bfd77c2bc3db4ea54a6f4516d43a Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Tue, 28 Apr 2026 13:23:14 +0200 Subject: [PATCH] Use the sq function to force constexpr expressions for numeric literals and enhance readability --- application/drivermode.cpp | 4 ++-- audio/audiorenderer.cpp | 4 ++-- audio/sound.cpp | 2 +- environment/moon.cpp | 4 ++-- model/AnimModel.cpp | 4 ++-- model/Model3d.cpp | 4 ++-- rendering/opengl33renderer.cpp | 20 ++++++++++---------- rendering/openglrenderer.cpp | 28 ++++++++++++++-------------- rendering/precipitation.cpp | 2 +- scene/scene.cpp | 18 +++++++++--------- scene/scene.h | 8 ++++---- utilities/utilities.h | 5 +++++ vehicle/DynObj.cpp | 6 +++--- vehicle/Train.cpp | 2 +- world/Track.cpp | 12 ++++++------ 15 files changed, 64 insertions(+), 59 deletions(-) diff --git a/application/drivermode.cpp b/application/drivermode.cpp index 33f1bcd5..1aec7a7a 100644 --- a/application/drivermode.cpp +++ b/application/drivermode.cpp @@ -569,7 +569,7 @@ void driver_mode::update_camera(double const Deltatime) Camera.Reset(); // likwidacja obrotów - patrzy horyzontalnie na południe 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 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) { // 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ż } diff --git a/audio/audiorenderer.cpp b/audio/audiorenderer.cpp index 19ced6bb..88fd74e8 100644 --- a/audio/audiorenderer.cpp +++ b/audio/audiorenderer.cpp @@ -150,7 +150,7 @@ openal_source::sync_with( sound_properties const &State ) { is_multipart ? 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 ); - 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(); sync = sync_state::bad_distance; // flag sync failure for the controller return; @@ -406,7 +406,7 @@ openal_renderer::update( double const Deltatime ) { cameramove = glm::dvec3{ 0.0 }; } // ... 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 }; } m_listenervelocity = limit_velocity( cameramove / Deltatime ); diff --git a/audio/sound.cpp b/audio/sound.cpp index 98e25a20..fafcd53b 100644 --- a/audio/sound.cpp +++ b/audio/sound.cpp @@ -363,7 +363,7 @@ sound_source::play( int const Flags ) { if( m_range != -1 ) { 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 // we act as if it was activated normally, meaning no need to include the opening bookend in subsequent calls m_playbeginning = false; diff --git a/environment/moon.cpp b/environment/moon.cpp index d7b4ab58..31d4b493 100644 --- a/environment/moon.cpp +++ b/environment/moon.cpp @@ -136,7 +136,7 @@ void cMoon::move() { // mean anomaly m_body.mnanom = clamp_circular( 115.3654 + 13.0649929509 * daynumber ); // M, degrees // eccentricity - double const e = 0.054900; + double constexpr e = 0.054900; // 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 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; // lunar orbit plane rectangular coordinates 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 m_body.distance = std::sqrt( xv*xv + yv*yv ); // r // true anomaly diff --git a/model/AnimModel.cpp b/model/AnimModel.cpp index 28f81ca9..96543401 100644 --- a/model/AnimModel.cpp +++ b/model/AnimModel.cpp @@ -97,7 +97,7 @@ void TAnimContainer::UpdateModel() { { auto dif = vTranslateTo - vTranslation; // wektor w kierunku docelowym 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 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 * @@ -113,7 +113,7 @@ void TAnimContainer::UpdateModel() { { // koniec animowania vTranslation = vTranslateTo; 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 if( evDone ) { // wykonanie eventu informującego o zakończeniu diff --git a/model/Model3d.cpp b/model/Model3d.cpp index 9b867bb4..0e5a12d8 100644 --- a/model/Model3d.cpp +++ b/model/Model3d.cpp @@ -631,8 +631,8 @@ std::pair TSubModel::Load(cParser &parser, bool dynamic) { // 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 - if ((glm::length2((vertex)->position - (vertex - 1)->position) > 1000.0 * 1000.0) || (glm::length2((vertex - 1)->position - (vertex - 2)->position) > 1000.0 * 1000.0) || - (glm::length2((vertex - 2)->position - (vertex)->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) > sq(1000.0))) { // 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) diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index 3ec1770d..b3cd2cd2 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -26,8 +26,8 @@ http://mozilla.org/MPL/2.0/. //#define EU07_DEBUG_OPENGL -int const 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_PICKBUFFERSIZE{ 1024 }; // size of (square) textures bound with the pick framebuffer +int constexpr EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction void GLAPIENTRY 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() }; 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 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 distancesquared = // 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() ) ); /* // TBD: take into account distance multipliers? @@ -2764,7 +2764,7 @@ void opengl33_renderer::Render(TAnimModel *Instance) return; } // TBD, TODO: bind offset value with setting variable? - distancesquared += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); + distancesquared += sq(EU07_REFLECTIONFIDELITYOFFSET); break; } 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) auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; - if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { + if( distancesquared > sq(drawdistancethreshold) ) { return; } // 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? // NOTE: combined 'squared' distance doesn't equal actual squared (distance + offset) but, eh - squaredistance += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); + squaredistance += sq(EU07_REFLECTIONFIDELITYOFFSET); break; } 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) auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; - if( squaredistance > drawdistancethreshold * drawdistancethreshold ) { + if( squaredistance > sq(drawdistancethreshold) ) { return false; } // 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) auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; - if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { + if( distancesquared > sq(drawdistancethreshold) ) { return; } // second stage visibility cull, reject modelstoo far away to be noticeable @@ -4619,7 +4619,7 @@ void opengl33_renderer::Update_Lights(light_array &Lights) break; } 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. // but there could still be weaker lights which are closer, so keep looking continue; diff --git a/rendering/openglrenderer.cpp b/rendering/openglrenderer.cpp index b3c196e3..75af6723 100644 --- a/rendering/openglrenderer.cpp +++ b/rendering/openglrenderer.cpp @@ -28,9 +28,9 @@ http://mozilla.org/MPL/2.0/. #include "rendering/screenshot.h" #include -int const 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 const EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction +int constexpr EU07_PICKBUFFERSIZE { 1024 }; // size of (square) textures bound with the pick framebuffer +int constexpr EU07_ENVIRONMENTBUFFERSIZE { 256 }; // size of (square) environmental cube map texture +int constexpr EU07_REFLECTIONFIDELITYOFFSET { 250 }; // artificial increase of range for reflection pass detail reduction float const EU07_OPACITYDEFAULT { 0.5f }; @@ -892,7 +892,7 @@ opengl_renderer::Render_reflections() { auto const timestamp { Timer::GetRenderTime() }; 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 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 distancesquared = // TBD, TODO: bind offset value with setting variable? - ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ) + sq(EU07_REFLECTIONFIDELITYOFFSET) // TBD: take into account distance multipliers? + glm::length2( ( data.area.center - m_renderpass.camera.position() ) ) /* / Global.fDistanceFactor */; break; @@ -2360,7 +2360,7 @@ opengl_renderer::Render( TAnimModel *Instance ) { return; } // TBD, TODO: bind offset value with setting variable? - distancesquared += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); + distancesquared += sq(EU07_REFLECTIONFIDELITYOFFSET); break; } 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) auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; - if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { + if( distancesquared > sq(drawdistancethreshold) ) { return; } // 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 ); if( false == FreeFlyModeFlag ) { // 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; } case rendermode::cabshadows: { squaredistance = glm::length2( glm::vec3{ glm::dvec3{ Dynamic->vPosition - Global.pCamera.Pos } } / Global.ZoomFactor ); // filter out small details - squaredistance = std::max( 100.f * 100.f, squaredistance ); + squaredistance = std::max( sq(100.f), squaredistance ); break; } case rendermode::reflections: { @@ -2441,7 +2441,7 @@ opengl_renderer::Render( TDynamicObject *Dynamic ) { // it also ignores zoom settings and distance multipliers squaredistance = std::max( - 100.f * 100.f, + sq(100.f), // TBD: take into account distance multipliers? glm::length2( glm::vec3{ originoffset } ) /* / Global.fDistanceFactor */ ); // NOTE: arbitrary draw range limit @@ -2450,7 +2450,7 @@ opengl_renderer::Render( TDynamicObject *Dynamic ) { } // TBD, TODO: bind offset value with setting variable? // NOTE: combined 'squared' distance doesn't equal actual squared (distance + offset) but, eh - squaredistance += ( EU07_REFLECTIONFIDELITYOFFSET * EU07_REFLECTIONFIDELITYOFFSET ); + squaredistance += sq(EU07_REFLECTIONFIDELITYOFFSET); break; } 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) auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; - if( squaredistance > drawdistancethreshold * drawdistancethreshold ) { + if( squaredistance > sq(drawdistancethreshold) ) { return false; } // 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) auto const drawdistancethreshold{ m_renderpass.draw_range + 250 }; - if( distancesquared > drawdistancethreshold * drawdistancethreshold ) { + if( distancesquared > sq(drawdistancethreshold) ) { return; } // 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 }; // 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. // but there could still be weaker lights which are closer, so keep looking continue; diff --git a/rendering/precipitation.cpp b/rendering/precipitation.cpp index cafc472e..3f9ecaec 100644 --- a/rendering/precipitation.cpp +++ b/rendering/precipitation.cpp @@ -71,7 +71,7 @@ basic_precipitation::update() { } // ... from camera jump to another location // 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 }; } diff --git a/scene/scene.cpp b/scene/scene.cpp index 21365bd7..9780b593 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -285,7 +285,7 @@ basic_cell::insert( shape_node Shape ) { && ( shapedata.rangesquared_max == targetshapedata.rangesquared_max ) // ...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 - && ( 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 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 ) // ...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 - && ( 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 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 ) { // 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 ); } } @@ -713,7 +713,7 @@ basic_section::update_events( glm::dvec3 const &Location, float const Radius ) { 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 cell.update_events(); } @@ -726,7 +726,7 @@ basic_section::update_sounds( glm::dvec3 const &Location, float const Radius ) { 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 cell.update_sounds(); } @@ -739,7 +739,7 @@ basic_section::radio_stop( glm::dvec3 const &Location, float const Radius ) { 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 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 ) { // 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; } 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 ) { // 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; } 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 ] }; 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 ); } diff --git a/scene/scene.h b/scene/scene.h index df641d02..31ae66e2 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -29,9 +29,9 @@ class opengl33_renderer; namespace scene { -int const EU07_CELLSIZE = 250; -int const EU07_SECTIONSIZE = 1000; -int const EU07_REGIONSIDESECTIONCOUNT = 500; // number of sections along a side of square region +int constexpr EU07_CELLSIZE = 250; +int constexpr EU07_SECTIONSIZE = 1000; +int constexpr EU07_REGIONSIDESECTIONCOUNT = 500; // number of sections along a side of square region struct scratch_data { @@ -428,7 +428,7 @@ public: //private: // types - using section_array = std::array; + using section_array = std::array; struct region_scratchpad { diff --git a/utilities/utilities.h b/utilities/utilities.h index 058220ca..4738b62f 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -29,6 +29,11 @@ template T sign(T x) #define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem #define RadToDeg(r) ((180.0 / M_PI) * (r)) +template constexpr T sq(T v) +{ + return v * v; +} + namespace paths { inline constexpr const char *scenery = "scenery/"; diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index fe4a9933..7952712c 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -1413,7 +1413,7 @@ TDynamicObject * TDynamicObject::ABuFindNearestObject(glm::vec3 pos, TTrack *Tra if( CouplNr == -2 ) { // wektor [kamera-obiekt] - poszukiwanie obiektu // 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 return dynamic; @@ -1422,13 +1422,13 @@ TDynamicObject * TDynamicObject::ABuFindNearestObject(glm::vec3 pos, TTrack *Tra else { // jeśli (CouplNr) inne niz -2, szukamy sprzęgu // 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 CouplNr = 0; return dynamic; } // 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 CouplNr = 1; return dynamic; diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index d79e1023..c2b96713 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -9724,7 +9724,7 @@ TTrain::radio_message( sound_source *Message, int const Channel ) { auto const soundrange { Message->range() }; 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 return; } diff --git a/world/Track.cpp b/world/Track.cpp index 37375b35..2f40a723 100644 --- a/world/Track.cpp +++ b/world/Track.cpp @@ -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 - if( (glm::length2(( p1 + p1 + p2 ) / 3.0 - p1 - cp1) < 0.02 * 0.02) - || (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + 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) < sq(0.02)) ) { // "prostowanie" prostych z kontrolnymi, dokładność 2cm cp1 = cp2 = glm::dvec3(0, 0, 0); } @@ -657,8 +657,8 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin) if( eType != tt_Cross ) { // dla skrzyżowań muszą być podane kontrolne // 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) - || (glm::length2(( p1 + p2 + p2 ) / 3.0 - p2 + 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 ) < sq(0.02) ) { // "prostowanie" prostych z kontrolnymi, dokładność 2cm cp1 = cp2 = glm::dvec3(0, 0, 0); } @@ -722,8 +722,8 @@ void TTrack::Load(cParser *parser, glm::dvec3 const &pOrigin) if( eType != tt_Cross ) { // dla skrzyżowań muszą być podane kontrolne // 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) - || (glm::length2(( p3 + p4 + p4 ) / 3.0 - p4 + 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) < sq(0.02)) ) { // "prostowanie" prostych z kontrolnymi, dokładność 2cm cp3 = cp4 = glm::dvec3(0, 0, 0); }