Massive Refactor

This commit is contained in:
2024-03-15 15:31:14 -04:00
parent e8ed68f3c7
commit 9f60f296c6
21 changed files with 379 additions and 127 deletions

View File

@@ -1,30 +1,14 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector3.h>
#include "J3ML/LinearAlgebra.h"
#include <J3ML/Geometry.h>
#include <J3ML/Geometry/Plane.h>
#include <J3ML/Geometry/Sphere.h>
#include <J3ML/Geometry/OBB.h>
#include <J3ML/Geometry/LineSegment.h>
#include <J3ML/Geometry/Triangle.h>
#include <J3ML/Geometry/Polygon.h>
#include <J3ML/Geometry/Frustum.h>
#include <J3ML/Geometry/Capsule.h>
#include <J3ML/Geometry/Ray.h>
#include <J3ML/Geometry/TriangleMesh.h>
#include <J3ML/Geometry/Polyhedron.h>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/Geometry/Common.h>
#include <J3ML/Geometry/Shape.h>
// TODO: Fix circular include between AABB and OBB
namespace J3ML::Geometry
{
using namespace LinearAlgebra;
using namespace J3ML::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
// computations can be avoided. In physics systems, bounding boxes are used as an efficient early-out test for geometry
@@ -34,11 +18,15 @@ namespace J3ML::Geometry
// be arbitrarily oriented in the space with respect to each other.
// If you need to represent a box in 3D space with arbitrary orientation, see the class OBB. */
class AABB {
class AABB : public Shape {
public:
Vector3 minPoint;
Vector3 maxPoint;
AABB();
AABB(const Vector3& min, const Vector3& max);
static int NumFaces() { return 6; }
static int NumEdges() { return 12; }
@@ -102,6 +90,7 @@ namespace J3ML::Geometry
static AABB MinimalEnclosingAABB(const Vector3 *pointArray, int numPoints);
float GetVolume() const;
float GetSurfaceArea() const;
Vector3 GetClosestPoint(const Vector3& point) const;
Vector3 GetRandomPointInside();
Vector3 GetRandomPointOnSurface();
Vector3 GetRandomPointOnEdge();

View File

@@ -1,12 +1,13 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.h>
#include "Shape.h"
namespace J3ML::Geometry
{
using LinearAlgebra::Vector2;
// CaveGame AABB
class AABB2D
class AABB2D : public Shape2D
{
public:

View File

@@ -1,12 +1,13 @@
#pragma once
#include "LineSegment.h"
#include "Shape.h"
#include <J3ML/LinearAlgebra/Vector3.h>
namespace J3ML::Geometry
{
using namespace LinearAlgebra;
class Capsule
class Capsule : public Shape
{
// Specifies the two inner points of this capsule
LineSegment l;

View File

@@ -0,0 +1,30 @@
#pragma once
// Forward declarations for classes that include each other
namespace J3ML::Geometry
{
class Shape;
class AABB2D;
class AABB;
class Capsule;
class Frustum;
class LineSegment;
class OBB;
class Plane;
class Polygon;
class Polyhedron;
template<typename T>class QuadTree;
class Ray;
class Shape;
class Sphere;
class Triangle;
class Triangle2D;
class TriangleMesh;
}
// Methods required by Geometry types
namespace J3ML::Geometry
{
}

View File

@@ -4,6 +4,7 @@
#pragma once
#include "Plane.h"
#include "Shape.h"
#include <J3ML/LinearAlgebra/CoordinateFrame.h>
namespace J3ML::Geometry
@@ -27,7 +28,7 @@ namespace J3ML::Geometry
Perspective
};
class Frustum {
class Frustum : public Shape {
public:
Plane TopFace;
Plane BottomFace;

View File

@@ -1,31 +1,36 @@
#pragma once
#include <J3ML/Geometry.h>
#include <J3ML/Geometry/Common.h>
#include <J3ML/Geometry/AABB.h>
#include <J3ML/Geometry/LineSegment.h>
#include <J3ML/Geometry/Polyhedron.h>
namespace J3ML::Geometry {
class OBB
class OBB : public Shape
{
public:
// The center position of this OBB
Vector3 pos;
// Stores half-sizes to x, y, and z directions in the local space of this OBB.
Vector3 r;
// Specifies normalized direc tion vectors for the local axes
// Specifies normalized direction vectors for the local axes
Vector3 axis[3];
OBB() {}
OBB(const Vector3& pos, const Vector3& radii, const Vector3& axis0, const Vector3& axis1, const Vector3& axis2);
OBB(const Geometry::AABB& aabb);
OBB(const AABB& aabb);
inline static int NumFaces() { return 6; }
inline static int NumEdges() { return 12; }
inline static int NumVertices() { return 8; }
Polyhedron ToPolyhedron() const;
Geometry::AABB MinimalEnclosingAABB() const;
AABB MinimalEnclosingAABB() const
{
AABB aabb;
aabb.SetFrom(*this);
return aabb;
}
Sphere MinimalEnclosingSphere() const;
Sphere MaximalContainedSphere() const;

View File

@@ -1,15 +1,18 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector3.h>
#include "Shape.h"
namespace J3ML::Geometry
{
using J3ML::LinearAlgebra::Vector3;
class Plane
class Plane : public Shape
{
public:
Plane() : Shape() {}
Plane(const Vector3& pos, const Vector3& norm)
: Shape(), Position(pos), Normal(norm) {}
Vector3 Position;
Vector3 Normal;
float distance = 0.f;

View File

@@ -1,7 +1,12 @@
#pragma once
namespace J3ML::Geometry {
class Polygon {
#include "Shape.h"
namespace J3ML::Geometry {
class Polygon : public Shape
{
public:
protected:
private:
};
}

View File

@@ -1,8 +1,37 @@
#pragma once
#include <J3ML/Geometry/Shape.h>
#include <vector>
#include <J3ML/LinearAlgebra/Vector3.h>
namespace J3ML::Geometry
{
class Polyhedron {
using namespace J3ML::LinearAlgebra;
// Represents a three-dimensional closed geometric solid defined by flat polygonal faces.
class Polyhedron : public Shape
{
public:
// Stores a list of indices of a single face of a Polygon
struct Face
{
// Specifies the indices of the corner vertices of the polyhedron.
// Indices point to the polyhedron vertex array.
// The face vertices should all lie on the same plane.
// The positive direction of the plane (the direction the face outwards normal points)
// is the one where the vertices are wound in counter-clockwise order.
std::vector<int> v;
// Reverses the winding order of this face. This has the effect of reversing the direction
// the normal of this face points to.
void FlipWindingOrder();
};
// Specifies the vertices of this polyhedron.
std::vector<Vector3> v;
protected:
private:
};
}

View File

@@ -12,11 +12,9 @@
namespace J3ML::Geometry
{
using LinearAlgebra::Vector3;
using J3ML::LinearAlgebra::Vector3;
class Shape {};
// RaycastResult structure containing the first object the ray collides with,
// the surface intersection point,
// and the surface normal at the point of intersection.
@@ -24,13 +22,16 @@ namespace J3ML::Geometry
{
Vector3 Intersection;
Vector3 SurfaceNormal;
Shape Hit;
bool Hit;
Shape* Target;
static RaycastResult NoHit() { return {Vector3::NaN, Vector3::NaN, false, nullptr};}
};
// A ray in 3D space is a line that starts from an origin point and extends to infinity in one direction
class Ray
{
public:
// The position of this ray.
Vector3 Origin;
// The normalized direction vector of this ray.
// @note: For proper functionality, this direction vector needs to always be normalized
@@ -45,19 +46,7 @@ namespace J3ML::Geometry
RaycastResult Cast(const Plane& target, float maxDistance = 99999999);
RaycastResult Cast(const AABB& target, float maxDistance = 99999999);
// https://gdbooks.gitbooks.io/3dcollisions/content/Chapter3/raycast_sphere.html
RaycastResult Cast(const Sphere& target, float maxDistance = 99999999)
{
Vector3 p0 = this->Origin;
Vector3 d = this->Direction.Normalize();
Vector3 c = target.Position;
float r = target.Radius;
Vector3 e = c - p0;
float Esq = Vector3::LengthSquared(e);
float a = Vector3::Dot(e, d);
}
RaycastResult Cast(const Sphere& target, float maxDistance = 99999999);
RaycastResult Cast(const OBB& target, float maxDistance = 99999999);
RaycastResult Cast(const Capsule& target, float maxDistance = 99999999);
RaycastResult Cast(const Frustum& target, float maxDistance = 99999999);

View File

@@ -0,0 +1,26 @@
#pragma once
namespace J3ML::Geometry
{
class GeometricPrimitive
{
public:
protected:
private:
};
class Shape
{
public:
protected:
private:
};
class Shape2D
{
public:
protected:
private:
};
}

View File

@@ -5,6 +5,7 @@
#include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/Geometry/LineSegment.h>
#include <J3ML/Geometry/TriangleMesh.h>
#include <J3ML/Geometry/Shape.h>
namespace J3ML::Geometry
{
@@ -12,7 +13,7 @@ namespace J3ML::Geometry
using J3ML::LinearAlgebra::Matrix4x4;
// A mathematical representation of a 3-dimensional sphere
class Sphere
class Sphere : public Shape
{
public:
Vector3 Position;

View File

@@ -4,6 +4,9 @@ namespace J3ML::Geometry
{
class Triangle
{
public:
Vector3 V0;
Vector3 V1;
Vector3 V2;
};
}

View File

@@ -3,7 +3,6 @@
namespace J3ML::Geometry
{
class Shape2D {};
class Triangle2D {
public:
};