From d012af1214295b3c57bba336b66f007a7ff45229 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 23 Jan 2024 21:46:07 -0500 Subject: [PATCH] Implement Transform2D --- include/J3ML/LinearAlgebra/Matrix3x3.h | 13 +++++++--- include/J3ML/LinearAlgebra/Transform2D.h | 30 ++++++++++++++++++------ src/J3ML/LinearAlgebra/Matrix3x3.cpp | 5 ++++ src/J3ML/LinearAlgebra/Transform2D.cpp | 25 ++++++++++++++++++++ 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/include/J3ML/LinearAlgebra/Matrix3x3.h b/include/J3ML/LinearAlgebra/Matrix3x3.h index 51aff49..58e3e8b 100644 --- a/include/J3ML/LinearAlgebra/Matrix3x3.h +++ b/include/J3ML/LinearAlgebra/Matrix3x3.h @@ -58,6 +58,7 @@ namespace LinearAlgebra { void SetRow(int i, const Vector3 &vector3); void SetColumn(int i, const Vector3& vector); + void SetAt(int x, int y, float value); void Orthonormalize(int c0, int c1, int c2) { @@ -85,14 +86,20 @@ namespace LinearAlgebra { // Transforming a vector v using this matrix computes the vector // v' == M * v == R*S*v == (R * (S * v)) which means the scale operation // is applied to the vector first, followed by rotation, and finally translation - static Matrix3x3 FromRS(const Quaternion& rotate, const Matrix3x3& scale); - static Matrix3x3 FromRS(const Matrix3x3 &rotate, const Matrix3x3& scale); + static Matrix3x3 FromRS(const Quaternion& rotate, const Vector3& scale) + { + return Matrix3x3(rotate) * Matrix3x3::Scale(scale); + } + static Matrix3x3 FromRS(const Matrix3x3 &rotate, const Vector3& scale) + { + return rotate * Matrix3x3::Scale(scale); + } /// 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 Matrix3x3& scale); + static Matrix3x3 Scale(const Vector3& scale); /// Returns the main diagonal. Vector3 Diagonal() const; diff --git a/include/J3ML/LinearAlgebra/Transform2D.h b/include/J3ML/LinearAlgebra/Transform2D.h index bd1078e..345ce4a 100644 --- a/include/J3ML/LinearAlgebra/Transform2D.h +++ b/include/J3ML/LinearAlgebra/Transform2D.h @@ -8,17 +8,33 @@ namespace LinearAlgebra { protected: Matrix3x3 transformation; public: + + const static Transform2D Identity; + const static Transform2D FlipX; + const static Transform2D FlipY; + + Transform2D(float rotation, const Vector2& pos); + Transform2D(float px, float py, float sx, float sy, float ox, float oy, float kx, float ky, float rotation) + { + transformation = Matrix3x3(px, py, rotation, sx, sy, ox, oy, kx, ky); + } + Transform2D(const Vector2& pos, const Vector2& scale, const Vector2& origin, const Vector2& skew, float rotation); + Transform2D(const Matrix3x3& transform); Transform2D Translate(const Vector2& offset) const; Transform2D Translate(float x, float y) const; - Transform2D Scale(float scale); // Perform Uniform Scale - Transform2D Scale(float x, float y); // Perform Nonunform Scale + Transform2D Scale(float scale); // Perform Uniform Scale + Transform2D Scale(float x, float y); // Perform Nonunform Scale Transform2D Scale(const Vector2& scales); // Perform Nonuniform Scale Transform2D Rotate(); - - Vector2 Transform(const Vector2& input) const - { - return transformation * input; - } + Vector2 Transform(const Vector2& input) const; + Transform2D Inverse() const; + Transform2D AffineInverse() const; + float Determinant() const; + Vector2 GetOrigin() const; + float GetRotation() const; + Vector2 GetScale() const; + float GetSkew() const; + Transform2D OrthoNormalize(); }; } diff --git a/src/J3ML/LinearAlgebra/Matrix3x3.cpp b/src/J3ML/LinearAlgebra/Matrix3x3.cpp index b246c2c..0d2273b 100644 --- a/src/J3ML/LinearAlgebra/Matrix3x3.cpp +++ b/src/J3ML/LinearAlgebra/Matrix3x3.cpp @@ -37,9 +37,14 @@ namespace LinearAlgebra { return {x, y, z}; } + float Matrix3x3::At(int x, int y) const { return this->elems[x][y]; + } + void Matrix3x3::SetAt(int x, int y, float value) + { + this->elems[x][y] = value; } Vector3 Matrix3x3::operator*(const Vector3 &rhs) const { diff --git a/src/J3ML/LinearAlgebra/Transform2D.cpp b/src/J3ML/LinearAlgebra/Transform2D.cpp index 4f335ae..8705120 100644 --- a/src/J3ML/LinearAlgebra/Transform2D.cpp +++ b/src/J3ML/LinearAlgebra/Transform2D.cpp @@ -2,4 +2,29 @@ namespace LinearAlgebra { + const Transform2D Transform2D::Identity = Transform2D({0, 0}, {1, 1}, {0,0}, {0,0}, 0); + const Transform2D Transform2D::FlipX = Transform2D({0, 0}, {-1, 1}, {0,0}, {0,0}, 0); + const Transform2D Transform2D::FlipY = Transform2D({0, 0}, {1, -1}, {0,0}, {0,0}, 0); + + + Vector2 Transform2D::Transform(const Vector2 &input) const { + return transformation * input; + } + + Transform2D::Transform2D(const Matrix3x3 &transform) : transformation(transform) { } + + Transform2D::Transform2D(const Vector2& pos, const Vector2& scale, const Vector2& origin, const Vector2& skew, float rotation) { + transformation = Matrix3x3(pos.x, pos.y, rotation, scale.x, scale.y, origin.x, origin.y, skew.x, skew.y); + } + + Transform2D Transform2D::Translate(float x, float y) const { + auto copy = Matrix3x3(transformation); + copy.SetAt(0, 0, transformation.At(0, 0) + x); + copy.SetAt(0, 1, transformation.At(0, 1) + y); + return Transform2D(copy); + } + + Transform2D Transform2D::Translate(const LinearAlgebra::Vector2 &input) const { + return Translate(input.x, input.y); + } } \ No newline at end of file