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.
495 lines
20 KiB
495 lines
20 KiB
|
16 years ago
|
#ifndef Magnum_Math_Matrix4_h
|
||
|
|
#define Magnum_Math_Matrix4_h
|
||
|
16 years ago
|
/*
|
||
|
|
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.
|
||
|
16 years ago
|
|
||
|
13 years ago
|
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
|
||
|
16 years ago
|
* @brief Class Magnum::Math::Matrix4
|
||
|
16 years ago
|
*/
|
||
|
|
|
||
|
13 years ago
|
#include "Math/Matrix.h"
|
||
|
|
#include "Math/Vector4.h"
|
||
|
16 years ago
|
|
||
|
13 years ago
|
#ifdef CORRADE_TARGET_WINDOWS /* I so HATE windef.h */
|
||
|
13 years ago
|
#undef near
|
||
|
|
#undef far
|
||
|
|
#endif
|
||
|
|
|
||
|
16 years ago
|
namespace Magnum { namespace Math {
|
||
|
16 years ago
|
|
||
|
16 years ago
|
/**
|
||
|
13 years ago
|
@brief 4x4 matrix
|
||
|
13 years ago
|
@tparam T Underlying data type
|
||
|
14 years ago
|
|
||
|
13 years ago
|
Represents 3D transformation. See @ref matrix-vector and @ref transformations
|
||
|
|
for brief introduction.
|
||
|
13 years ago
|
@see Magnum::Matrix4, Magnum::Matrix4d, DualQuaternion,
|
||
|
|
SceneGraph::MatrixTransformation3D
|
||
|
14 years ago
|
@configurationvalueref{Magnum::Math::Matrix4}
|
||
|
16 years ago
|
*/
|
||
|
14 years ago
|
template<class T> class Matrix4: public Matrix<4, T> {
|
||
|
16 years ago
|
public:
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief 3D translation
|
||
|
14 years ago
|
* @param vector Translation vector
|
||
|
14 years ago
|
*
|
||
|
13 years ago
|
* @see translation(), DualQuaternion::translation(),
|
||
|
|
* Matrix3::translation(const Vector2&), Vector3::xAxis(),
|
||
|
|
* Vector3::yAxis(), Vector3::zAxis()
|
||
|
16 years ago
|
*/
|
||
|
13 years ago
|
constexpr static Matrix4<T> translation(const Vector3<T>& vector) {
|
||
|
|
return {{ T(1), T(0), T(0), T(0)},
|
||
|
|
{ T(0), T(1), T(0), T(0)},
|
||
|
|
{ T(0), T(0), T(1), T(0)},
|
||
|
|
{vector.x(), vector.y(), vector.z(), T(1)}};
|
||
|
|
}
|
||
|
16 years ago
|
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief 3D scaling
|
||
|
14 years ago
|
* @param vector Scaling vector
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* @see rotationScaling() const, Matrix3::scaling(const Vector2&),
|
||
|
|
* Vector3::xScale(), Vector3::yScale(), Vector3::zScale()
|
||
|
16 years ago
|
*/
|
||
|
13 years ago
|
constexpr static Matrix4<T> scaling(const Vector3<T>& vector) {
|
||
|
|
return {{vector.x(), T(0), T(0), T(0)},
|
||
|
|
{ T(0), vector.y(), T(0), T(0)},
|
||
|
|
{ T(0), T(0), vector.z(), T(0)},
|
||
|
|
{ T(0), T(0), T(0), T(1)}};
|
||
|
|
}
|
||
|
16 years ago
|
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief 3D rotation around arbitrary axis
|
||
|
13 years ago
|
* @param angle Rotation angle (counterclockwise)
|
||
|
14 years ago
|
* @param normalizedAxis Normalized rotation axis
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* Expects that the rotation axis is normalized. If possible, use
|
||
|
|
* faster alternatives like rotationX(), rotationY() and rotationZ().
|
||
|
13 years ago
|
* @see rotation() const, Quaternion::rotation(), DualQuaternion::rotation(),
|
||
|
|
* Matrix3::rotation(Rad), Vector3::xAxis(), Vector3::yAxis(),
|
||
|
13 years ago
|
* Vector3::zAxis(), Vector::isNormalized()
|
||
|
16 years ago
|
*/
|
||
|
13 years ago
|
static Matrix4<T> rotation(Rad<T> angle, const Vector3<T>& normalizedAxis);
|
||
|
16 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 3D rotation around X axis
|
||
|
13 years ago
|
* @param angle Rotation angle (counterclockwise)
|
||
|
14 years ago
|
*
|
||
|
|
* Faster than calling `Matrix4::rotation(angle, Vector3::xAxis())`.
|
||
|
13 years ago
|
* @see rotation(Rad, const Vector3&), rotationY(), rotationZ(),
|
||
|
|
* rotation() const, Quaternion::rotation(), Matrix3::rotation(Rad)
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
static Matrix4<T> rotationX(Rad<T> angle);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief 3D rotation around Y axis
|
||
|
13 years ago
|
* @param angle Rotation angle (counterclockwise)
|
||
|
14 years ago
|
*
|
||
|
|
* Faster than calling `Matrix4::rotation(angle, Vector3::yAxis())`.
|
||
|
13 years ago
|
* @see rotation(Rad, const Vector3&), rotationX(), rotationZ(),
|
||
|
|
* rotation() const, Quaternion::rotation(), Matrix3::rotation(Rad)
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
static Matrix4<T> rotationY(Rad<T> angle);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief 3D rotation matrix around Z axis
|
||
|
13 years ago
|
* @param angle Rotation angle (counterclockwise)
|
||
|
14 years ago
|
*
|
||
|
|
* Faster than calling `Matrix4::rotation(angle, Vector3::zAxis())`.
|
||
|
13 years ago
|
* @see rotation(Rad, const Vector3&), rotationX(), rotationY(),
|
||
|
|
* rotation() const, Quaternion::rotation(), Matrix3::rotation(Rad)
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
static Matrix4<T> rotationZ(Rad<T> angle);
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 3D reflection matrix
|
||
|
|
* @param normal Normal of the plane through which to reflect
|
||
|
|
*
|
||
|
14 years ago
|
* Expects that the normal is normalized.
|
||
|
13 years ago
|
* @see Matrix3::reflection(), Vector::isNormalized()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
static Matrix4<T> reflection(const Vector3<T>& normal);
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 3D orthographic projection matrix
|
||
|
|
* @param size Size of the view
|
||
|
|
* @param near Near clipping plane
|
||
|
|
* @param far Far clipping plane
|
||
|
|
*
|
||
|
|
* @see perspectiveProjection(), Matrix3::projection()
|
||
|
|
*/
|
||
|
13 years ago
|
static Matrix4<T> orthographicProjection(const Vector2<T>& size, T near, T far);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief 3D perspective projection matrix
|
||
|
|
* @param size Size of near clipping plane
|
||
|
|
* @param near Near clipping plane
|
||
|
|
* @param far Far clipping plane
|
||
|
|
*
|
||
|
|
* @see orthographicProjection(), Matrix3::projection()
|
||
|
|
*/
|
||
|
13 years ago
|
static Matrix4<T> perspectiveProjection(const Vector2<T>& size, T near, T far);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief 3D perspective projection matrix
|
||
|
13 years ago
|
* @param fov Field of view angle (horizontal)
|
||
|
14 years ago
|
* @param aspectRatio Aspect ratio
|
||
|
|
* @param near Near clipping plane
|
||
|
|
* @param far Far clipping plane
|
||
|
|
*
|
||
|
|
* @see orthographicProjection(), Matrix3::projection()
|
||
|
|
*/
|
||
|
13 years ago
|
static Matrix4<T> perspectiveProjection(Rad<T> fov, T aspectRatio, T near, T far) {
|
||
|
13 years ago
|
const T xyScale = 2*std::tan(T(fov)/2)*near;
|
||
|
14 years ago
|
return perspectiveProjection(Vector2<T>(xyScale, xyScale/aspectRatio), near, far);
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Create matrix from rotation/scaling part and translation part
|
||
|
|
* @param rotationScaling Rotation/scaling part (upper-left 3x3
|
||
|
|
* matrix)
|
||
|
|
* @param translation Translation part (first three elements of
|
||
|
|
* fourth column)
|
||
|
|
*
|
||
|
|
* @see rotationScaling() const, translation() const
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr static Matrix4<T> from(const Matrix<3, T>& rotationScaling, const Vector3<T>& translation) {
|
||
|
|
return {{rotationScaling[0], T(0)},
|
||
|
|
{rotationScaling[1], T(0)},
|
||
|
|
{rotationScaling[2], T(0)},
|
||
|
|
{ translation, T(1)}};
|
||
|
|
}
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/** @copydoc Matrix::Matrix(ZeroType) */
|
||
|
13 years ago
|
constexpr explicit Matrix4(typename Matrix<4, T>::ZeroType): Matrix<4, T>(Matrix<4, T>::Zero) {}
|
||
|
14 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Default constructor
|
||
|
|
*
|
||
|
|
* Creates identity matrix. You can also explicitly call this
|
||
|
|
* constructor with `Matrix4 m(Matrix4::Identity);`. Optional parameter
|
||
|
|
* @p value allows you to specify value on diagonal.
|
||
|
|
*/
|
||
|
13 years ago
|
constexpr /*implicit*/ Matrix4(typename Matrix<4, T>::IdentityType = (Matrix<4, T>::Identity), T value = T(1)): Matrix<4, T>(Matrix<4, T>::Identity, value) {}
|
||
|
16 years ago
|
|
||
|
13 years ago
|
/** @brief %Matrix from column vectors */
|
||
|
13 years ago
|
constexpr /*implicit*/ Matrix4(const Vector4<T>& first, const Vector4<T>& second, const Vector4<T>& third, const Vector4<T>& fourth): Matrix<4, T>(first, second, third, fourth) {}
|
||
|
16 years ago
|
|
||
|
13 years ago
|
/** @copydoc Matrix::Matrix(const RectangularMatrix<size, size, U>&) */
|
||
|
13 years ago
|
template<class U> constexpr explicit Matrix4(const RectangularMatrix<4, 4, U>& other): Matrix<4, T>(other) {}
|
||
|
13 years ago
|
|
||
|
13 years ago
|
/** @brief Construct matrix from external representation */
|
||
|
13 years ago
|
template<class U, class V = decltype(Implementation::RectangularMatrixConverter<4, 4, T, U>::from(std::declval<U>()))> constexpr explicit Matrix4(const U& other): Matrix<4, T>(Implementation::RectangularMatrixConverter<4, 4, T, U>::from(other)) {}
|
||
|
13 years ago
|
|
||
|
14 years ago
|
/** @brief Copy constructor */
|
||
|
13 years ago
|
constexpr Matrix4(const RectangularMatrix<4, 4, T>& other): Matrix<4, 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(3) == Vector4<T>(T(0), T(0), T(0), T(1));
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 3D rotation and scaling part of the matrix
|
||
|
|
*
|
||
|
|
* Upper-left 3x3 part of the matrix.
|
||
|
14 years ago
|
* @see from(const Matrix<3, T>&, const Vector3&), rotation() const,
|
||
|
13 years ago
|
* rotationNormalized(), @ref uniformScaling(),
|
||
|
|
* rotation(T, const Vector3&), Matrix3::rotationScaling() const
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
/* Not Matrix3, because it is for affine 2D transformations */
|
||
|
13 years ago
|
constexpr Matrix<3, T> rotationScaling() const {
|
||
|
|
return {(*this)[0].xyz(),
|
||
|
|
(*this)[1].xyz(),
|
||
|
|
(*this)[2].xyz()};
|
||
|
|
}
|
||
|
14 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief 3D 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 Matrix3::rotationNormalized()
|
||
|
13 years ago
|
* @todo assert also orthogonality or this is good enough?
|
||
|
|
*/
|
||
|
|
/* Not Matrix3, because it is for affine 2D transformations */
|
||
|
|
Matrix<3, T> rotationNormalized() const {
|
||
|
|
CORRADE_ASSERT((*this)[0].xyz().isNormalized() && (*this)[1].xyz().isNormalized() && (*this)[2].xyz().isNormalized(),
|
||
|
|
"Math::Matrix4::rotationNormalized(): the rotation part is not normalized", {});
|
||
|
|
return {(*this)[0].xyz(),
|
||
|
|
(*this)[1].xyz(),
|
||
|
|
(*this)[2].xyz()};
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief 3D rotation part of the matrix
|
||
|
|
*
|
||
|
13 years ago
|
* Normalized upper-left 3x3 part of the matrix. Expects uniform
|
||
|
|
* scaling.
|
||
|
13 years ago
|
* @see rotationNormalized(), rotationScaling() const,
|
||
|
13 years ago
|
* @ref uniformScaling(), rotation(T, const Vector3&),
|
||
|
|
* Matrix3::rotation() const
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
/* Not Matrix3, because it is for affine 2D transformations */
|
||
|
|
Matrix<3, T> rotation() const;
|
||
|
14 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Uniform scaling part of the matrix, squared
|
||
|
|
*
|
||
|
|
* Squared length of vectors in upper-left 3x3 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(),
|
||
|
13 years ago
|
* @ref rotationNormalized(), @ref scaling(const Vector3<T>&),
|
||
|
13 years ago
|
* @ref Matrix3::uniformScaling()
|
||
|
|
*/
|
||
|
|
T uniformScalingSquared() const;
|
||
|
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Uniform scaling part of the matrix
|
||
|
|
*
|
||
|
|
* Length of vectors in upper-left 3x3 part of the matrix. Expects that
|
||
|
13 years ago
|
* the scaling is the same in all axes. Use faster alternative
|
||
|
|
* @ref uniformScalingSquared() where possible.
|
||
|
13 years ago
|
* @see @ref rotationScaling(), @ref rotation(),
|
||
|
13 years ago
|
* @ref rotationNormalized(), @ref scaling(const Vector3<T>&),
|
||
|
13 years ago
|
* @ref Matrix3::uniformScaling()
|
||
|
|
*/
|
||
|
13 years ago
|
T uniformScaling() const { return std::sqrt(uniformScalingSquared()); }
|
||
|
13 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
14 years ago
|
* @brief Right-pointing 3D vector
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* First three elements of first column.
|
||
|
13 years ago
|
* @see up(), backward(), Vector3::xAxis(), Matrix3::right()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector3<T>& right() { return (*this)[0].xyz(); }
|
||
|
|
constexpr Vector3<T> right() const { return (*this)[0].xyz(); } /**< @overload */
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Up-pointing 3D vector
|
||
|
|
*
|
||
|
|
* First three elements of second column.
|
||
|
13 years ago
|
* @see right(), backward(), Vector3::yAxis(), Matrix3::up()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector3<T>& up() { return (*this)[1].xyz(); }
|
||
|
|
constexpr Vector3<T> up() const { return (*this)[1].xyz(); } /**< @overload */
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Backward-pointing 3D vector
|
||
|
|
*
|
||
|
|
* First three elements of third column.
|
||
|
13 years ago
|
* @see right(), up(), Vector3::yAxis()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector3<T>& backward() { return (*this)[2].xyz(); }
|
||
|
|
constexpr Vector3<T> backward() const { return (*this)[2].xyz(); } /**< @overload */
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief 3D translation part of the matrix
|
||
|
|
*
|
||
|
|
* First three elements of fourth column.
|
||
|
14 years ago
|
* @see from(const Matrix<3, T>&, const Vector3&),
|
||
|
|
* translation(const Vector3&), Matrix3::translation()
|
||
|
14 years ago
|
*/
|
||
|
13 years ago
|
Vector3<T>& translation() { return (*this)[3].xyz(); }
|
||
|
|
constexpr Vector3<T> translation() const { return (*this)[3].xyz(); } /**< @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
|
Matrix4<T> invertedRigid() const;
|
||
|
14 years ago
|
|
||
|
13 years ago
|
/**
|
||
|
|
* @brief Transform 3D vector with the matrix
|
||
|
|
*
|
||
|
13 years ago
|
* Unlike in transformVector(), translation is not involved in the
|
||
|
|
* transformation. @f[
|
||
|
13 years ago
|
* \boldsymbol v' = \boldsymbol M \begin{pmatrix} v_x \\ v_y \\ v_z \\ 0 \end{pmatrix}
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see Quaternion::transformVector(), Matrix3::transformVector()
|
||
|
13 years ago
|
* @todo extract 3x3 matrix and multiply directly? (benchmark that)
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Vector3<T> transformVector(const Vector3<T>& vector) const {
|
||
|
13 years ago
|
return ((*this)*Vector4<T>(vector, T(0))).xyz();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Transform 3D 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 \\ v_z \\ 1 \end{pmatrix}
|
||
|
13 years ago
|
* @f]
|
||
|
13 years ago
|
* @see DualQuaternion::transformPoint(), Matrix3::transformPoint()
|
||
|
13 years ago
|
*/
|
||
|
13 years ago
|
Vector3<T> transformPoint(const Vector3<T>& vector) const {
|
||
|
13 years ago
|
return ((*this)*Vector4<T>(vector, T(1))).xyz();
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(4, 4, Matrix4<T>)
|
||
|
13 years ago
|
MAGNUM_MATRIX_SUBCLASS_IMPLEMENTATION(4, Matrix4, Vector4)
|
||
|
16 years ago
|
};
|
||
|
|
|
||
|
13 years ago
|
MAGNUM_MATRIXn_OPERATOR_IMPLEMENTATION(4, Matrix4)
|
||
|
13 years ago
|
|
||
|
14 years ago
|
/** @debugoperator{Magnum::Math::Matrix4} */
|
||
|
14 years ago
|
template<class T> inline Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Matrix4<T>& value) {
|
||
|
|
return debug << static_cast<const Matrix<4, T>&>(value);
|
||
|
15 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class T> Matrix4<T> Matrix4<T>::rotation(const Rad<T> angle, const Vector3<T>& normalizedAxis) {
|
||
|
|
CORRADE_ASSERT(normalizedAxis.isNormalized(),
|
||
|
|
"Math::Matrix4::rotation(): axis must be normalized", {});
|
||
|
|
|
||
|
|
const T sine = std::sin(T(angle));
|
||
|
|
const T cosine = std::cos(T(angle));
|
||
|
|
const T oneMinusCosine = T(1) - cosine;
|
||
|
|
|
||
|
|
const T xx = normalizedAxis.x()*normalizedAxis.x();
|
||
|
|
const T xy = normalizedAxis.x()*normalizedAxis.y();
|
||
|
|
const T xz = normalizedAxis.x()*normalizedAxis.z();
|
||
|
|
const T yy = normalizedAxis.y()*normalizedAxis.y();
|
||
|
|
const T yz = normalizedAxis.y()*normalizedAxis.z();
|
||
|
|
const T zz = normalizedAxis.z()*normalizedAxis.z();
|
||
|
|
|
||
|
|
return {
|
||
|
|
{cosine + xx*oneMinusCosine,
|
||
|
|
xy*oneMinusCosine + normalizedAxis.z()*sine,
|
||
|
|
xz*oneMinusCosine - normalizedAxis.y()*sine,
|
||
|
|
T(0)},
|
||
|
|
{xy*oneMinusCosine - normalizedAxis.z()*sine,
|
||
|
|
cosine + yy*oneMinusCosine,
|
||
|
|
yz*oneMinusCosine + normalizedAxis.x()*sine,
|
||
|
|
T(0)},
|
||
|
|
{xz*oneMinusCosine + normalizedAxis.y()*sine,
|
||
|
|
yz*oneMinusCosine - normalizedAxis.x()*sine,
|
||
|
|
cosine + zz*oneMinusCosine,
|
||
|
|
T(0)},
|
||
|
|
{T(0), T(0), T(0), T(1)}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> Matrix4<T> Matrix4<T>::rotationX(const Rad<T> angle) {
|
||
|
|
const T sine = std::sin(T(angle));
|
||
|
|
const T cosine = std::cos(T(angle));
|
||
|
|
|
||
|
|
return {{T(1), T(0), T(0), T(0)},
|
||
|
|
{T(0), cosine, sine, T(0)},
|
||
|
|
{T(0), -sine, cosine, T(0)},
|
||
|
|
{T(0), T(0), T(0), T(1)}};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> Matrix4<T> Matrix4<T>::rotationY(const Rad<T> angle) {
|
||
|
|
const T sine = std::sin(T(angle));
|
||
|
|
const T cosine = std::cos(T(angle));
|
||
|
|
|
||
|
|
return {{cosine, T(0), -sine, T(0)},
|
||
|
|
{ T(0), T(1), T(0), T(0)},
|
||
|
|
{ sine, T(0), cosine, T(0)},
|
||
|
|
{ T(0), T(0), T(0), T(1)}};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> Matrix4<T> Matrix4<T>::rotationZ(const Rad<T> angle) {
|
||
|
|
const T sine = std::sin(T(angle));
|
||
|
|
const T cosine = std::cos(T(angle));
|
||
|
|
|
||
|
|
return {{cosine, sine, T(0), T(0)},
|
||
|
|
{ -sine, cosine, T(0), T(0)},
|
||
|
|
{ T(0), T(0), T(1), T(0)},
|
||
|
|
{ T(0), T(0), T(0), T(1)}};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> Matrix4<T> Matrix4<T>::reflection(const Vector3<T>& normal) {
|
||
|
|
CORRADE_ASSERT(normal.isNormalized(),
|
||
|
|
"Math::Matrix4::reflection(): normal must be normalized", {});
|
||
|
|
return from(Matrix<3, T>() - T(2)*normal*RectangularMatrix<1, 3, T>(normal).transposed(), {});
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> Matrix4<T> Matrix4<T>::orthographicProjection(const Vector2<T>& size, const T near, const T far) {
|
||
|
|
const Vector2<T> xyScale = T(2.0)/size;
|
||
|
|
const T zScale = T(2.0)/(near-far);
|
||
|
|
|
||
|
|
return {{xyScale.x(), T(0), T(0), T(0)},
|
||
|
|
{ T(0), xyScale.y(), T(0), T(0)},
|
||
|
|
{ T(0), T(0), zScale, T(0)},
|
||
|
|
{ T(0), T(0), near*zScale-T(1), T(1)}};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> Matrix4<T> Matrix4<T>::perspectiveProjection(const Vector2<T>& size, const T near, const T far) {
|
||
|
|
Vector2<T> xyScale = 2*near/size;
|
||
|
|
T zScale = T(1.0)/(near-far);
|
||
|
|
|
||
|
|
return {{xyScale.x(), T(0), T(0), T(0)},
|
||
|
|
{ T(0), xyScale.y(), T(0), T(0)},
|
||
|
|
{ T(0), T(0), (far+near)*zScale, T(-1)},
|
||
|
|
{ T(0), T(0), T(2)*far*near*zScale, T(0)}};
|
||
|
|
}
|
||
|
|
|
||
|
|
template<class T> inline Matrix<3, T> Matrix4<T>::rotation() const {
|
||
|
13 years ago
|
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[0].xyz().dot(), (*this)[1].xyz().dot()) &&
|
||
|
|
TypeTraits<T>::equals((*this)[1].xyz().dot(), (*this)[2].xyz().dot()),
|
||
|
|
"Math::Matrix4::rotation(): the matrix doesn't have uniform scaling", {});
|
||
|
13 years ago
|
return {(*this)[0].xyz().normalized(),
|
||
|
|
(*this)[1].xyz().normalized(),
|
||
|
|
(*this)[2].xyz().normalized()};
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class T> T Matrix4<T>::uniformScalingSquared() const {
|
||
|
13 years ago
|
const T scalingSquared = (*this)[0].xyz().dot();
|
||
|
|
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[1].xyz().dot(), scalingSquared) &&
|
||
|
|
TypeTraits<T>::equals((*this)[2].xyz().dot(), scalingSquared),
|
||
|
|
"Math::Matrix4::uniformScaling(): the matrix doesn't have uniform scaling", {});
|
||
|
13 years ago
|
return scalingSquared;
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
template<class T> Matrix4<T> Matrix4<T>::invertedRigid() const {
|
||
|
|
CORRADE_ASSERT(isRigidTransformation(),
|
||
|
|
"Math::Matrix4::invertedRigid(): the matrix doesn't represent rigid transformation", {});
|
||
|
|
|
||
|
|
Matrix<3, T> inverseRotation = rotationScaling().transposed();
|
||
|
|
return from(inverseRotation, inverseRotation*-translation());
|
||
|
|
}
|
||
|
|
|
||
|
16 years ago
|
}}
|
||
|
16 years ago
|
|
||
|
14 years ago
|
namespace Corrade { namespace Utility {
|
||
|
|
/** @configurationvalue{Magnum::Math::Matrix4} */
|
||
|
|
template<class T> struct ConfigurationValue<Magnum::Math::Matrix4<T>>: public ConfigurationValue<Magnum::Math::Matrix<4, T>> {};
|
||
|
|
}}
|
||
|
|
|
||
|
16 years ago
|
#endif
|