#ifndef Magnum_SceneGraph_RigidMatrixTransformation3D_h #define Magnum_SceneGraph_RigidMatrixTransformation3D_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš 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 Magnum::SceneGraph::RigidMatrixTransformation3D */ #include "Math/Matrix4.h" #include "Math/Algorithms/GramSchmidt.h" #include "AbstractTranslationRotation3D.h" #include "Object.h" namespace Magnum { namespace SceneGraph { /** @brief Three-dimensional rigid transformation implemented using matrices Unlike MatrixTransformation3D this class allows only rotation, reflection and translation (no scaling or setting arbitrary transformations). This allows to use Matrix4::invertedRigid() for faster computation of inverse transformations. @see @ref scenegraph, RigidMatrixTransformation2D */ #ifndef DOXYGEN_GENERATING_OUTPUT template #else template #endif class RigidMatrixTransformation3D: public AbstractTranslationRotation3D { public: /** @brief Transformation matrix type */ typedef typename DimensionTraits<3, T>::MatrixType DataType; #ifndef DOXYGEN_GENERATING_OUTPUT inline constexpr static Math::Matrix4 fromMatrix(const Math::Matrix4& matrix) { return matrix; } inline constexpr static Math::Matrix4 toMatrix(const Math::Matrix4& transformation) { return transformation; } inline static Math::Matrix4 compose(const Math::Matrix4& parent, const Math::Matrix4& child) { return parent*child; } inline static Math::Matrix4 inverted(const Math::Matrix4& transformation) { return transformation.invertedRigid(); } inline Math::Matrix4 transformation() const { return _transformation; } #endif /** * @brief Normalize rotation part * @return Pointer to self (for method chaining) * * Normalizes the rotation part using Math::Algorithms::gramSchmidt() * to prevent rounding errors when rotating the object subsequently. */ RigidMatrixTransformation3D* normalizeRotation() { setTransformation(Math::Matrix4::from( Math::Algorithms::gramSchmidtOrthonormalize(_transformation.rotationScaling()), _transformation.translation())); return this; } inline RigidMatrixTransformation3D* resetTransformation() override { setTransformation({}); return this; } /** * @brief Translate object * @param vector Translation vector * @param type Transformation type * @return Pointer to self (for method chaining) * * @see Vector3::xAxis(), Vector3::yAxis(), Vector3::zAxis(), * Matrix4::translation() */ inline RigidMatrixTransformation3D* translate(const Math::Vector3& vector, TransformationType type = TransformationType::Global) override { transform(Math::Matrix4::translation(vector), type); return this; } /** * @brief Rotate object * @param angle Angle (counterclockwise) * @param normalizedAxis Normalized rotation axis * @param type Transformation type * @return Pointer to self (for method chaining) * * @see rotateX(), rotateY(), rotateZ(), Vector3::xAxis(), * Vector3::yAxis(), Vector3::zAxis(), normalizeRotation(), * Matrix4::rotation() */ inline RigidMatrixTransformation3D* rotate(Math::Rad angle, const Math::Vector3& normalizedAxis, TransformationType type = TransformationType::Global) override { transform(Math::Matrix4::rotation(angle, normalizedAxis), type); return this; } /** * @brief Rotate object around X axis * @param angle Angle (counterclockwise) * @param type Transformation type * @return Pointer to self (for method chaining) * * @see normalizeRotation(), Matrix4::rotationX() */ inline RigidMatrixTransformation3D* rotateX(Math::Rad angle, TransformationType type = TransformationType::Global) override { transform(Math::Matrix4::rotationX(angle), type); return this; } /** * @brief Rotate object around Y axis * @param angle Angle (counterclockwise) * @param type Transformation type * @return Pointer to self (for method chaining) * * @see normalizeRotation(), Matrix4::rotationY() */ inline RigidMatrixTransformation3D* rotateY(Math::Rad angle, TransformationType type = TransformationType::Global) override { transform(Math::Matrix4::rotationY(angle), type); return this; } /** * @brief Rotate object around Z axis * @param angle Angle (counterclockwise) * @param type Transformation type * @return Pointer to self (for method chaining) * * @see normalizeRotation(), Matrix4::rotationZ() */ inline RigidMatrixTransformation3D* rotateZ(Math::Rad angle, TransformationType type = TransformationType::Global) override { transform(Math::Matrix4::rotationZ(angle), type); return this; } /** * @brief Reflect object * @param normal Normal of the plane through which to reflect * (normalized) * @param type Transformation type * @return Pointer to self (for method chaining) * * @see Matrix4::reflection() */ inline RigidMatrixTransformation3D* reflect(const Math::Vector3& normal, TransformationType type = TransformationType::Global) { transform(Math::Matrix4::reflection(normal), type); return this; } protected: /* Allow construction only from Object */ inline explicit RigidMatrixTransformation3D() = default; private: inline void setTransformation(const Math::Matrix4& 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>*>(this)->isScene()) { _transformation = transformation; static_cast>*>(this)->setDirty(); } } inline void transform(const Math::Matrix4& transformation, TransformationType type) { setTransformation(type == TransformationType::Global ? transformation*_transformation : _transformation*transformation); } Math::Matrix4 _transformation; }; }} #endif