Browse Source

Math: C++14 constexpr in Matrix3 and Matrix4.

pull/276/head
Vladimír Vondruš 12 years ago
parent
commit
02ebf7aed6
  1. 12
      src/Magnum/Math/Matrix3.h
  2. 20
      src/Magnum/Math/Matrix4.h
  3. 3
      src/Magnum/Math/Test/Matrix3Test.cpp
  4. 8
      src/Magnum/Math/Test/Matrix4Test.cpp

12
src/Magnum/Math/Matrix3.h

@ -105,7 +105,7 @@ template<class T> class Matrix3: public Matrix3x3<T> {
* @see @ref Matrix4::orthographicProjection(),
* @ref Matrix4::perspectiveProjection()
*/
static Matrix3<T> projection(const Vector2<T>& size) {
constexpr static Matrix3<T> projection(const Vector2<T>& size) {
return scaling(2.0f/size);
}
@ -247,7 +247,7 @@ template<class T> class Matrix3: public Matrix3x3<T> {
* 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() { return (*this)[0].xy(); }
constexpr Vector2<T> right() const { return (*this)[0].xy(); } /**< @overload */
/**
@ -256,7 +256,7 @@ template<class T> class Matrix3: public Matrix3x3<T> {
* 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() { return (*this)[1].xy(); }
constexpr Vector2<T> up() const { return (*this)[1].xy(); } /**< @overload */
/**
@ -267,7 +267,7 @@ template<class T> class Matrix3: public Matrix3x3<T> {
* @ref translation(const Vector2<T>&),
* @ref Matrix4::translation()
*/
Vector2<T>& translation() { return (*this)[2].xy(); }
constexpr Vector2<T>& translation() { return (*this)[2].xy(); }
constexpr Vector2<T> translation() const { return (*this)[2].xy(); } /**< @overload */
/**
@ -297,7 +297,7 @@ template<class T> class Matrix3: public Matrix3x3<T> {
* @ref Matrix4::transformVector()
* @todo extract 2x2 matrix and multiply directly? (benchmark that)
*/
Vector2<T> transformVector(const Vector2<T>& vector) const {
constexpr Vector2<T> transformVector(const Vector2<T>& vector) const {
return ((*this)*Vector3<T>(vector, T(0))).xy();
}
@ -311,7 +311,7 @@ template<class T> class Matrix3: public Matrix3x3<T> {
* @see @ref DualComplex::transformPoint(),
* @ref Matrix4::transformPoint()
*/
Vector2<T> transformPoint(const Vector2<T>& vector) const {
constexpr Vector2<T> transformPoint(const Vector2<T>& vector) const {
return ((*this)*Vector3<T>(vector, T(1))).xy();
}

20
src/Magnum/Math/Matrix4.h

@ -151,7 +151,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
*
* @see @ref perspectiveProjection(), @ref Matrix3::projection()
*/
static Matrix4<T> orthographicProjection(const Vector2<T>& size, T near, T far);
constexpr static Matrix4<T> orthographicProjection(const Vector2<T>& size, T near, T far);
/**
* @brief 3D perspective projection matrix
@ -161,7 +161,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
*
* @see @ref orthographicProjection(), @ref Matrix3::projection()
*/
static Matrix4<T> perspectiveProjection(const Vector2<T>& size, T near, T far);
constexpr static Matrix4<T> perspectiveProjection(const Vector2<T>& size, T near, T far);
/**
* @brief 3D perspective projection matrix
@ -309,7 +309,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
* @see @ref up(), @ref backward(), @ref Vector3::xAxis(),
* @ref Matrix3::right()
*/
Vector3<T>& right() { return (*this)[0].xyz(); }
constexpr Vector3<T>& right() { return (*this)[0].xyz(); }
constexpr Vector3<T> right() const { return (*this)[0].xyz(); } /**< @overload */
/**
@ -319,7 +319,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
* @see @ref right(), @ref backward(), @ref Vector3::yAxis(),
* @ref Matrix3::up()
*/
Vector3<T>& up() { return (*this)[1].xyz(); }
constexpr Vector3<T>& up() { return (*this)[1].xyz(); }
constexpr Vector3<T> up() const { return (*this)[1].xyz(); } /**< @overload */
/**
@ -328,7 +328,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
* First three elements of third column.
* @see @ref right(), @ref up(), @ref Vector3::yAxis()
*/
Vector3<T>& backward() { return (*this)[2].xyz(); }
constexpr Vector3<T>& backward() { return (*this)[2].xyz(); }
constexpr Vector3<T> backward() const { return (*this)[2].xyz(); } /**< @overload */
/**
@ -339,7 +339,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
* @ref translation(const Vector3<T>&),
* @ref Matrix3::translation()
*/
Vector3<T>& translation() { return (*this)[3].xyz(); }
constexpr Vector3<T>& translation() { return (*this)[3].xyz(); }
constexpr Vector3<T> translation() const { return (*this)[3].xyz(); } /**< @overload */
/**
@ -369,7 +369,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
* @ref Matrix3::transformVector()
* @todo extract 3x3 matrix and multiply directly? (benchmark that)
*/
Vector3<T> transformVector(const Vector3<T>& vector) const {
constexpr Vector3<T> transformVector(const Vector3<T>& vector) const {
return ((*this)*Vector4<T>(vector, T(0))).xyz();
}
@ -383,7 +383,7 @@ template<class T> class Matrix4: public Matrix4x4<T> {
* @see @ref DualQuaternion::transformPoint(),
* @ref Matrix3::transformPoint()
*/
Vector3<T> transformPoint(const Vector3<T>& vector) const {
constexpr Vector3<T> transformPoint(const Vector3<T>& vector) const {
return ((*this)*Vector4<T>(vector, T(1))).xyz();
}
@ -466,7 +466,7 @@ template<class T> Matrix4<T> Matrix4<T>::reflection(const Vector3<T>& normal) {
return from(Matrix3x3<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) {
template<class T> constexpr 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);
@ -476,7 +476,7 @@ template<class T> Matrix4<T> Matrix4<T>::orthographicProjection(const Vector2<T>
{ 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) {
template<class T> constexpr 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);

3
src/Magnum/Math/Test/Matrix3Test.cpp

@ -259,7 +259,8 @@ void Matrix3Test::projection() {
{ 0.0f, 2.0f/3.0f, 0.0f},
{ 0.0f, 0.0f, 1.0f});
CORRADE_COMPARE(Matrix3::projection({4.0f, 3.0f}), expected);
constexpr auto a = Matrix3::projection({4.0f, 3.0f});
CORRADE_COMPARE(a, expected);
}
void Matrix3Test::fromParts() {

8
src/Magnum/Math/Test/Matrix4Test.cpp

@ -318,19 +318,23 @@ void Matrix4Test::reflection() {
}
void Matrix4Test::orthographicProjection() {
constexpr auto a = Matrix4::orthographicProjection({5.0f, 4.0f}, 1, 9);
Matrix4 expected({0.4f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.5f, 0.0f, 0.0f},
{0.0f, 0.0f, -0.25f, 0.0f},
{0.0f, 0.0f, -1.25f, 1.0f});
CORRADE_COMPARE(Matrix4::orthographicProjection({5.0f, 4.0f}, 1, 9), expected);
CORRADE_COMPARE(a, expected);
}
void Matrix4Test::perspectiveProjection() {
constexpr auto a = Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, 100);
Matrix4 expected({4.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 7.111111f, 0.0f, 0.0f},
{0.0f, 0.0f, -1.9411764f, -1.0f},
{0.0f, 0.0f, -94.1176452f, 0.0f});
CORRADE_COMPARE(Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, 100), expected);
CORRADE_COMPARE(a, expected);
}
void Matrix4Test::perspectiveProjectionFov() {

Loading…
Cancel
Save