1
0
forked from josh/j3ml

Adding code x2

This commit is contained in:
2023-12-26 21:29:05 -06:00
parent d9bd070fd1
commit f90f1cf40b
25 changed files with 188 additions and 88 deletions

View File

@@ -4,11 +4,8 @@
#include <cstdlib> #include <cstdlib>
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
// TODO: Make GLM redundant, and remove it thereafter.
// TODO: SIMD operations for fast math (parallelization!!)
namespace Math namespace Math
{ {
const float Pi = M_PI; const float Pi = M_PI;

View File

@@ -1,8 +1,10 @@
// #pragma once
// Created by josh on 12/25/2023.
//
#ifndef J3ML_ANGLE2D_H #include <J3ML/LinearAlgebra.h>
#define J3ML_ANGLE2D_H
#endif //J3ML_ANGLE2D_H namespace LinearAlgebra {
class Angle2D {
public:
};
}

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector3.h>
namespace LinearAlgebra namespace LinearAlgebra
{ {

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
namespace LinearAlgebra namespace LinearAlgebra
{ {
/// The CFrame is fundamentally 4 vectors (position, forward, right, up vector) /// The CFrame is fundamentally 4 vectors (position, forward, right, up vector)

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h> #include <J3ML/LinearAlgebra/Vector2.h>
namespace LinearAlgebra { namespace LinearAlgebra {

View File

@@ -1,6 +1,9 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/LinearAlgebra/Quaternion.h>
namespace LinearAlgebra { namespace LinearAlgebra {
/// A 3-by-3 matrix for linear transformations of 3D geometry. /// A 3-by-3 matrix for linear transformations of 3D geometry.

View File

@@ -1,5 +1,8 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
namespace LinearAlgebra { namespace LinearAlgebra {
/// A 4-by-4 matrix for affine transformations and perspective projections of 3D geometry. /// A 4-by-4 matrix for affine transformations and perspective projections of 3D geometry.

View File

@@ -1,15 +1,16 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
namespace LinearAlgebra namespace LinearAlgebra
{ {
class Quaternion : public Vector4 class Quaternion
{ {
public: public:
Quaternion() {} Quaternion();
Quaternion(const Quaternion& rhs) = default; Quaternion(const Quaternion& rhs) = default;
explicit Quaternion(Matrix3x3& rotationMtrx) {} explicit Quaternion(const Matrix3x3& rotationMtrx);
explicit Quaternion(Matrix4x4& rotationMtrx) {} explicit Quaternion(const Matrix4x4& rotationMtrx);
// @note The input data is not normalized after construction, this has to be done manually. // @note The input data is not normalized after construction, this has to be done manually.
Quaternion(float x, float y, float z, float w); 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 // 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; Quaternion Inverse() const;
//void Normalize(); //void Normalize();
Quaternion Normalize() const; Quaternion Normalize() const;
Vector3 GetWorldX() const { return Transform(1.f, 0.f, 0.f); } Vector3 GetWorldX() const;
Vector3 GetWorldY() const { return Transform(0.f, 1.f, 0.f); } Vector3 GetWorldY() const;
Vector3 GetWorldZ() const { return Transform(0.f, 0.f, 1.f); } Vector3 GetWorldZ() const;
Matrix3x3 ToMatrix3x3() const; Matrix3x3 ToMatrix3x3() const;
Vector3 Transform(const Vector3& vec) const Vector3 Transform(const Vector3& vec) const;
{ Vector3 Transform(float X, float Y, float Z) const;
Matrix3x3 mat = this->ToMatrix3x3();
return mat * vec;
}
Vector3 Transform(float X, float Y, float Z) const
{
return Transform(Vector3{X, Y, Z});
}
// Note: We only transform the x,y,z components of 4D vectors, w is left untouched // Note: We only transform the x,y,z components of 4D vectors, w is left untouched
Vector4 Transform(const Vector4& vec) const Vector4 Transform(const Vector4& vec) const;
{ Vector4 Transform(float X, float Y, float Z, float W) 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));
}
Quaternion GetInverse() const; Quaternion GetInverse() const;
Quaternion Lerp(const Quaternion& b, float t) 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 Slerp(const Quaternion& target) const; Quaternion Slerp(const Quaternion& target) const;
void SetFromAxisAngle(const Vector3& axis, float angle) void SetFromAxisAngle(const Vector3& axis, float angle);
{
float sinz, cosz;
}
void SetFromAxisAngle(const Vector4& axis, float angle) void SetFromAxisAngle(const Vector4& axis, float angle)
{ {
@@ -75,10 +52,7 @@ namespace LinearAlgebra
// The rotation q2 is applied first before q1. // The rotation q2 is applied first before q1.
Quaternion operator * (const Quaternion& rhs) const; Quaternion operator * (const Quaternion& rhs) const;
Quaternion operator * (float scalar) const Quaternion operator * (float scalar) const;
{
return Quaternion(x * scalar, y * scalar, z * scalar, w * scalar);
}
// Transforms the given vector by this Quaternion. // Transforms the given vector by this Quaternion.
Vector3 operator * (const Vector3& rhs) const; 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 // 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& rhs) const; Quaternion operator +(const Quaternion& rhs) const;
Quaternion operator +() const { return *this; } Quaternion operator +() const;
Quaternion operator -() const; Quaternion operator -() const;
public:
float x = 0;
float y = 0;
float z = 0;
float w = 0;
}; };
} }

View File

@@ -1,13 +1,19 @@
#pragma once #pragma once
class Transform2D { #include <J3ML/LinearAlgebra.h>
protected: #include <J3ML/LinearAlgebra/Matrix3x3.h>
Matrix3x3 transformation;
public: namespace LinearAlgebra {
Transform2D Translate(const Vector2& offset) const; class Transform2D {
Transform2D Translate(float x, float y) const; protected:
Transform2D Scale(float scale); // Perform Uniform Scale Matrix3x3 transformation;
Transform2D Scale(float x, float y); // Perform Nonunform Scale public:
Transform2D Scale(const Vector2& scales); // Perform Nonuniform Scale Transform2D Translate(const Vector2& offset) const;
Transform2D Rotate(); 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();
};
}

View File

@@ -1,7 +1,9 @@
#include <cstddef>
#pragma once #pragma once
#include <J3ML/LinearAlgebra.h>
#include <cstddef>
namespace LinearAlgebra { namespace LinearAlgebra {
@@ -25,6 +27,7 @@ namespace LinearAlgebra {
static const Vector2 Left; static const Vector2 Left;
static const Vector2 Down; static const Vector2 Down;
static const Vector2 Right; static const Vector2 Right;
static const Vector2 NaN;
float operator[](std::size_t index); float operator[](std::size_t index);
@@ -126,8 +129,8 @@ namespace LinearAlgebra {
float x = 0; float x = 0;
float y = 0; float y = 0;
#else #else
const float x = 0; float x = 0;
const float y = 0; float y = 0;
#endif #endif
}; };
} }

View File

@@ -132,9 +132,9 @@ public:
float y = 0; float y = 0;
float z = 0; float z = 0;
#else #else
const float x = 0; float x = 0;
const float y = 0; float y = 0;
const float z = 0; float z = 0;
#endif #endif
}; };
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra.h>
namespace LinearAlgebra { namespace LinearAlgebra {
class Vector4 { class Vector4 {
@@ -13,9 +14,9 @@ namespace LinearAlgebra {
Vector4(float X, float Y, float Z, float W); Vector4(float X, float Y, float Z, float W);
Vector4(const Vector4& copy) = default; Vector4(const Vector4& copy) = default;
Vector4(Vector4&& move) = default; Vector4(Vector4&& move) = default;
Vector4& operator=(const Vector4& rhs); Vector4& operator=(const Vector4& rhs);
float GetX() const; float GetX() const;
float GetY() const; float GetY() const;
float GetZ() const; float GetZ() const;
@@ -89,10 +90,10 @@ namespace LinearAlgebra {
float z = 0; float z = 0;
float w = 0; float w = 0;
#else #else
const float x = 0; float x = 0;
const float y = 0; float y = 0;
const float z = 0; float z = 0;
const float w = 0; float w = 0;
#endif #endif
}; };

View File

@@ -0,0 +1 @@
#include <J3ML/LinearAlgebra/CoordinateFrame.h>

View File

@@ -1,4 +1,5 @@
#include <J3ML/LinearAlgebra/Matrix3x3.h> #include <J3ML/LinearAlgebra/Matrix3x3.h>
#include <cmath>
namespace LinearAlgebra { namespace LinearAlgebra {
@@ -95,5 +96,9 @@ namespace LinearAlgebra {
this->elems[2][2] = r3.z; this->elems[2][2] = r3.z;
} }
Matrix3x3::Matrix3x3(const Quaternion &orientation) {
}
} }

View File

@@ -1,8 +1,59 @@
#include <J3ML/LinearAlgebra/Quaternion.h> #include <J3ML/LinearAlgebra/Quaternion.h>
#include <J3ML/LinearAlgebra/Vector3.h>
namespace LinearAlgebra { namespace LinearAlgebra {
Quaternion Quaternion::operator-() const Quaternion Quaternion::operator-() const
{ {
return {-x, -y, -z, -w}; 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() {}
} }

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//

View File

@@ -1,11 +1,11 @@
#include <J3ML/LinearAlgebra/Vector2.h> #include <J3ML/LinearAlgebra/Vector2.h>
#include <cassert>
#include <algorithm>
#include <valarray>
namespace LinearAlgebra { namespace LinearAlgebra {
#pragma region vector2
Vector2::Vector2(): x(0), y(0) 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::Down = {0, 1};
const Vector2 Vector2::Left = {-1, 0}; const Vector2 Vector2::Left = {-1, 0};
const Vector2 Vector2::Right = {1, 0}; const Vector2 Vector2::Right = {1, 0};
const Vector2 Vector2::NaN = {NAN, NAN};
float Vector2::GetX() const { return x; } 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(); } float Vector2::Length(const Vector2 &of) { return of.Length(); }
#pragma endregion
} }

View File

@@ -1,6 +1,7 @@
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cmath>
namespace LinearAlgebra { 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) Vector3::Vector3(const Vector3& rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {}
{
this->x = rhs.x;
this->y = rhs.y;
this->z = rhs.z;
}
Vector3& Vector3::operator=(const Vector3& rhs) Vector3& Vector3::operator=(const Vector3& rhs)
{ {

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//

View File

@@ -0,0 +1,28 @@
#include <gtest/gtest.h>
#include <J3ML/LinearAlgebra/Vector2.h>
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) { }

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 12/26/2023.
//