Browse Source

Support for printing Matrix and Vector to debug output.

Added tests for Matrix3, Vector2 just to be sure that debug output works
also there.
pull/279/head
Vladimír Vondruš 15 years ago
parent
commit
8555475807
  1. 15
      src/Math/Matrix.h
  2. 6
      src/Math/Matrix3.h
  3. 6
      src/Math/Matrix4.h
  4. 9
      src/Math/Test/CMakeLists.txt
  5. 46
      src/Math/Test/Matrix3Test.cpp
  6. 31
      src/Math/Test/Matrix3Test.h
  7. 20
      src/Math/Test/Matrix4Test.cpp
  8. 2
      src/Math/Test/Matrix4Test.h
  9. 20
      src/Math/Test/MatrixTest.cpp
  10. 2
      src/Math/Test/MatrixTest.h
  11. 38
      src/Math/Test/Vector2Test.cpp
  12. 31
      src/Math/Test/Vector2Test.h
  13. 10
      src/Math/Test/Vector3Test.cpp
  14. 2
      src/Math/Test/Vector3Test.h
  15. 10
      src/Math/Test/Vector4Test.cpp
  16. 2
      src/Math/Test/Vector4Test.h
  17. 10
      src/Math/Test/VectorTest.cpp
  18. 2
      src/Math/Test/VectorTest.h
  19. 13
      src/Math/Vector.h
  20. 6
      src/Math/Vector2.h
  21. 6
      src/Math/Vector3.h
  22. 6
      src/Math/Vector4.h

15
src/Math/Matrix.h

@ -231,6 +231,21 @@ template<class T> class Matrix<T, 2> {
}; };
#endif #endif
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T, size_t size> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Matrix<T, size>& value) {
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false);
debug << "Matrix(";
for(size_t row = 0; row != size; ++row) {
if(row != 0) debug << ",\n ";
for(size_t col = 0; col != size; ++col) {
if(col != 0) debug << ", ";
debug << value.at(row, col);
}
}
return debug << ')';
}
#endif
}} }}
#endif #endif

6
src/Math/Matrix3.h

@ -58,6 +58,12 @@ template<class T> class Matrix3: public Matrix<T, 3> {
inline Matrix3<T> inverse() const { return Matrix<T, 3>::inverse(); } inline Matrix3<T> inverse() const { return Matrix<T, 3>::inverse(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Matrix3<T>& value) {
return debug << static_cast<const Magnum::Math::Matrix<T, 3>&>(value);
}
#endif
}} }}
#endif #endif

6
src/Math/Matrix4.h

@ -161,6 +161,12 @@ template<class T> class Matrix4: public Matrix<T, 4> {
inline Matrix4<T> inverse() const { return Matrix<T, 4>::inverse(); } inline Matrix4<T> inverse() const { return Matrix<T, 4>::inverse(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Matrix4<T>& value) {
return debug << static_cast<const Magnum::Math::Matrix<T, 4>&>(value);
}
#endif
}} }}
#endif #endif

9
src/Math/Test/CMakeLists.txt

@ -1,8 +1,17 @@
corrade_add_test(VectorTest VectorTest.h VectorTest.cpp) corrade_add_test(VectorTest VectorTest.h VectorTest.cpp)
target_link_libraries(VectorTest ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(Vector2Test Vector2Test.h Vector2Test.cpp)
target_link_libraries(Vector2Test ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(Vector3Test Vector3Test.h Vector3Test.cpp) corrade_add_test(Vector3Test Vector3Test.h Vector3Test.cpp)
target_link_libraries(Vector3Test ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(Vector4Test Vector4Test.h Vector4Test.cpp) corrade_add_test(Vector4Test Vector4Test.h Vector4Test.cpp)
target_link_libraries(Vector4Test ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(MatrixTest MatrixTest.h MatrixTest.cpp) corrade_add_test(MatrixTest MatrixTest.h MatrixTest.cpp)
target_link_libraries(MatrixTest ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(Matrix3Test Matrix3Test.h Matrix3Test.cpp)
target_link_libraries(Matrix3Test ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(Matrix4Test Matrix4Test.h Matrix4Test.cpp) corrade_add_test(Matrix4Test Matrix4Test.h Matrix4Test.cpp)
target_link_libraries(Matrix4Test ${CORRADE_UTILITY_LIBRARY})
corrade_add_test(GeometryUtilsTest GeometryUtilsTest.h GeometryUtilsTest.cpp) corrade_add_test(GeometryUtilsTest GeometryUtilsTest.h GeometryUtilsTest.cpp)

46
src/Math/Test/Matrix3Test.cpp

@ -0,0 +1,46 @@
/*
Copyright © 2010, 2011 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 "Matrix3Test.h"
#include <sstream>
#include <QtTest/QTest>
#include "Matrix3.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::Matrix3Test)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test {
typedef Math::Matrix3<float> Matrix3;
void Matrix3Test::debug() {
float m[] = {
3, 5, 8,
4, 4, 7,
7, -1, 8,
};
ostringstream o;
Debug(&o) << Matrix3(m);
QCOMPARE(QString::fromStdString(o.str()), QString("Matrix(3, 4, 7,\n"
" 5, 4, -1,\n"
" 8, 7, 8)\n"));
}
}}}

31
src/Math/Test/Matrix3Test.h

@ -0,0 +1,31 @@
#ifndef Magnum_Math_Test_Matrix3Test_h
#define Magnum_Math_Test_Matrix3Test_h
/*
Copyright © 2010, 2011 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 <QtCore/QObject>
namespace Magnum { namespace Math { namespace Test {
class Matrix3Test: public QObject {
Q_OBJECT
private slots:
void debug();
};
}}}
#endif

20
src/Math/Test/Matrix4Test.cpp

@ -15,6 +15,7 @@
#include "Matrix4Test.h" #include "Matrix4Test.h"
#include <sstream>
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Matrix4.h" #include "Matrix4.h"
@ -22,6 +23,9 @@
QTEST_APPLESS_MAIN(Magnum::Math::Test::Matrix4Test) QTEST_APPLESS_MAIN(Magnum::Math::Test::Matrix4Test)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test { namespace Magnum { namespace Math { namespace Test {
typedef Math::Matrix4<float> Matrix4; typedef Math::Matrix4<float> Matrix4;
@ -59,4 +63,20 @@ void Matrix4Test::rotation() {
QVERIFY(Matrix4::rotation(-74*PI/180.0f, -1.0f, 2.0f, 2.0f) == Matrix4(matrix)); QVERIFY(Matrix4::rotation(-74*PI/180.0f, -1.0f, 2.0f, 2.0f) == Matrix4(matrix));
} }
void Matrix4Test::debug() {
float m[] = {
3, 5, 8, 4,
4, 4, 7, 3,
7, -1, 8, 0,
9, 4, 5, 9
};
ostringstream o;
Debug(&o) << Matrix4(m);
QCOMPARE(QString::fromStdString(o.str()), QString("Matrix(3, 4, 7, 9,\n"
" 5, 4, -1, 4,\n"
" 8, 7, 8, 5,\n"
" 4, 3, 0, 9)\n"));
}
}}} }}}

2
src/Math/Test/Matrix4Test.h

@ -26,6 +26,8 @@ class Matrix4Test: public QObject {
void translation(); void translation();
void scaling(); void scaling();
void rotation(); void rotation();
void debug();
}; };
}}} }}}

20
src/Math/Test/MatrixTest.cpp

@ -15,12 +15,16 @@
#include "MatrixTest.h" #include "MatrixTest.h"
#include <sstream>
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Matrix.h" #include "Matrix.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::MatrixTest) QTEST_APPLESS_MAIN(Magnum::Math::Test::MatrixTest)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test { namespace Magnum { namespace Math { namespace Test {
typedef Matrix<float, 4> Matrix4; typedef Matrix<float, 4> Matrix4;
@ -229,4 +233,20 @@ void MatrixTest::inverse() {
QVERIFY(_inverse*Matrix4(m) == Matrix4()); QVERIFY(_inverse*Matrix4(m) == Matrix4());
} }
void MatrixTest::debug() {
float m[] = {
3, 5, 8, 4,
4, 4, 7, 3,
7, -1, 8, 0,
9, 4, 5, 9
};
ostringstream o;
Debug(&o) << Matrix4(m);
QCOMPARE(QString::fromStdString(o.str()), QString("Matrix(3, 4, 7, 9,\n"
" 5, 4, -1, 4,\n"
" 8, 7, 8, 5,\n"
" 4, 3, 0, 9)\n"));
}
}}} }}}

2
src/Math/Test/MatrixTest.h

@ -34,6 +34,8 @@ class MatrixTest: public QObject {
void ij(); void ij();
void determinant(); void determinant();
void inverse(); void inverse();
void debug();
}; };
}}} }}}

38
src/Math/Test/Vector2Test.cpp

@ -0,0 +1,38 @@
/*
Copyright © 2010, 2011 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 "Vector2Test.h"
#include <sstream>
#include <QtTest/QTest>
#include "Vector2.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::Vector2Test)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test {
typedef Math::Vector2<float> Vector2;
void Vector2Test::debug() {
ostringstream o;
Debug(&o) << Vector2(0.5f, 15.0f);
QCOMPARE(QString::fromStdString(o.str()), QString("Vector(0.5, 15)\n"));
}
}}}

31
src/Math/Test/Vector2Test.h

@ -0,0 +1,31 @@
#ifndef Magnum_Math_Test_Vector2Test_h
#define Magnum_Math_Test_Vector2Test_h
/*
Copyright © 2010, 2011 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 <QtCore/QObject>
namespace Magnum { namespace Math { namespace Test {
class Vector2Test: public QObject {
Q_OBJECT
private slots:
void debug();
};
}}}
#endif

10
src/Math/Test/Vector3Test.cpp

@ -15,12 +15,16 @@
#include "Vector3Test.h" #include "Vector3Test.h"
#include <sstream>
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Vector3.h" #include "Vector3.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::Vector3Test) QTEST_APPLESS_MAIN(Magnum::Math::Test::Vector3Test)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test { namespace Magnum { namespace Math { namespace Test {
typedef Math::Vector3<float> Vector3; typedef Math::Vector3<float> Vector3;
@ -32,4 +36,10 @@ void Vector3Test::cross() {
QVERIFY(Vector3::cross(a, b) == Vector3(-10, -3, 7)); QVERIFY(Vector3::cross(a, b) == Vector3(-10, -3, 7));
} }
void Vector3Test::debug() {
ostringstream o;
Debug(&o) << Vector3(0.5f, 15.0f, 1.0f);
QCOMPARE(QString::fromStdString(o.str()), QString("Vector(0.5, 15, 1)\n"));
}
}}} }}}

2
src/Math/Test/Vector3Test.h

@ -24,6 +24,8 @@ class Vector3Test: public QObject {
private slots: private slots:
void cross(); void cross();
void debug();
}; };
}}} }}}

10
src/Math/Test/Vector4Test.cpp

@ -15,12 +15,16 @@
#include "Vector4Test.h" #include "Vector4Test.h"
#include <sstream>
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Vector4.h" #include "Vector4.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::Vector4Test) QTEST_APPLESS_MAIN(Magnum::Math::Test::Vector4Test)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test { namespace Magnum { namespace Math { namespace Test {
typedef Math::Vector4<float> Vector4; typedef Math::Vector4<float> Vector4;
@ -34,4 +38,10 @@ void Vector4Test::threeComponent() {
QVERIFY(Vector4(1.0f, 2.0f, 3.0f, 4.0f).xyz() == Vector3(1.0f, 2.0f, 3.0f)); QVERIFY(Vector4(1.0f, 2.0f, 3.0f, 4.0f).xyz() == Vector3(1.0f, 2.0f, 3.0f));
} }
void Vector4Test::debug() {
ostringstream o;
Debug(&o) << Vector4(0.5f, 15.0f, 1.0f, 1.0f);
QCOMPARE(QString::fromStdString(o.str()), QString("Vector(0.5, 15, 1, 1)\n"));
}
}}} }}}

2
src/Math/Test/Vector4Test.h

@ -25,6 +25,8 @@ class Vector4Test: public QObject {
private slots: private slots:
void construct(); void construct();
void threeComponent(); void threeComponent();
void debug();
}; };
}}} }}}

10
src/Math/Test/VectorTest.cpp

@ -15,6 +15,7 @@
#include "VectorTest.h" #include "VectorTest.h"
#include <sstream>
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Vector.h" #include "Vector.h"
@ -22,6 +23,7 @@
QTEST_APPLESS_MAIN(Magnum::Math::Test::VectorTest) QTEST_APPLESS_MAIN(Magnum::Math::Test::VectorTest)
using namespace std; using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Math { namespace Test { namespace Magnum { namespace Math { namespace Test {
@ -132,4 +134,12 @@ void VectorTest::negative() {
QVERIFY(-Vector4(vec) == negative); QVERIFY(-Vector4(vec) == negative);
} }
void VectorTest::debug() {
float vec[] = { 0.5f, 15.0f, 1.0f, 1.0f };
ostringstream o;
Debug(&o) << Vector4(vec);
QCOMPARE(QString::fromStdString(o.str()), QString("Vector(0.5, 15, 1, 1)\n"));
}
}}} }}}

2
src/Math/Test/VectorTest.h

@ -34,6 +34,8 @@ class VectorTest: public QObject {
void normalized(); void normalized();
void angle(); void angle();
void negative(); void negative();
void debug();
}; };
}}} }}}

13
src/Math/Vector.h

@ -22,6 +22,7 @@
#include <cstring> #include <cstring>
#include <cmath> #include <cmath>
#include "Utility/Debug.h"
#include "constants.h" #include "constants.h"
namespace Magnum { namespace Math { namespace Magnum { namespace Math {
@ -176,6 +177,18 @@ template<class T, size_t size> class Vector {
T _data[size]; T _data[size];
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T, size_t size> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector<T, size>& value) {
debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false);
debug << "Vector(";
for(size_t i = 0; i != size; ++i) {
if(i != 0) debug << ", ";
debug << value.at(i);
}
return debug << ')';
}
#endif
}} }}
#endif #endif

6
src/Math/Vector2.h

@ -75,6 +75,12 @@ template<class T> class Vector2: public Vector<T, 2> {
inline Vector2<T> normalized() const { return Vector<T, 2>::normalized(); } inline Vector2<T> normalized() const { return Vector<T, 2>::normalized(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector2<T>& value) {
return debug << static_cast<const Magnum::Math::Vector<T, 2>&>(value);
}
#endif
}} }}
#endif #endif

6
src/Math/Vector3.h

@ -102,6 +102,12 @@ template<class T> class Vector3: public Vector<T, 3> {
inline Vector3<T> normalized() const { return Vector<T, 3>::normalized(); } inline Vector3<T> normalized() const { return Vector<T, 3>::normalized(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector3<T>& value) {
return debug << static_cast<const Magnum::Math::Vector<T, 3>&>(value);
}
#endif
}} }}
#endif #endif

6
src/Math/Vector4.h

@ -118,6 +118,12 @@ template<class T> class Vector4: public Vector<T, 4> {
inline Vector4<T> normalized() const { return Vector<T, 4>::normalized(); } inline Vector4<T> normalized() const { return Vector<T, 4>::normalized(); }
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Vector4<T>& value) {
return debug << static_cast<const Magnum::Math::Vector<T, 4>&>(value);
}
#endif
}} }}
#endif #endif

Loading…
Cancel
Save