mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
332 lines
13 KiB
332 lines
13 KiB
|
16 years ago
|
#ifndef Magnum_Math_Matrix3_h
|
||
|
|
#define Magnum_Math_Matrix3_h
|
||
|
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
13 years ago
|
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.
|
||
|
16 years ago
|
*/
|
||
|
|
|
||
|
|
/** @file
|
||
|
|
* @brief Class Magnum::Math::Matrix3
|
||
|
|
*/
|
||
|
|
|
||
|
13 years ago
|
#include "Math/Matrix.h"
|
||
|
|
#include "Math/Vector3.h"
|
||
|
16 years ago
|
|
||
|
|
namespace Magnum { namespace Math {
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
13 years ago
|
@brief 3x3 matrix
|
||
|
13 years ago
|
@tparam T Underlying data type
|
||
|
14 years ago
|
|
||
|
13 years ago
|
Represents 2D transformation. See @ref matrix-vector and @ref transformations
|
||
|
|
for brief introduction.
|
||
|
13 years ago
|
@see Magnum::Matrix3, Magnum::Matrix3d, DualComplex,
|
||
|
|
SceneGraph::MatrixTransformation2D
|
||
|
14 years ago
|
@configurationvalueref{Magnum::Math::Matrix3}
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
template<class T> class Matrix3: public Matrix<3, T> {
|
||
|
16 years ago
|
public:
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 2D translation matrix
|
||
|
14 years ago
|
* @param vector Translation vector
|
||
|
14 years ago
|
*
|
||
|
13 years ago
|
* @see translation(), DualComplex::translation(),
|
||
|
|
* Matrix4::translation(const Vector3&), Vector2::xAxis(),
|
||
|
|
* Vector2::yAxis()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
constexpr static Matrix3<T> translation(const Vector2<T>& vector) {
|
||
|
|
return {{ T(1), T(0), T(0)},
|
||
|
|
{ T(0), T(1), T(0)},
|
||
|
|
{vector.x(), vector.y(), T(1)}};
|
||
|
|
}
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief 2D scaling matrix
|
||
|
14 years ago
|
* @param vector Scaling vector
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* @see rotationScaling() const, Matrix4::scaling(const Vector3&),
|
||
|
|
* Vector2::xScale(), Vector2::yScale()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
constexpr static Matrix3<T> scaling(const Vector2<T>& vector) {
|
||
|
|
return {{vector.x(), T(0), T(0)},
|
||
|
|
{ T(0), vector.y(), T(0)},
|
||
|
|
{ T(0), T(0), T(1)}};
|
||
|
|
}
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief 2D rotation matrix
|
||
|
13 years ago
|
* @param angle Rotation angle (counterclockwise)
|
||
|
14 years ago
|
*
|
||
|
13 years ago
|
* @see rotation() const, Complex::rotation(), DualComplex::rotation(),
|
||
|
13 years ago
|
* Matrix4::rotation(Rad, const Vector3&)
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
static Matrix3<T> rotation(Rad<T> angle);
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 2D reflection matrix
|
||
|
|
* @param normal Normal of the line through which to reflect
|
||
|
|
*
|
||
|
14 years ago
|
* Expects that the normal is normalized.
|
||
|
13 years ago
|
* @see Matrix4::reflection(), Vector::isNormalized()
|
||
|
14 years ago
|
*/
|
||
|
|
static Matrix3<T> reflection(const Vector2<T>& normal) {
|
||
|
13 years ago
|
CORRADE_ASSERT(normal.isNormalized(),
|
||
|
14 years ago
|
"Math::Matrix3::reflection(): normal must be normalized", {});
|
||
|
13 years ago
|
return from(Matrix<2, T>() - T(2)*normal*RectangularMatrix<1, 2, T>(normal).transposed(), {});
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 2D projection matrix
|
||
|
|
* @param size Size of the view
|
||
|
|
*
|
||
|
|
* @see Matrix4::orthographicProjection(), Matrix4::perspectiveProjection()
|
||
|
|
*/
|
||
|
|
static Matrix3<T> projection(const Vector2<T>& size) {
|
||
|
|
return scaling(2.0f/size);
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Create matrix from rotation/scaling part and translation part
|
||
|
|
* @param rotationScaling Rotation/scaling part (upper-left 2x2
|
||
|
|
* matrix)
|
||
|
|
* @param translation Translation part (first two elements of
|
||
|
|
* third column)
|
||
|
|
*
|
||
|
|
* @see rotationScaling() const, translation() const
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr static Matrix3<T> from(const Matrix<2, T>& rotationScaling, const Vector2<T>& translation) {
|
||
|
13 years ago
|
return {{rotationScaling[0], T(0)},
|
||
|
|
{rotationScaling[1], T(0)},
|
||
|
|
{ translation, T(1)}};
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/** @copydoc Matrix::Matrix(ZeroType) */
|
||
|
13 years ago
|
constexpr explicit Matrix3(typename Matrix<3, T>::ZeroType): Matrix<3, T>(Matrix<3, T>::Zero) {}
|
||
|
14 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Default constructor
|
||
|
|
*
|
||
|
|
* Creates identity matrix. You can also explicitly call this
|
||
|
|
* constructor with `Matrix3 m(Matrix3::Identity);`. Optional parameter
|
||
|
|
* @p value allows you to specify value on diagonal.
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr /*implicit*/ Matrix3(typename Matrix<3, T>::IdentityType = (Matrix<3, T>::Identity), T value = T(1)): Matrix<3, T>(Matrix<3, T>::Identity, value) {}
|
||
|
16 years ago
|
|
||
|
13 years ago
|
/** @brief %Matrix from column vectors */
|
||
|
13 years ago
|
constexpr /*implicit*/ Matrix3(const Vector3<T>& first, const Vector3<T>& second, const Vector3<T>& third): Matrix<3, T>(first, second, third) {}
|
||
|
16 years ago
|
|
||
|
13 years ago
|
/** @copydoc Matrix::Matrix(const RectangularMatrix<size, size, U>&) */
|
||
|
13 years ago
|
template<class U> constexpr explicit Matrix3(const RectangularMatrix<3, 3, U>& other): Matrix<3, T>(other) {}
|
||
|
13 years ago
|
|
||
|
|
/** @brief Construct matrix from external representation */
|
||
|
13 years ago
|
template<class U, class V = decltype(Implementation::RectangularMatrixConverter<3, 3, T, U>::from(std::declval<U>()))> constexpr explicit Matrix3(const U& other): Matrix<3, T>(Implementation::RectangularMatrixConverter<3, 3, T, U>::from(other)) {}
|
||
|
13 years ago
|
|
||
|
14 years ago
|
/** @brief Copy constructor */
|
||
|
13 years ago
|
constexpr Matrix3(const RectangularMatrix<3, 3, T>& other): Matrix<3, T>(other) {}
|
||
|
16 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Check whether the matrix represents rigid transformation
|
||
|
|
*
|
||
|
|
* Rigid transformation consists only of rotation and translation (i.e.
|
||
|
|
* no scaling or projection).
|
||
|
|
* @see isOrthogonal()
|
||
|
|
*/
|
||
|
13 years ago
|
bool isRigidTransformation() const {
|
||
|
13 years ago
|
return rotationScaling().isOrthogonal() && row(2) == Vector3<T>(T(0), T(0), T(1));
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 2D rotation and scaling part of the matrix
|
||
|
|
*
|
||
|
|
* Upper-left 2x2 part of the matrix.
|
||
|
13 years ago
|
* @see from(const Matrix<2, T>&, const Vector2&), rotation() const
|
||
|
13 years ago
|
* rotationNormalized(), @ref uniformScaling(), rotation(T),
|
||
|
13 years ago
|
* Matrix4::rotationScaling() const
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
constexpr Matrix<2, T> rotationScaling() const {
|
||
|
13 years ago
|
return {(*this)[0].xy(),
|
||
|
|
(*this)[1].xy()};
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief 2D rotation part of the matrix assuming there is no scaling
|
||
|
|
*
|
||
|
|
* Similar to @ref rotationScaling(), but additionally checks that the
|
||
|
|
* base vectors are normalized.
|
||
|
13 years ago
|
* @see rotation() const, @ref uniformScaling(),
|
||
|
|
* @ref Matrix4::rotationNormalized()
|
||
|
13 years ago
|
* @todo assert also orthogonality or this is good enough?
|
||
|
|
*/
|
||
|
|
Matrix<2, T> rotationNormalized() const {
|
||
|
|
CORRADE_ASSERT((*this)[0].xy().isNormalized() && (*this)[1].xy().isNormalized(),
|
||
|
|
"Math::Matrix3::rotationNormalized(): the rotation part is not normalized", {});
|
||
|
|
return {(*this)[0].xy(),
|
||
|
|
(*this)[1].xy()};
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 2D rotation part of the matrix
|
||
|
|
*
|
||
|
13 years ago
|
* Normalized upper-left 2x2 part of the matrix. Expects uniform
|
||
|
|
* scaling.
|
||
|
13 years ago
|
* @see rotationNormalized(), rotationScaling(), @ref uniformScaling(),
|
||
|
|
* rotation(T), Matrix4::rotation() const
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Matrix<2, T> rotation() const {
|
||
|
13 years ago
|
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[0].xy().dot(), (*this)[1].xy().dot()),
|
||
|
|
"Math::Matrix3::rotation(): the matrix doesn't have uniform scaling", {});
|
||
|
13 years ago
|
return {(*this)[0].xy().normalized(),
|
||
|
|
(*this)[1].xy().normalized()};
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
13 years ago
|
* @brief Uniform scaling part of the matrix, squared
|
||
|
13 years ago
|
*
|
||
|
13 years ago
|
* Squared length of vectors in upper-left 2x2 part of the matrix.
|
||
|
|
* Expects that the scaling is the same in all axes. Faster alternative
|
||
|
|
* to @ref uniformScaling(), because it doesn't compute the square
|
||
|
|
* root.
|
||
|
13 years ago
|
* @see @ref rotationScaling(), @ref rotation(),
|
||
|
13 years ago
|
* @ref rotationNormalized(), @ref scaling(const Vector2<T>&),
|
||
|
13 years ago
|
* @ref Matrix4::uniformScaling()
|
||
|
|
*/
|
||
|
13 years ago
|
T uniformScalingSquared() const {
|
||
|
13 years ago
|
const T scalingSquared = (*this)[0].xy().dot();
|
||
|
|
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[1].xy().dot(), scalingSquared),
|
||
|
|
"Math::Matrix3::uniformScaling(): the matrix doesn't have uniform scaling", {});
|
||
|
13 years ago
|
return scalingSquared;
|
||
|
13 years ago
|
}
|
||
|
13 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Uniform scaling part of the matrix
|
||
|
|
*
|
||
|
|
* Length of vectors in upper-left 2x2 part of the matrix. Expects that
|
||
|
|
* the scaling is the same in all axes. Use faster alternative
|
||
|
|
* @ref uniformScalingSquared() where possible.
|
||
|
|
* @see @ref rotationScaling(), @ref rotation(),
|
||
|
13 years ago
|
* @ref rotationNormalized(), @ref scaling(const Vector2<T>&),
|
||
|
13 years ago
|
* @ref Matrix4::uniformScaling()
|
||
|
|
*/
|
||
|
|
T uniformScaling() const { return std::sqrt(uniformScalingSquared()); }
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
14 years ago
|
* @brief Right-pointing 2D vector
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* First two elements of first column.
|
||
|
13 years ago
|
* @see up(), Vector2::xAxis(), Matrix4::right()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector2<T>& right() { return (*this)[0].xy(); }
|
||
|
|
constexpr Vector2<T> right() const { return (*this)[0].xy(); } /**< @overload */
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Up-pointing 2D vector
|
||
|
|
*
|
||
|
|
* First two elements of second column.
|
||
|
13 years ago
|
* @see right(), Vector2::yAxis(), Matrix4::up()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector2<T>& up() { return (*this)[1].xy(); }
|
||
|
|
constexpr Vector2<T> up() const { return (*this)[1].xy(); } /**< @overload */
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 2D translation part of the matrix
|
||
|
|
*
|
||
|
|
* First two elements of third column.
|
||
|
14 years ago
|
* @see from(const Matrix<2, T>&, const Vector2&),
|
||
|
|
* translation(const Vector2&), Matrix4::translation()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector2<T>& translation() { return (*this)[2].xy(); }
|
||
|
|
constexpr Vector2<T> translation() const { return (*this)[2].xy(); } /**< @overload */
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
13 years ago
|
* @brief Inverted rigid transformation matrix
|
||
|
14 years ago
|
*
|
||
|
13 years ago
|
* Expects that the matrix represents rigid transformation.
|
||
|
14 years ago
|
* Significantly faster than the general algorithm in inverted().
|
||
|
13 years ago
|
* @see isRigidTransformation(), invertedOrthogonal(),
|
||
|
|
* rotationScaling() const, translation() const
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Matrix3<T> invertedRigid() const;
|
||
|
14 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Transform 2D vector with the matrix
|
||
|
|
*
|
||
|
13 years ago
|
* Unlike in transformPoint(), translation is not involved in the
|
||
|
|
* transformation. @f[
|
||
|
13 years ago
|
* \boldsymbol v' = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ 0 \end{pmatrix}
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see Complex::transformVector(), Matrix4::transformVector()
|
||
|
13 years ago
|
* @todo extract 2x2 matrix and multiply directly? (benchmark that)
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Vector2<T> transformVector(const Vector2<T>& vector) const {
|
||
|
13 years ago
|
return ((*this)*Vector3<T>(vector, T(0))).xy();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Transform 2D point with the matrix
|
||
|
|
*
|
||
|
13 years ago
|
* Unlike in transformVector(), translation is also involved in the
|
||
|
|
* transformation. @f[
|
||
|
13 years ago
|
* \boldsymbol v' = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ 1 \end{pmatrix}
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see DualComplex::transformPoint(), Matrix4::transformPoint()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Vector2<T> transformPoint(const Vector2<T>& vector) const {
|
||
|
13 years ago
|
return ((*this)*Vector3<T>(vector, T(1))).xy();
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(3, 3, Matrix3<T>)
|
||
|
13 years ago
|
MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(3, Matrix3, Vector3)
|
||
|
16 years ago
|
};
|
||
|
|
|
||
|
13 years ago
|
MAGNUM_MATRIXn_OPERATOR_IMPLEMENTATION(3, Matrix3)
|
||
|
13 years ago
|
|
||
|
14 years ago
|
/** @debugoperator{Magnum::Math::Matrix3} */
|
||
|
14 years ago
|
template<class T> inline Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Matrix3<T>& value) {
|
||
|
|
return debug << static_cast<const Matrix<3, T>&>(value);
|
||
|
15 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class T> Matrix3<T> Matrix3<T>::rotation(const Rad<T> angle) {
|
||
|
|
const T sine = std::sin(T(angle));
|
||
|
|
const T cosine = std::cos(T(angle));
|
||
|
|
|
||
|
|
return {{ cosine, sine, T(0)},
|
||
|
|
{ -sine, cosine, T(0)},
|
||
|
|
{ T(0), T(0), T(1)}};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> inline Matrix3<T> Matrix3<T>::invertedRigid() const {
|
||
|
|
CORRADE_ASSERT(isRigidTransformation(),
|
||
|
|
"Math::Matrix3::invertedRigid(): the matrix doesn't represent rigid transformation", {});
|
||
|
|
|
||
|
|
Matrix<2, T> inverseRotation = rotationScaling().transposed();
|
||
|
|
return from(inverseRotation, inverseRotation*-translation());
|
||
|
|
}
|
||
|
|
|
||
|
16 years ago
|
}}
|
||
|
|
|
||
|
14 years ago
|
namespace Corrade { namespace Utility {
|
||
|
|
/** @configurationvalue{Magnum::Math::Matrix3} */
|
||
|
|
template<class T> struct ConfigurationValue<Magnum::Math::Matrix3<T>>: public ConfigurationValue<Magnum::Math::Matrix<3, T>> {};
|
||
|
|
}}
|
||
|
|
|
||
|
16 years ago
|
#endif
|