From 09922ac0bdf96f480e12156ba61c323860a0633a Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 2 Jan 2024 16:29:19 -0500 Subject: [PATCH] Implementing Vector3 Unit Tests --- include/J3ML/LinearAlgebra/Vector2.h | 7 +- include/J3ML/LinearAlgebra/Vector3.h | 7 +- src/J3ML/LinearAlgebra/Matrix3x3.cpp | 3 +- src/J3ML/LinearAlgebra/Vector2.cpp | 34 ++++++++ src/J3ML/LinearAlgebra/Vector3.cpp | 57 +++++++++++++ tests/LinearAlgebra/Vector2Tests.cpp | 84 ++++++++++++++++++- tests/LinearAlgebra/Vector3Tests.cpp | 120 ++++++++++++++++++++++++++- 7 files changed, 298 insertions(+), 14 deletions(-) diff --git a/include/J3ML/LinearAlgebra/Vector2.h b/include/J3ML/LinearAlgebra/Vector2.h index 1bd0abb..e927afd 100644 --- a/include/J3ML/LinearAlgebra/Vector2.h +++ b/include/J3ML/LinearAlgebra/Vector2.h @@ -11,7 +11,7 @@ namespace LinearAlgebra { // Constructs a new Vector2 with the value (X, Y) Vector2(float X, float Y); Vector2(const Vector2& rhs); // Copy Constructor - Vector2(Vector2&&) = default; // Move Constructor + //Vector2(Vector2&&) = default; // Move Constructor static const Vector2 Zero; static const Vector2 Up; @@ -80,7 +80,7 @@ namespace LinearAlgebra { Vector2 Lerp(const Vector2& rhs, float alpha) const; // @see Lerp static Vector2 Lerp(const Vector2& lhs, const Vector2& rhs, float alpha); - + // Note: Input vectors MUST be normalized first! float AngleBetween(const Vector2& rhs) const; static float AngleBetween(const Vector2& lhs, const Vector2& rhs); @@ -104,12 +104,11 @@ namespace LinearAlgebra { Vector2 Div(float scalar) const; static Vector2 Div(const Vector2& lhs, float rhs); - // Unary operator + Vector2 operator +() const; // TODO: Implement Vector2 operator -() const; // Assigns a vector to another - Vector2& operator=(const Vector2&v); + Vector2& operator+=(const Vector2& rhs); // Adds a vector to this vector, in-place. diff --git a/include/J3ML/LinearAlgebra/Vector3.h b/include/J3ML/LinearAlgebra/Vector3.h index 3385faa..7f5eeef 100644 --- a/include/J3ML/LinearAlgebra/Vector3.h +++ b/include/J3ML/LinearAlgebra/Vector3.h @@ -36,8 +36,6 @@ public: void SetY(float newY); void SetZ(float newZ); - - bool IsWithinMarginOfError(const Vector3& rhs, float margin=0.001f) const; bool IsNormalized(float epsilonSq = 1e-5f) const; bool IsZero(float epsilonSq = 1e-6f) const; @@ -68,10 +66,7 @@ public: // Returns the length of the vector, which is sqrt(x^2 + y^2 + z^2) float Magnitude() const; - static float Magnitude(const Vector3& of) - { - - } + static float Magnitude(const Vector3& of); // Returns a float value equal to the magnitudes of the two vectors multiplied together and then multiplied by the cosine of the angle between them. // For normalized vectors, dot returns 1 if they point in exactly the same direction, diff --git a/src/J3ML/LinearAlgebra/Matrix3x3.cpp b/src/J3ML/LinearAlgebra/Matrix3x3.cpp index 5502000..da03d86 100644 --- a/src/J3ML/LinearAlgebra/Matrix3x3.cpp +++ b/src/J3ML/LinearAlgebra/Matrix3x3.cpp @@ -152,6 +152,7 @@ namespace LinearAlgebra { auto m21 = this->elems[2][1]; auto m22 = this->elems[2][2]; + // NO: This is correct order for transposition! return { m00, m10, m20, m01, m11, m21, @@ -162,7 +163,7 @@ namespace LinearAlgebra { Vector2 Matrix3x3::Transform(const Vector2 &rhs) const { return { At(0,0) * rhs.x + At(0, 1) * rhs.y, - At(1, 0) * rhs.x + At(1, 1) * rhs.y + At(1,0) * rhs.x + At(1, 1) * rhs.y }; } diff --git a/src/J3ML/LinearAlgebra/Vector2.cpp b/src/J3ML/LinearAlgebra/Vector2.cpp index 9cd4ad9..895efaa 100644 --- a/src/J3ML/LinearAlgebra/Vector2.cpp +++ b/src/J3ML/LinearAlgebra/Vector2.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace LinearAlgebra { @@ -115,6 +116,7 @@ namespace LinearAlgebra { { auto numer = this->Dot(rhs); auto denom = this->Magnitude() * rhs.Magnitude(); + std::cout << numer << ", " << denom << std::endl; return std::acos(numer / denom); } @@ -198,5 +200,37 @@ namespace LinearAlgebra { Vector2 Vector2::Lerp(const Vector2 &lhs, const Vector2 &rhs, float alpha) { return lhs.Lerp(rhs, alpha); } + Vector2 Vector2::Div(const Vector2 &lhs, float rhs) { + return lhs.Div(rhs); + } + + Vector2 Vector2::Mul(const Vector2 &lhs, float rhs) { + return lhs.Mul(rhs); + } + + Vector2 Vector2::Sub(const Vector2 &lhs, const Vector2 &rhs) { + return lhs.Sub(rhs); + } + + Vector2 Vector2::Add(const Vector2 &lhs, const Vector2 &rhs) { + return lhs.Add(rhs); + } + + Vector2 Vector2::Add(const Vector2 &rhs) const { + return *this + rhs; + } + + Vector2 Vector2::Sub(const Vector2 &rhs) const { + return *this - rhs; + } + + Vector2 Vector2::Mul(float scalar) const { + return *this * scalar; + } + + Vector2 Vector2::Div(float scalar) const { + return *this / scalar; + } + } \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra/Vector3.cpp b/src/J3ML/LinearAlgebra/Vector3.cpp index f10d519..0ad9992 100644 --- a/src/J3ML/LinearAlgebra/Vector3.cpp +++ b/src/J3ML/LinearAlgebra/Vector3.cpp @@ -14,6 +14,7 @@ namespace LinearAlgebra { const Vector3 Vector3::Right = {1, 0, 0}; const Vector3 Vector3::Forward = {0, 0, -1}; const Vector3 Vector3::Backward = {0, 0, 1}; + const Vector3 Vector3::NaN = {NAN, NAN, NAN}; Vector3 Vector3::operator+(const Vector3& rhs) const { @@ -231,5 +232,61 @@ namespace LinearAlgebra { return std::abs(LengthSquared()-1.f) <= epsilonSq; } + Vector3 Vector3::Cross(const Vector3 &lhs, const Vector3 &rhs) { + return lhs.Cross(rhs); + } + + Vector3 Vector3::Normalize(const Vector3 &targ) { + return targ.Normalize(); + } + + Vector3 Vector3::Project(const Vector3 &lhs, const Vector3 &rhs) { + return lhs.Project(rhs); + } + + float Vector3::Dot(const Vector3 &lhs, const Vector3 &rhs) { + return lhs.Dot(rhs); + } + + float Vector3::Magnitude(const Vector3 &of) { + return of.Magnitude(); + } + + Vector3 Vector3::Lerp(const Vector3 &lhs, const Vector3 &rhs, float alpha) { + return lhs.Lerp(rhs, alpha); + } + + Vector3 Vector3::Add(const Vector3 &lhs, const Vector3 &rhs) { + return lhs.Add(rhs); + } + + Vector3 Vector3::Add(const Vector3 &rhs) const { + return *this + rhs; + } + + Vector3 Vector3::Sub(const Vector3 &rhs) const { + return *this - rhs; + } + + Vector3 Vector3::Sub(const Vector3 &lhs, const Vector3 &rhs) { + lhs.Sub(rhs); + } + + Vector3 Vector3::Mul(float scalar) const { + return *this * scalar; + } + + Vector3 Vector3::Mul(const Vector3 &lhs, float rhs) { + return lhs.Mul(rhs); + } + + Vector3 Vector3::Div(float scalar) const { + return *this / scalar; + } + + Vector3 Vector3::Div(const Vector3 &lhs, float rhs) { + return lhs.Div(rhs); + } + #pragma endregion } \ No newline at end of file diff --git a/tests/LinearAlgebra/Vector2Tests.cpp b/tests/LinearAlgebra/Vector2Tests.cpp index 938e52a..a9213f5 100644 --- a/tests/LinearAlgebra/Vector2Tests.cpp +++ b/tests/LinearAlgebra/Vector2Tests.cpp @@ -24,7 +24,27 @@ TEST(Vector2Test, V2_Addition_Op) EXPECT_EQ(A+B, C); } -TEST(Vector2Test, V2_Subtraction_Op) +TEST(Vector2Test, V2_Addition_Method) +{ + Vector2 A {2,2}; + Vector2 B {2,2}; + + Vector2 C {4, 4}; + + EXPECT_EQ(A.Add(B), C); +} + +TEST(Vector2Test, V2_Addition_Static) +{ + Vector2 A {3, 3}; + Vector2 B {2, 2}; + + Vector2 C {5, 5}; + + EXPECT_EQ(Vector2::Add(A, B), C); +} + +TEST(Vector2Test, V2_Subtract_Op) { Vector2 A {1,1}; Vector2 B {2,2}; @@ -34,6 +54,26 @@ TEST(Vector2Test, V2_Subtraction_Op) EXPECT_EQ(A-B, C); } +TEST(Vector2Test, V2_Subtract_Method) +{ + Vector2 A {1,1}; + Vector2 B {2,2}; + + Vector2 C {-1, -1}; + + EXPECT_EQ(A.Sub(B), C); +} + +TEST(Vector2Test, V2_Subtract_Static) +{ + Vector2 A {1,1}; + Vector2 B {2,2}; + + Vector2 C {-1, -1}; + + EXPECT_EQ(Vector2::Sub(A, B), C); +} + TEST(Vector2Test, V2_Scalar_Multiplication) { Vector2 A {5, 1}; @@ -84,20 +124,60 @@ TEST(Vector2Test, V2_Max) TEST(Vector2Test, V2_Clamp) { + Vector2 Input{0, 20}; + Vector2 Minimum { 2, 2}; + Vector2 Maximum {16, 16}; + + Vector2 ExpectedResult {2, 16}; + + EXPECT_EQ(Input.Clamp(Minimum, Maximum), ExpectedResult); } TEST(Vector2Test, V2_DotProduct) { - + // TODO: Equality + Vector2 A {2, 2}; + Vector2 B {1, 1}; + EXPECT_FLOAT_EQ(A.Dot(B), 1.f); } TEST(Vector2Test, V2_Project) { + Vector2 Base {1, 1}; + Vector2 Projected {1, 1}; + Vector2 ExpectedResult {0.5, 0.5}; + + EXPECT_EQ(Base.Project(Projected), ExpectedResult); } TEST(Vector2Test, V2_Normalize) { + Vector2 A{2, 0}; + Vector2 B{1, 0}; + EXPECT_EQ(A.Normalize(), B); +} +TEST(Vector2Test, V2_Lerp) +{ + Vector2 A {2,2}; + Vector2 B {10, 10}; + Vector2 C {6, 6}; + + EXPECT_EQ(A.Lerp(B, 0.f), A); + EXPECT_EQ(A.Lerp(B, 1.f), B); + EXPECT_EQ(A.Lerp(B, 0.5f), C); +} + +TEST(Vector2Test, V2_AngleBetween) +{ + Vector2 A {0.5f, 0.5}; + Vector2 B {0.5f, 0.1f}; + + A = A.Normalize(); + B = B.Normalize(); + + // TODO: AngleBetween returns not a number + EXPECT_FLOAT_EQ(A.AngleBetween(B), 0.58800244); } \ No newline at end of file diff --git a/tests/LinearAlgebra/Vector3Tests.cpp b/tests/LinearAlgebra/Vector3Tests.cpp index 37460f7..3dba895 100644 --- a/tests/LinearAlgebra/Vector3Tests.cpp +++ b/tests/LinearAlgebra/Vector3Tests.cpp @@ -5,5 +5,123 @@ using Vector3 = LinearAlgebra::Vector3; TEST(Vector3Test, V3_Constructor_Default) { + EXPECT_EQ(Vector3(), Vector3::Zero); +} -} \ No newline at end of file +TEST(Vector3Test, V3_Constructor_XYZ) +{ + Vector3 Input {0, 1, 0}; + + EXPECT_EQ(Input, Vector3::Down); +} + +TEST(Vector3Test, V3_Addition_Op) { + Vector3 A {}; + Vector3 B {}; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(A + B, ExpectedResult); +} +TEST(Vector3Test, V3_Addition_Method) { + Vector3 A {}; + Vector3 B {}; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(A.Add(B), ExpectedResult); +} +TEST(Vector3Test, V3_Addition_Static) { + Vector3 A {}; + Vector3 B {}; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(Vector3::Add(A, B), ExpectedResult); +} +TEST(Vector3Test, V3_Subtract_Op) { + Vector3 A {}; + Vector3 B {}; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(A - B, ExpectedResult); +} +TEST(Vector3Test, V3_Subtract_Method) { + Vector3 A {}; + Vector3 B {}; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(A.Sub(B), ExpectedResult); +} +TEST(Vector3Test, V3_Subtract_Static) { + Vector3 A {}; + Vector3 B {}; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(Vector3::Sub(A, B), ExpectedResult); +} +TEST(Vector3Test, V3_Scalar_Mult_Op) { + Vector3 A { }; + float B = 1.5f; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(A * B, ExpectedResult); +} +TEST(Vector3Test, V3_Scalar_Mult_Method) { + Vector3 A { }; + float B = 1.5f; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(A.Mul(B), ExpectedResult); +} +TEST(Vector3Test, V3_Scalar_Mult_Static) { + Vector3 A { }; + float B = 1.5f; + + Vector3 ExpectedResult {}; + + EXPECT_EQ(Vector3::Mul(A, B), ExpectedResult); +} +TEST(Vector3Test, V3_Scalar_Div_Op) { + Vector3 A {}; + float B = 1.5f; + + Vector3 ExpectedResult { }; + EXPECT_EQ(A / B, ExpectedResult); +} +TEST(Vector3Test, V3_Scalar_Div_Method) { + Vector3 A { }; + float B = 1.5f; + Vector3 ExpectedResult { }; + + EXPECT_EQ(A.Div(B), ExpectedResult); +} +TEST(Vector3Test, V3_Scalar_Div_Static) { + Vector3 A { }; + float B = 1.5f; + + Vector3 ExpectedResult { }; + + EXPECT_EQ(Vector3::Div(A, B), ExpectedResult); +} +TEST(Vector3Test, V3_Sizeof) { + EXPECT_EQ(sizeof(Vector3), 12); +} +TEST(Vector3Test, V3_NaN) { + EXPECT_NE(Vector3(0, 0, 0), Vector3::NaN); + +} +TEST(Vector3Test, V3_Min) {} +TEST(Vector3Test, V3_Max) {} +TEST(Vector3Test, V3_Clamp) {} +TEST(Vector3Test, V3_DotProduct) {} +TEST(Vector3Test, V3_CrossProduct) {} +TEST(Vector3Test, V3_Project) {} +TEST(Vector3Test, V3_Normalize) {} +TEST(Vector3Test, V3_Lerp) {} +TEST(Vector3Test, V3_AngleBetween) {}