Browse Source

Math: ability to save/restore Bezier to/from Configuration.

pull/175/merge
Vladimír Vondruš 10 years ago
parent
commit
12e19eff37
  1. 54
      src/Magnum/Math/Bezier.h
  2. 16
      src/Magnum/Math/Test/BezierTest.cpp
  3. 9
      src/Magnum/Math/instantiation.cpp

54
src/Magnum/Math/Bezier.h

@ -224,5 +224,59 @@ extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utili
}}
namespace Corrade { namespace Utility {
/** @configurationvalue{Magnum::Math::Bezier} */
template<Magnum::UnsignedInt order, Magnum::UnsignedInt dimensions, class T> struct ConfigurationValue<Magnum::Math::Bezier<order, dimensions, T>> {
ConfigurationValue() = delete;
/** @brief Writes elements separated with spaces */
static std::string toString(const Magnum::Math::Bezier<order, dimensions, T>& value, ConfigurationValueFlags flags) {
std::string output;
for(std::size_t o = 0; o != order + 1; ++o) {
for(std::size_t i = 0; i != dimensions; ++i) {
if(!output.empty()) output += ' ';
output += ConfigurationValue<T>::toString(value[o][i], flags);
}
}
return output;
}
/** @brief Reads elements separated with whitespace */
static Magnum::Math::Bezier<order, dimensions, T> fromString(const std::string& stringValue, ConfigurationValueFlags flags) {
Magnum::Math::Bezier<order, dimensions, T> result;
std::size_t oldpos = 0, pos = std::string::npos, i = 0;
do {
pos = stringValue.find(' ', oldpos);
std::string part = stringValue.substr(oldpos, pos-oldpos);
if(!part.empty()) {
result[i/dimensions][i%dimensions] = ConfigurationValue<T>::fromString(part, flags);
++i;
}
oldpos = pos+1;
} while(pos != std::string::npos);
return result;
}
};
#if !defined(DOXYGEN_GENERATING_OUTPUT) && !defined(__MINGW32__)
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<2, 2, Magnum::Float>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<2, 3, Magnum::Float>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<3, 2, Magnum::Float>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<3, 3, Magnum::Float>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<2, 2, Magnum::Double>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<2, 3, Magnum::Double>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<3, 2, Magnum::Double>>;
extern template struct MAGNUM_EXPORT ConfigurationValue<Magnum::Math::Bezier<3, 3, Magnum::Double>>;
#endif
}}
#endif

16
src/Magnum/Math/Test/BezierTest.cpp

@ -26,6 +26,7 @@
#include <sstream>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/Utility/Configuration.h>
#include "Magnum/Math/Bezier.h"
#include "Magnum/Math/Vector2.h"
@ -46,6 +47,7 @@ struct BezierTest : Corrade::TestSuite::Tester {
void lerpCubic();
void debug();
void configuration();
};
BezierTest::BezierTest() {
@ -54,7 +56,8 @@ BezierTest::BezierTest() {
&BezierTest::lerpQuadratic,
&BezierTest::lerpCubic,
&BezierTest::debug});
&BezierTest::debug,
&BezierTest::configuration});
}
void BezierTest::implicitConstructor() {
@ -91,6 +94,17 @@ void BezierTest::debug() {
CORRADE_COMPARE(out.str(), "Bezier({0, 1}, {1.5, -0.3}, {2.1, 0.5}, {0, 2})\n");
}
void BezierTest::configuration() {
Corrade::Utility::Configuration c;
CubicBezier2D bezier{Vector2{0.0f, 1.0f}, Vector2{1.5f, -0.3f}, Vector2{2.1f, 0.5f}, Vector2{0.0f, 2.0f}};
std::string value("0 1 1.5 -0.3 2.1 0.5 0 2");
c.setValue("bezier", bezier);
CORRADE_COMPARE(c.value("bezier"), value);
CORRADE_COMPARE(c.value<CubicBezier2D>("bezier"), bezier);
}
}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::BezierTest)

9
src/Magnum/Math/instantiation.cpp

@ -31,6 +31,15 @@
namespace Corrade { namespace Utility {
#ifndef DOXYGEN_GENERATING_OUTPUT
template struct ConfigurationValue<Magnum::Math::Bezier<2, 2, Magnum::Float>>;
template struct ConfigurationValue<Magnum::Math::Bezier<2, 3, Magnum::Float>>;
template struct ConfigurationValue<Magnum::Math::Bezier<3, 2, Magnum::Float>>;
template struct ConfigurationValue<Magnum::Math::Bezier<3, 3, Magnum::Float>>;
template struct ConfigurationValue<Magnum::Math::Bezier<2, 2, Magnum::Double>>;
template struct ConfigurationValue<Magnum::Math::Bezier<2, 3, Magnum::Double>>;
template struct ConfigurationValue<Magnum::Math::Bezier<3, 2, Magnum::Double>>;
template struct ConfigurationValue<Magnum::Math::Bezier<3, 3, Magnum::Double>>;
template struct ConfigurationValue<Magnum::Math::RectangularMatrix<2, 2, Magnum::Float>>;
template struct ConfigurationValue<Magnum::Math::RectangularMatrix<3, 3, Magnum::Float>>;
template struct ConfigurationValue<Magnum::Math::RectangularMatrix<4, 4, Magnum::Float>>;

Loading…
Cancel
Save