Browse Source

Added templated Math::Constants structure to replace PI #define.

And added sqrt(2) and sqrt(3) constants while we are at it.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
f0d88fbf9f
  1. 25
      src/Math/Math.h
  2. 9
      src/Math/Test/MathTest.cpp
  3. 6
      src/Primitives/Capsule.cpp
  4. 4
      src/Primitives/UVSphere.cpp

25
src/Math/Math.h

@ -32,8 +32,27 @@ namespace Magnum {
*/
namespace Math {
/** @brief Pi */
#define PI 3.1415926535
/** @brief Numeric constants */
template<class T> 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<double> {
static constexpr double Pi = 3.14159265359;
static constexpr double Sqrt2 = 1.41421356237;
static constexpr double Sqrt3 = 1.73205080757;
};
template<> struct Constants<float> {
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<class T> inline constexpr T deg(T value) { return value*PI/180; }
template<class T> inline constexpr T deg(T value) { return value*Constants<T>::Pi/180; }
/**
* @brief Angle in radians

9
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<double>::Pi/2);
QCOMPARE(deg(90.0f), Constants<float>::Pi/2);
QCOMPARE(rad(Constants<double>::Pi/2), Constants<double>::Pi/2);
}
void MathTest::pow() {

6
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<GLfloat>::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<GLfloat>::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<GLfloat>::Pi/segments;
GLfloat x, y, z;
for(unsigned int i = 0; i != count; ++i) {
GLfloat ringAngle = startRingAngle + i*ringAngleIncrement;

4
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<GLfloat>::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<GLfloat>::Pi/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement);
/* Top cap vertex */
capVertex(1.0f, 1.0f, 1.0f);

Loading…
Cancel
Save