Browse Source

Support for storing matrices and vectors in configuration.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
0e74c3847d
  1. 2
      Doxyfile
  2. 4
      doc/coding-style.dox
  3. 8
      src/Color.h
  4. 46
      src/Math/Matrix.h
  5. 6
      src/Math/Matrix3.h
  6. 6
      src/Math/Matrix4.h
  7. 14
      src/Math/Test/Matrix3Test.cpp
  8. 2
      src/Math/Test/Matrix3Test.h
  9. 15
      src/Math/Test/Matrix4Test.cpp
  10. 1
      src/Math/Test/Matrix4Test.h
  11. 15
      src/Math/Test/MatrixTest.cpp
  12. 1
      src/Math/Test/MatrixTest.h
  13. 10
      src/Math/Test/Vector2Test.cpp
  14. 2
      src/Math/Test/Vector2Test.h
  15. 10
      src/Math/Test/Vector3Test.cpp
  16. 1
      src/Math/Test/Vector3Test.h
  17. 10
      src/Math/Test/Vector4Test.cpp
  18. 1
      src/Math/Test/Vector4Test.h
  19. 10
      src/Math/Test/VectorTest.cpp
  20. 1
      src/Math/Test/VectorTest.h
  21. 42
      src/Math/Vector.h
  22. 11
      src/Math/Vector2.h
  23. 11
      src/Math/Vector3.h
  24. 11
      src/Math/Vector4.h
  25. 15
      src/Test/ColorTest.cpp
  26. 1
      src/Test/ColorTest.h

2
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." \

4
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

8
src/Color.h

@ -408,4 +408,12 @@ template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb
}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Color3} */
template<class T> struct ConfigurationValue<Magnum::Color3<T>>: public ConfigurationValue<Magnum::Math::Vector<3, T>> {};
/** @configurationvalue{Magnum::Color4} */
template<class T> struct ConfigurationValue<Magnum::Color4<T>>: public ConfigurationValue<Magnum::Math::Vector<4, T>> {};
}}
#endif

46
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<size_t size, class T> class Matrix {
static_assert(size != 0, "Matrix cannot have zero elements");
@ -355,4 +356,41 @@ template<class T> class MatrixDeterminant<1, T> {
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Matrix} */
template<size_t size, class T> struct ConfigurationValue<Magnum::Math::Matrix<size, T>> {
/** @brief Writes elements separated with spaces */
static std::string toString(const Magnum::Math::Matrix<size, T>& 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<T>::toString(value(col, row), flags);
}
}
return output;
}
/** @brief Reads elements separated with whitespace */
static Magnum::Math::Matrix<size, T> fromString(const std::string& stringValue, int flags = 0) {
Magnum::Math::Matrix<size, T> result(Magnum::Math::Matrix<size, T>::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<T>::fromString(num, flags);
}
}
return result;
}
};
}}
#endif

6
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 T> class Matrix3: public Matrix<3, T> {
public:
@ -104,4 +105,9 @@ template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Matrix3} */
template<class T> struct ConfigurationValue<Magnum::Math::Matrix3<T>>: public ConfigurationValue<Magnum::Math::Matrix<3, T>> {};
}}
#endif

6
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<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Matrix4} */
template<class T> struct ConfigurationValue<Magnum::Math::Matrix4<T>>: public ConfigurationValue<Magnum::Math::Matrix<4, T>> {};
}}
#endif

14
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<Matrix3>::toString(m), value);
CORRADE_COMPARE(ConfigurationValue<Matrix3>::fromString(value), m);
}
}}}

2
src/Math/Test/Matrix3Test.h

@ -28,7 +28,9 @@ class Matrix3Test: public Corrade::TestSuite::Tester<Matrix3Test> {
void translation();
void scaling();
void rotation();
void debug();
void configuration();
};
}}}

15
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<Matrix4>::toString(m), value);
CORRADE_COMPARE(ConfigurationValue<Matrix4>::fromString(value), m);
}
}}}

1
src/Math/Test/Matrix4Test.h

@ -32,6 +32,7 @@ class Matrix4Test: public Corrade::TestSuite::Tester<Matrix4Test> {
void rotationPart();
void debug();
void configuration();
};
}}}

15
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<Matrix4>::toString(m), value);
CORRADE_COMPARE(ConfigurationValue<Matrix4>::fromString(value), m);
}
}}}

1
src/Math/Test/MatrixTest.h

@ -38,6 +38,7 @@ class MatrixTest: public Corrade::TestSuite::Tester<MatrixTest> {
void inverted();
void debug();
void configuration();
};
}}}

10
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<Vector2>::toString(vec), value);
CORRADE_COMPARE(ConfigurationValue<Vector2>::fromString(value), vec);
}
}}}

2
src/Math/Test/Vector2Test.h

@ -26,7 +26,9 @@ class Vector2Test: public Corrade::TestSuite::Tester<Vector2Test> {
void construct();
void axes();
void scales();
void debug();
void configuration();
};
}}}

10
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<Vector3>::toString(vec), value);
CORRADE_COMPARE(ConfigurationValue<Vector3>::fromString(value), vec);
}
}}}

1
src/Math/Test/Vector3Test.h

@ -30,6 +30,7 @@ class Vector3Test: public Corrade::TestSuite::Tester<Vector3Test> {
void twoComponent();
void debug();
void configuration();
};
}}}

10
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<Vector4>::toString(vec), value);
CORRADE_COMPARE(ConfigurationValue<Vector4>::fromString(value), vec);
}
}}}

1
src/Math/Test/Vector4Test.h

@ -28,6 +28,7 @@ class Vector4Test: public Corrade::TestSuite::Tester<Vector4Test> {
void twoComponent();
void debug();
void configuration();
};
}}}

10
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<Vector4>::toString(vec), value);
CORRADE_COMPARE(ConfigurationValue<Vector4>::fromString(value), vec);
}
}}}

1
src/Math/Test/VectorTest.h

@ -42,6 +42,7 @@ class VectorTest: public Corrade::TestSuite::Tester<VectorTest> {
void negative();
void debug();
void configuration();
};
}}}

42
src/Math/Vector.h

@ -22,6 +22,7 @@
#include <cmath>
#include <limits>
#include <Utility/Debug.h>
#include <Utility/Configuration.h>
#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<size_t size, class T> class Vector {
static_assert(size != 0, "Vector cannot have zero elements");
@ -527,4 +532,37 @@ template<size_t size, class T> Corrade::Utility::Debug operator<<(Corrade::Utili
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Vector} */
template<size_t size, class T> struct ConfigurationValue<Magnum::Math::Vector<size, T>> {
/** @brief Writes elements separated with spaces */
static std::string toString(const Magnum::Math::Vector<size, T>& value, int flags = 0) {
std::string output;
for(size_t pos = 0; pos != size; ++pos) {
if(!output.empty()) output += ' ';
output += ConfigurationValue<T>::toString(value[pos], flags);
}
return output;
}
/** @brief Reads elements separated with whitespace */
static Magnum::Math::Vector<size, T> fromString(const std::string& stringValue, int flags = 0) {
Magnum::Math::Vector<size, T> result;
std::istringstream in(stringValue);
std::string num;
for(size_t pos = 0; pos != size; ++pos) {
in >> num;
result[pos] = ConfigurationValue<T>::fromString(num, flags);
}
return result;
}
};
}}
#endif

11
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 T> class Vector2: public Vector<2, T> {
public:
/**
@ -95,4 +99,9 @@ template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Vector2} */
template<class T> struct ConfigurationValue<Magnum::Math::Vector2<T>>: public ConfigurationValue<Magnum::Math::Vector<2, T>> {};
}}
#endif

11
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 T> class Vector3: public Vector<3, T> {
public:
/**
@ -147,4 +151,9 @@ template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Vector3} */
template<class T> struct ConfigurationValue<Magnum::Math::Vector3<T>>: public ConfigurationValue<Magnum::Math::Vector<3, T>> {};
}}
#endif

11
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 T> class Vector4: public Vector<4, T> {
public:
/**
@ -95,4 +99,9 @@ template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug deb
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Vector4} */
template<class T> struct ConfigurationValue<Magnum::Math::Vector4<T>>: public ConfigurationValue<Magnum::Math::Vector<4, T>> {};
}}
#endif

15
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<Color3f>::toString(color3), value3);
CORRADE_COMPARE(ConfigurationValue<Color3f>::fromString(value3), color3);
Color4f color4(0.5f, 0.75f, 0.0f, 1.0f);
string value4("0.5 0.75 0 1");
CORRADE_COMPARE(ConfigurationValue<Color4f>::toString(color4), value4);
CORRADE_COMPARE(ConfigurationValue<Color4f>::fromString(value4), color4);
}
}}

1
src/Test/ColorTest.h

@ -39,6 +39,7 @@ class ColorTest: public Corrade::TestSuite::Tester<ColorTest> {
void hsvAlpha();
void debug();
void configuration();
};
}}

Loading…
Cancel
Save