Browse Source

Useful functions for making angle entering less error-prone.

Both functions convert the value to radians at compile time. For
example deg(180.0f) is converted to 3.14f. Less intuitive than
user-defined literals (C++11 feature), but works everywhere.
pull/279/head
Vladimír Vondruš 15 years ago
parent
commit
3c1f5392e8
  1. 4
      src/Magnum.h
  2. 2
      src/Math/Test/Matrix4Test.cpp
  3. 2
      src/Math/Test/VectorTest.cpp
  4. 15
      src/Math/constants.h
  5. 2
      src/Test/CameraTest.cpp

4
src/Magnum.h

@ -41,6 +41,10 @@ typedef Math::Matrix3<GLfloat> Matrix3;
/** @brief 4x4 floating-point matrix */
typedef Math::Matrix4<GLfloat> Matrix4;
/* Copying angle converters from Math namespace */
using Math::deg;
using Math::rad;
}
#endif

2
src/Math/Test/Matrix4Test.cpp

@ -60,7 +60,7 @@ void Matrix4Test::rotation() {
0.0f, 0.0f, 0.0f, 1.0f
};
QVERIFY(Matrix4::rotation(-74*PI/180.0f, -1.0f, 2.0f, 2.0f) == Matrix4(matrix));
QVERIFY(Matrix4::rotation(deg(-74.0f), -1.0f, 2.0f, 2.0f) == Matrix4(matrix));
}
void Matrix4Test::debug() {

2
src/Math/Test/VectorTest.cpp

@ -124,7 +124,7 @@ void VectorTest::angle() {
float a[] = { 2.0f, 3.0f, 4.0f };
float b[] = { 1.0f, -2.0f, 3.0f };
QCOMPARE(Vector3::angle(a, b), 1.16251f);
QCOMPARE(Vector3::angle(a, b), rad(1.16251f));
}
void VectorTest::negative() {

15
src/Math/constants.h

@ -27,6 +27,21 @@ namespace Magnum { namespace Math {
/** @brief Maximal tolerance when comparing floats */
#define EPSILON 1.0e-6
/**
* @brief Angle in degrees
*
* Function to make angle entering less error-prone. Converts the value to
* radians at compile time. For example @c deg(180.0f) is converted to @c 3.14f.
*/
template<class T> inline constexpr T deg(T value) { return value*PI/180; }
/**
* @brief Angle in radians
*
* See also deg().
*/
template<class T> inline constexpr T rad(T value) { return value; }
}}
#endif

2
src/Test/CameraTest.cpp

@ -40,7 +40,7 @@ void CameraTest::orthographic() {
void CameraTest::perspective() {
Camera camera;
camera.setPerspective(27*PI/180, 32.0f, 100);
camera.setPerspective(deg(27.0f), 32.0f, 100);
GLfloat a[] = {
4.1652994f, 0, 0, 0,

Loading…
Cancel
Save