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 <algorithm>
#include <functional>
// TODO: Make GLM redundant, and remove it thereafter.
// TODO: SIMD operations for fast math (parallelization!!)
namespace Math
{
const float Pi = M_PI;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,15 +1,16 @@
#pragma once
#include <J3ML/LinearAlgebra.h>
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;
};
}

View File

@@ -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();
};
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Matrix3x3.h>
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();
};
}

View File

@@ -1,7 +1,9 @@
#include <cstddef>
#pragma once
#include <J3ML/LinearAlgebra.h>
#include <cstddef>
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
};
}

View File

@@ -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
};
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/LinearAlgebra.h>
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
};