Implement by-Reference operators

This commit is contained in:
2024-02-14 18:12:02 -05:00
parent efead577a5
commit fd2e3f894a
10 changed files with 191 additions and 12 deletions

View File

@@ -59,7 +59,9 @@ add_library(J3ML SHARED ${J3ML_SRC}
include/J3ML/Geometry/Polyhedron.h
src/J3ML/Geometry/Polyhedron.cpp
include/J3ML/Algorithm/RNG.h
src/J3ML/Algorithm/RNG.cpp)
src/J3ML/Algorithm/RNG.cpp
include/J3ML/Algorithm/Spring.h
include/J3ML/Algorithm/DifferentialSolvers.h)
set_target_properties(J3ML PROPERTIES LINKER_LANGUAGE CXX)
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})

View File

@@ -0,0 +1,36 @@
//
// Created by dawsh on 2/8/24.
//
namespace J3ML::Algorithm
{
/// Implementations for a variety of Differential Equation Solving algorithms
namespace Solvers
{
// Consider a differential equation
// dy/dx = (x + y + xy)
float eq(float x, float y)
{
return (x + y + x*y);
}
float euler(float x0, float y, float h, float x)
{
float temp = -0.f;
// Iterating till the point at which we need approximation
while (x0 < x) {
temp = y;
y = y + h * eq(x0, y);
x0 = x0 + h;
}
return y;
}
class EulerMethodSolver {};
class SemiImplicitEulerMethodSolver {};
class GaussSeidelMethodSolver {};
class GradientDescentSolver {};
class VerletIntegrationSolver {};
}
}

View File

@@ -0,0 +1,21 @@
//
// Created by dawsh on 2/8/24.
//
namespace J3ML::Algorithm
{
// Numerical model of a "Spring" object
// Simulates any oscillating system i.e. a mass suspended from a spring.
class Spring
{
float Dampening;
float Stiffness;
float Goal;
float RestLength = 1.f;
bool Overdamped() const;
bool Undamped() const;
bool Underdamped() const;
bool CriticallyDamped() const;
};
}

View File

@@ -24,7 +24,6 @@
namespace J3ML::Geometry
{
using namespace LinearAlgebra;
// A 3D axis-aligned bounding box
// This data structure can be used to represent coarse bounds of objects, in situations where detailed triangle-level
@@ -135,6 +134,7 @@ namespace J3ML::Geometry
AABB Intersection(const AABB& rhs) const;
bool IntersectLineAABB(const Vector3& linePos, const Vector3& lineDir, float tNear, float tFar) const;
void SetFrom(const Vector3 *pVector3, int i);
void SetFromCenterAndSize(const Vector3 &center, const Vector3 &size);

View File

@@ -6,9 +6,6 @@
namespace J3ML::LinearAlgebra {
using namespace J3ML;
/// A 2D (x, y) ordered pair.
class Vector2 {
public:
@@ -16,6 +13,7 @@ namespace J3ML::LinearAlgebra {
Vector2();
/// Constructs a new Vector2 with the value (X, Y)
Vector2(float X, float Y);
Vector2(float* xyPtr);
Vector2(const Vector2& rhs); // Copy Constructor
//Vector2(Vector2&&) = default; // Move Constructor
@@ -31,6 +29,11 @@ namespace J3ML::LinearAlgebra {
void SetX(float newX);
void SetY(float newY);
float* ptr()
{
return &x;
}
Vector2 Abs() const;
bool IsWithinMarginOfError(const Vector2& rhs, float margin=0.001f) const;
@@ -39,7 +42,8 @@ namespace J3ML::LinearAlgebra {
bool IsZero(float epsilonSq = 1e-6f) const;
bool IsPerpendicular(const Vector2& other, float epsilonSq=1e-5f) const;
float operator[](std::size_t index);
float operator[](std::size_t index) const;
float &operator[](std::size_t index);
bool operator == (const Vector2& rhs) const;
bool operator != (const Vector2& rhs) const;
@@ -74,10 +78,6 @@ namespace J3ML::LinearAlgebra {
static float Magnitude(const Vector2& of);
bool IsFinite() const;
static bool IsFinite(const Vector2& v);

View File

@@ -32,6 +32,11 @@ public:
static const Vector3 Infinity;
static const Vector3 NegativeInfinity;
float* ptr()
{
return &x;
}
static void Orthonormalize(Vector3& a, Vector3& b)
{
a = a.Normalize();
@@ -79,6 +84,7 @@ public:
bool IsPerpendicular(const Vector3& other, float epsilonSq=1e-5f) const;
float operator[](std::size_t index) const;
float &operator[](std::size_t index);
bool operator == (const Vector3& rhs) const;
bool operator != (const Vector3& rhs) const;

View File

@@ -2,7 +2,6 @@
#include <J3ML/LinearAlgebra.h>
namespace J3ML::LinearAlgebra {
class Vector4 {
public:
@@ -16,6 +15,11 @@ namespace J3ML::LinearAlgebra {
Vector4(Vector4&& move) = default;
Vector4& operator=(const Vector4& rhs);
float* ptr()
{
return &x;
}
float GetX() const;
float GetY() const;
float GetZ() const;

View File

@@ -1,4 +1,5 @@
#include <J3ML/Geometry/AABB.h>
#include <cassert>
namespace J3ML::Geometry {
@@ -140,4 +141,97 @@ namespace J3ML::Geometry {
case 5: return {minPoint.x + u * d.x, minPoint.y + v * d.y, maxPoint.z};
}
}
Vector3 AABB::FaceNormal(int faceIndex) const {
// TODO: assert(0 <= faceIndex && faceIndex <= 5);
switch(faceIndex) {
default:
case 0: return {-1, 0, 0};
case 1: return { 1, 0, 0};
case 2: return { 0, -1, 0};
case 3: return { 0, 1, 0};
case 4: return { 0, 0, -1};
case 5: return { 0, 0, 1};
}
}
Plane AABB::FacePlane(int faceIndex) const {
// TODO: assert(0 <= faceIndex && faceIndex <= 5);
return Plane(FaceCenterPoint(faceIndex), FaceNormal(faceIndex));
}
AABB AABB::MinimalEnclosingAABB(const Vector3 *pointArray, int numPoints) {
AABB aabb;
aabb.SetFrom(pointArray, numPoints);
return aabb;
}
float AABB::GetVolume() const {
Vector3 sz = Size();
return sz.x * sz.y * sz.z;
}
float AABB::GetSurfaceArea() const {
Vector3 size = Size();
return 2.f * (size.x*size.y + size.x*size.z + size.y*size.z);
}
void AABB::SetFromCenterAndSize(const Vector3& center, const Vector3& size)
{
}
void AABB::SetFrom(const OBB& obb)
{
}
void AABB::SetFrom(const Sphere& s)
{
}
void AABB::SetFrom(const Vector3 *pointArray, int numPoints) {
assert(pointArray || numPoints == 0);
SetNegativeInfinity();
if (!pointArray)
return;
for (int i = 0; i < numPoints; ++i)
Enclose(pointArray[i]);
}
Vector3 AABB::GetRandomPointInside() const {
}
void AABB::SetNegativeInfinity() {
minPoint = Vector3::Infinity;
maxPoint = Vector3::NegativeInfinity;
}
void AABB::Enclose(const Vector3& point) {
minPoint = Vector3::Min(minPoint, point);
maxPoint = Vector3::Max(maxPoint, point);
}
void AABB::Enclose(const Vector3& aabbMinPt, const Vector3& aabbMaxPt)
{
minPoint = Vector3::Min(minPoint, aabbMinPt);
maxPoint = Vector3::Max(maxPoint, aabbMaxPt);
}
void AABB::Enclose(const LineSegment& lineSegment)
{
Enclose(Vector3::Min(lineSegment.A, lineSegment.B), Vector3::Max(lineSegment.A, lineSegment.B));
}
void AABB::Enclose(const OBB& obb)
{
Vector3 absAxis0 = obb.axis[0].Abs();
Vector3 absAxis1 = obb.axis[1].Abs();
Vector3 absAxis2 = obb.axis[2].Abs();
Vector3 d = obb.r.x * absAxis0 + obb.r.y * absAxis1 + obb.r.z * absAxis2;
}
}

View File

@@ -15,13 +15,21 @@ namespace J3ML::LinearAlgebra {
Vector2::Vector2(const Vector2& rhs): x(rhs.x), y(rhs.y)
{}
float Vector2::operator[](std::size_t index)
float Vector2::operator[](std::size_t index) const
{
assert(index < 2);
if (index == 0) return x;
if (index == 1) return y;
return 0;
}
float &Vector2::operator[](std::size_t index)
{
assert(index < 2);
if (index == 0) return x;
if (index == 1) return y;
}
bool Vector2::IsWithinMarginOfError(const Vector2& rhs, float margin) const
{
return this->Distance(rhs) <= margin;

View File

@@ -82,6 +82,14 @@ namespace J3ML::LinearAlgebra {
return 0;
}
float &Vector3::operator[](std::size_t index)
{
assert(index < 3);
if (index == 0) return x;
if (index == 1) return y;
if (index == 2) return z;
}
bool Vector3::IsWithinMarginOfError(const Vector3& rhs, float margin) const
{
return this->Distance(rhs) <= margin;