|
|
|
|
#ifndef Magnum_Math_Matrix_h
|
|
|
|
|
#define Magnum_Math_Matrix_h
|
|
|
|
|
/*
|
|
|
|
|
This file is part of Magnum.
|
|
|
|
|
|
|
|
|
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016
|
|
|
|
|
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::Matrix, typedef @ref Magnum::Math::Matrix2x2, @ref Magnum::Math::Matrix3x3, @ref Magnum::Math::Matrix4x4
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Magnum/Math/RectangularMatrix.h"
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Math {
|
|
|
|
|
|
|
|
|
|
namespace Implementation {
|
|
|
|
|
template<std::size_t, class> struct MatrixDeterminant;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Square matrix
|
|
|
|
|
@tparam size Matrix size
|
|
|
|
|
@tparam T Data type
|
|
|
|
|
|
|
|
|
|
See @ref matrix-vector for brief introduction.
|
|
|
|
|
|
|
|
|
|
@configurationvalueref{Magnum::Math::Matrix}
|
|
|
|
|
@see @ref Matrix2x2, @ref Matrix3x3, @ref Matrix4x4
|
|
|
|
|
*/
|
|
|
|
|
template<std::size_t size, class T> class Matrix: public RectangularMatrix<size, size, T> {
|
|
|
|
|
public:
|
|
|
|
|
enum: std::size_t {
|
|
|
|
|
Size = size /**< Matrix size */
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
#ifdef MAGNUM_BUILD_DEPRECATED
|
|
|
|
|
/**
|
|
|
|
|
* @brief Pass to constructor to create zero-filled matrix
|
|
|
|
|
* @deprecated Use @ref ZeroInitT and @ref ZeroInit instead.
|
|
|
|
|
*/
|
|
|
|
|
#ifdef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
enum ZeroType { Zero };
|
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
|
|
|
#else
|
|
|
|
|
CORRADE_DEPRECATED("use Math::ZeroInitT instead") typedef ZeroInitT ZeroType;
|
|
|
|
|
CORRADE_DEPRECATED("use Math::ZeroInit instead") constexpr static ZeroInitT Zero{ZeroInitT::Init{}};
|
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
|
|
|
#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
|
|
|
* @brief Pass to constructor to create identity matrix
|
|
|
|
|
* @deprecated Use @ref IdentityInitT and @ref IdentityInit instead.
|
|
|
|
|
*/
|
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
|
|
|
#ifdef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
enum IdentityType { Identity };
|
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
|
|
|
#else
|
|
|
|
|
CORRADE_DEPRECATED("use Math::IdentityInitT instead") typedef IdentityInitT IdentityType;
|
|
|
|
|
CORRADE_DEPRECATED("use Math::IdentityInit instead") constexpr static IdentityInitT Identity{IdentityInitT::Init{}};
|
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
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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*/ Matrix(IdentityInitT = IdentityInit, T value = T(1))
|
|
|
|
|
/** @todoc remove workaround when doxygen is sane */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
/* MSVC 2015 can't handle {} here */
|
|
|
|
|
: RectangularMatrix<size, size, T>(typename Implementation::GenerateSequence<size>::Type(), Vector<size, T>(value))
|
|
|
|
|
#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 RectangularMatrix::RectangularMatrix(ZeroInitT) */
|
|
|
|
|
constexpr explicit Matrix(ZeroInitT)
|
|
|
|
|
/** @todoc remove workaround when doxygen is sane */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
/* MSVC 2015 can't handle {} here */
|
|
|
|
|
: RectangularMatrix<size, size, T>(ZeroInit)
|
|
|
|
|
#endif
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/** @copydoc RectangularMatrix::RectangularMatrix(NoInitT) */
|
|
|
|
|
constexpr explicit Matrix(NoInitT)
|
|
|
|
|
/** @todoc remove workaround when doxygen is sane */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
/* MSVC 2015 can't handle {} here */
|
|
|
|
|
: RectangularMatrix<size, size, T>(NoInit)
|
|
|
|
|
#endif
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Matrix from column vectors
|
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
|
|
|
* @param first First column vector
|
|
|
|
|
* @param next Next column vectors
|
|
|
|
|
*/
|
|
|
|
|
template<class ...U> constexpr /*implicit*/ Matrix(const Vector<size, T>& first, const U&... next): RectangularMatrix<size, size, T>(first, next...) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Construct matrix from another of different type
|
|
|
|
|
*
|
|
|
|
|
* Performs only default casting on the values, no rounding or
|
|
|
|
|
* anything else. Example usage:
|
|
|
|
|
* @code
|
|
|
|
|
* Matrix2x2<Float> floatingPoint({1.3f, 2.7f},
|
|
|
|
|
* {-15.0f, 7.0f});
|
|
|
|
|
* Matrix2x2<Byte> integral(floatingPoint);
|
|
|
|
|
* // integral == {{1, 2}, {-15, 7}}
|
|
|
|
|
* @endcode
|
|
|
|
|
*/
|
|
|
|
|
template<class U> constexpr explicit Matrix(const RectangularMatrix<size, size, U>& other): RectangularMatrix<size, size, T>(other) {}
|
|
|
|
|
|
|
|
|
|
/** @brief Construct matrix from external representation */
|
|
|
|
|
template<class U, class V = decltype(Implementation::RectangularMatrixConverter<size, size, T, U>::from(std::declval<U>()))> constexpr explicit Matrix(const U& other): RectangularMatrix<size, size, T>(Implementation::RectangularMatrixConverter<size, size, T, U>::from(other)) {}
|
|
|
|
|
|
|
|
|
|
/** @brief Copy constructor */
|
|
|
|
|
constexpr Matrix(const RectangularMatrix<size, size, T>& other): RectangularMatrix<size, size, T>(other) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Whether the matrix is orthogonal
|
|
|
|
|
*
|
|
|
|
|
* The matrix is orthogonal if its transpose is equal to its inverse: @f[
|
|
|
|
|
* Q^T = Q^{-1}
|
|
|
|
|
* @f]
|
|
|
|
|
* @see @ref transposed(), @ref inverted(),
|
|
|
|
|
* @ref Matrix3::isRigidTransformation(),
|
|
|
|
|
* @ref Matrix4::isRigidTransformation()
|
|
|
|
|
*/
|
|
|
|
|
bool isOrthogonal() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Trace of the matrix
|
|
|
|
|
*
|
|
|
|
|
* @f[
|
|
|
|
|
* tr(A) = \sum_{i=1}^n a_{i,i}
|
|
|
|
|
* @f]
|
|
|
|
|
*/
|
|
|
|
|
T trace() const { return RectangularMatrix<size, size, T>::diagonal().sum(); }
|
|
|
|
|
|
|
|
|
|
/** @brief Matrix without given column and row */
|
|
|
|
|
Matrix<size-1, T> ij(std::size_t skipCol, std::size_t skipRow) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Determinant
|
|
|
|
|
*
|
|
|
|
|
* Computed recursively using Laplace's formula: @f[
|
|
|
|
|
* \det(A) = \sum_{j=1}^n (-1)^{i+j} a_{i,j} \det(A^{i,j})
|
|
|
|
|
* @f] @f$ A^{i, j} @f$ is matrix without i-th row and j-th column, see
|
|
|
|
|
* @ref ij(). The formula is expanded down to 2x2 matrix, where the
|
|
|
|
|
* determinant is computed directly: @f[
|
|
|
|
|
* \det(A) = a_{0, 0} a_{1, 1} - a_{1, 0} a_{0, 1}
|
|
|
|
|
* @f]
|
|
|
|
|
*/
|
|
|
|
|
T determinant() const { return Implementation::MatrixDeterminant<size, T>()(*this); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Inverted matrix
|
|
|
|
|
*
|
|
|
|
|
* Computed using Cramer's rule: @f[
|
|
|
|
|
* A^{-1} = \frac{1}{\det(A)} Adj(A)
|
|
|
|
|
* @f]
|
|
|
|
|
* See @ref invertedOrthogonal(), @ref Matrix3::invertedRigid() and
|
|
|
|
|
* @ref Matrix4::invertedRigid() which are faster alternatives for
|
|
|
|
|
* particular matrix types.
|
|
|
|
|
*/
|
|
|
|
|
Matrix<size, T> inverted() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Inverted orthogonal matrix
|
|
|
|
|
*
|
|
|
|
|
* Equivalent to @ref transposed(), expects that the matrix is
|
|
|
|
|
* orthogonal. @f[
|
|
|
|
|
* A^{-1} = A^T
|
|
|
|
|
* @f]
|
|
|
|
|
* @see @ref inverted(), @ref isOrthogonal(),
|
|
|
|
|
* @ref Matrix3::invertedRigid(),
|
|
|
|
|
* @ref Matrix4::invertedRigid()
|
|
|
|
|
*/
|
|
|
|
|
Matrix<size, T> invertedOrthogonal() const {
|
|
|
|
|
CORRADE_ASSERT(isOrthogonal(),
|
|
|
|
|
"Math::Matrix::invertedOrthogonal(): the matrix is not orthogonal", {});
|
|
|
|
|
return RectangularMatrix<size, size, T>::transposed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
/* Reimplementation of functions to return correct type */
|
|
|
|
|
Matrix<size, T> operator*(const Matrix<size, T>& other) const {
|
|
|
|
|
return RectangularMatrix<size, size, T>::operator*(other);
|
|
|
|
|
}
|
|
|
|
|
template<std::size_t otherCols> RectangularMatrix<otherCols, size, T> operator*(const RectangularMatrix<otherCols, size, T>& other) const {
|
|
|
|
|
return RectangularMatrix<size, size, T>::operator*(other);
|
|
|
|
|
}
|
|
|
|
|
Vector<size, T> operator*(const Vector<size, T>& other) const {
|
|
|
|
|
return RectangularMatrix<size, size, T>::operator*(other);
|
|
|
|
|
}
|
|
|
|
|
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(size, size, Matrix<size, T>)
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief 2x2 matrix
|
|
|
|
|
|
|
|
|
|
Convenience alternative to `Matrix<2, T>`. See @ref Matrix for more
|
|
|
|
|
information.
|
|
|
|
|
@see @ref Magnum::Matrix2x2, @ref Magnum::Matrix2x2d
|
|
|
|
|
*/
|
|
|
|
|
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */
|
|
|
|
|
template<class T> using Matrix2x2 = Matrix<2, T>;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief 3x3 matrix
|
|
|
|
|
|
|
|
|
|
Convenience alternative to `Matrix<3, T>`. See @ref Matrix for more
|
|
|
|
|
information. Note that this is different from @ref Matrix3, which contains
|
|
|
|
|
additional functions for transformations in 2D.
|
|
|
|
|
@see @ref Magnum::Matrix3x3, @ref Magnum::Matrix3x3d
|
|
|
|
|
*/
|
|
|
|
|
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */
|
|
|
|
|
template<class T> using Matrix3x3 = Matrix<3, T>;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief 4x4 matrix
|
|
|
|
|
|
|
|
|
|
Convenience alternative to `Matrix<4, T>`. See @ref Matrix for more
|
|
|
|
|
information. Note that this is different from @ref Matrix4, which contains
|
|
|
|
|
additional functions for transformations in 3D.
|
|
|
|
|
@see @ref Magnum::Matrix4x4, @ref Magnum::Matrix4x4d
|
|
|
|
|
*/
|
|
|
|
|
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */
|
|
|
|
|
template<class T> using Matrix4x4 = Matrix<4, T>;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
MAGNUM_MATRIX_OPERATOR_IMPLEMENTATION(Matrix<size, T>)
|
|
|
|
|
|
|
|
|
|
/** @debugoperator{Magnum::Math::Matrix} */
|
|
|
|
|
template<std::size_t size, class T> inline Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Matrix<size, T>& value) {
|
|
|
|
|
return debug << static_cast<const RectangularMatrix<size, size, T>&>(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
#define MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(size, Type, VectorType) \
|
|
|
|
|
VectorType<T>& operator[](std::size_t col) { \
|
|
|
|
|
return static_cast<VectorType<T>&>(Matrix<size, T>::operator[](col)); \
|
|
|
|
|
} \
|
|
|
|
|
constexpr const VectorType<T> operator[](std::size_t col) const { \
|
|
|
|
|
return VectorType<T>(Matrix<size, T>::operator[](col)); \
|
|
|
|
|
} \
|
|
|
|
|
VectorType<T> row(std::size_t row) const { \
|
|
|
|
|
return VectorType<T>(Matrix<size, T>::row(row)); \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
Type<T> operator*(const Matrix<size, T>& other) const { \
|
|
|
|
|
return Matrix<size, T>::operator*(other); \
|
|
|
|
|
} \
|
|
|
|
|
template<std::size_t otherCols> RectangularMatrix<otherCols, size, T> operator*(const RectangularMatrix<otherCols, size, T>& other) const { \
|
|
|
|
|
return Matrix<size, T>::operator*(other); \
|
|
|
|
|
} \
|
|
|
|
|
VectorType<T> operator*(const Vector<size, T>& other) const { \
|
|
|
|
|
return Matrix<size, T>::operator*(other); \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
Type<T> transposed() const { return Matrix<size, T>::transposed(); } \
|
|
|
|
|
constexpr VectorType<T> diagonal() const { return Matrix<size, T>::diagonal(); } \
|
|
|
|
|
Type<T> inverted() const { return Matrix<size, T>::inverted(); } \
|
|
|
|
|
Type<T> invertedOrthogonal() const { \
|
|
|
|
|
return Matrix<size, T>::invertedOrthogonal(); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Implementation {
|
|
|
|
|
|
|
|
|
|
template<std::size_t size, class T> struct MatrixDeterminant {
|
|
|
|
|
T operator()(const Matrix<size, T>& m);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<std::size_t size, class T> T MatrixDeterminant<size, T>::operator()(const Matrix<size, T>& m) {
|
|
|
|
|
T out(0);
|
|
|
|
|
|
|
|
|
|
for(std::size_t col = 0; col != size; ++col)
|
|
|
|
|
out += ((col & 1) ? -1 : 1)*m[col][0]*m.ij(col, 0).determinant();
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T> struct MatrixDeterminant<2, T> {
|
|
|
|
|
constexpr T operator()(const Matrix<2, T>& m) const {
|
|
|
|
|
return m[0][0]*m[1][1] - m[1][0]*m[0][1];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class T> struct MatrixDeterminant<1, T> {
|
|
|
|
|
constexpr T operator()(const Matrix<1, T>& m) const {
|
|
|
|
|
return m[0][0];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
template<std::size_t size, class T> bool Matrix<size, T>::isOrthogonal() const {
|
|
|
|
|
/* Normality */
|
|
|
|
|
for(std::size_t i = 0; i != size; ++i)
|
|
|
|
|
if(!(*this)[i].isNormalized()) return false;
|
|
|
|
|
|
|
|
|
|
/* Orthogonality */
|
|
|
|
|
for(std::size_t i = 0; i != size-1; ++i)
|
|
|
|
|
for(std::size_t j = i+1; j != size; ++j)
|
Math: made dot(), angle(), *lerp() and cross() free functions.
It is often annoying to write e.g. this, especially in generic code:
T dot = Math::Vector<size, T>::dot(a, b);
When this is more than enough and the compiler can infer the rest from
the context:
T dot = Math::dot(a, b);
There are more downsides and confusing cases (you can call
Math::Vector<3, T>::dot(), Math::Vector3<T>::dot() and Color3::dot() and
it is still the same function), so I made these as free functions in
Math namespace. You can now also abuse ADL for the calls, but I would
advise against that for better readability:
T d = dot(a, b); // dot?! what on earth is dot? and what is a?
The only downside found when porting is that you need to specify the
type somehow when having both parameters as initializer lists:
T d = dot({2.0f, -1.5f}, {1.0f, 2.5f}); // error
T d = dot(Complex{2.0f, -1.5f}, {1.0f, 2.5f}); // okay
But that's probably reasonable (and it's also highly corner case,
the functions were used this way only in tests).
The original static member functions are of course still present, but
marked as deprecated and will be removed at some point in future.
11 years ago
|
|
|
if(dot((*this)[i], (*this)[j]) > TypeTraits<T>::epsilon())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<std::size_t size, class T> Matrix<size-1, T> Matrix<size, T>::ij(const std::size_t skipCol, const std::size_t skipRow) const {
|
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
|
|
|
Matrix<size-1, T> out{ZeroInit};
|
|
|
|
|
|
|
|
|
|
for(std::size_t col = 0; col != size-1; ++col)
|
|
|
|
|
for(std::size_t row = 0; row != size-1; ++row)
|
|
|
|
|
out[col][row] = (*this)[col + (col >= skipCol)]
|
|
|
|
|
[row + (row >= skipRow)];
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<std::size_t size, class T> Matrix<size, T> Matrix<size, T>::inverted() const {
|
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
|
|
|
Matrix<size, T> out{ZeroInit};
|
|
|
|
|
|
|
|
|
|
const T _determinant = determinant();
|
|
|
|
|
|
|
|
|
|
for(std::size_t col = 0; col != size; ++col)
|
|
|
|
|
for(std::size_t row = 0; row != size; ++row)
|
|
|
|
|
out[col][row] = (((row+col) & 1) ? -1 : 1)*ij(row, col).determinant()/_determinant;
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
namespace Corrade { namespace Utility {
|
|
|
|
|
/** @configurationvalue{Magnum::Math::Matrix} */
|
|
|
|
|
template<std::size_t size, class T> struct ConfigurationValue<Magnum::Math::Matrix<size, T>>: public ConfigurationValue<Magnum::Math::RectangularMatrix<size, size, T>> {};
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
#endif
|