Browse Source

SceneGraph: new TranslationTransformation class.

Translation-only transformation, supports also integer coordinates.
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
a7f77f41b9
  1. 1
      src/SceneGraph/CMakeLists.txt
  2. 2
      src/SceneGraph/Object.h
  3. 11
      src/SceneGraph/SceneGraph.h
  4. 2
      src/SceneGraph/Test/CMakeLists.txt
  5. 146
      src/SceneGraph/Test/TranslationTransformationTest.cpp
  6. 207
      src/SceneGraph/TranslationTransformation.h
  7. 3
      src/SceneGraph/instantiation.cpp

1
src/SceneGraph/CMakeLists.txt

@ -63,6 +63,7 @@ set(MagnumSceneGraph_HEADERS
Object.hpp
Scene.h
SceneGraph.h
TranslationTransformation.h
magnumSceneGraphVisibility.h)

2
src/SceneGraph/Object.h

@ -85,6 +85,8 @@ See @ref compilation-speedup-hpp for more information.
- @ref MatrixTransformation3D "Object<MatrixTransformation3D<Float>>"
- @ref RigidMatrixTransformation2D "Object<RigidMatrixTransformation2D<Float>>"
- @ref RigidMatrixTransformation3D "Object<RigidMatrixTransformation3D<Float>>"
- @ref TranslationTransformation2D "Object<TranslationTransformation2D<Float>>"
- @ref TranslationTransformation3D "Object<TranslationTransformation3D<Float>>"
@see Scene, AbstractFeature, AbstractTransformation, DebugTools::ObjectRenderer
*/

11
src/SceneGraph/SceneGraph.h

@ -191,6 +191,17 @@ typedef BasicRigidMatrixTransformation3D<Float> RigidMatrixTransformation3D;
template<class Transformation> class Scene;
template<UnsignedInt, class T, class = T> class TranslationTransformation;
#ifndef CORRADE_GCC46_COMPATIBILITY
template<class T, class TranslationType = T> using BasicTranslationTransformation2D = TranslationTransformation<2, T, TranslationType>;
template<class T, class TranslationType = T> using BasicTranslationTransformation3D = TranslationTransformation<3, T, TranslationType>;
typedef BasicTranslationTransformation2D<Float> TranslationTransformation2D;
typedef BasicTranslationTransformation3D<Float> TranslationTransformation3D;
#else
typedef TranslationTransformation<2, Float> TranslationTransformation2D;
typedef TranslationTransformation<3, Float> TranslationTransformation3D;
#endif
}}
#endif

2
src/SceneGraph/Test/CMakeLists.txt

@ -32,9 +32,11 @@ corrade_add_test(SceneGraphObjectTest ObjectTest.cpp LIBRARIES MagnumSceneGraphT
corrade_add_test(SceneGraphRigidMatrixTrans___2DTest RigidMatrixTransformation2DTest.cpp LIBRARIES MagnumSceneGraph)
corrade_add_test(SceneGraphRigidMatrixTrans___3DTest RigidMatrixTransformation3DTest.cpp LIBRARIES MagnumSceneGraph)
corrade_add_test(SceneGraphSceneTest SceneTest.cpp LIBRARIES MagnumSceneGraph)
corrade_add_test(SceneGraphTranslationTransfo___Test TranslationTransformationTest.cpp LIBRARIES MagnumSceneGraph)
set_target_properties(SceneGraphDualComplexTransfo___Test
SceneGraphDualQuaternionTran___Test
SceneGraphRigidMatrixTrans___2DTest
SceneGraphRigidMatrixTrans___3DTest
SceneGraphTranslationTransfo___Test
PROPERTIES COMPILE_FLAGS "-DCORRADE_GRACEFUL_ASSERT")

146
src/SceneGraph/Test/TranslationTransformationTest.cpp

@ -0,0 +1,146 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <sstream>
#include <TestSuite/Tester.h>
#include "SceneGraph/TranslationTransformation.h"
#include "SceneGraph/Scene.h"
/* For Object2Di */
#include "SceneGraph/Object.hpp"
namespace Magnum { namespace SceneGraph { namespace Test {
typedef Object<TranslationTransformation2D> Object2D;
typedef Scene<TranslationTransformation2D> Scene2D;
class TranslationTransformationTest: public TestSuite::Tester {
public:
explicit TranslationTransformationTest();
void fromMatrix();
void toMatrix();
void compose();
void inverted();
void setTransformation();
void resetTransformation();
void transform();
void translate();
void integral();
};
TranslationTransformationTest::TranslationTransformationTest() {
addTests({&TranslationTransformationTest::fromMatrix,
&TranslationTransformationTest::toMatrix,
&TranslationTransformationTest::compose,
&TranslationTransformationTest::inverted,
&TranslationTransformationTest::setTransformation,
&TranslationTransformationTest::resetTransformation,
&TranslationTransformationTest::transform,
&TranslationTransformationTest::translate,
&TranslationTransformationTest::integral});
}
void TranslationTransformationTest::fromMatrix() {
std::ostringstream o;
Error::setOutput(&o);
Implementation::Transformation<TranslationTransformation2D>::fromMatrix(Matrix3::scaling(Vector2(4.0f)));
CORRADE_COMPARE(o.str(), "SceneGraph::TranslationTransformation: the matrix doesn't represent pure translation\n");
const Vector2 v(1.0f, -0.3f);
CORRADE_COMPARE(Implementation::Transformation<TranslationTransformation2D>::fromMatrix(Matrix3::translation(v)), v);
}
void TranslationTransformationTest::toMatrix() {
const Vector2 v(1.0f, -0.3f);
CORRADE_COMPARE(Implementation::Transformation<TranslationTransformation2D>::toMatrix(v), Matrix3::translation(v));
}
void TranslationTransformationTest::compose() {
const Vector2 parent(-0.5f, 2.0f);
const Vector2 child(1.0f, -0.3f);
CORRADE_COMPARE(Implementation::Transformation<TranslationTransformation2D>::compose(parent, child), Vector2(0.5f, 1.7f));
}
void TranslationTransformationTest::inverted() {
const Vector2 v(1.0f, -0.3f);
CORRADE_COMPARE(Implementation::Transformation<TranslationTransformation2D>::inverted(v), Vector2(-1.0f, 0.3f));
}
void TranslationTransformationTest::setTransformation() {
Object2D o;
/* Dirty after setting transformation */
o.setClean();
CORRADE_VERIFY(!o.isDirty());
o.setTransformation({1.0f, -0.3f});
CORRADE_VERIFY(o.isDirty());
CORRADE_COMPARE(o.transformationMatrix(), Matrix3::translation({1.0f, -0.3f}));
/* Scene cannot be transformed */
Scene2D s;
s.setClean();
s.setTransformation({1.0f, -0.3f});
CORRADE_VERIFY(!s.isDirty());
CORRADE_COMPARE(s.transformationMatrix(), Matrix3());
}
void TranslationTransformationTest::resetTransformation() {
Object2D o;
o.setTransformation({1.0f, -0.3f});
CORRADE_VERIFY(o.transformationMatrix() != Matrix3());
o.resetTransformation();
CORRADE_COMPARE(o.transformationMatrix(), Matrix3());
}
void TranslationTransformationTest::transform() {
Object2D o;
o.setTransformation({1.0f, -0.3f})
.transform({-0.5f, 2.0f});
CORRADE_COMPARE(o.transformationMatrix(), Matrix3::translation({0.5f, 1.7f}));
}
void TranslationTransformationTest::translate() {
Object2D o;
o.setTransformation({1.0f, -0.3f})
.translate({-0.5f, 2.0f});
CORRADE_COMPARE(o.transformationMatrix(), Matrix3::translation({1.0f, -0.3f})*Matrix3::translation({-0.5f, 2.0f}));
}
void TranslationTransformationTest::integral() {
typedef Object<BasicTranslationTransformation2D<Float, Short>> Object2Di;
Object2Di o;
o.translate({3, -7});
CORRADE_COMPARE(o.transformationMatrix(), Matrix3::translation({3, -7}));
}
}}}
CORRADE_TEST_MAIN(Magnum::SceneGraph::Test::TranslationTransformationTest)

207
src/SceneGraph/TranslationTransformation.h

@ -0,0 +1,207 @@
#ifndef Magnum_SceneGraph_TranslationTransformation_h
#define Magnum_SceneGraph_TranslationTransformation_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/** @file
* @brief Class @ref Magnum::SceneGraph::TranslationTransformation, alias @ref Magnum::SceneGraph::BasicTranslationTransformation2D, @ref Magnum::SceneGraph::BasicTranslationTransformation3D, typedef @ref Magnum::SceneGraph::TranslationTransformation2D, @ref Magnum::SceneGraph::BasicTranslationTransformation3D
*/
#include "Math/Matrix3.h"
#include "Math/Matrix4.h"
#include "SceneGraph/AbstractTranslation.h"
#include "SceneGraph/Object.h"
namespace Magnum { namespace SceneGraph {
/**
@brief Translation-only transformation
Uses @ref Math::Vector2 or @ref Math::Vector3 as underlying type. By default
the translation is stored with the same underlying type as resulting
transformation matrix, but it's possible to store translation in e.g. integral
coordinates while having floating-point transformation matrix.
Note that translation is commutative, so all @ref TransformationType parameters
have no effect and are included only for compatibility with other
transformation implementations.
@see @ref BasicTranslationTransformation2D, @ref BasicTranslationTransformation3D,
@ref TranslationTransformation2D, @ref TranslationTransformation3D,
@ref scenegraph
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<UnsignedInt dimensions, class T, class TranslationType = T>
#else
template<UnsignedInt dimensions, class T, class TranslationType>
#endif
class TranslationTransformation: public AbstractTranslation<dimensions, T, TranslationType> {
public:
/** @brief Underlying transformation type */
typedef typename DimensionTraits<dimensions, TranslationType>::VectorType DataType;
/** @brief Object transformation */
typename DimensionTraits<dimensions, TranslationType>::VectorType transformation() const { return _transformation; }
/**
* @brief Set transformation
* @return Reference to self (for method chaining)
*/
Object<TranslationTransformation<dimensions, T, TranslationType>>& setTransformation(const typename DimensionTraits<dimensions, TranslationType>::VectorType& transformation) {
/* Setting transformation is forbidden for the scene */
/** @todo Assert for this? */
/** @todo Do this in some common code so we don't need to include Object? */
if(!static_cast<Object<TranslationTransformation<dimensions, T, TranslationType>>*>(this)->isScene()) {
_transformation = transformation;
static_cast<Object<TranslationTransformation<dimensions, T, TranslationType>>*>(this)->setDirty();
}
return static_cast<Object<TranslationTransformation<dimensions, T, TranslationType>>&>(*this);
}
/** @copydoc AbstractTranslation::resetTransformation() */
Object<TranslationTransformation<dimensions, T, TranslationType>>& resetTransformation() {
return setTransformation({});
}
/**
* @brief Add transformation
* @param transformation Transformation
* @return Reference to self (for method chaining)
*
* Equivalent to @ref translate(), provided only for compatibility with
* other implementations.
*/
Object<TranslationTransformation<dimensions, T, TranslationType>>& transform(const typename DimensionTraits<dimensions, TranslationType>::VectorType& transformation, TransformationType = TransformationType::Global) {
return translate(transformation);
}
/**
* @brief Translate object
* @param vector Translation vector
* @return Reference to self (for method chaining)
*
* @see @ref Vector2::xAxis(), @ref Vector2::yAxis(), @ref Vector3::xAxis(),
* @ref Vector3::yAxis(), @ref Vector3::zAxis()
*/
Object<TranslationTransformation<dimensions, T, TranslationType>>& translate(const typename DimensionTraits<dimensions, TranslationType>::VectorType& vector, TransformationType = TransformationType::Global) {
_transformation += vector;
return static_cast<Object<TranslationTransformation<dimensions, T, TranslationType>>&>(*this);
}
private:
void doResetTransformation() override final { resetTransformation(); }
void doTranslate(const typename DimensionTraits<dimensions, TranslationType>::VectorType& vector, TransformationType) override final {
translate(vector);
}
typename DimensionTraits<dimensions, TranslationType>::VectorType _transformation;
};
#ifndef CORRADE_GCC46_COMPATIBILITY
/**
@brief Base transformation for two-dimensional scenes supporting translation
Convenience alternative to <tt>%TranslationTransformation<2, T, TranslationType></tt>.
See @ref TranslationTransformation for more information.
@note Not available on GCC < 4.7. Use <tt>%TranslationTransformation<2, T, TranslationType></tt>
instead.
@see @ref TranslationTransformation2D, @ref BasicTranslationTransformation3D
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T, class TranslationType = T>
#else
template<class T, class TranslationType>
#endif
using BasicTranslationTransformation2D = TranslationTransformation<2, T, TranslationType>;
#endif
/**
@brief Base transformation for two-dimensional float scenes supporting translation
@see @ref TranslationTransformation3D
*/
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef BasicTranslationTransformation2D<Float> TranslationTransformation2D;
#else
typedef TranslationTransformation<2, Float> TranslationTransformation2D;
#endif
#ifndef CORRADE_GCC46_COMPATIBILITY
/**
@brief Base transformation for three-dimensional scenes supporting translation
Convenience alternative to <tt>%TranslationTransformation<3, T, TranslationType></tt>.
See @ref TranslationTransformation for more information.
@note Not available on GCC < 4.7. Use <tt>%TranslationTransformation<3, T, TranslationType></tt>
instead.
@see @ref TranslationTransformation3D, @ref BasicTranslationTransformation2D
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T, class TranslationType = T>
#else
template<class T, class TranslationType>
#endif
using BasicTranslationTransformation3D = TranslationTransformation<3, T, TranslationType>;
#endif
/**
@brief Base transformation for three-dimensional float scenes supporting translation
@see @ref TranslationTransformation2D
*/
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef BasicTranslationTransformation3D<Float> TranslationTransformation3D;
#else
typedef TranslationTransformation<3, Float> TranslationTransformation3D;
#endif
namespace Implementation {
template<UnsignedInt dimensions, class T, class TranslationType> struct Transformation<TranslationTransformation<dimensions, T, TranslationType>> {
static typename DimensionTraits<dimensions, TranslationType>::VectorType fromMatrix(const typename DimensionTraits<dimensions, T>::MatrixType& matrix) {
CORRADE_ASSERT((matrix.rotationScaling() == Math::Matrix<dimensions, T>()),
"SceneGraph::TranslationTransformation: the matrix doesn't represent pure translation", {});
return typename DimensionTraits<dimensions, TranslationType>::VectorType(matrix.translation());
}
constexpr static typename DimensionTraits<dimensions, T>::MatrixType toMatrix(const typename DimensionTraits<dimensions, TranslationType>::VectorType& transformation) {
return DimensionTraits<dimensions, T>::MatrixType::translation(typename DimensionTraits<dimensions, T>::VectorType(transformation));
}
static typename DimensionTraits<dimensions, TranslationType>::VectorType compose(const typename DimensionTraits<dimensions, TranslationType>::VectorType& parent, const typename DimensionTraits<dimensions, TranslationType>::VectorType& child) {
return parent+child;
}
static typename DimensionTraits<dimensions, TranslationType>::VectorType inverted(const typename DimensionTraits<dimensions, TranslationType>::VectorType& transformation) {
return -transformation;
}
};
}
}}
#endif

3
src/SceneGraph/instantiation.cpp

@ -33,6 +33,7 @@
#include "SceneGraph/Object.hpp"
#include "SceneGraph/RigidMatrixTransformation2D.h"
#include "SceneGraph/RigidMatrixTransformation3D.h"
#include "SceneGraph/TranslationTransformation.h"
namespace Magnum { namespace SceneGraph {
@ -58,6 +59,8 @@ template class MAGNUM_SCENEGRAPH_EXPORT Object<BasicMatrixTransformation2D<Float
template class MAGNUM_SCENEGRAPH_EXPORT Object<BasicMatrixTransformation3D<Float>>;
template class MAGNUM_SCENEGRAPH_EXPORT Object<BasicRigidMatrixTransformation2D<Float>>;
template class MAGNUM_SCENEGRAPH_EXPORT Object<BasicRigidMatrixTransformation3D<Float>>;
template class MAGNUM_SCENEGRAPH_EXPORT Object<BasicTranslationTransformation2D<Float>>;
template class MAGNUM_SCENEGRAPH_EXPORT Object<BasicTranslationTransformation3D<Float>>;
#endif
}}

Loading…
Cancel
Save