From f7a7ec38d79e67502018717b08c5183c8511a615 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 23 Jan 2024 04:14:24 -0500 Subject: [PATCH] Implement Transform2D.h --- include/J3ML/LinearAlgebra/Matrix3x3.h | 1 + include/J3ML/LinearAlgebra/Quaternion.h | 21 ++++- include/J3ML/LinearAlgebra/Transform2D.h | 5 ++ src/J3ML/LinearAlgebra/Quaternion.cpp | 8 +- tests/LinearAlgebra/Vector4Tests.cpp | 109 ++++++++++++++++++++++- 5 files changed, 132 insertions(+), 12 deletions(-) diff --git a/include/J3ML/LinearAlgebra/Matrix3x3.h b/include/J3ML/LinearAlgebra/Matrix3x3.h index c18d4d2..51aff49 100644 --- a/include/J3ML/LinearAlgebra/Matrix3x3.h +++ b/include/J3ML/LinearAlgebra/Matrix3x3.h @@ -125,6 +125,7 @@ namespace LinearAlgebra { Vector2 Transform(const Vector2& rhs) const; Vector3 Transform(const Vector3& rhs) const; + Vector2 operator * (const Vector2& rhs) const; Vector3 operator * (const Vector3& rhs) const; Matrix3x3 operator * (const Matrix3x3& rhs) const; diff --git a/include/J3ML/LinearAlgebra/Quaternion.h b/include/J3ML/LinearAlgebra/Quaternion.h index 7161ecd..89713ff 100644 --- a/include/J3ML/LinearAlgebra/Quaternion.h +++ b/include/J3ML/LinearAlgebra/Quaternion.h @@ -37,6 +37,20 @@ namespace LinearAlgebra Vector3 GetWorldY() const; Vector3 GetWorldZ() const; + Vector3 GetAxis() const + { + float rcpSinAngle = 1 - (std::sqrt(1 - w * w)); + + return Vector3(x, y, z) * rcpSinAngle; + } + + float GetAngle() const + { + return std::acos(w) * 2.f; + } + + + Matrix3x3 ToMatrix3x3() const; Vector3 Transform(const Vector3& vec) const; @@ -57,7 +71,6 @@ namespace LinearAlgebra // TODO: Document (But do not override!) certain math functions that are the same for Vec4 // TODO: Double Check which operations need to be overriden for correct behavior! - // Multiplies two quaternions together. // The product q1 * q2 returns a quaternion that concatenates the two orientation rotations. // The rotation q2 is applied first before q1. @@ -71,9 +84,9 @@ namespace LinearAlgebra // Divides a quaternion by another. Divison "a / b" results in a quaternion that rotates the orientation b to coincide with orientation of Quaternion operator / (const Quaternion& rhs) const; - Quaternion operator +(const Quaternion& rhs) const; - Quaternion operator +() const; - Quaternion operator -() const; + Quaternion operator + (const Quaternion& rhs) const; + Quaternion operator + () const; + Quaternion operator - () const; float Dot(const Quaternion &quaternion) const; float Angle() const; diff --git a/include/J3ML/LinearAlgebra/Transform2D.h b/include/J3ML/LinearAlgebra/Transform2D.h index ca0f5b5..bd1078e 100644 --- a/include/J3ML/LinearAlgebra/Transform2D.h +++ b/include/J3ML/LinearAlgebra/Transform2D.h @@ -14,6 +14,11 @@ namespace LinearAlgebra { 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; + } }; } diff --git a/src/J3ML/LinearAlgebra/Quaternion.cpp b/src/J3ML/LinearAlgebra/Quaternion.cpp index 44844e1..b7de362 100644 --- a/src/J3ML/LinearAlgebra/Quaternion.cpp +++ b/src/J3ML/LinearAlgebra/Quaternion.cpp @@ -60,16 +60,14 @@ namespace LinearAlgebra { Quaternion::Quaternion(float X, float Y, float Z, float W) : Vector4(X,Y,Z,W) {} // TODO: implement - float Quaternion::Dot(const Quaternion &quaternion) const {} + float Quaternion::Dot(const Quaternion &rhs) const { + return x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w; + } Quaternion::Quaternion(Vector4 vector4) { } - float Quaternion::Angle() const { - return std::acos(w) * 2.f; - } - Quaternion Quaternion::Normalize() const { float length = Length(); if (length < 1e-4f) diff --git a/tests/LinearAlgebra/Vector4Tests.cpp b/tests/LinearAlgebra/Vector4Tests.cpp index a8cec1d..1ab861b 100644 --- a/tests/LinearAlgebra/Vector4Tests.cpp +++ b/tests/LinearAlgebra/Vector4Tests.cpp @@ -1,3 +1,106 @@ -// -// Created by josh on 12/26/2023. -// +#include +#include + +using Vector4 = LinearAlgebra::Vector4; + + +void EXPECT_V4_EQ(const Vector4& lhs, const Vector4& rhs) +{ + +} + +TEST(Vector4Test, V4_Constructor_Default) +{ + EXPECT_V4_EQ(Vector4(), Vector4::Zero); +} + +TEST(Vector4Test, V4_Constructor_XYZW) +{ + Vector4 Input {0, 1, 0, 1}; + EXPECT_FLOAT_EQ(Input.x, 0); + EXPECT_FLOAT_EQ(Input.y, 1); + EXPECT_FLOAT_EQ(Input.z, 0); + EXPECT_FLOAT_EQ(Input.w, 1); +} + +TEST(Vector4Test, V4_Addition_Op) { + Vector4 A {1, 1, 1, 1}; + Vector4 B {2, 2, 2, 2}; + + Vector4 ExpectedResult {3, 3, 3, 3}; + + EXPECT_V4_EQ(A + B, ExpectedResult); +} + +TEST(Vector4Test, V4_Addition_Method) +{ + +} + +TEST(Vector4Test, V4_Addition_Static) +{ + +} + +TEST(Vector4Test, V4_Subtract_Op) +{ + +} + +TEST(Vector4Test, V4_Subtract_Method) +{ + +} + +TEST(Vector4Test, V4_Subtract_Static) +{ + +} + +TEST(Vector4Test, V4_Scalar_Mult_Op) +{ + +} + +TEST(Vector4Test, V4_Scalar_Mult_Method) +{ + +} + +TEST(Vector4Test, V4_Scalar_Mult_Static) +{ + +} + +TEST(Vector4Test, V4_Scalar_Div_Op) +{ + +} + +TEST(Vector4Test, V4_Scalar_Div_Method) +{ + +} + +TEST(Vector4Test, V4_Scalar_Div_Static) +{ + +} + +TEST(Vector4Test, V4_Sizeof) +{ + +} + +TEST(Vector4Test, V4_NaN) +{ + +} + +TEST(Vector4Tests, V4_Min) {} +TEST(Vector4Tests, V4_Max) {} +TEST(Vector4Tests, V4_Clamp) {} +TEST(Vector4Tests, V4_DotProduct) {} +TEST(Vector4Tests, V4_CrossProduct) {} +TEST(Vector4Tests, V4_Project) {} +TEST(Vector4Test, V4_Normalize) {} \ No newline at end of file