From 6dda7e4c1f26e5ace7ec632a912270cb3aa38c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 20 Nov 2012 12:15:36 +0100 Subject: [PATCH] Convenience functions for accessing Matrix[34] vectors. --- src/Math/Matrix3.h | 32 +++++++++++++++++--------- src/Math/Matrix4.h | 42 ++++++++++++++++++++++++++--------- src/Math/Test/Matrix3Test.cpp | 14 +++++++----- src/Math/Test/Matrix3Test.h | 2 +- src/Math/Test/Matrix4Test.cpp | 17 ++++++++------ src/Math/Test/Matrix4Test.h | 2 +- src/Math/Vector2.h | 4 ++-- src/Math/Vector3.h | 6 ++--- 8 files changed, 77 insertions(+), 42 deletions(-) diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index 9d0ec0223..ae8f3a6a7 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -127,21 +127,31 @@ template class Matrix3: public Matrix<3, T> { } /** - * @brief 2D translation part of the matrix + * @brief Right-pointing 2D vector * - * First two elements of last column. - * @see translation(const Vector2&), Matrix4::translation() + * First two elements of first column. + * @see Vector2::xAxis() */ - inline Vector2& translation() { - return (*this)[2].xy(); - } + inline Vector2& right() { return (*this)[0].xy(); } + inline constexpr Vector2 right() const { return (*this)[0].xy(); } /**< @overload */ - /** @overload */ - inline constexpr Vector2 translation() const { - return (*this)[2].xy(); - } + /** + * @brief Up-pointing 2D vector + * + * First two elements of second column. + * @see Vector2::yAxis() + */ + inline Vector2& up() { return (*this)[1].xy(); } + inline constexpr Vector2 up() const { return (*this)[1].xy(); } /**< @overload */ - /** @todo up(), right() */ + /** + * @brief 2D translation part of the matrix + * + * First two elements of third column. + * @see translation(const Vector2&), Matrix4::translation() + */ + inline Vector2& translation() { return (*this)[2].xy(); } + inline constexpr Vector2 translation() const { return (*this)[2].xy(); } /**< @overload */ #ifndef DOXYGEN_GENERATING_OUTPUT inline Point2D operator*(const Point2D& other) const { diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 7fda233b9..296e4dfb4 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -223,22 +223,42 @@ template class Matrix4: public Matrix<4, T> { (*this)[2].xyz().normalized()); } + /** - * @brief 3D translation part of the matrix + * @brief Right-pointing 3D vector * - * First three elements of last column. - * @see translation(const Vector3&), Matrix3::translation() + * First three elements of first column. + * @see Vector3::xAxis() */ - inline Vector3& translation() { - return (*this)[3].xyz(); - } + inline Vector3& right() { return (*this)[0].xyz(); } + inline constexpr Vector3 right() const { return (*this)[0].xyz(); } /**< @overload */ - /** @overload */ - inline constexpr Vector3 translation() const { - return (*this)[3].xyz(); - } + /** + * @brief Up-pointing 3D vector + * + * First three elements of second column. + * @see Vector3::yAxis() + */ + inline Vector3& up() { return (*this)[1].xyz(); } + inline constexpr Vector3 up() const { return (*this)[1].xyz(); } /**< @overload */ - /** @todo up(), forward(), right() */ + /** + * @brief Backward-pointing 3D vector + * + * First three elements of third column. + * @see Vector3::yAxis() + */ + inline Vector3& backward() { return (*this)[2].xyz(); } + inline constexpr Vector3 backward() const { return (*this)[2].xyz(); } /**< @overload */ + + /** + * @brief 3D translation part of the matrix + * + * First three elements of fourth column. + * @see translation(const Vector3&), Matrix3::translation() + */ + inline Vector3& translation() { return (*this)[3].xyz(); } + inline constexpr Vector3 translation() const { return (*this)[3].xyz(); } /**< @overload */ #ifndef DOXYGEN_GENERATING_OUTPUT inline Point3D operator*(const Point3D& other) const { diff --git a/src/Math/Test/Matrix3Test.cpp b/src/Math/Test/Matrix3Test.cpp index e2e8b8975..6df43a7ca 100644 --- a/src/Math/Test/Matrix3Test.cpp +++ b/src/Math/Test/Matrix3Test.cpp @@ -38,7 +38,7 @@ Matrix3Test::Matrix3Test() { &Matrix3Test::rotation, &Matrix3Test::rotationScalingPart, &Matrix3Test::rotationPart, - &Matrix3Test::translationPart, + &Matrix3Test::vectorParts, &Matrix3Test::debug, &Matrix3Test::configuration); } @@ -123,12 +123,14 @@ void Matrix3Test::rotationPart() { CORRADE_COMPARE(rotationTransformed.rotation(), expectedRotationPart); } -void Matrix3Test::translationPart() { - Matrix3 m(1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, +void Matrix3Test::vectorParts() { + Matrix3 m(15.0f, 0.0f, 0.0f, + 0.0f, -3.0f, 0.0f, -5.0f, 12.0f, 1.0f); - Vector2 expected(-5.0f, 12.0f); - CORRADE_COMPARE(m.translation(), expected); + + CORRADE_COMPARE(m.right(), Vector2::xAxis(15.0f)); + CORRADE_COMPARE(m.up(), Vector2::yAxis(-3.0f)); + CORRADE_COMPARE(m.translation(), Vector2(-5.0f, 12.0f)); } void Matrix3Test::debug() { diff --git a/src/Math/Test/Matrix3Test.h b/src/Math/Test/Matrix3Test.h index c76040678..ede0d7fca 100644 --- a/src/Math/Test/Matrix3Test.h +++ b/src/Math/Test/Matrix3Test.h @@ -30,7 +30,7 @@ class Matrix3Test: public Corrade::TestSuite::Tester { void rotation(); void rotationScalingPart(); void rotationPart(); - void translationPart(); + void vectorParts(); void debug(); void configuration(); diff --git a/src/Math/Test/Matrix4Test.cpp b/src/Math/Test/Matrix4Test.cpp index a124d2b4c..89792fa4c 100644 --- a/src/Math/Test/Matrix4Test.cpp +++ b/src/Math/Test/Matrix4Test.cpp @@ -41,7 +41,7 @@ Matrix4Test::Matrix4Test() { &Matrix4Test::rotationZ, &Matrix4Test::rotationScalingPart, &Matrix4Test::rotationPart, - &Matrix4Test::translationPart, + &Matrix4Test::vectorParts, &Matrix4Test::debug, &Matrix4Test::configuration); } @@ -166,13 +166,16 @@ void Matrix4Test::rotationPart() { CORRADE_COMPARE(rotationTransformed.rotation(), expectedRotationPart); } -void Matrix4Test::translationPart() { - Matrix4 m(1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, +void Matrix4Test::vectorParts() { + Matrix4 m(-1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 12.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 35.0f, 0.0f, -5.0f, 12.0f, 0.5f, 1.0f); - Vector3 expected(-5.0f, 12.0f, 0.5f); - CORRADE_COMPARE(m.translation(), expected); + + CORRADE_COMPARE(m.right(), Vector3::xAxis(-1.0f)); + CORRADE_COMPARE(m.up(), Vector3::yAxis(12.0f)); + CORRADE_COMPARE(m.backward(), Vector3::zAxis(35.0f)); + CORRADE_COMPARE(m.translation(), Vector3(-5.0f, 12.0f, 0.5f)); } void Matrix4Test::debug() { diff --git a/src/Math/Test/Matrix4Test.h b/src/Math/Test/Matrix4Test.h index 783c6adb2..7c360f618 100644 --- a/src/Math/Test/Matrix4Test.h +++ b/src/Math/Test/Matrix4Test.h @@ -33,7 +33,7 @@ class Matrix4Test: public Corrade::TestSuite::Tester { void rotationZ(); void rotationScalingPart(); void rotationPart(); - void translationPart(); + void vectorParts(); void debug(); void configuration(); diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index 25361ea73..b9503b14d 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -39,7 +39,7 @@ template class Vector2: public Vector<2, T> { * @code * Matrix3::translation(Vector2::xAxis(5.0f)); // same as Matrix3::translation({5.0f, 0.0f}); * @endcode - * @see yAxis(), xScale() + * @see yAxis(), xScale(), Matrix3::right() */ inline constexpr static Vector2 xAxis(T length = T(1)) { return Vector2(length, T()); } @@ -47,7 +47,7 @@ template class Vector2: public Vector<2, T> { * @brief %Vector in direction of Y axis (up) * * See xAxis() for more information. - * @see yScale() + * @see yScale(), Matrix3::up() */ inline constexpr static Vector2 yAxis(T length = T(1)) { return Vector2(T(), length); } diff --git a/src/Math/Vector3.h b/src/Math/Vector3.h index f0668a352..f320272e9 100644 --- a/src/Math/Vector3.h +++ b/src/Math/Vector3.h @@ -41,7 +41,7 @@ template class Vector3: public Vector<3, T> { * Matrix4::translation(Vector3::xAxis(5.0f)); // same as Matrix4::translation({5.0f, 0.0f, 0.0f}); * Matrix4::rotation(deg(30.0f), Vector3::xAxis()); // same as Matrix::rotation(deg(30.0f), {1.0f, 0.0f, 0.0f}); * @endcode - * @see yAxis(), zAxis(), xScale() + * @see yAxis(), zAxis(), xScale(), Matrix4::right() */ inline constexpr static Vector3 xAxis(T length = T(1)) { return Vector3(length, T(), T()); } @@ -49,7 +49,7 @@ template class Vector3: public Vector<3, T> { * @brief %Vector in direction of Y axis (up) * * See xAxis() for more information. - * @see yScale() + * @see yScale(), Matrix4::up() */ inline constexpr static Vector3 yAxis(T length = T(1)) { return Vector3(T(), length, T()); } @@ -57,7 +57,7 @@ template class Vector3: public Vector<3, T> { * @brief %Vector in direction of Z axis (backward) * * See xAxis() for more information. - * @see zScale() + * @see zScale(), Matrix4::backward() */ inline constexpr static Vector3 zAxis(T length = T(1)) { return Vector3(T(), T(), length); }