From d37b685df9334043a9f8170f377abf10ac1ead78 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 1 Feb 2024 21:02:18 -0500 Subject: [PATCH] Implement Matrix3x3 FromScale and ScaleBy --- include/J3ML/LinearAlgebra/Matrix3x3.h | 24 +++++---------- include/J3ML/LinearAlgebra/Matrix4x4.h | 5 +++ src/J3ML/LinearAlgebra/Matrix3x3.cpp | 42 ++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/include/J3ML/LinearAlgebra/Matrix3x3.h b/include/J3ML/LinearAlgebra/Matrix3x3.h index 3596cb9..8d18501 100644 --- a/include/J3ML/LinearAlgebra/Matrix3x3.h +++ b/include/J3ML/LinearAlgebra/Matrix3x3.h @@ -61,16 +61,7 @@ namespace LinearAlgebra { void SetColumn(int i, const Vector3& vector); void SetAt(int x, int y, float value); - void Orthonormalize(int c0, int c1, int c2) - { - Vector3 v0 = GetColumn(c0); - Vector3 v1 = GetColumn(c1); - Vector3 v2 = GetColumn(c2); - Vector3::Orthonormalize(v0, v1, v2); - SetColumn(c0, v0); - SetColumn(c1, v1); - SetColumn(c2, v2); - } + void Orthonormalize(int c0, int c1, int c2); static Matrix3x3 LookAt(const Vector3& forward, const Vector3& target, const Vector3& localUp, const Vector3& worldUp); @@ -90,8 +81,8 @@ namespace LinearAlgebra { /// Creates a new transformation matrix that scales by the given factors. // This matrix scales with respect to origin. - static Matrix3x3 Scale(float sx, float sy, float sz); - static Matrix3x3 Scale(const Vector3& scale); + static Matrix3x3 FromScale(float sx, float sy, float sz); + static Matrix3x3 FromScale(const Vector3& scale); /// Returns the main diagonal. Vector3 Diagonal() const; @@ -124,10 +115,11 @@ namespace LinearAlgebra { Vector2 Transform(const Vector2& rhs) const; Vector3 Transform(const Vector3& rhs) const; - Vector3 operator[](int row) const - { - return Vector3{elems[row][0], elems[row][1], elems[row][2]}; - } + + Matrix3x3 ScaleBy(const Vector3& rhs); + Vector3 GetScale() const; + + Vector3 operator[](int row) const; Vector3 operator * (const Vector3& rhs) const; Matrix3x3 operator * (const Matrix3x3& rhs) const; diff --git a/include/J3ML/LinearAlgebra/Matrix4x4.h b/include/J3ML/LinearAlgebra/Matrix4x4.h index 54d213d..5fa9bd9 100644 --- a/include/J3ML/LinearAlgebra/Matrix4x4.h +++ b/include/J3ML/LinearAlgebra/Matrix4x4.h @@ -118,6 +118,11 @@ namespace LinearAlgebra { Vector3 GetRow3(int index) const; Vector3 GetColumn3(int index) const; + Vector3 GetScale() const + { + + } + float &At(int row, int col); float At(int x, int y) const; diff --git a/src/J3ML/LinearAlgebra/Matrix3x3.cpp b/src/J3ML/LinearAlgebra/Matrix3x3.cpp index 3f32c48..0fd58d1 100644 --- a/src/J3ML/LinearAlgebra/Matrix3x3.cpp +++ b/src/J3ML/LinearAlgebra/Matrix3x3.cpp @@ -313,16 +313,54 @@ namespace LinearAlgebra { } Matrix3x3 Matrix3x3::FromRS(const Quaternion &rotate, const Vector3 &scale) { - return Matrix3x3(rotate) * Matrix3x3::Scale(scale); + return Matrix3x3(rotate) * Matrix3x3::FromScale(scale); } Matrix3x3 Matrix3x3::FromRS(const Matrix3x3 &rotate, const Vector3 &scale) { - return rotate * Matrix3x3::Scale(scale); + return rotate * Matrix3x3::FromScale(scale); } Matrix3x3 Matrix3x3::FromQuat(const Quaternion &orientation) { return Matrix3x3(orientation); } + Matrix3x3 Matrix3x3::ScaleBy(const Vector3 &rhs) { + return *this * FromScale(rhs); + } + + Vector3 Matrix3x3::GetScale() const { + return Vector3(GetColumn(0).Length(), GetColumn(1).Length(), GetColumn(2).Length()); + } + + Vector3 Matrix3x3::operator[](int row) const { + return Vector3{elems[row][0], elems[row][1], elems[row][2]}; + } + + Matrix3x3 Matrix3x3::FromScale(const Vector3 &scale) { + Matrix3x3 m; + m.At(0,0) = scale.x; + m.At(1,1) = scale.y; + m.At(2,2) = scale.z; + return m; + } + + Matrix3x3 Matrix3x3::FromScale(float sx, float sy, float sz) { + Matrix3x3 m; + m.At(0,0) = sx; + m.At(1,1) = sy; + m.At(2,2) = sz; + return m; + } + + void Matrix3x3::Orthonormalize(int c0, int c1, int c2) { + Vector3 v0 = GetColumn(c0); + Vector3 v1 = GetColumn(c1); + Vector3 v2 = GetColumn(c2); + Vector3::Orthonormalize(v0, v1, v2); + SetColumn(c0, v0); + SetColumn(c1, v1); + SetColumn(c2, v2); + } + }