From 5513eba398fa66a371297d55fc528a9d6a896446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 15 Mar 2022 20:35:09 +0100 Subject: [PATCH] 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. --- src/Magnum/Math/Angle.h | 4 ++++ src/Magnum/Math/RectangularMatrix.h | 9 +++++--- src/Magnum/Math/Test/AngleTest.cpp | 22 ++++++++++++++++++- .../Math/Test/RectangularMatrixTest.cpp | 20 ++++++++++++++++- src/Magnum/Math/Test/VectorTest.cpp | 11 +++++++++- src/Magnum/Math/Vector.h | 7 ++++-- 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/Magnum/Math/Angle.h b/src/Magnum/Math/Angle.h index 4df4957b2..6c4b78f29 100644 --- a/src/Magnum/Math/Angle.h +++ b/src/Magnum/Math/Angle.h @@ -231,11 +231,15 @@ template constexpr Rad::Rad(Unit value): Unit( #ifndef CORRADE_NO_DEBUG /** @debugoperator{Rad} */ template Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Unit& 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 Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Unit& 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 << ")"; } diff --git a/src/Magnum/Math/RectangularMatrix.h b/src/Magnum/Math/RectangularMatrix.h index eb6418980..59ad03094 100644 --- a/src/Magnum/Math/RectangularMatrix.h +++ b/src/Magnum/Math/RectangularMatrix.h @@ -660,15 +660,18 @@ template inline RectangularMatrix Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Magnum::Math::RectangularMatrix& 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 */ diff --git a/src/Magnum/Math/Test/AngleTest.cpp b/src/Magnum/Math/Test/AngleTest.cpp index b27b95d19..29ea0b4fa 100644 --- a/src/Magnum/Math/Test/AngleTest.cpp +++ b/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 void tweakable(); template 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({ @@ -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 void AngleTest::tweakable() { auto&& data = TweakableData[testCaseInstanceId()]; diff --git a/src/Magnum/Math/Test/RectangularMatrixTest.cpp b/src/Magnum/Math/Test/RectangularMatrixTest.cpp index 811dd90a0..9265cc556 100644 --- a/src/Magnum/Math/Test/RectangularMatrixTest.cpp +++ b/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) diff --git a/src/Magnum/Math/Test/VectorTest.cpp b/src/Magnum/Math/Test/VectorTest.cpp index 0a970cb16..bf3b479d8 100644 --- a/src/Magnum/Math/Test/VectorTest.cpp +++ b/src/Magnum/Math/Test/VectorTest.cpp @@ -118,6 +118,7 @@ struct VectorTest: Corrade::TestSuite::Tester { void strictWeakOrdering(); void debug(); + void debugPacked(); }; typedef Math::Constants 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) diff --git a/src/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index 9f48a2d38..57d3ec4e6 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -1231,12 +1231,15 @@ operator/(const Vector& a, const Vector& b) #ifndef CORRADE_NO_DEBUG /** @debugoperator{Vector} */ template Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Vector& 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 */