diff --git a/include/J3ML/LinearAlgebra.h b/include/J3ML/LinearAlgebra.h index e45c08a..6fd6231 100644 --- a/include/J3ML/LinearAlgebra.h +++ b/include/J3ML/LinearAlgebra.h @@ -4,11 +4,8 @@ #include #include #include -// TODO: Make GLM redundant, and remove it thereafter. -// TODO: SIMD operations for fast math (parallelization!!) - namespace Math { const float Pi = M_PI; diff --git a/include/J3ML/LinearAlgebra/Angle2D.h b/include/J3ML/LinearAlgebra/Angle2D.h index e279482..5583c50 100644 --- a/include/J3ML/LinearAlgebra/Angle2D.h +++ b/include/J3ML/LinearAlgebra/Angle2D.h @@ -1,8 +1,10 @@ -// -// Created by josh on 12/25/2023. -// +#pragma once -#ifndef J3ML_ANGLE2D_H -#define J3ML_ANGLE2D_H +#include -#endif //J3ML_ANGLE2D_H +namespace LinearAlgebra { + class Angle2D { + public: + + }; +} \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/AxisAngle.h b/include/J3ML/LinearAlgebra/AxisAngle.h index 97ec70f..240e88a 100644 --- a/include/J3ML/LinearAlgebra/AxisAngle.h +++ b/include/J3ML/LinearAlgebra/AxisAngle.h @@ -1,5 +1,7 @@ #pragma once +#include +#include namespace LinearAlgebra { diff --git a/include/J3ML/LinearAlgebra/CoordinateFrame.h b/include/J3ML/LinearAlgebra/CoordinateFrame.h index 5a3aaaa..c751b55 100644 --- a/include/J3ML/LinearAlgebra/CoordinateFrame.h +++ b/include/J3ML/LinearAlgebra/CoordinateFrame.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace LinearAlgebra { /// The CFrame is fundamentally 4 vectors (position, forward, right, up vector) diff --git a/include/J3ML/LinearAlgebra/Matrix2x2.h b/include/J3ML/LinearAlgebra/Matrix2x2.h index be5be5e..9ff362f 100644 --- a/include/J3ML/LinearAlgebra/Matrix2x2.h +++ b/include/J3ML/LinearAlgebra/Matrix2x2.h @@ -1,5 +1,6 @@ #pragma once +#include #include namespace LinearAlgebra { diff --git a/include/J3ML/LinearAlgebra/Matrix3x3.h b/include/J3ML/LinearAlgebra/Matrix3x3.h index eb07958..0d748e3 100644 --- a/include/J3ML/LinearAlgebra/Matrix3x3.h +++ b/include/J3ML/LinearAlgebra/Matrix3x3.h @@ -1,6 +1,9 @@ #pragma once +#include +#include #include +#include namespace LinearAlgebra { /// A 3-by-3 matrix for linear transformations of 3D geometry. diff --git a/include/J3ML/LinearAlgebra/Matrix4x4.h b/include/J3ML/LinearAlgebra/Matrix4x4.h index 04ffe09..f77cd7d 100644 --- a/include/J3ML/LinearAlgebra/Matrix4x4.h +++ b/include/J3ML/LinearAlgebra/Matrix4x4.h @@ -1,5 +1,8 @@ #pragma once +#include + + namespace LinearAlgebra { /// A 4-by-4 matrix for affine transformations and perspective projections of 3D geometry. diff --git a/include/J3ML/LinearAlgebra/Quaternion.h b/include/J3ML/LinearAlgebra/Quaternion.h index 8da77d2..5fabd3f 100644 --- a/include/J3ML/LinearAlgebra/Quaternion.h +++ b/include/J3ML/LinearAlgebra/Quaternion.h @@ -1,15 +1,16 @@ #pragma once +#include namespace LinearAlgebra { - class Quaternion : public Vector4 + class Quaternion { public: - Quaternion() {} + Quaternion(); Quaternion(const Quaternion& rhs) = default; - explicit Quaternion(Matrix3x3& rotationMtrx) {} - explicit Quaternion(Matrix4x4& rotationMtrx) {} + explicit Quaternion(const Matrix3x3& rotationMtrx); + explicit Quaternion(const Matrix4x4& rotationMtrx); // @note The input data is not normalized after construction, this has to be done manually. Quaternion(float x, float y, float z, float w); // Constructs this quaternion by specifying a rotation axis and the amount of rotation to be performed about that axis @@ -20,47 +21,23 @@ namespace LinearAlgebra Quaternion Inverse() const; //void Normalize(); Quaternion Normalize() const; - Vector3 GetWorldX() const { return Transform(1.f, 0.f, 0.f); } - Vector3 GetWorldY() const { return Transform(0.f, 1.f, 0.f); } - Vector3 GetWorldZ() const { return Transform(0.f, 0.f, 1.f); } + Vector3 GetWorldX() const; + Vector3 GetWorldY() const; + Vector3 GetWorldZ() const; Matrix3x3 ToMatrix3x3() const; - Vector3 Transform(const Vector3& vec) const - { - Matrix3x3 mat = this->ToMatrix3x3(); - return mat * vec; - } - Vector3 Transform(float X, float Y, float Z) const - { - return Transform(Vector3{X, Y, Z}); - } + Vector3 Transform(const Vector3& vec) const; + Vector3 Transform(float X, float Y, float Z) const; // Note: We only transform the x,y,z components of 4D vectors, w is left untouched - Vector4 Transform(const Vector4& vec) const - { - return Vector4(Transform(vec.x, vec.y, vec.z), vec.w); - } - Vector4 Transform(float X, float Y, float Z, float W) const - { - return Transform(Vector4(X, Y, Z, W)); - } + Vector4 Transform(const Vector4& vec) const; + Vector4 Transform(float X, float Y, float Z, float W) const; Quaternion GetInverse() const; - Quaternion Lerp(const Quaternion& b, float t) const - { - float angle = this->dot(b); - if (angle >= 0.f) // Make sure we rotate the shorter arc - return (*this * (1.f - t) + b * t).Normalize(); - else - return (*this * (t - 1.f) + b * t).Normalize(); - } + Quaternion Lerp(const Quaternion& b, float t) const; Quaternion Slerp(const Quaternion& target) const; - void SetFromAxisAngle(const Vector3& axis, float angle) - { - float sinz, cosz; - - } + void SetFromAxisAngle(const Vector3& axis, float angle); void SetFromAxisAngle(const Vector4& axis, float angle) { @@ -75,10 +52,7 @@ namespace LinearAlgebra // The rotation q2 is applied first before q1. Quaternion operator * (const Quaternion& rhs) const; - Quaternion operator * (float scalar) const - { - return Quaternion(x * scalar, y * scalar, z * scalar, w * scalar); - } + Quaternion operator * (float scalar) const; // Transforms the given vector by this Quaternion. Vector3 operator * (const Vector3& rhs) const; @@ -87,7 +61,12 @@ 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 { return *this; } + Quaternion operator +() const; Quaternion operator -() const; + public: + float x = 0; + float y = 0; + float z = 0; + float w = 0; }; } \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/Transform2D.h b/include/J3ML/LinearAlgebra/Transform2D.h index 18aed94..ca0f5b5 100644 --- a/include/J3ML/LinearAlgebra/Transform2D.h +++ b/include/J3ML/LinearAlgebra/Transform2D.h @@ -1,13 +1,19 @@ #pragma once -class Transform2D { -protected: - Matrix3x3 transformation; -public: - 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(const Vector2& scales); // Perform Nonuniform Scale - Transform2D Rotate(); -}; \ No newline at end of file +#include +#include + +namespace LinearAlgebra { + class Transform2D { + protected: + Matrix3x3 transformation; + public: + 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(const Vector2& scales); // Perform Nonuniform Scale + Transform2D Rotate(); + }; +} + diff --git a/include/J3ML/LinearAlgebra/Vector2.h b/include/J3ML/LinearAlgebra/Vector2.h index 428cd50..f9c7cfd 100644 --- a/include/J3ML/LinearAlgebra/Vector2.h +++ b/include/J3ML/LinearAlgebra/Vector2.h @@ -1,7 +1,9 @@ -#include #pragma once +#include +#include + namespace LinearAlgebra { @@ -25,6 +27,7 @@ namespace LinearAlgebra { static const Vector2 Left; static const Vector2 Down; static const Vector2 Right; + static const Vector2 NaN; float operator[](std::size_t index); @@ -126,8 +129,8 @@ namespace LinearAlgebra { float x = 0; float y = 0; #else - const float x = 0; - const float y = 0; + float x = 0; + float y = 0; #endif }; } \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/Vector3.h b/include/J3ML/LinearAlgebra/Vector3.h index f74b161..540abe5 100644 --- a/include/J3ML/LinearAlgebra/Vector3.h +++ b/include/J3ML/LinearAlgebra/Vector3.h @@ -132,9 +132,9 @@ public: float y = 0; float z = 0; #else - const float x = 0; - const float y = 0; - const float z = 0; + float x = 0; + float y = 0; + float z = 0; #endif }; } \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/Vector4.h b/include/J3ML/LinearAlgebra/Vector4.h index 9ff3240..3f1372f 100644 --- a/include/J3ML/LinearAlgebra/Vector4.h +++ b/include/J3ML/LinearAlgebra/Vector4.h @@ -1,6 +1,7 @@ #pragma once -#include +#include + namespace LinearAlgebra { class Vector4 { @@ -13,9 +14,9 @@ namespace LinearAlgebra { Vector4(float X, float Y, float Z, float W); Vector4(const Vector4& copy) = default; Vector4(Vector4&& move) = default; - Vector4& operator=(const Vector4& rhs); + float GetX() const; float GetY() const; float GetZ() const; @@ -89,10 +90,10 @@ namespace LinearAlgebra { float z = 0; float w = 0; #else - const float x = 0; - const float y = 0; - const float z = 0; - const float w = 0; + float x = 0; + float y = 0; + float z = 0; + float w = 0; #endif }; diff --git a/src/J3ML/LinearAlgebra/CoordinateFrame.cpp b/src/J3ML/LinearAlgebra/CoordinateFrame.cpp new file mode 100644 index 0000000..9fe1765 --- /dev/null +++ b/src/J3ML/LinearAlgebra/CoordinateFrame.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra/Matrix3x3.cpp b/src/J3ML/LinearAlgebra/Matrix3x3.cpp index 838d655..9a771d5 100644 --- a/src/J3ML/LinearAlgebra/Matrix3x3.cpp +++ b/src/J3ML/LinearAlgebra/Matrix3x3.cpp @@ -1,4 +1,5 @@ #include +#include namespace LinearAlgebra { @@ -95,5 +96,9 @@ namespace LinearAlgebra { this->elems[2][2] = r3.z; } + Matrix3x3::Matrix3x3(const Quaternion &orientation) { + + } + } diff --git a/src/J3ML/LinearAlgebra/Quaternion.cpp b/src/J3ML/LinearAlgebra/Quaternion.cpp index f79d16b..55756bd 100644 --- a/src/J3ML/LinearAlgebra/Quaternion.cpp +++ b/src/J3ML/LinearAlgebra/Quaternion.cpp @@ -1,8 +1,59 @@ #include +#include + + namespace LinearAlgebra { Quaternion Quaternion::operator-() const { return {-x, -y, -z, -w}; } + + Quaternion::Quaternion(const Matrix3x3 &rotationMtrx) {} + + Quaternion::Quaternion(const Matrix4x4 &rotationMtrx) {} + + Vector3 Quaternion::GetWorldX() const { return Transform(1.f, 0.f, 0.f); } + + Vector3 Quaternion::GetWorldY() const { return Transform(0.f, 1.f, 0.f); } + + Vector3 Quaternion::GetWorldZ() const { return Transform(0.f, 0.f, 1.f); } + + Vector3 Quaternion::Transform(const Vector3 &vec) const { + Matrix3x3 mat = this->ToMatrix3x3(); + return mat * vec; + } + + Vector3 Quaternion::Transform(float X, float Y, float Z) const { + return Transform(Vector3{X, Y, Z}); + } + + Vector4 Quaternion::Transform(const Vector4 &vec) const { + return Vector4(Transform(vec.x, vec.y, vec.z), vec.w); + } + + Vector4 Quaternion::Transform(float X, float Y, float Z, float W) const { + return Transform(Vector4(X, Y, Z, W)); + } + + Quaternion Quaternion::Lerp(const Quaternion &b, float t) const { + float angle = this->Dot(b); + if (angle >= 0.f) // Make sure we rotate the shorter arc + return (*this * (1.f - t) + b * t).Normalize(); + else + return (*this * (t - 1.f) + b * t).Normalize(); + } + + void Quaternion::SetFromAxisAngle(const Vector3 &axis, float angle) { + float sinz, cosz; + + } + + Quaternion Quaternion::operator*(float scalar) const { + return Quaternion(x * scalar, y * scalar, z * scalar, w * scalar); + } + + Quaternion Quaternion::operator+() const { return *this; } + + Quaternion::Quaternion() {} } \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra/Transform2D.cpp b/src/J3ML/LinearAlgebra/Transform2D.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/src/J3ML/LinearAlgebra/Transform2D.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +// diff --git a/src/J3ML/LinearAlgebra/Transform3D.cpp b/src/J3ML/LinearAlgebra/Transform3D.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/src/J3ML/LinearAlgebra/Transform3D.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +// diff --git a/src/J3ML/LinearAlgebra/Vector2.cpp b/src/J3ML/LinearAlgebra/Vector2.cpp index 372f100..bd32981 100644 --- a/src/J3ML/LinearAlgebra/Vector2.cpp +++ b/src/J3ML/LinearAlgebra/Vector2.cpp @@ -1,11 +1,11 @@ #include +#include +#include +#include namespace LinearAlgebra { - -#pragma region vector2 - Vector2::Vector2(): x(0), y(0) {} @@ -158,6 +158,7 @@ const Vector2 Vector2::Up = {0, -1}; const Vector2 Vector2::Down = {0, 1}; const Vector2 Vector2::Left = {-1, 0}; const Vector2 Vector2::Right = {1, 0}; +const Vector2 Vector2::NaN = {NAN, NAN}; float Vector2::GetX() const { return x; } @@ -171,5 +172,5 @@ const Vector2 Vector2::Right = {1, 0}; float Vector2::Length(const Vector2 &of) { return of.Length(); } -#pragma endregion + } \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra/Vector3.cpp b/src/J3ML/LinearAlgebra/Vector3.cpp index d2b8c94..3373fa3 100644 --- a/src/J3ML/LinearAlgebra/Vector3.cpp +++ b/src/J3ML/LinearAlgebra/Vector3.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace LinearAlgebra { @@ -55,18 +56,11 @@ namespace LinearAlgebra { - Vector3::Vector3(): x(0), y(0), z(0) - {} + Vector3::Vector3(): x(0), y(0), z(0) {} - Vector3::Vector3(float X, float Y, float Z): x(X), y(Y), z(Z) - {} + Vector3::Vector3(float X, float Y, float Z): x(X), y(Y), z(Z) {} - Vector3::Vector3(const Vector3& rhs) - { - this->x = rhs.x; - this->y = rhs.y; - this->z = rhs.z; - } + Vector3::Vector3(const Vector3& rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {} Vector3& Vector3::operator=(const Vector3& rhs) { diff --git a/tests/LinearAlgebra/EulerAngleTests.cpp b/tests/LinearAlgebra/EulerAngleTests.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/tests/LinearAlgebra/EulerAngleTests.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +// diff --git a/tests/LinearAlgebra/Matrix2x2Tests.cpp b/tests/LinearAlgebra/Matrix2x2Tests.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/tests/LinearAlgebra/Matrix2x2Tests.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +// diff --git a/tests/LinearAlgebra/QuaternionTests.cpp b/tests/LinearAlgebra/QuaternionTests.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/tests/LinearAlgebra/QuaternionTests.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +// diff --git a/tests/LinearAlgebra/Vector2Tests.cpp b/tests/LinearAlgebra/Vector2Tests.cpp new file mode 100644 index 0000000..9cc6d5a --- /dev/null +++ b/tests/LinearAlgebra/Vector2Tests.cpp @@ -0,0 +1,28 @@ +#include +#include + + +TEST(Vector2Test, V2_Constructor_Default) { } +TEST(Vector2Test, V2_Addition) { } +TEST(Vector2Test, V2_Subtraction) { } +TEST(Vector2Test, V2_Multiplication) { } +TEST(Vector2Test, V2_Division) { } +TEST(Vector2Test, V2_Equality) { } +TEST(Vector2Test, V2_Array_Operator_Indexing) { } + + +TEST(Vector2Test, V2_Normalize) +{ + +} + +TEST(Vector2Test, V2_Dot) +{ + +} + +TEST(Vector2Test, V2_Min) { } +TEST(Vector2Test, V2_Max) { } +TEST(Vector2Test, V2_Distance) { } +TEST(Vector2Test, V2_Length) { } +TEST(Vector2Test, V2_Clamp) { } \ No newline at end of file diff --git a/tests/LinearAlgebra/Vector3Tests.cpp b/tests/LinearAlgebra/Vector3Tests.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/tests/LinearAlgebra/Vector3Tests.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +// diff --git a/tests/LinearAlgebra/Vector4Tests.cpp b/tests/LinearAlgebra/Vector4Tests.cpp new file mode 100644 index 0000000..a8cec1d --- /dev/null +++ b/tests/LinearAlgebra/Vector4Tests.cpp @@ -0,0 +1,3 @@ +// +// Created by josh on 12/26/2023. +//