Browse Source

Math: packed debug output for angles, matrices and vectors.

Not documenting this publicly as it has a certain smell (needs better
naming, mainly), but I need this for a less verbose output in certain
tools.
pull/556/head
Vladimír Vondruš 4 years ago
parent
commit
5513eba398
  1. 4
      src/Magnum/Math/Angle.h
  2. 9
      src/Magnum/Math/RectangularMatrix.h
  3. 22
      src/Magnum/Math/Test/AngleTest.cpp
  4. 20
      src/Magnum/Math/Test/RectangularMatrixTest.cpp
  5. 11
      src/Magnum/Math/Test/VectorTest.cpp
  6. 7
      src/Magnum/Math/Vector.h

4
src/Magnum/Math/Angle.h

@ -231,11 +231,15 @@ template<class T> constexpr Rad<T>::Rad(Unit<Deg, T> value): Unit<Math::Rad, T>(
#ifndef CORRADE_NO_DEBUG
/** @debugoperator{Rad} */
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Unit<Rad, T>& value) {
if(debug.immediateFlags() >= Corrade::Utility::Debug::Flag::Packed)
return debug << T(value);
return debug << "Rad(" << Corrade::Utility::Debug::nospace << T(value) << Corrade::Utility::Debug::nospace << ")";
}
/** @debugoperator{Deg} */
template<class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Unit<Deg, T>& value) {
if(debug.immediateFlags() >= Corrade::Utility::Debug::Flag::Packed)
return debug << T(value);
return debug << "Deg(" << Corrade::Utility::Debug::nospace << T(value) << Corrade::Utility::Debug::nospace << ")";
}

9
src/Magnum/Math/RectangularMatrix.h

@ -660,15 +660,18 @@ template<std::size_t size, std::size_t cols, class T> inline RectangularMatrix<c
#ifndef CORRADE_NO_DEBUG
/** @debugoperator{RectangularMatrix} */
template<std::size_t cols, std::size_t rows, class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Magnum::Math::RectangularMatrix<cols, rows, T>& value) {
debug << "Matrix(" << Corrade::Utility::Debug::nospace;
/** @todo might make sense to propagate the flags also, for hex value
printing etc */
const bool packed = debug.immediateFlags() >= Corrade::Utility::Debug::Flag::Packed;
debug << (packed ? "{" : "Matrix(") << Corrade::Utility::Debug::nospace;
for(std::size_t row = 0; row != rows; ++row) {
if(row != 0) debug << Corrade::Utility::Debug::nospace << ",\n ";
if(row != 0) debug << Corrade::Utility::Debug::nospace << (packed ? ",\n" : ",\n ");
for(std::size_t col = 0; col != cols; ++col) {
if(col != 0) debug << Corrade::Utility::Debug::nospace << ",";
debug << value[col][row];
}
}
return debug << Corrade::Utility::Debug::nospace << ")";
return debug << Corrade::Utility::Debug::nospace << (packed ? "}" : ")");
}
/* Explicit instantiation for commonly used types */

22
src/Magnum/Math/Test/AngleTest.cpp

@ -52,7 +52,9 @@ struct AngleTest: Corrade::TestSuite::Tester {
void conversion();
void debugDeg();
void debugDegPacked();
void debugRad();
void debugRadPacked();
#if defined(DOXYGEN_GENERATING_OUTPUT) || defined(CORRADE_TARGET_UNIX) || (defined(CORRADE_TARGET_WINDOWS) && !defined(CORRADE_TARGET_WINDOWS_RT)) || defined(CORRADE_TARGET_EMSCRIPTEN)
template<class T> void tweakable();
template<class T> void tweakableError();
@ -125,7 +127,9 @@ AngleTest::AngleTest() {
&AngleTest::conversion,
&AngleTest::debugDeg,
&AngleTest::debugRad});
&AngleTest::debugDegPacked,
&AngleTest::debugRad,
&AngleTest::debugRadPacked});
#if defined(DOXYGEN_GENERATING_OUTPUT) || defined(CORRADE_TARGET_UNIX) || (defined(CORRADE_TARGET_WINDOWS) && !defined(CORRADE_TARGET_WINDOWS_RT)) || defined(CORRADE_TARGET_EMSCRIPTEN)
addInstancedTests<AngleTest>({
@ -282,6 +286,14 @@ void AngleTest::debugDeg() {
CORRADE_COMPARE(o.str(), "Deg(22)\n");
}
void AngleTest::debugDegPacked() {
std::ostringstream out;
/* Second is not packed, the first should not make any flags persistent */
Debug{&out} << Debug::packed << 90.0_degf << 45.0_degf;
CORRADE_COMPARE(out.str(), "90 Deg(45)\n");
}
void AngleTest::debugRad() {
std::ostringstream o;
@ -294,6 +306,14 @@ void AngleTest::debugRad() {
CORRADE_COMPARE(o.str(), "Rad(-1.5708)\n");
}
void AngleTest::debugRadPacked() {
std::ostringstream out;
/* Second is not packed, the first should not make any flags persistent */
Debug{&out} << Debug::packed << 1.5708_radf << 3.1416_radf;
CORRADE_COMPARE(out.str(), "1.5708 Rad(3.1416)\n");
}
#if defined(DOXYGEN_GENERATING_OUTPUT) || defined(CORRADE_TARGET_UNIX) || (defined(CORRADE_TARGET_WINDOWS) && !defined(CORRADE_TARGET_WINDOWS_RT)) || defined(CORRADE_TARGET_EMSCRIPTEN)
template<class T> void AngleTest::tweakable() {
auto&& data = TweakableData[testCaseInstanceId()];

20
src/Magnum/Math/Test/RectangularMatrixTest.cpp

@ -97,6 +97,7 @@ struct RectangularMatrixTest: Corrade::TestSuite::Tester {
void strictWeakOrdering();
void debug();
void debugPacked();
};
typedef RectangularMatrix<4, 3, Float> Matrix4x3;
@ -156,7 +157,8 @@ RectangularMatrixTest::RectangularMatrixTest() {
&RectangularMatrixTest::strictWeakOrdering,
&RectangularMatrixTest::debug});
&RectangularMatrixTest::debug,
&RectangularMatrixTest::debugPacked});
}
void RectangularMatrixTest::construct() {
@ -809,6 +811,22 @@ void RectangularMatrixTest::debug() {
" 0, 0, 0, 0)\n");
}
void RectangularMatrixTest::debugPacked() {
Matrix3x4 m(Vector4(3.0f, 5.0f, 8.0f, 4.0f),
Vector4(4.0f, 4.0f, 7.0f, 3.0f),
Vector4(7.0f, -1.0f, 8.0f, 0.0f));
std::ostringstream out;
/* Second is not packed, the first should not make any flags persistent */
Debug{&out} << Debug::packed << m << Matrix2x2{};
CORRADE_COMPARE(out.str(),
"{3, 4, 7,\n"
" 5, 4, -1,\n"
" 8, 7, 8,\n"
" 4, 3, 0} Matrix(0, 0,\n"
" 0, 0)\n");
}
}}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::RectangularMatrixTest)

11
src/Magnum/Math/Test/VectorTest.cpp

@ -118,6 +118,7 @@ struct VectorTest: Corrade::TestSuite::Tester {
void strictWeakOrdering();
void debug();
void debugPacked();
};
typedef Math::Constants<Float> Constants;
@ -191,7 +192,8 @@ VectorTest::VectorTest() {
&VectorTest::strictWeakOrdering,
&VectorTest::debug});
&VectorTest::debug,
&VectorTest::debugPacked});
}
void VectorTest::construct() {
@ -841,6 +843,13 @@ void VectorTest::debug() {
CORRADE_COMPARE(o.str(), "a Vector(0, 0, 0, 0) b Vector(0, 0, 0, 0)\n");
}
void VectorTest::debugPacked() {
std::ostringstream out;
/* Second is not packed, the first should not make any flags persistent */
Debug{&out} << Debug::packed << Vector4(0.5f, 15.0f, 1.0f, 1.0f) << Vector4();
CORRADE_COMPARE(out.str(), "{0.5, 15, 1, 1} Vector(0, 0, 0, 0)\n");
}
}}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::VectorTest)

7
src/Magnum/Math/Vector.h

@ -1231,12 +1231,15 @@ operator/(const Vector<size, Integral>& a, const Vector<size, FloatingPoint>& b)
#ifndef CORRADE_NO_DEBUG
/** @debugoperator{Vector} */
template<std::size_t size, class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Vector<size, T>& value) {
debug << "Vector(" << Corrade::Utility::Debug::nospace;
/** @todo might make sense to propagate the flags also, for hex value
printing etc */
const bool packed = debug.immediateFlags() >= Corrade::Utility::Debug::Flag::Packed;
debug << (packed ? "{" : "Vector(") << Corrade::Utility::Debug::nospace;
for(std::size_t i = 0; i != size; ++i) {
if(i != 0) debug << Corrade::Utility::Debug::nospace << ",";
debug << value[i];
}
return debug << Corrade::Utility::Debug::nospace << ")";
return debug << Corrade::Utility::Debug::nospace << (packed ? "}" : ")");
}
/* Explicit instantiation for commonly used types */

Loading…
Cancel
Save