From c858d3b889b7a97fa6ecf418e5befd5e593ce421 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 1 Feb 2024 17:23:48 -0500 Subject: [PATCH] Implement Mat4x4 member docs --- include/J3ML/LinearAlgebra/Matrix4x4.h | 31 +++++++++++++++++++------- src/J3ML/LinearAlgebra/Matrix4x4.cpp | 21 ++++++++++++++--- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/include/J3ML/LinearAlgebra/Matrix4x4.h b/include/J3ML/LinearAlgebra/Matrix4x4.h index 878c329..a45c3dc 100644 --- a/include/J3ML/LinearAlgebra/Matrix4x4.h +++ b/include/J3ML/LinearAlgebra/Matrix4x4.h @@ -65,7 +65,14 @@ namespace LinearAlgebra { /// Logically, the translation occurs after the rotation has been performed. Matrix4x4(const Quaternion& orientation, const Vector3 &translation); + /// Returns the translation part. + /** The translation part is stored in the fourth column of this matrix. + This is equivalent to decomposing this matrix in the form M = T * M', i.e. this translation is applied last, + after applying rotation and scale. If this matrix represents a local->world space transformation for an object, + then this gives the world space position of the object. + @note This function assumes that this matrix does not contain projection (the fourth row of this matrix is [0 0 0 1]). */ Vector3 GetTranslatePart() const; + /// Returns the top-left 3x3 part of this matrix. This stores the rotation part of this matrix (if this matrix represents a rotation). Matrix3x3 GetRotatePart() const; void SetTranslatePart(float translateX, float translateY, float translateZ); void SetTranslatePart(const Vector3& offset); @@ -97,9 +104,22 @@ namespace LinearAlgebra { Vector4 Diagonal() const; Vector3 Diagonal3() const; - Vector4 WorldX() const; - Vector4 WorldY() const; - Vector4 WorldZ() const; + /// Returns the local +X axis in world space. + /// This is the same as transforming the vector (1,0,0) by this matrix. + Vector3 WorldX() const; + /// Returns the local +Y axis in world space. + /// This is the same as transforming the vector (0,1,0) by this matrix. + Vector3 WorldY() const; + /// Returns the local +Z axis in world space. + /// This is the same as transforming the vector (0,0,1) by this matrix. + Vector3 WorldZ() const; + + /// Accesses this structure as a float array. + /// @return A pointer to the upper-left element. The data is contiguous in memory. + /// ptr[0] gives the element [0][0], ptr[1] is [0][1], ptr[2] is [0][2]. + /// ptr[4] == [1][0], ptr[5] == [1][1], ..., and finally, ptr[15] == [3][3]. + float *ptr() { return &elems[0][0]; } + const float *ptr() const { return &elems[0][0]; } float Determinant3x3() const; /// Computes the determinant of this matrix. @@ -129,8 +149,6 @@ namespace LinearAlgebra { static Matrix4x4 FromTranslation(const Vector3& rhs); - - static Matrix4x4 D3DOrthoProjLH(float nearPlane, float farPlane, float hViewportSize, float vViewportSize); static Matrix4x4 D3DOrthoProjRH(float nearPlane, float farPlane, float hViewportSize, float vViewportSize); static Matrix4x4 D3DPerspProjLH(float nearPlane, float farPlane, float hViewportSize, float vViewportSize); @@ -141,9 +159,6 @@ namespace LinearAlgebra { static Matrix4x4 OpenGLPerspProjLH(float nearPlane, float farPlane, float hViewportSize, float vViewportSize); static Matrix4x4 OpenGLPerspProjRH(float nearPlane, float farPlane, float hViewportSize, float vViewportSize); - Vector3 GetTranslationComponent() const; - Matrix3x3 GetRotationComponent() const; - Vector4 GetRow() const; Vector4 GetColumn() const; diff --git a/src/J3ML/LinearAlgebra/Matrix4x4.cpp b/src/J3ML/LinearAlgebra/Matrix4x4.cpp index 12c0356..09103ad 100644 --- a/src/J3ML/LinearAlgebra/Matrix4x4.cpp +++ b/src/J3ML/LinearAlgebra/Matrix4x4.cpp @@ -375,15 +375,15 @@ namespace LinearAlgebra { return Vector3 { At(0, 0), At(1,1), At(2,2) }; } - Vector4 Matrix4x4::WorldX() const { + Vector3 Matrix4x4::WorldX() const { return GetColumn3(0); } - Vector4 Matrix4x4::WorldY() const { + Vector3 Matrix4x4::WorldY() const { return GetColumn3(1); } - Vector4 Matrix4x4::WorldZ() const { + Vector3 Matrix4x4::WorldZ() const { return GetColumn3(2); } @@ -404,4 +404,19 @@ namespace LinearAlgebra { Vector3 Matrix4x4::operator*(const Vector3 &rhs) const { return this->Transform(rhs);} Vector4 Matrix4x4::operator*(const Vector4 &rhs) const { return this->Transform(rhs);} + + Vector4 Matrix4x4::Transform(float tx, float ty, float tz, float tw) const { + return Transform({tx, ty, tz, tw}); + } + + Vector4 Matrix4x4::Transform(const Vector4 &rhs) const { + return Vector4(At(0, 0) * rhs.x + At(0, 1) * rhs.y + At(0, 2) * rhs.z + At(0, 3) * rhs.w, + At(1, 0) * rhs.x + At(1, 1) * rhs.y + At(1, 2) * rhs.z + At(1, 3) * rhs.w, + At(2, 0) * rhs.x + At(2, 1) * rhs.y + At(2, 2) * rhs.z + At(2, 3) * rhs.w, + At(3, 0) * rhs.x + At(3, 1) * rhs.y + At(3, 2) * rhs.z + At(3, 3) * rhs.w); + } + + Vector3 Matrix4x4::GetTranslatePart() const { + return GetColumn3(3); + } } \ No newline at end of file