#ifndef Magnum_Math_Matrix4_h #define Magnum_Math_Matrix4_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš This file is part of Magnum. Magnum is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 only, as published by the Free Software Foundation. Magnum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License version 3 for more details. */ /** @file * @brief Class Magnum::Math::Matrix4 */ #include "Matrix3.h" #include "Vector4.h" namespace Magnum { namespace Math { /** * @brief 4x4 matrix * * @todo Rotation with Euler angles * @todo Shearing * @todo Reflection */ template class Matrix4: public Matrix { public: /** @copydoc Matrix::from(T*) */ inline constexpr static Matrix4& from(T* data) { return *reinterpret_cast*>(data); } /** @copydoc Matrix::from(const T*) */ inline constexpr static const Matrix4& from(const T* data) { return *reinterpret_cast*>(data); } /** @copydoc Matrix::from(const Vector&, const U&...) */ template inline constexpr static Matrix4 from(const Vector& first, const U&... next) { return Matrix::from(first, next...); } /** * @brief Translation matrix * @param vec Translation vector */ static constexpr Matrix4 translation(const Vector3& vec) { return Matrix4( /* Column-major! */ 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, vec.x(), vec.y(), vec.z(), 1.0f ); } /** * @brief Scaling matrix * @param vec Scaling vector */ static constexpr Matrix4 scaling(const Vector3& vec) { return Matrix4( /* Column-major! */ vec.x(), 0.0f, 0.0f, 0.0f, 0.0f, vec.y(), 0.0f, 0.0f, 0.0f, 0.0f, vec.z(), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ); } /** * @brief Rotation matrix * @param angle Rotation angle (counterclockwise, in radians) * @param vec Rotation vector */ static Matrix4 rotation(T angle, const Vector3& vec) { Vector3 vn = vec.normalized(); T sine = sin(angle); T cosine = cos(angle); T oneMinusCosine = T(1) - cosine; T xx = vn.x()*vn.x(); T xy = vn.x()*vn.y(); T xz = vn.x()*vn.z(); T yy = vn.y()*vn.y(); T yz = vn.y()*vn.z(); T zz = vn.z()*vn.z(); return Matrix4( /* Column-major! */ cosine + xx*oneMinusCosine, xy*oneMinusCosine + vn.z()*sine, xz*oneMinusCosine - vn.y()*sine, T(0), xy*oneMinusCosine - vn.z()*sine, cosine + yy*oneMinusCosine, yz*oneMinusCosine + vn.x()*sine, T(0), xz*oneMinusCosine + vn.y()*sine, yz*oneMinusCosine - vn.x()*sine, cosine + zz*oneMinusCosine, T(0), T(0), T(0), T(0), T(1) ); } /** @copydoc Matrix::Matrix(ZeroType) */ inline constexpr explicit Matrix4(typename Matrix::ZeroType): Matrix(Matrix::Zero) {} /** @copydoc Matrix::Matrix(IdentityType, T) */ inline constexpr explicit Matrix4(typename Matrix::IdentityType = Matrix::Identity, T value = T(1)): Matrix{ /** @todo Make this in Matrix itself, after it will be constexpr */ value, 0.0f, 0.0f, 0.0f, 0.0f, value, 0.0f, 0.0f, 0.0f, 0.0f, value, 0.0f, 0.0f, 0.0f, 0.0f, value } {} #ifndef DOXYGEN_GENERATING_OUTPUT template explicit Matrix4(U) = delete; #endif /** @copydoc Matrix::Matrix(T, U...) */ #ifndef DOXYGEN_GENERATING_OUTPUT template inline constexpr Matrix4(T first, U... next): Matrix(first, next...) {} #else template inline constexpr Matrix4(T first, U... next) {} #endif /** @copydoc Matrix::Matrix(const Matrix&) */ inline constexpr Matrix4(const Matrix& other): Matrix(other) {} /** @copydoc Matrix::operator=() */ inline Matrix4& operator=(const Matrix4& other) { Matrix::operator=(other); return *this; } /** @copydoc Matrix::operator[](size_t) */ inline Vector4& operator[](size_t col) { return Vector4::from(Matrix::data()+col*4); } /** @copydoc Matrix::operator[](size_t) const */ inline constexpr const Vector4& operator[](size_t col) const { return Vector4::from(Matrix::data()+col*4); } /** @copydoc Matrix::operator*(const Matrix&) const */ inline Matrix4 operator*(const Matrix& other) const { return Matrix::operator*(other); } /** @copydoc Matrix::operator*=() */ inline Matrix4& operator*=(const Matrix& other) { Matrix::operator*=(other); return *this; } /** @copydoc Matrix::operator*(const Vector&) const */ inline Vector4 operator*(const Vector& other) const { return Matrix::operator*(other); } /** @copydoc Matrix::transposed() */ inline Matrix4 transposed() const { return Matrix::transposed(); } /** @copydoc Matrix::ij() */ inline Matrix3 ij(size_t skipRow, size_t skipCol) const { return Matrix::ij(skipRow, skipCol); } /** @copydoc Matrix::inverted() */ inline Matrix4 inverted() const { return Matrix::inverted(); } /** @brief Rotation and scaling part of the matrix */ inline Matrix3 rotationScaling() const { return Matrix3::from( (*this)[0].xyz(), (*this)[1].xyz(), (*this)[2].xyz()); } /** @brief Rotation part of the matrix */ inline Matrix3 rotation() const { return Matrix3::from( (*this)[0].xyz().normalized(), (*this)[1].xyz().normalized(), (*this)[2].xyz().normalized()); } }; #ifndef DOXYGEN_GENERATING_OUTPUT template Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const Magnum::Math::Matrix4& value) { return debug << static_cast&>(value); } #endif }} #endif