Skip to content

Commit 9ac8339

Browse files
committed
Replace some math function calls with constants, use inv_pi where applicable
1 parent 53e5508 commit 9ac8339

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

src/celengine/body.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ float Body::getBoundingRadius() const
233233
if (geometry == InvalidResource)
234234
return radius;
235235

236-
return radius * celestia::numbers::sqrt3_v<float>; // sqrt(3)
236+
return radius * celestia::numbers::sqrt3_v<float>;
237237
}
238238

239239

@@ -354,7 +354,7 @@ float Body::getTemperature(double time) const
354354
float lum = square(s->getRadius()) * pow(s->getTemperature(), 4.0f);
355355
flux += lum / square(distFromSun);
356356
}
357-
temp = (float) pow((1.0f - getBondAlbedo()) * flux, 0.25f) / sqrt(2.0f);
357+
temp = std::pow((1.0f - getBondAlbedo()) * flux, 0.25f) * (celestia::numbers::sqrt2_v<float> * 0.5f);
358358
}
359359
return getTempDiscrepancy() + temp;
360360
}

src/celengine/render.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "orbitsampler.h"
4949
#include "rendcontext.h"
5050
#include "textlayout.h"
51+
#include <celcompat/numbers.h>
5152
#include <celengine/observer.h>
5253
#include <celmath/frustum.h>
5354
#include <celmath/distance.h>
@@ -347,7 +348,9 @@ static void BuildGaussianDiscMipLevel(unsigned char* mipPixels,
347348
unsigned int size = 1 << log2size;
348349
float sigma = fwhm / 2.3548f;
349350
float isig2 = 1.0f / (2.0f * sigma * sigma);
350-
float s = 1.0f / (sigma * (float) sqrt(2.0 * celestia::numbers::pi));
351+
// Store 1/sqrt(2*pi) in constexpr sfactor
352+
constexpr auto sfactor = static_cast<float>(0.5 * celestia::numbers::sqrt2 * celestia::numbers::inv_sqrtpi);
353+
float s = sfactor / sigma;
351354

352355
for (unsigned int i = 0; i < size; i++)
353356
{
@@ -2381,7 +2384,7 @@ void Renderer::renderObject(const Vector3f& pos,
23812384
cloudNormalMap = atmosphere->cloudNormalMap.find(textureResolution);
23822385
}
23832386
if (atmosphere->cloudSpeed != 0.0f)
2384-
cloudTexOffset = (float) (-pfmod(now * atmosphere->cloudSpeed / (2 * celestia::numbers::pi), 1.0));
2387+
cloudTexOffset = (float) (-pfmod(now * atmosphere->cloudSpeed * 0.5 * celestia::numbers::inv_pi, 1.0));
23852388
}
23862389

23872390
if (obj.geometry == InvalidResource)

src/celengine/skygrid.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,10 @@ SkyGrid::render(Renderer& renderer,
475475
int raIncrement = meridianSpacing(idealMeridianSpacing);
476476
int decIncrement = parallelSpacing(idealParallelSpacing);
477477

478-
int startRa = (int) std::ceil (totalLongitudeUnits * (minTheta / (celestia::numbers::pi * 2.0)) / (double) raIncrement) * raIncrement;
479-
int endRa = (int) std::floor(totalLongitudeUnits * (maxTheta / (celestia::numbers::pi * 2.0)) / (double) raIncrement) * raIncrement;
480-
int startDec = (int) std::ceil (DEG_MIN_SEC_TOTAL * (minDec / celestia::numbers::pi) / (double) decIncrement) * decIncrement;
481-
int endDec = (int) std::floor(DEG_MIN_SEC_TOTAL * (maxDec / celestia::numbers::pi) / (double) decIncrement) * decIncrement;
478+
int startRa = (int) std::ceil (totalLongitudeUnits * (minTheta * 0.5 * celestia::numbers::inv_pi) / (double) raIncrement) * raIncrement;
479+
int endRa = (int) std::floor(totalLongitudeUnits * (maxTheta * 0.5 * celestia::numbers::inv_pi) / (double) raIncrement) * raIncrement;
480+
int startDec = (int) std::ceil (DEG_MIN_SEC_TOTAL * (minDec * celestia::numbers::inv_pi) / (double) decIncrement) * decIncrement;
481+
int endDec = (int) std::floor(DEG_MIN_SEC_TOTAL * (maxDec * celestia::numbers::inv_pi) / (double) decIncrement) * decIncrement;
482482

483483
// Get the orientation at single precision
484484
Quaterniond q = xrot90 * m_orientation * xrot90.conjugate();

src/celengine/stardb.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <celcompat/bit.h>
2828
#include <celcompat/charconv.h>
29+
#include <celcompat/numbers.h>
2930
#include <celutil/gettext.h>
3031
#include <celutil/intrusiveptr.h>
3132
#include <celutil/logger.h>
@@ -1622,7 +1623,7 @@ void StarDatabaseBuilder::buildOctree()
16221623

16231624
GetLogger()->debug("Sorting stars into octree . . .\n");
16241625
float absMag = astro::appToAbsMag(STAR_OCTREE_MAGNITUDE,
1625-
STAR_OCTREE_ROOT_SIZE * (float) sqrt(3.0));
1626+
STAR_OCTREE_ROOT_SIZE * celestia::numbers::sqrt3_v<float>);
16261627
DynamicStarOctree* root = new DynamicStarOctree(Eigen::Vector3f(1000.0f, 1000.0f, 1000.0f),
16271628
absMag);
16281629
for (unsigned int i = 0; i < unsortedStars.size(); ++i)

src/celengine/universe.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ ExactPlanetPickTraversal(Body* body, PlanetPickInfo& pickInfo)
214214
Eigen::Vector3d bodyMiss = bodyDir - pickInfo.pickRay.direction();
215215

216216
if (double sinAngle2 = bodyMiss.norm() / 2.0;
217-
sinAngle2 < std::sin(celestia::numbers::pi/4.0) && distance > 0.0 &&
218-
distance <= pickInfo.closestDistance)
217+
sinAngle2 < (celestia::numbers::sqrt2 * 0.5) && // sin(45 degrees) = sqrt(2)/2
218+
distance > 0.0 && distance <= pickInfo.closestDistance)
219219
{
220220
pickInfo.closestDistance = distance;
221221
pickInfo.closestBody = body;

0 commit comments

Comments
 (0)