From 0e74c3847dcc0f18072c48d3cf6381cc4df696a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 20 Aug 2012 18:04:10 +0200 Subject: [PATCH] Support for storing matrices and vectors in configuration. --- Doxyfile | 2 ++ doc/coding-style.dox | 4 +-- src/Color.h | 8 ++++++ src/Math/Matrix.h | 46 ++++++++++++++++++++++++++++++++--- src/Math/Matrix3.h | 6 +++++ src/Math/Matrix4.h | 6 +++++ src/Math/Test/Matrix3Test.cpp | 14 ++++++++++- src/Math/Test/Matrix3Test.h | 2 ++ src/Math/Test/Matrix4Test.cpp | 15 +++++++++++- src/Math/Test/Matrix4Test.h | 1 + src/Math/Test/MatrixTest.cpp | 15 +++++++++++- src/Math/Test/MatrixTest.h | 1 + src/Math/Test/Vector2Test.cpp | 10 +++++++- src/Math/Test/Vector2Test.h | 2 ++ src/Math/Test/Vector3Test.cpp | 10 +++++++- src/Math/Test/Vector3Test.h | 1 + src/Math/Test/Vector4Test.cpp | 10 +++++++- src/Math/Test/Vector4Test.h | 1 + src/Math/Test/VectorTest.cpp | 10 +++++++- src/Math/Test/VectorTest.h | 1 + src/Math/Vector.h | 42 ++++++++++++++++++++++++++++++-- src/Math/Vector2.h | 11 ++++++++- src/Math/Vector3.h | 11 ++++++++- src/Math/Vector4.h | 11 ++++++++- src/Test/ColorTest.cpp | 15 +++++++++++- src/Test/ColorTest.h | 1 + 26 files changed, 237 insertions(+), 19 deletions(-) diff --git a/Doxyfile b/Doxyfile index 8c6c050e4..a9b8a684f 100644 --- a/Doxyfile +++ b/Doxyfile @@ -196,6 +196,8 @@ TAB_SIZE = 8 ALIASES = \ "debugoperator{1}=@relates \1\n@brief Debug output operator @xrefitem debugoperators \"Debug output operator\" \"Debug output operators for custom types\" Allows printing \1 with Corrade::Utility::Debug and friends." \ + "configurationvalueref{1}=@see @ref configurationvalues \"Corrade::Utility::ConfigurationValue<\1>\"" \ + "configurationvalue{1}=@brief %Configuration value parser and writer @xrefitem configurationvalues \"Configuration value parser and writer\" \"Configuration value parsers and writers for custom types\" Allows parsing and writing \1 from and to Corrade::Utility::Configuration." \ "collisionoperator{2}=@relates \1\n@brief Collision of %\1 and %\2\n@see \2::operator%(const \1&) const" \ "todoc=@xrefitem todoc \"Documentation todo\" \"Documentation-related todo list\"" \ "requires_gl=@xrefitem requires-gl \"Requires desktop OpenGL\" \"Functionality requiring desktop OpenGL (not available on OpenGL ES)\" Not available on OpenGL ES." \ diff --git a/doc/coding-style.dox b/doc/coding-style.dox index acab6dc81..09fbdcb80 100644 --- a/doc/coding-style.dox +++ b/doc/coding-style.dox @@ -33,8 +33,8 @@ removing redundant prefixes) is encouraged. @subsection documentation-commands Special documentation commands -Additionally to @c \@todoc and @c \@debugoperator (same as in Corrade), these -are defined: +Additionally to @c \@todoc, @c \@debugoperator @c \@configurationvalue and +@c \@configurationvalueref (same as in Corrade), these are defined: @subsubsection documentation-commands-collisionoperator Physics collision operators diff --git a/src/Color.h b/src/Color.h index 626e3adb8..c95e010a3 100644 --- a/src/Color.h +++ b/src/Color.h @@ -408,4 +408,12 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb } +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Color3} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; + + /** @configurationvalue{Magnum::Color4} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + #endif diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 4cb2a60c7..263185396 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -30,10 +30,11 @@ namespace Implementation { #endif /** - * @brief %Matrix - * - * @todo @c PERFORMANCE - loop unrolling for Matrix<3, T> and Matrix<4, T> - * @todo first col, then row (cache adjacency) +@brief %Matrix + +@configurationvalueref{Magnum::Math::Matrix} +@todo @c PERFORMANCE - loop unrolling for Matrix<3, T> and Matrix<4, T> +@todo first col, then row (cache adjacency) */ template class Matrix { static_assert(size != 0, "Matrix cannot have zero elements"); @@ -355,4 +356,41 @@ template class MatrixDeterminant<1, T> { }} +namespace Corrade { namespace Utility { + +/** @configurationvalue{Magnum::Math::Matrix} */ +template struct ConfigurationValue> { + /** @brief Writes elements separated with spaces */ + static std::string toString(const Magnum::Math::Matrix& value, int flags = 0) { + std::string output; + + for(size_t row = 0; row != size; ++row) { + for(size_t col = 0; col != size; ++col) { + if(!output.empty()) output += ' '; + output += ConfigurationValue::toString(value(col, row), flags); + } + } + + return output; + } + + /** @brief Reads elements separated with whitespace */ + static Magnum::Math::Matrix fromString(const std::string& stringValue, int flags = 0) { + Magnum::Math::Matrix result(Magnum::Math::Matrix::Zero); + std::istringstream in(stringValue); + + for(size_t row = 0; row != size; ++row) { + for(size_t col = 0; col != size; ++col) { + std::string num; + in >> num; + result(col, row) = ConfigurationValue::fromString(num, flags); + } + } + + return result; + } +}; + +}} + #endif diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index 35b121f6d..1cf5640b8 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -29,6 +29,7 @@ namespace Magnum { namespace Math { Provides functions for transformations in 2D. See also Matrix4 for 3D transformations. +@configurationvalueref{Magnum::Math::Matrix3} */ template class Matrix3: public Matrix<3, T> { public: @@ -104,4 +105,9 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb }} +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Math::Matrix3} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + #endif diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index a7e8912a7..c4b0f3935 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -29,6 +29,7 @@ namespace Magnum { namespace Math { Provides functions for transformations in 3D. See also Matrix3 for 2D transformations. +@configurationvalueref{Magnum::Math::Matrix4} @todo Shearing @todo Reflection */ @@ -153,4 +154,9 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb }} +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Math::Matrix4} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + #endif diff --git a/src/Math/Test/Matrix3Test.cpp b/src/Math/Test/Matrix3Test.cpp index dc01cb2f2..840e75c92 100644 --- a/src/Math/Test/Matrix3Test.cpp +++ b/src/Math/Test/Matrix3Test.cpp @@ -34,7 +34,8 @@ Matrix3Test::Matrix3Test() { &Matrix3Test::translation, &Matrix3Test::scaling, &Matrix3Test::rotation, - &Matrix3Test::debug); + &Matrix3Test::debug, + &Matrix3Test::configuration); } void Matrix3Test::constructIdentity() { @@ -103,4 +104,15 @@ void Matrix3Test::debug() { " 8, 7, 8)\n"); } +void Matrix3Test::configuration() { + Matrix3 m( + 5.0f, 8.0f, 4.0f, + 4.0f, 7.0f, 3.125f, + 4.0f, 5.0f, 9.55f + ); + string value("5 4 4 8 7 5 4 3.125 9.55"); + CORRADE_COMPARE(ConfigurationValue::toString(m), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), m); +} + }}} diff --git a/src/Math/Test/Matrix3Test.h b/src/Math/Test/Matrix3Test.h index 27544cd0e..2402fbc93 100644 --- a/src/Math/Test/Matrix3Test.h +++ b/src/Math/Test/Matrix3Test.h @@ -28,7 +28,9 @@ class Matrix3Test: public Corrade::TestSuite::Tester { void translation(); void scaling(); void rotation(); + void debug(); + void configuration(); }; }}} diff --git a/src/Math/Test/Matrix4Test.cpp b/src/Math/Test/Matrix4Test.cpp index 0d9b813cb..9cc253cc6 100644 --- a/src/Math/Test/Matrix4Test.cpp +++ b/src/Math/Test/Matrix4Test.cpp @@ -37,7 +37,8 @@ Matrix4Test::Matrix4Test() { &Matrix4Test::rotation, &Matrix4Test::rotationScalingPart, &Matrix4Test::rotationPart, - &Matrix4Test::debug); + &Matrix4Test::debug, + &Matrix4Test::configuration); } void Matrix4Test::constructIdentity() { @@ -144,4 +145,16 @@ void Matrix4Test::debug() { " 4, 3, 0, 9)\n"); } +void Matrix4Test::configuration() { + Matrix4 m( + 3.0f, 5.0f, 8.0f, 4.0f, + 4.0f, 4.0f, 7.0f, 3.125f, + 7.0f, -1.0f, 8.0f, 0.0f, + 9.0f, 4.0f, 5.0f, 9.55f + ); + string value("3 4 7 9 5 4 -1 4 8 7 8 5 4 3.125 0 9.55"); + CORRADE_COMPARE(ConfigurationValue::toString(m), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), m); +} + }}} diff --git a/src/Math/Test/Matrix4Test.h b/src/Math/Test/Matrix4Test.h index 5138e3a3a..a49cfddb1 100644 --- a/src/Math/Test/Matrix4Test.h +++ b/src/Math/Test/Matrix4Test.h @@ -32,6 +32,7 @@ class Matrix4Test: public Corrade::TestSuite::Tester { void rotationPart(); void debug(); + void configuration(); }; }}} diff --git a/src/Math/Test/MatrixTest.cpp b/src/Math/Test/MatrixTest.cpp index 2452a78b7..d9e986c85 100644 --- a/src/Math/Test/MatrixTest.cpp +++ b/src/Math/Test/MatrixTest.cpp @@ -44,7 +44,8 @@ MatrixTest::MatrixTest() { &MatrixTest::ij, &MatrixTest::determinant, &MatrixTest::inverted, - &MatrixTest::debug); + &MatrixTest::debug, + &MatrixTest::configuration); } void MatrixTest::construct() { @@ -308,4 +309,16 @@ void MatrixTest::debug() { " 0, 0, 0, 1)\n"); } +void MatrixTest::configuration() { + Matrix4 m( + 3.0f, 5.0f, 8.0f, 4.0f, + 4.0f, 4.0f, 7.0f, 3.125f, + 7.0f, -1.0f, 8.0f, 0.0f, + 9.0f, 4.0f, 5.0f, 9.55f + ); + string value("3 4 7 9 5 4 -1 4 8 7 8 5 4 3.125 0 9.55"); + CORRADE_COMPARE(ConfigurationValue::toString(m), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), m); +} + }}} diff --git a/src/Math/Test/MatrixTest.h b/src/Math/Test/MatrixTest.h index 704c95ff3..bb4eab650 100644 --- a/src/Math/Test/MatrixTest.h +++ b/src/Math/Test/MatrixTest.h @@ -38,6 +38,7 @@ class MatrixTest: public Corrade::TestSuite::Tester { void inverted(); void debug(); + void configuration(); }; }}} diff --git a/src/Math/Test/Vector2Test.cpp b/src/Math/Test/Vector2Test.cpp index a3a6e9753..a60a253d7 100644 --- a/src/Math/Test/Vector2Test.cpp +++ b/src/Math/Test/Vector2Test.cpp @@ -32,7 +32,8 @@ Vector2Test::Vector2Test() { addTests(&Vector2Test::construct, &Vector2Test::axes, &Vector2Test::scales, - &Vector2Test::debug); + &Vector2Test::debug, + &Vector2Test::configuration); } void Vector2Test::construct() { @@ -55,4 +56,11 @@ void Vector2Test::debug() { CORRADE_COMPARE(o.str(), "Vector(0.5, 15)\n"); } +void Vector2Test::configuration() { + Vector2 vec(3.125f, 9.0f); + string value("3.125 9"); + CORRADE_COMPARE(ConfigurationValue::toString(vec), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), vec); +} + }}} diff --git a/src/Math/Test/Vector2Test.h b/src/Math/Test/Vector2Test.h index 128f271be..48bcc1b9f 100644 --- a/src/Math/Test/Vector2Test.h +++ b/src/Math/Test/Vector2Test.h @@ -26,7 +26,9 @@ class Vector2Test: public Corrade::TestSuite::Tester { void construct(); void axes(); void scales(); + void debug(); + void configuration(); }; }}} diff --git a/src/Math/Test/Vector3Test.cpp b/src/Math/Test/Vector3Test.cpp index 1ea4ad574..710c60cd0 100644 --- a/src/Math/Test/Vector3Test.cpp +++ b/src/Math/Test/Vector3Test.cpp @@ -35,7 +35,8 @@ Vector3Test::Vector3Test() { &Vector3Test::axes, &Vector3Test::scales, &Vector3Test::twoComponent, - &Vector3Test::debug); + &Vector3Test::debug, + &Vector3Test::configuration); } void Vector3Test::construct() { @@ -72,4 +73,11 @@ void Vector3Test::debug() { CORRADE_COMPARE(o.str(), "Vector(0.5, 15, 1)\n"); } +void Vector3Test::configuration() { + Vector3 vec(3.0f, 3.125f, 9.55f); + string value("3 3.125 9.55"); + CORRADE_COMPARE(ConfigurationValue::toString(vec), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), vec); +} + }}} diff --git a/src/Math/Test/Vector3Test.h b/src/Math/Test/Vector3Test.h index 247c63e0d..1542c6a06 100644 --- a/src/Math/Test/Vector3Test.h +++ b/src/Math/Test/Vector3Test.h @@ -30,6 +30,7 @@ class Vector3Test: public Corrade::TestSuite::Tester { void twoComponent(); void debug(); + void configuration(); }; }}} diff --git a/src/Math/Test/Vector4Test.cpp b/src/Math/Test/Vector4Test.cpp index 57a0b9b7d..b8765b65b 100644 --- a/src/Math/Test/Vector4Test.cpp +++ b/src/Math/Test/Vector4Test.cpp @@ -34,7 +34,8 @@ Vector4Test::Vector4Test() { addTests(&Vector4Test::construct, &Vector4Test::threeComponent, &Vector4Test::twoComponent, - &Vector4Test::debug); + &Vector4Test::debug, + &Vector4Test::configuration); } void Vector4Test::construct() { @@ -57,4 +58,11 @@ void Vector4Test::debug() { CORRADE_COMPARE(o.str(), "Vector(0.5, 15, 1, 1)\n"); } +void Vector4Test::configuration() { + Vector4 vec(3.0f, 3.125f, 9.0f, 9.55f); + string value("3 3.125 9 9.55"); + CORRADE_COMPARE(ConfigurationValue::toString(vec), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), vec); +} + }}} diff --git a/src/Math/Test/Vector4Test.h b/src/Math/Test/Vector4Test.h index d31df492a..e06ce248a 100644 --- a/src/Math/Test/Vector4Test.h +++ b/src/Math/Test/Vector4Test.h @@ -28,6 +28,7 @@ class Vector4Test: public Corrade::TestSuite::Tester { void twoComponent(); void debug(); + void configuration(); }; }}} diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 93f14bef3..cbafca1c9 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -49,7 +49,8 @@ VectorTest::VectorTest() { &VectorTest::max, &VectorTest::angle, &VectorTest::negative, - &VectorTest::debug); + &VectorTest::debug, + &VectorTest::configuration); } void VectorTest::construct() { @@ -194,4 +195,11 @@ void VectorTest::debug() { CORRADE_COMPARE(o.str(), "a Vector(0, 0, 0, 0) b Vector(0, 0, 0, 0)\n"); } +void VectorTest::configuration() { + Vector4 vec(3.0f, 3.125f, 9.0f, 9.55f); + string value("3 3.125 9 9.55"); + CORRADE_COMPARE(ConfigurationValue::toString(vec), value); + CORRADE_COMPARE(ConfigurationValue::fromString(value), vec); +} + }}} diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index 75a9d3cc7..cde0cafa9 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -42,6 +42,7 @@ class VectorTest: public Corrade::TestSuite::Tester { void negative(); void debug(); + void configuration(); }; }}} diff --git a/src/Math/Vector.h b/src/Math/Vector.h index eede07b42..c6344ac0b 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "MathTypeTraits.h" @@ -48,8 +49,12 @@ namespace Implementation { } #endif -/** @brief %Vector */ -/** @todo Constexprize all for loops */ +/** +@brief %Vector + +@configurationvalueref{Magnum::Math::Vector} +@todo Constexprize all for loops +*/ template class Vector { static_assert(size != 0, "Vector cannot have zero elements"); @@ -527,4 +532,37 @@ template Corrade::Utility::Debug operator<<(Corrade::Utili }} +namespace Corrade { namespace Utility { + +/** @configurationvalue{Magnum::Math::Vector} */ +template struct ConfigurationValue> { + /** @brief Writes elements separated with spaces */ + static std::string toString(const Magnum::Math::Vector& value, int flags = 0) { + std::string output; + + for(size_t pos = 0; pos != size; ++pos) { + if(!output.empty()) output += ' '; + output += ConfigurationValue::toString(value[pos], flags); + } + + return output; + } + + /** @brief Reads elements separated with whitespace */ + static Magnum::Math::Vector fromString(const std::string& stringValue, int flags = 0) { + Magnum::Math::Vector result; + std::istringstream in(stringValue); + + std::string num; + for(size_t pos = 0; pos != size; ++pos) { + in >> num; + result[pos] = ConfigurationValue::fromString(num, flags); + } + + return result; + } +}; + +}} + #endif diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index 3dde7c5a1..68d19cb33 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -23,7 +23,11 @@ namespace Magnum { namespace Math { -/** @brief Two-component vector */ +/** +@brief Two-component vector + +@configurationvalueref{Magnum::Math::Vector2} +*/ template class Vector2: public Vector<2, T> { public: /** @@ -95,4 +99,9 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb }} +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Math::Vector2} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + #endif diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index 9beae4e77..212e79ac4 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -23,7 +23,11 @@ namespace Magnum { namespace Math { -/** @brief Three-component vector */ +/** +@brief Three-component vector + +@configurationvalueref{Magnum::Math::Vector3} +*/ template class Vector3: public Vector<3, T> { public: /** @@ -147,4 +151,9 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb }} +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Math::Vector3} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + #endif diff --git a/src/Math/Vector4.h b/src/Math/Vector4.h index a33752b8d..303361921 100644 --- a/src/Math/Vector4.h +++ b/src/Math/Vector4.h @@ -23,7 +23,11 @@ namespace Magnum { namespace Math { -/** @brief Four-component vector */ +/** +@brief Four-component vector + +@configurationvalueref{Magnum::Math::Vector4} +*/ template class Vector4: public Vector<4, T> { public: /** @@ -95,4 +99,9 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb }} +namespace Corrade { namespace Utility { + /** @configurationvalue{Magnum::Math::Vector4} */ + template struct ConfigurationValue>: public ConfigurationValue> {}; +}} + #endif diff --git a/src/Test/ColorTest.cpp b/src/Test/ColorTest.cpp index 3c3334ccd..7813b5f06 100644 --- a/src/Test/ColorTest.cpp +++ b/src/Test/ColorTest.cpp @@ -46,7 +46,8 @@ ColorTest::ColorTest() { &ColorTest::hsvOverflow, &ColorTest::hsvAlpha, - &ColorTest::debug); + &ColorTest::debug, + &ColorTest::configuration); } void ColorTest::fromDenormalized() { @@ -133,4 +134,16 @@ void ColorTest::debug() { CORRADE_COMPARE(o.str(), "Vector(0.5, 0.75, 0, 1)\n"); } +void ColorTest::configuration() { + Color3f color3(0.5f, 0.75f, 1.0f); + string value3("0.5 0.75 1"); + CORRADE_COMPARE(ConfigurationValue::toString(color3), value3); + CORRADE_COMPARE(ConfigurationValue::fromString(value3), color3); + + Color4f color4(0.5f, 0.75f, 0.0f, 1.0f); + string value4("0.5 0.75 0 1"); + CORRADE_COMPARE(ConfigurationValue::toString(color4), value4); + CORRADE_COMPARE(ConfigurationValue::fromString(value4), color4); +} + }} diff --git a/src/Test/ColorTest.h b/src/Test/ColorTest.h index a1ee2a727..47b18101d 100644 --- a/src/Test/ColorTest.h +++ b/src/Test/ColorTest.h @@ -39,6 +39,7 @@ class ColorTest: public Corrade::TestSuite::Tester { void hsvAlpha(); void debug(); + void configuration(); }; }}