1
0
forked from josh/j3ml

Implement J3ML Namespace

This commit is contained in:
2024-02-02 13:53:23 -05:00
parent d37b685df9
commit 9a5f12e505
45 changed files with 108 additions and 99 deletions

View File

@@ -3,9 +3,9 @@
#pragma once #pragma once
namespace Geometry { namespace J3ML::Geometry {
using Vector2 = LinearAlgebra::Vector2; using Vector2 = J3ML::LinearAlgebra::Vector2;
using Vector3 = LinearAlgebra::Vector3; using Vector3 = J3ML::LinearAlgebra::Vector3;
class LineSegment2D class LineSegment2D
{ {

View File

@@ -22,7 +22,7 @@
namespace Geometry namespace J3ML::Geometry
{ {
using namespace LinearAlgebra; using namespace LinearAlgebra;

View File

@@ -2,7 +2,7 @@
#include <J3ML/LinearAlgebra/Vector2.h> #include <J3ML/LinearAlgebra/Vector2.h>
namespace Geometry namespace J3ML::Geometry
{ {
using LinearAlgebra::Vector2; using LinearAlgebra::Vector2;
// CaveGame AABB // CaveGame AABB

View File

@@ -3,7 +3,7 @@
#include "LineSegment.h" #include "LineSegment.h"
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
namespace Geometry namespace J3ML::Geometry
{ {
using namespace LinearAlgebra; using namespace LinearAlgebra;
class Capsule class Capsule

View File

@@ -6,9 +6,11 @@
#include "Plane.h" #include "Plane.h"
#include <J3ML/LinearAlgebra/CoordinateFrame.h> #include <J3ML/LinearAlgebra/CoordinateFrame.h>
namespace Geometry namespace J3ML::Geometry
{ {
using J3ML::LinearAlgebra::CoordinateFrame;
enum class FrustumType enum class FrustumType
{ {
Invalid, Invalid,

View File

@@ -2,7 +2,7 @@
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
namespace Geometry namespace J3ML::Geometry
{ {
using LinearAlgebra::Vector3; using LinearAlgebra::Vector3;
class LineSegment class LineSegment

View File

@@ -5,7 +5,7 @@
#include <J3ML/Geometry/LineSegment.h> #include <J3ML/Geometry/LineSegment.h>
#include <J3ML/Geometry/Polyhedron.h> #include <J3ML/Geometry/Polyhedron.h>
namespace Geometry { namespace J3ML::Geometry {
class OBB class OBB
{ {
public: public:

View File

@@ -1,13 +1,18 @@
#pragma once #pragma once
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
using namespace LinearAlgebra;
class Plane
namespace J3ML::Geometry
{ {
public: using J3ML::LinearAlgebra::Vector3;
Vector3 Position;
Vector3 Normal;
float distance = 0.f;
}; class Plane
{
public:
Vector3 Position;
Vector3 Normal;
float distance = 0.f;
};
}

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
namespace Geometry { namespace J3ML::Geometry {
class Polygon { class Polygon {
}; };

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
namespace Geometry namespace J3ML::Geometry
{ {
class Polyhedron { class Polyhedron {

View File

@@ -5,7 +5,7 @@
#include <J3ML/LinearAlgebra/Vector2.h> #include <J3ML/LinearAlgebra/Vector2.h>
#include "AABB2D.h" #include "AABB2D.h"
namespace Geometry { namespace J3ML::Geometry {
using LinearAlgebra::Vector2; using LinearAlgebra::Vector2;

View File

@@ -6,7 +6,7 @@
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
namespace Geometry namespace J3ML::Geometry
{ {
using LinearAlgebra::Vector3; using LinearAlgebra::Vector3;
class Ray class Ray

View File

@@ -2,7 +2,7 @@
#include "J3ML/Geometry.h" #include "J3ML/Geometry.h"
namespace Geometry namespace J3ML::Geometry
{ {
class Sphere class Sphere
{ {

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
namespace Geometry namespace J3ML::Geometry
{ {
class Triangle class Triangle
{ {

View File

@@ -1,8 +1,10 @@
// #pragma once
// Created by dawsh on 1/25/24.
//
#ifndef J3ML_TRIANGLE2D_H
#define J3ML_TRIANGLE2D_H
#endif //J3ML_TRIANGLE2D_H namespace J3ML::Geometry
{
class Shape2D {};
class Triangle2D {
public:
};
}

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
namespace Geometry namespace J3ML::Geometry
{ {
class TriangleMesh class TriangleMesh
{ {

View File

@@ -6,7 +6,7 @@
#include <functional> #include <functional>
namespace Math namespace J3ML::Math
{ {
const float Pi = M_PI; const float Pi = M_PI;
inline float Radians(float degrees) { return degrees * (Pi/180.f); } inline float Radians(float degrees) { return degrees * (Pi/180.f); }
@@ -29,7 +29,7 @@ namespace Math
// Dawsh Linear Algebra Library - Everything you need for 3D math // Dawsh Linear Algebra Library - Everything you need for 3D math
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
class Vector2; // A type representing a position in a 2-dimensional coordinate space. class Vector2; // A type representing a position in a 2-dimensional coordinate space.
class Vector3; // A type representing a position in a 3-dimensional coordinate space. class Vector3; // A type representing a position in a 3-dimensional coordinate space.
class Vector4; // A type representing a position in a 4-dimensional coordinate space. class Vector4; // A type representing a position in a 4-dimensional coordinate space.

View File

@@ -2,7 +2,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
class Angle2D { class Angle2D {
public: public:
float x; float x;

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
namespace LinearAlgebra namespace J3ML::LinearAlgebra
{ {
/// Transitional datatype, not useful for internal representation of rotation /// Transitional datatype, not useful for internal representation of rotation

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
namespace LinearAlgebra namespace J3ML::LinearAlgebra
{ {
/// The CFrame is fundamentally 4 vectors (position, forward, right, up vector) /// The CFrame is fundamentally 4 vectors (position, forward, right, up vector)
class CoordinateFrame class CoordinateFrame

View File

@@ -2,7 +2,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
// Essential Reading: // Essential Reading:
// http://www.essentialmath.com/GDC2012/GDC2012_JMV_Rotations.pdf // http://www.essentialmath.com/GDC2012/GDC2012_JMV_Rotations.pdf

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h> #include <J3ML/LinearAlgebra/Vector2.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
class Matrix2x2 { class Matrix2x2 {
public: public:
enum { Rows = 3 }; enum { Rows = 3 };

View File

@@ -5,7 +5,7 @@
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/LinearAlgebra/Quaternion.h> #include <J3ML/LinearAlgebra/Quaternion.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
/// A 3-by-3 matrix for linear transformations of 3D geometry. /// A 3-by-3 matrix for linear transformations of 3D geometry.
/* This can represent any kind of linear transformations of 3D geometry, which include /* This can represent any kind of linear transformations of 3D geometry, which include
* rotation, scale, shear, mirroring, and orthographic projection. * rotation, scale, shear, mirroring, and orthographic projection.

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Quaternion.h> #include <J3ML/LinearAlgebra/Quaternion.h>
namespace LinearAlgebra { namespace J3ML::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.
/* This matrix can represent the most generic form of transformations for 3D objects, /* This matrix can represent the most generic form of transformations for 3D objects,

View File

@@ -6,7 +6,7 @@
#include <J3ML/LinearAlgebra/AxisAngle.h> #include <J3ML/LinearAlgebra/AxisAngle.h>
#include <J3ML/LinearAlgebra/Matrix3x3.h> #include <J3ML/LinearAlgebra/Matrix3x3.h>
namespace LinearAlgebra namespace J3ML::LinearAlgebra
{ {
class Quaternion : public Vector4 { class Quaternion : public Vector4 {
public: public:

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Matrix3x3.h> #include <J3ML/LinearAlgebra/Matrix3x3.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
class Transform2D { class Transform2D {
protected: protected:
Matrix3x3 transformation; Matrix3x3 transformation;

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
#include <cstddef> #include <cstddef>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
using namespace J3ML; using namespace J3ML;

View File

@@ -6,7 +6,7 @@
#include <J3ML/LinearAlgebra/Angle2D.h> #include <J3ML/LinearAlgebra/Angle2D.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
// A 3D (x, y, z) ordered pair. // A 3D (x, y, z) ordered pair.
class Vector3 { class Vector3 {

View File

@@ -3,7 +3,7 @@
#include <J3ML/LinearAlgebra.h> #include <J3ML/LinearAlgebra.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
class Vector4 { class Vector4 {
public: public:
// Default Constructor // Default Constructor

View File

@@ -1,6 +1,6 @@
#include <J3ML/Geometry/Capsule.h> #include <J3ML/Geometry/Capsule.h>
namespace Geometry namespace J3ML::Geometry
{ {
Capsule::Capsule() : l() {} Capsule::Capsule() : l() {}

View File

@@ -1,6 +1,6 @@
#include <J3ML/Geometry/Frustum.h> #include <J3ML/Geometry/Frustum.h>
namespace Geometry namespace J3ML::Geometry
{ {
Frustum Frustum::CreateFrustumFromCamera(const CoordinateFrame &cam, float aspect, float fovY, float zNear, float zFar) { Frustum Frustum::CreateFrustumFromCamera(const CoordinateFrame &cam, float aspect, float fovY, float zNear, float zFar) {
Frustum frustum; Frustum frustum;

View File

@@ -1,6 +1,6 @@
#include <J3ML/Geometry/LineSegment.h> #include <J3ML/Geometry/LineSegment.h>
namespace Geometry { namespace J3ML::Geometry {
LineSegment::LineSegment(const Vector3 &a, const Vector3 &b) : A(a), B(b) LineSegment::LineSegment(const Vector3 &a, const Vector3 &b) : A(a), B(b)
{ {

View File

@@ -1,6 +1,6 @@
#include <J3ML/LinearAlgebra/AxisAngle.h> #include <J3ML/LinearAlgebra/AxisAngle.h>
#include <J3ML/LinearAlgebra/Quaternion.h> #include <J3ML/LinearAlgebra/Quaternion.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
AxisAngle::AxisAngle() : axis(Vector3::Zero) {} AxisAngle::AxisAngle() : axis(Vector3::Zero) {}

View File

@@ -3,50 +3,49 @@
#include <algorithm> #include <algorithm>
#pragma region EulerAngle namespace J3ML::LinearAlgebra {
namespace LinearAlgebra { EulerAngle::EulerAngle(float pitch, float yaw, float roll): pitch(pitch), yaw(yaw), roll(roll)
EulerAngle::EulerAngle(float pitch, float yaw, float roll): pitch(pitch), yaw(yaw), roll(roll) {}
{}
float EulerAngle::GetPitch(float pitch_limit) const float EulerAngle::GetPitch(float pitch_limit) const
{ return std::clamp( std::remainderf(pitch,360.f), -pitch_limit, pitch_limit); } { return std::clamp( std::remainderf(pitch,360.f), -pitch_limit, pitch_limit); }
float EulerAngle::GetYaw(float yaw_limit) const float EulerAngle::GetYaw(float yaw_limit) const
{ return std::clamp(std::remainderf(yaw, 360.f), -yaw_limit, yaw_limit); } { return std::clamp(std::remainderf(yaw, 360.f), -yaw_limit, yaw_limit); }
float EulerAngle::GetRoll(float pitch_limit) const float EulerAngle::GetRoll(float pitch_limit) const
{ return std::clamp( std::remainderf(pitch,360.f), -pitch_limit, pitch_limit); } { return std::clamp( std::remainderf(pitch,360.f), -pitch_limit, pitch_limit); }
bool EulerAngle::operator==(const EulerAngle& a) const bool EulerAngle::operator==(const EulerAngle& a) const
{ {
return (pitch == a.pitch) && (yaw == a.yaw) && (roll == a.roll); return (pitch == a.pitch) && (yaw == a.yaw) && (roll == a.roll);
} }
void EulerAngle::clamp() void EulerAngle::clamp()
{ {
if (this->pitch > 89.0f) if (this->pitch > 89.0f)
this->pitch = 89.0f; this->pitch = 89.0f;
if (this->pitch <= -89.0f) if (this->pitch <= -89.0f)
this->pitch = -89.0f; this->pitch = -89.0f;
//TODO: Make this entirely seamless by getting the amount they rotated passed -180 and +180 by. //TODO: Make this entirely seamless by getting the amount they rotated passed -180 and +180 by.
if (this->yaw <= -180.0f) if (this->yaw <= -180.0f)
this->yaw = 180.0f; this->yaw = 180.0f;
if (this->yaw >= 180.01f) if (this->yaw >= 180.01f)
this->yaw = -179.9f; this->yaw = -179.9f;
if (this->roll >= 360.0f) if (this->roll >= 360.0f)
this->roll = 0.0; this->roll = 0.0;
if (this->roll <= -360.0f) if (this->roll <= -360.0f)
this->roll = 0.0; this->roll = 0.0;
} }
EulerAngle EulerAngle::movementAngle() const EulerAngle EulerAngle::movementAngle() const
{ {
EulerAngle a; EulerAngle a;
a.pitch = (cos(Math::Radians(yaw)) * cos(Math::Radians(pitch))); a.pitch = (cos(Math::Radians(yaw)) * cos(Math::Radians(pitch)));
a.yaw = -sin(Math::Radians(pitch)); a.yaw = -sin(Math::Radians(pitch));
a.roll = (sin(Math::Radians(yaw)) * cos(Math::Radians(pitch))); a.roll = (sin(Math::Radians(yaw)) * cos(Math::Radians(pitch)));
return a; return a;
} }
EulerAngle::EulerAngle() : pitch(0), yaw(0), roll(0) {} EulerAngle::EulerAngle() : pitch(0), yaw(0), roll(0) {}
} }

View File

@@ -1,6 +1,6 @@
#include <J3ML/LinearAlgebra/Matrix2x2.h> #include <J3ML/LinearAlgebra/Matrix2x2.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
Vector2 Matrix2x2::GetRow(int index) const { Vector2 Matrix2x2::GetRow(int index) const {
float x = this->elems[index][0]; float x = this->elems[index][0];

View File

@@ -1,7 +1,7 @@
#include <J3ML/LinearAlgebra/Matrix3x3.h> #include <J3ML/LinearAlgebra/Matrix3x3.h>
#include <cmath> #include <cmath>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
const Matrix3x3 Matrix3x3::Zero = Matrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0); const Matrix3x3 Matrix3x3::Zero = Matrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0);
const Matrix3x3 Matrix3x3::Identity = Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1); const Matrix3x3 Matrix3x3::Identity = Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);

View File

@@ -1,7 +1,7 @@
#include <J3ML/LinearAlgebra/Matrix4x4.h> #include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/LinearAlgebra/Vector4.h> #include <J3ML/LinearAlgebra/Vector4.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
const Matrix4x4 Matrix4x4::Zero = Matrix4x4(0); const Matrix4x4 Matrix4x4::Zero = Matrix4x4(0);
const Matrix4x4 Matrix4x4::Identity = Matrix4x4({1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}); const Matrix4x4 Matrix4x4::Identity = Matrix4x4({1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1});
const Matrix4x4 Matrix4x4::NaN = Matrix4x4(NAN); const Matrix4x4 Matrix4x4::NaN = Matrix4x4(NAN);

View File

@@ -4,7 +4,7 @@
#include <J3ML/LinearAlgebra/Matrix4x4.h> #include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/LinearAlgebra/Quaternion.h> #include <J3ML/LinearAlgebra/Quaternion.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
Quaternion Quaternion::operator-() const Quaternion Quaternion::operator-() const
{ {
return {-x, -y, -z, -w}; return {-x, -y, -z, -w};

View File

@@ -1,6 +1,6 @@
#include <J3ML/LinearAlgebra/Transform2D.h> #include <J3ML/LinearAlgebra/Transform2D.h>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
const Transform2D Transform2D::Identity = Transform2D({0, 0}, {1, 1}, {0,0}, {0,0}, 0); const Transform2D Transform2D::Identity = Transform2D({0, 0}, {1, 1}, {0,0}, {0,0}, 0);
const Transform2D Transform2D::FlipX = Transform2D({0, 0}, {-1, 1}, {0,0}, {0,0}, 0); const Transform2D Transform2D::FlipX = Transform2D({0, 0}, {-1, 1}, {0,0}, {0,0}, 0);

View File

@@ -4,7 +4,7 @@
#include <valarray> #include <valarray>
#include <iostream> #include <iostream>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
Vector2::Vector2(): x(0), y(0) Vector2::Vector2(): x(0), y(0)
{} {}

View File

@@ -3,9 +3,9 @@
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
#pragma region vector3
const Vector3 Vector3::Zero = {0,0,0}; const Vector3 Vector3::Zero = {0,0,0};
const Vector3 Vector3::Up = {0, -1, 0}; const Vector3 Vector3::Up = {0, -1, 0};
@@ -308,5 +308,5 @@ namespace LinearAlgebra {
return {x, y, z}; return {x, y, z};
} }
#pragma endregion
} }

View File

@@ -7,7 +7,7 @@
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
namespace LinearAlgebra { namespace J3ML::LinearAlgebra {
const Vector4 Vector4::Zero = {0,0,0,0}; const Vector4 Vector4::Zero = {0,0,0,0};
const Vector4 Vector4::NaN = {NAN, NAN, NAN, NAN}; const Vector4 Vector4::NaN = {NAN, NAN, NAN, NAN};

View File

@@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <J3ML/LinearAlgebra/Vector2.h> #include <J3ML/LinearAlgebra/Vector2.h>
using Vector2 = LinearAlgebra::Vector2; using J3ML::LinearAlgebra::Vector2;
TEST(Vector2Test, V2_Constructor_Default) TEST(Vector2Test, V2_Constructor_Default)
{ {

View File

@@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
using Vector3 = LinearAlgebra::Vector3; using J3ML::LinearAlgebra::Vector3;
void EXPECT_V3_EQ(const Vector3& lhs, const Vector3& rhs) void EXPECT_V3_EQ(const Vector3& lhs, const Vector3& rhs)
{ {
@@ -185,11 +185,12 @@ TEST(Vector3Test, V3_Lerp)
EXPECT_V3_EQ(Start.Lerp(Finish, Percent), ExpectedResult); EXPECT_V3_EQ(Start.Lerp(Finish, Percent), ExpectedResult);
} }
TEST(Vector3Test, V3_AngleBetween) { TEST(Vector3Test, V3_AngleBetween) {
using J3ML::LinearAlgebra::Angle2D;
Vector3 A{ .5f, .5f, .5f}; Vector3 A{ .5f, .5f, .5f};
Vector3 B {.25f, .75f, .25f}; Vector3 B {.25f, .75f, .25f};
A = A.Normalize(); A = A.Normalize();
B = B.Normalize(); B = B.Normalize();
LinearAlgebra::Angle2D ExpectedResult {-0.69791365, -2.3561945}; Angle2D ExpectedResult {-0.69791365, -2.3561945};
std::cout << A.AngleBetween(B).x << ", " << A.AngleBetween(B).y << ""; std::cout << A.AngleBetween(B).x << ", " << A.AngleBetween(B).y << "";
auto angle = A.AngleBetween(B); auto angle = A.AngleBetween(B);
EXPECT_FLOAT_EQ(angle.x, ExpectedResult.x); EXPECT_FLOAT_EQ(angle.x, ExpectedResult.x);

View File

@@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <J3ML/LinearAlgebra/Vector4.h> #include <J3ML/LinearAlgebra/Vector4.h>
using Vector4 = LinearAlgebra::Vector4; using Vector4 = J3ML::LinearAlgebra::Vector4;
void EXPECT_V4_EQ(const Vector4& lhs, const Vector4& rhs) void EXPECT_V4_EQ(const Vector4& lhs, const Vector4& rhs)