mirror of https://github.com/mosra/magnum.git
Browse Source
Magnum.h now doesn't include anything except OpenGL headers, thus changes in Math library don't trigger recompilation of everything, but only of things really depending on it. Math constants moved to separate file for similar reasons, de-inlined some functions to remove the need for some #includes.vectorfields
84 changed files with 363 additions and 99 deletions
@ -0,0 +1,79 @@
|
||||
#ifndef Magnum_Math_Constants_h |
||||
#define Magnum_Math_Constants_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class Magnum::Math::Constants, functions Magnum::Math::deg(), Magnum::Math::rad() |
||||
*/ |
||||
|
||||
namespace Magnum { namespace Math { |
||||
|
||||
/**
|
||||
@brief Numeric constants |
||||
|
||||
@internal See MathTypeTraits class for implementation notes. |
||||
*/ |
||||
template<class T> struct Constants { |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
/**
|
||||
* @brief Pi |
||||
* |
||||
* @see deg(), rad() |
||||
*/ |
||||
static inline constexpr T pi(); |
||||
|
||||
static inline constexpr T sqrt2(); /**< @brief Square root of 2 */ |
||||
static inline constexpr T sqrt3(); /**< @brief Square root of 3 */ |
||||
#endif |
||||
}; |
||||
|
||||
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||
template<> struct Constants<double> { |
||||
static inline constexpr double pi() { return 3.141592653589793; } |
||||
static inline constexpr double sqrt2() { return 1.414213562373095; } |
||||
static inline constexpr double sqrt3() { return 1.732050807568877; } |
||||
}; |
||||
template<> struct Constants<float> { |
||||
static inline constexpr float pi() { return 3.141592654f; } |
||||
static inline constexpr float sqrt2() { return 1.414213562f; } |
||||
static inline constexpr float sqrt3() { return 1.732050808f; } |
||||
}; |
||||
#endif |
||||
|
||||
/**
|
||||
@brief Angle in degrees |
||||
|
||||
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`. |
||||
|
||||
Usable for entering e.g. rotation: |
||||
@code |
||||
Matrix4::rotation(deg(30.0f), Vector3::yAxis()); |
||||
@endcode |
||||
@see Constants, rad() |
||||
*/ |
||||
template<class T> inline constexpr T deg(T value) { return value*Constants<T>::pi()/180; } |
||||
|
||||
/**
|
||||
* @brief Angle in radians |
||||
* |
||||
* See deg() for more information. |
||||
*/ |
||||
template<class T> inline constexpr T rad(T value) { return value; } |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "ConstantsTest.h" |
||||
|
||||
#include "Constants.h" |
||||
#include "Math.h" |
||||
|
||||
using namespace std; |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Math::Test::ConstantsTest) |
||||
|
||||
namespace Magnum { namespace Math { namespace Test { |
||||
|
||||
ConstantsTest::ConstantsTest() { |
||||
addTests(&ConstantsTest::constants, |
||||
&ConstantsTest::degrad); |
||||
} |
||||
|
||||
void ConstantsTest::constants() { |
||||
CORRADE_COMPARE(Math::pow<2>(Constants<float>::sqrt2()), 2.0f); |
||||
CORRADE_COMPARE(Math::pow<2>(Constants<float>::sqrt3()), 3.0f); |
||||
|
||||
CORRADE_COMPARE(Math::pow<2>(Constants<double>::sqrt2()), 2.0); |
||||
CORRADE_COMPARE(Math::pow<2>(Constants<double>::sqrt3()), 3.0); |
||||
} |
||||
|
||||
void ConstantsTest::degrad() { |
||||
CORRADE_COMPARE(deg(90.0), Constants<double>::pi()/2); |
||||
CORRADE_COMPARE(deg(90.0f), Constants<float>::pi()/2); |
||||
CORRADE_COMPARE(rad(Constants<double>::pi()/2), Constants<double>::pi()/2); |
||||
} |
||||
|
||||
}}} |
||||
@ -0,0 +1,32 @@
|
||||
#ifndef Magnum_Math_Test_ConstantsTest_h |
||||
#define Magnum_Math_Test_ConstantsTest_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include <TestSuite/Tester.h> |
||||
|
||||
namespace Magnum { namespace Math { namespace Test { |
||||
|
||||
class ConstantsTest: public Corrade::TestSuite::Tester<ConstantsTest> { |
||||
public: |
||||
ConstantsTest(); |
||||
|
||||
void constants(); |
||||
void degrad(); |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue