|
|
|
|
#ifndef Magnum_Math_Matrix3_h
|
|
|
|
|
#define Magnum_Math_Matrix3_h
|
|
|
|
|
/*
|
|
|
|
|
This file is part of Magnum.
|
|
|
|
|
|
|
|
|
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015
|
|
|
|
|
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::Math::Matrix3
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Magnum/Math/Matrix.h"
|
|
|
|
|
#include "Magnum/Math/Vector3.h"
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Math {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief 2D transformation matrix
|
|
|
|
|
@tparam T Underlying data type
|
|
|
|
|
|
|
|
|
|
See @ref matrix-vector and @ref transformations for brief introduction.
|
|
|
|
|
@see @ref Magnum::Matrix3, @ref Magnum::Matrix3d, @ref Matrix3x3,
|
|
|
|
|
@ref DualComplex, @ref SceneGraph::MatrixTransformation2D
|
|
|
|
|
@configurationvalueref{Magnum::Math::Matrix3}
|
|
|
|
|
*/
|
|
|
|
|
template<class T> class Matrix3: public Matrix3x3<T> {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D translation matrix
|
|
|
|
|
* @param vector Translation vector
|
|
|
|
|
*
|
|
|
|
|
* @see @ref translation() const, @ref DualComplex::translation(),
|
|
|
|
|
* @ref Matrix4::translation(const Vector3<T>&),
|
|
|
|
|
* @ref Vector2::xAxis(), @ref Vector2::yAxis()
|
|
|
|
|
*/
|
|
|
|
|
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)}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D scaling matrix
|
|
|
|
|
* @param vector Scaling vector
|
|
|
|
|
*
|
|
|
|
|
* @see @ref rotationScaling(),
|
|
|
|
|
* @ref Matrix4::scaling(const Vector3<T>&),
|
|
|
|
|
* @ref Vector2::xScale(), @ref Vector2::yScale()
|
|
|
|
|
*/
|
|
|
|
|
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)}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D rotation matrix
|
|
|
|
|
* @param angle Rotation angle (counterclockwise)
|
|
|
|
|
*
|
|
|
|
|
* @see @ref rotation() const, @ref Complex::rotation(),
|
|
|
|
|
* @ref DualComplex::rotation(),
|
|
|
|
|
* @ref Matrix4::rotation(Rad, const Vector3<T>&)
|
|
|
|
|
*/
|
|
|
|
|
static Matrix3<T> rotation(Rad<T> angle);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D reflection matrix
|
|
|
|
|
* @param normal Normal of the line through which to reflect
|
|
|
|
|
*
|
|
|
|
|
* Expects that the normal is normalized. Reflection along axes can be
|
|
|
|
|
* done in a slightly simpler way also using @ref scaling(), e.g.
|
|
|
|
|
* `Matrix3::reflection(Vector2::yAxis())` is equivalent to
|
|
|
|
|
* `Matrix3::scaling(Vector2::yScale(-1.0f))`.
|
|
|
|
|
* @see @ref Matrix4::reflection(), @ref Vector::isNormalized()
|
|
|
|
|
*/
|
|
|
|
|
static Matrix3<T> reflection(const Vector2<T>& normal) {
|
|
|
|
|
CORRADE_ASSERT(normal.isNormalized(),
|
|
|
|
|
"Math::Matrix3::reflection(): normal must be normalized", {});
|
|
|
|
|
return from(Matrix2x2<T>() - T(2)*normal*RectangularMatrix<1, 2, T>(normal).transposed(), {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D shearing matrix along X axis
|
|
|
|
|
* @param amount Shearing amount
|
|
|
|
|
*
|
|
|
|
|
* Y axis remains unchanged.
|
|
|
|
|
* @see @ref shearingY(), @ref Matrix4::shearingXY(),
|
|
|
|
|
* @ref Matrix4::shearingXZ(), @ref Matrix4::shearingYZ()
|
|
|
|
|
*/
|
|
|
|
|
constexpr static Matrix3<T> shearingX(T amount) {
|
|
|
|
|
return {{ T(1), T(0), T(0)},
|
|
|
|
|
{amount, T(1), T(0)},
|
|
|
|
|
{ T(0), T(0), T(1)}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D shearing matrix along Y axis
|
|
|
|
|
* @param amount Shearing amount
|
|
|
|
|
*
|
|
|
|
|
* X axis remains unchanged.
|
|
|
|
|
* @see @ref shearingX(), @ref Matrix4::shearingXY(),
|
|
|
|
|
* @ref Matrix4::shearingXZ(), @ref Matrix4::shearingYZ()
|
|
|
|
|
*/
|
|
|
|
|
constexpr static Matrix3<T> shearingY(T amount) {
|
|
|
|
|
return {{T(1), amount, T(0)},
|
|
|
|
|
{T(0), T(1), T(0)},
|
|
|
|
|
{T(0), T(0), T(1)}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D projection matrix
|
|
|
|
|
* @param size Size of the view
|
|
|
|
|
*
|
|
|
|
|
* @see @ref Matrix4::orthographicProjection(),
|
|
|
|
|
* @ref Matrix4::perspectiveProjection()
|
|
|
|
|
*/
|
|
|
|
|
static Matrix3<T> projection(const Vector2<T>& size) {
|
|
|
|
|
return scaling(2.0f/size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 @ref rotationScaling(), @ref translation() const
|
|
|
|
|
*/
|
|
|
|
|
constexpr static Matrix3<T> from(const Matrix2x2<T>& rotationScaling, const Vector2<T>& translation) {
|
Math: matrix/vector rework, part 2: matrix as array of column vectors.
Overall architecture is simplififed with this change and also it's not
needed to use reinterpret_cast in matrix internals anymore, thus there
is no need for operator() and [][] works now always as expected without
any risk of GCC misoptimizations.
On the other side, constructing matrix from list of elements is not
possible anymore. You have to specify the elements as list of
column vectors, which might be less convenient to write, but it helps to
distinguish what is column and what is row:
Matrix<2, int> a(1, 2, // before
3, 4);
Matrix<2, int> a(Vector<2, int>(1, 2), // now
Vector<2, int>(3, 4));
For some matrix specializations (i.e. Matrix3 and Matrix4) it is
possible to use list-initialization instead of explicit type
specification:
Matrix<3, int>({1, 2, 3},
{4, 5, 6},
{7, 8, 9});
I didn't yet figure out how to properly implement the general
(constexpr) constructor to also take lists, so it's a bit ugly for now.
Matrix operations are now done column-wise, which should help with
future SIMD implementations, documentation is also updated accordingly.
I also removed forgotten remains of matrix/matrix operator*=(), which
can be confusing, as the multiplication is not commutative. Why it is
not present is explained in d9c900f076f2f87c7b7ba3f37a3179c0c0e4a02c.
13 years ago
|
|
|
return {{rotationScaling[0], T(0)},
|
|
|
|
|
{rotationScaling[1], T(0)},
|
|
|
|
|
{ translation, T(1)}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Default constructor
|
|
|
|
|
*
|
Math: more explicit default zero/identity constructors.
Some classes are by default constructed zero-filled while other are set
to identity and the only way to to check this is to look into the
documentation. This changes the default constructor of all classes to
take an optional "tag" which acts as documentation about how the type is
constructed. Note that this result in no behavioral changes, just
ability to be more explicit when writing the code. Example:
// These two are equivalent
Quaternion q1;
Quaternion q2{Math::IdentityInit};
// These two are equivalent
Vector4 vec1;
Vector4 vec2{Math::ZeroInit};
Matrix4 a{Math::IdentityInit, 2}; // 2 on diagonal
Matrix4 b{Math::ZeroInit}; // all zero
This functionality was already present in some ugly form in Matrix,
Matrix3 and Matrix4 classes. It was long and ugly to write, so it is
now generalized into the new Math::IdentityInit and Math::ZeroInit tags,
the original Matrix::IdentityType, Matrix::Identity, Matrix::ZeroType
and Matrix::Zero are deprecated and will be removed in the future
release.
Math::Matrix<7, Int> m{Math::Matrix<7, Int>::Identity}; // before
Math::Matrix<7, Int> m{Math::IdentityInit}; // now
11 years ago
|
|
|
* Creates identity matrix. @p value allows you to specify value on
|
|
|
|
|
* diagonal.
|
|
|
|
|
*/
|
|
|
|
|
constexpr /*implicit*/ Matrix3(IdentityInitT = IdentityInit, T value = T{1})
|
|
|
|
|
/** @todoc remove workaround when doxygen is sane */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
#ifndef CORRADE_MSVC2015_COMPATIBILITY
|
|
|
|
|
: Matrix3x3<T>{IdentityInit, value}
|
|
|
|
|
#else
|
|
|
|
|
/* Avoid using non-constexpr version, also MSVC 2015 can't handle
|
|
|
|
|
{} here */
|
|
|
|
|
: Matrix3x3<T>(Vector<3, T>{value, T(0), T(0)},
|
|
|
|
|
Vector<3, T>{T(0), value, T(0)},
|
|
|
|
|
Vector<3, T>{T(0), T(0), value})
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
{}
|
Math: more explicit default zero/identity constructors.
Some classes are by default constructed zero-filled while other are set
to identity and the only way to to check this is to look into the
documentation. This changes the default constructor of all classes to
take an optional "tag" which acts as documentation about how the type is
constructed. Note that this result in no behavioral changes, just
ability to be more explicit when writing the code. Example:
// These two are equivalent
Quaternion q1;
Quaternion q2{Math::IdentityInit};
// These two are equivalent
Vector4 vec1;
Vector4 vec2{Math::ZeroInit};
Matrix4 a{Math::IdentityInit, 2}; // 2 on diagonal
Matrix4 b{Math::ZeroInit}; // all zero
This functionality was already present in some ugly form in Matrix,
Matrix3 and Matrix4 classes. It was long and ugly to write, so it is
now generalized into the new Math::IdentityInit and Math::ZeroInit tags,
the original Matrix::IdentityType, Matrix::Identity, Matrix::ZeroType
and Matrix::Zero are deprecated and will be removed in the future
release.
Math::Matrix<7, Int> m{Math::Matrix<7, Int>::Identity}; // before
Math::Matrix<7, Int> m{Math::IdentityInit}; // now
11 years ago
|
|
|
|
|
|
|
|
/** @copydoc Matrix::Matrix(ZeroInitT) */
|
|
|
|
|
constexpr explicit Matrix3(ZeroInitT)
|
|
|
|
|
/** @todoc remove workaround when doxygen is sane */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
#ifndef CORRADE_MSVC2015_COMPATIBILITY
|
|
|
|
|
: Matrix3x3<T>(ZeroInit)
|
|
|
|
|
#else
|
|
|
|
|
/* Avoid using non-constexpr version, also MSVC 2015 can't handle
|
|
|
|
|
{} here */
|
|
|
|
|
: Matrix3x3<T>(Vector<3, T>{T(0), T(0), T(0)},
|
|
|
|
|
Vector<3, T>{T(0), T(0), T(0)},
|
|
|
|
|
Vector<3, T>{T(0), T(0), T(0)})
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/** @copydoc Matrix::Matrix(NoInitT) */
|
|
|
|
|
constexpr explicit Matrix3(NoInitT)
|
|
|
|
|
/** @todoc remove workaround when doxygen is sane */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
/* MSVC 2015 can't handle {} here */
|
|
|
|
|
: Matrix3x3<T>(NoInit)
|
|
|
|
|
#endif
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/** @brief Matrix from column vectors */
|
|
|
|
|
constexpr /*implicit*/ Matrix3(const Vector3<T>& first, const Vector3<T>& second, const Vector3<T>& third): Matrix3x3<T>(first, second, third) {}
|
|
|
|
|
|
|
|
|
|
/** @copydoc Matrix::Matrix(const RectangularMatrix<size, size, U>&) */
|
|
|
|
|
template<class U> constexpr explicit Matrix3(const RectangularMatrix<3, 3, U>& other): Matrix3x3<T>(other) {}
|
|
|
|
|
|
|
|
|
|
/** @brief Construct matrix from external representation */
|
|
|
|
|
template<class U, class V = decltype(Implementation::RectangularMatrixConverter<3, 3, T, U>::from(std::declval<U>()))> constexpr explicit Matrix3(const U& other): Matrix3x3<T>(Implementation::RectangularMatrixConverter<3, 3, T, U>::from(other)) {}
|
|
|
|
|
|
|
|
|
|
/** @brief Copy constructor */
|
|
|
|
|
constexpr Matrix3(const RectangularMatrix<3, 3, T>& other): Matrix3x3<T>(other) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Check whether the matrix represents rigid transformation
|
|
|
|
|
*
|
|
|
|
|
* Rigid transformation consists only of rotation and translation (i.e.
|
|
|
|
|
* no scaling or projection).
|
|
|
|
|
* @see @ref isOrthogonal()
|
|
|
|
|
*/
|
|
|
|
|
bool isRigidTransformation() const {
|
|
|
|
|
return rotationScaling().isOrthogonal() && row(2) == Vector3<T>(T(0), T(0), T(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D rotation and scaling part of the matrix
|
|
|
|
|
*
|
|
|
|
|
* Upper-left 2x2 part of the matrix.
|
|
|
|
|
* @see @ref from(const Matrix2x2<T>&, const Vector2<T>&),
|
|
|
|
|
* @ref rotation() const, @ref rotationNormalized(),
|
|
|
|
|
* @ref uniformScaling(), @ref rotation(Rad<T>),
|
|
|
|
|
* @ref Matrix4::rotationScaling()
|
|
|
|
|
*/
|
|
|
|
|
constexpr Matrix2x2<T> rotationScaling() const {
|
Math: matrix/vector rework, part 2: matrix as array of column vectors.
Overall architecture is simplififed with this change and also it's not
needed to use reinterpret_cast in matrix internals anymore, thus there
is no need for operator() and [][] works now always as expected without
any risk of GCC misoptimizations.
On the other side, constructing matrix from list of elements is not
possible anymore. You have to specify the elements as list of
column vectors, which might be less convenient to write, but it helps to
distinguish what is column and what is row:
Matrix<2, int> a(1, 2, // before
3, 4);
Matrix<2, int> a(Vector<2, int>(1, 2), // now
Vector<2, int>(3, 4));
For some matrix specializations (i.e. Matrix3 and Matrix4) it is
possible to use list-initialization instead of explicit type
specification:
Matrix<3, int>({1, 2, 3},
{4, 5, 6},
{7, 8, 9});
I didn't yet figure out how to properly implement the general
(constexpr) constructor to also take lists, so it's a bit ugly for now.
Matrix operations are now done column-wise, which should help with
future SIMD implementations, documentation is also updated accordingly.
I also removed forgotten remains of matrix/matrix operator*=(), which
can be confusing, as the multiplication is not commutative. Why it is
not present is explained in d9c900f076f2f87c7b7ba3f37a3179c0c0e4a02c.
13 years ago
|
|
|
return {(*this)[0].xy(),
|
|
|
|
|
(*this)[1].xy()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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.
|
|
|
|
|
* @see @ref rotation() const, @ref uniformScaling(),
|
|
|
|
|
* @ref Matrix4::rotationNormalized()
|
|
|
|
|
* @todo assert also orthogonality or this is good enough?
|
|
|
|
|
*/
|
|
|
|
|
Matrix2x2<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()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D rotation part of the matrix
|
|
|
|
|
*
|
|
|
|
|
* Normalized upper-left 2x2 part of the matrix. Expects uniform
|
|
|
|
|
* scaling.
|
|
|
|
|
* @see @ref rotationNormalized(), @ref rotationScaling(),
|
|
|
|
|
* @ref uniformScaling(), @ref rotation(Rad<T>),
|
|
|
|
|
* @ref Matrix4::rotation() const
|
|
|
|
|
*/
|
|
|
|
|
Matrix2x2<T> rotation() const {
|
|
|
|
|
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[0].xy().dot(), (*this)[1].xy().dot()),
|
|
|
|
|
"Math::Matrix3::rotation(): the matrix doesn't have uniform scaling", {});
|
Math: matrix/vector rework, part 2: matrix as array of column vectors.
Overall architecture is simplififed with this change and also it's not
needed to use reinterpret_cast in matrix internals anymore, thus there
is no need for operator() and [][] works now always as expected without
any risk of GCC misoptimizations.
On the other side, constructing matrix from list of elements is not
possible anymore. You have to specify the elements as list of
column vectors, which might be less convenient to write, but it helps to
distinguish what is column and what is row:
Matrix<2, int> a(1, 2, // before
3, 4);
Matrix<2, int> a(Vector<2, int>(1, 2), // now
Vector<2, int>(3, 4));
For some matrix specializations (i.e. Matrix3 and Matrix4) it is
possible to use list-initialization instead of explicit type
specification:
Matrix<3, int>({1, 2, 3},
{4, 5, 6},
{7, 8, 9});
I didn't yet figure out how to properly implement the general
(constexpr) constructor to also take lists, so it's a bit ugly for now.
Matrix operations are now done column-wise, which should help with
future SIMD implementations, documentation is also updated accordingly.
I also removed forgotten remains of matrix/matrix operator*=(), which
can be confusing, as the multiplication is not commutative. Why it is
not present is explained in d9c900f076f2f87c7b7ba3f37a3179c0c0e4a02c.
13 years ago
|
|
|
return {(*this)[0].xy().normalized(),
|
|
|
|
|
(*this)[1].xy().normalized()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Uniform scaling part of the matrix, squared
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* @see @ref rotationScaling(), @ref rotation() const,
|
|
|
|
|
* @ref rotationNormalized(), @ref scaling(const Vector2<T>&),
|
|
|
|
|
* @ref Matrix4::uniformScaling()
|
|
|
|
|
*/
|
|
|
|
|
T uniformScalingSquared() const {
|
|
|
|
|
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", {});
|
|
|
|
|
return scalingSquared;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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() const,
|
|
|
|
|
* @ref rotationNormalized(), @ref scaling(const Vector2<T>&),
|
|
|
|
|
* @ref Matrix4::uniformScaling()
|
|
|
|
|
*/
|
|
|
|
|
T uniformScaling() const { return std::sqrt(uniformScalingSquared()); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Right-pointing 2D vector
|
|
|
|
|
*
|
|
|
|
|
* First two elements of first column.
|
|
|
|
|
* @see @ref up(), @ref Vector2::xAxis(), @ref Matrix4::right()
|
|
|
|
|
*/
|
|
|
|
|
Vector2<T>& right() { return (*this)[0].xy(); }
|
|
|
|
|
constexpr Vector2<T> right() const { return (*this)[0].xy(); } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Up-pointing 2D vector
|
|
|
|
|
*
|
|
|
|
|
* First two elements of second column.
|
|
|
|
|
* @see @ref right(), @ref Vector2::yAxis(), @ref Matrix4::up()
|
|
|
|
|
*/
|
|
|
|
|
Vector2<T>& up() { return (*this)[1].xy(); }
|
|
|
|
|
constexpr Vector2<T> up() const { return (*this)[1].xy(); } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 2D translation part of the matrix
|
|
|
|
|
*
|
|
|
|
|
* First two elements of third column.
|
|
|
|
|
* @see @ref from(const Matrix2x2<T>&, const Vector2<T>&),
|
|
|
|
|
* @ref translation(const Vector2<T>&),
|
|
|
|
|
* @ref Matrix4::translation()
|
|
|
|
|
*/
|
|
|
|
|
Vector2<T>& translation() { return (*this)[2].xy(); }
|
|
|
|
|
constexpr Vector2<T> translation() const { return (*this)[2].xy(); } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Inverted rigid transformation matrix
|
|
|
|
|
*
|
|
|
|
|
* Expects that the matrix represents rigid transformation.
|
|
|
|
|
* Significantly faster than the general algorithm in @ref inverted(). @f[
|
|
|
|
|
* A^{-1} = \begin{pmatrix} (A^{2,2})^T & (A^{2,2})^T \begin{pmatrix} a_{2,0} \\ a_{2,1} \end{pmatrix} \\ \begin{array}{cc} 0 & 0 \end{array} & 1 \end{pmatrix}
|
|
|
|
|
* @f]
|
|
|
|
|
* @f$ A^{i, j} @f$ is matrix without i-th row and j-th column, see
|
|
|
|
|
* @ref ij()
|
|
|
|
|
* @see @ref isRigidTransformation(), @ref invertedOrthogonal(),
|
|
|
|
|
* @ref rotationScaling(), @ref translation() const,
|
|
|
|
|
* @ref Matrix4::invertedRigid()
|
|
|
|
|
*/
|
|
|
|
|
Matrix3<T> invertedRigid() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Transform 2D vector with the matrix
|
|
|
|
|
*
|
|
|
|
|
* Unlike in @ref transformPoint(), translation is not involved in the
|
|
|
|
|
* transformation. @f[
|
|
|
|
|
* \boldsymbol v' = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ 0 \end{pmatrix}
|
|
|
|
|
* @f]
|
|
|
|
|
* @see @ref Complex::transformVector(),
|
|
|
|
|
* @ref Matrix4::transformVector()
|
|
|
|
|
* @todo extract 2x2 matrix and multiply directly? (benchmark that)
|
|
|
|
|
*/
|
|
|
|
|
Vector2<T> transformVector(const Vector2<T>& vector) const {
|
|
|
|
|
return ((*this)*Vector3<T>(vector, T(0))).xy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Transform 2D point with the matrix
|
|
|
|
|
*
|
|
|
|
|
* Unlike in @ref transformVector(), translation is also involved in
|
|
|
|
|
* the transformation. @f[
|
|
|
|
|
* \boldsymbol v' = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ 1 \end{pmatrix}
|
|
|
|
|
* @f]
|
|
|
|
|
* @see @ref DualComplex::transformPoint(),
|
|
|
|
|
* @ref Matrix4::transformPoint()
|
|
|
|
|
*/
|
|
|
|
|
Vector2<T> transformPoint(const Vector2<T>& vector) const {
|
|
|
|
|
return ((*this)*Vector3<T>(vector, T(1))).xy();
|
|
|
|
|
}
|
|
|
|
|
|
Math: matrix/vector rework, part 2: matrix as array of column vectors.
Overall architecture is simplififed with this change and also it's not
needed to use reinterpret_cast in matrix internals anymore, thus there
is no need for operator() and [][] works now always as expected without
any risk of GCC misoptimizations.
On the other side, constructing matrix from list of elements is not
possible anymore. You have to specify the elements as list of
column vectors, which might be less convenient to write, but it helps to
distinguish what is column and what is row:
Matrix<2, int> a(1, 2, // before
3, 4);
Matrix<2, int> a(Vector<2, int>(1, 2), // now
Vector<2, int>(3, 4));
For some matrix specializations (i.e. Matrix3 and Matrix4) it is
possible to use list-initialization instead of explicit type
specification:
Matrix<3, int>({1, 2, 3},
{4, 5, 6},
{7, 8, 9});
I didn't yet figure out how to properly implement the general
(constexpr) constructor to also take lists, so it's a bit ugly for now.
Matrix operations are now done column-wise, which should help with
future SIMD implementations, documentation is also updated accordingly.
I also removed forgotten remains of matrix/matrix operator*=(), which
can be confusing, as the multiplication is not commutative. Why it is
not present is explained in d9c900f076f2f87c7b7ba3f37a3179c0c0e4a02c.
13 years ago
|
|
|
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(3, 3, Matrix3<T>)
|
|
|
|
|
MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(3, Matrix3, Vector3)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
MAGNUM_MATRIXn_OPERATOR_IMPLEMENTATION(3, Matrix3)
|
|
|
|
|
#endif
|
Math: matrix/vector rework, part 2: matrix as array of column vectors.
Overall architecture is simplififed with this change and also it's not
needed to use reinterpret_cast in matrix internals anymore, thus there
is no need for operator() and [][] works now always as expected without
any risk of GCC misoptimizations.
On the other side, constructing matrix from list of elements is not
possible anymore. You have to specify the elements as list of
column vectors, which might be less convenient to write, but it helps to
distinguish what is column and what is row:
Matrix<2, int> a(1, 2, // before
3, 4);
Matrix<2, int> a(Vector<2, int>(1, 2), // now
Vector<2, int>(3, 4));
For some matrix specializations (i.e. Matrix3 and Matrix4) it is
possible to use list-initialization instead of explicit type
specification:
Matrix<3, int>({1, 2, 3},
{4, 5, 6},
{7, 8, 9});
I didn't yet figure out how to properly implement the general
(constexpr) constructor to also take lists, so it's a bit ugly for now.
Matrix operations are now done column-wise, which should help with
future SIMD implementations, documentation is also updated accordingly.
I also removed forgotten remains of matrix/matrix operator*=(), which
can be confusing, as the multiplication is not commutative. Why it is
not present is explained in d9c900f076f2f87c7b7ba3f37a3179c0c0e4a02c.
13 years ago
|
|
|
|
|
|
|
|
/** @debugoperator{Magnum::Math::Matrix3} */
|
|
|
|
|
template<class T> inline Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Matrix3<T>& value) {
|
|
|
|
|
return debug << static_cast<const Matrix3x3<T>&>(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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", {});
|
|
|
|
|
|
|
|
|
|
Matrix2x2<T> inverseRotation = rotationScaling().transposed();
|
|
|
|
|
return from(inverseRotation, inverseRotation*-translation());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
namespace Corrade { namespace Utility {
|
|
|
|
|
/** @configurationvalue{Magnum::Math::Matrix3} */
|
|
|
|
|
template<class T> struct ConfigurationValue<Magnum::Math::Matrix3<T>>: public ConfigurationValue<Magnum::Math::Matrix3x3<T>> {};
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
#endif
|