From f0d88fbf9f031985fe7d4cbb497f1b1cf8227e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 23 Apr 2012 19:27:31 +0200 Subject: [PATCH] Added templated Math::Constants structure to replace PI #define. And added sqrt(2) and sqrt(3) constants while we are at it. --- src/Math/Math.h | 25 ++++++++++++++++++++++--- src/Math/Test/MathTest.cpp | 9 +++------ src/Primitives/Capsule.cpp | 6 +++--- src/Primitives/UVSphere.cpp | 4 ++-- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Math/Math.h b/src/Math/Math.h index cf69b82c5..827ba6c81 100644 --- a/src/Math/Math.h +++ b/src/Math/Math.h @@ -32,8 +32,27 @@ namespace Magnum { */ namespace Math { -/** @brief Pi */ -#define PI 3.1415926535 +/** @brief Numeric constants */ +template struct Constants { + #ifdef DOXYGEN_GENERATING_OUTPUT + static constexpr T Pi; /**< @brief Pi */ + static constexpr T Sqrt2; /**< @brief Square root of 2 */ + static constexpr T Sqrt3; /**< @brief Square root of 3 */ + #endif +}; + +#ifndef DOXYGEN_GENERATING_OUTPUT +template<> struct Constants { + static constexpr double Pi = 3.14159265359; + static constexpr double Sqrt2 = 1.41421356237; + static constexpr double Sqrt3 = 1.73205080757; +}; +template<> struct Constants { + static constexpr float Pi = 3.14159265359f; + static constexpr float Sqrt2 = 1.41421356237f; + static constexpr float Sqrt3 = 1.73205080757f; +}; +#endif /** * @brief Integral power @@ -61,7 +80,7 @@ size_t MAGNUM_EXPORT log(size_t base, size_t number); * Function to make angle entering less error-prone. Converts the value to * radians at compile time. For example `deg(180.0f)` is converted to `3.14f`. */ -template inline constexpr T deg(T value) { return value*PI/180; } +template inline constexpr T deg(T value) { return value*Constants::Pi/180; } /** * @brief Angle in radians diff --git a/src/Math/Test/MathTest.cpp b/src/Math/Test/MathTest.cpp index 27af23668..63080e8a3 100644 --- a/src/Math/Test/MathTest.cpp +++ b/src/Math/Test/MathTest.cpp @@ -24,12 +24,9 @@ QTEST_APPLESS_MAIN(Magnum::Math::Test::MathTest) namespace Magnum { namespace Math { namespace Test { void MathTest::degrad() { - QCOMPARE(deg(90.0), PI/2); - QCOMPARE(deg(90.0f), float(PI/2)); - QCOMPARE(rad(PI/2), PI/2); - - QEXPECT_FAIL(0, "Integral parameter is not converted to floating point", Continue); - QVERIFY(deg(90) == PI/2); + QCOMPARE(deg(90.0), Constants::Pi/2); + QCOMPARE(deg(90.0f), Constants::Pi/2); + QCOMPARE(rad(Constants::Pi/2), Constants::Pi/2); } void MathTest::pow() { diff --git a/src/Primitives/Capsule.cpp b/src/Primitives/Capsule.cpp index 9c60682ec..98534e598 100644 --- a/src/Primitives/Capsule.cpp +++ b/src/Primitives/Capsule.cpp @@ -24,13 +24,13 @@ Capsule::Capsule(unsigned int rings, unsigned int segments, GLfloat length, Text GLfloat height = 2.0f+length; GLfloat textureCoordsVIncrement = 1.0f/(rings*height); - GLfloat ringAngleIncrement = PI/(2*rings); + GLfloat ringAngleIncrement = Math::Constants::Pi/(2*rings); /* Bottom cap vertex */ capVertex(-height/2, -1.0f, 0.0f); /* Rings of bottom hemisphere */ - vertexRings(rings, -length/2, -PI/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement); + vertexRings(rings, -length/2, -Math::Constants::Pi/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement); /* Rings of top hemisphere */ vertexRings(rings, length/2, 0.0f, ringAngleIncrement, (1.0f + length)/height, textureCoordsVIncrement); @@ -53,7 +53,7 @@ void Capsule::capVertex(GLfloat y, GLfloat normalY, GLfloat textureCoordsV) { } void Capsule::vertexRings(unsigned int count, GLfloat centerY, GLfloat startRingAngle, GLfloat ringAngleIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement) { - GLfloat segmentAngleIncrement = 2*PI/segments; + GLfloat segmentAngleIncrement = 2*Math::Constants::Pi/segments; GLfloat x, y, z; for(unsigned int i = 0; i != count; ++i) { GLfloat ringAngle = startRingAngle + i*ringAngleIncrement; diff --git a/src/Primitives/UVSphere.cpp b/src/Primitives/UVSphere.cpp index bd3a25a48..825c758e8 100644 --- a/src/Primitives/UVSphere.cpp +++ b/src/Primitives/UVSphere.cpp @@ -24,13 +24,13 @@ UVSphere::UVSphere(unsigned int rings, unsigned int segments, TextureCoords text CORRADE_ASSERT(rings >= 2 && segments >= 3, "UVSphere must have at least two rings and three segments", ) GLfloat textureCoordsVIncrement = 1.0f/rings; - GLfloat ringAngleIncrement = PI/rings; + GLfloat ringAngleIncrement = Math::Constants::Pi/rings; /* Bottom cap vertex */ capVertex(-1.0f, -1.0f, 0.0f); /* Vertex rings */ - vertexRings(rings-1, 0.0f, -PI/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement); + vertexRings(rings-1, 0.0f, -Math::Constants::Pi/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement); /* Top cap vertex */ capVertex(1.0f, 1.0f, 1.0f);