159 lines
5.4 KiB
C++
159 lines
5.4 KiB
C++
#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>
|
|
|
|
|
|
// TODO: Fix circular include between AABB and OBB
|
|
|
|
|
|
|
|
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
|
|
// computations can be avoided. In physics systems, bounding boxes are used as an efficient early-out test for geometry
|
|
// intersection queries.
|
|
// the 'Axis-aligned' part in the name means that the local axes of this bounding box are restricted to align with the
|
|
// axes of the world space coordinate system. This makes computation involving AABB's very fast, since AABB's cannot
|
|
// 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 {
|
|
public:
|
|
Vector3 minPoint;
|
|
Vector3 maxPoint;
|
|
|
|
static int NumFaces() { return 6; }
|
|
|
|
static int NumEdges() { return 12; }
|
|
|
|
static int NumVertices() { return 8; }
|
|
|
|
static AABB FromCenterAndSize(const Vector3 ¢er, const Vector3 &size);
|
|
|
|
float MinX() const;
|
|
|
|
float MinY() const;
|
|
|
|
float MinZ() const;
|
|
|
|
float MaxX() const;
|
|
|
|
float MaxY() const;
|
|
|
|
float MaxZ() const;
|
|
|
|
/// Returns the smallest sphere that contains this AABB.
|
|
/// This function computes the minimal volume sphere that contains all the points inside this AABB
|
|
Sphere MinimalEnclosingSphere() const;
|
|
|
|
Vector3 HalfSize() const;
|
|
|
|
/// Returns the largest sphere that can fit inside this AABB
|
|
/// This function computes the largest sphere that can fit inside this AABB.
|
|
Sphere MaximalContainedSphere() const;
|
|
|
|
bool IsFinite() const;
|
|
|
|
Vector3 Centroid() const;
|
|
|
|
Vector3 Size() const;
|
|
|
|
// Quickly returns an arbitrary point inside this AABB
|
|
Vector3 AnyPointFast() const;
|
|
|
|
Vector3 PointInside(float x, float y, float z) const;
|
|
|
|
// Returns an edge of this AABB
|
|
LineSegment Edge(int edgeIndex) const;
|
|
|
|
Vector3 CornerPoint(int cornerIndex) const;
|
|
|
|
Vector3 ExtremePoint(const Vector3 &direction) const;
|
|
|
|
Vector3 ExtremePoint(const Vector3 &direction, float &projectionDistance);
|
|
|
|
Vector3 PointOnEdge(int edgeIndex, float u) const;
|
|
|
|
Vector3 FaceCenterPoint(int faceIndex) const;
|
|
|
|
Vector3 FacePoint(int faceIndex, float u, float v) const;
|
|
|
|
Vector3 FaceNormal(int faceIndex) const;
|
|
|
|
Plane FacePlane(int faceIndex) const;
|
|
|
|
static AABB MinimalEnclosingAABB(const Vector3 *pointArray, int numPoints);
|
|
float GetVolume() const;
|
|
float GetSurfaceArea() const;
|
|
Vector3 GetRandomPointInside();
|
|
Vector3 GetRandomPointOnSurface();
|
|
Vector3 GetRandomPointOnEdge();
|
|
Vector3 GetRandomCornerPoint();
|
|
AABB Translated(const Vector3& offset) const;
|
|
AABB TransformAABB(const Matrix3x3& transform);
|
|
AABB TransformAABB(const Matrix4x4& transform);
|
|
AABB TransformAABB(const Quaternion& transform);
|
|
OBB Transform(const Matrix3x3& transform);
|
|
OBB Transform(const Matrix4x4& transform);
|
|
OBB Transform(const Quaternion& transform);
|
|
bool Contains(const Vector3& point) const;
|
|
bool Contains(const LineSegment& lineSegment) const;
|
|
bool Contains(const AABB& aabb) const;
|
|
bool Contains(const OBB& obb) const;
|
|
bool Contains(const Sphere& sphere) const;
|
|
bool Contains(const Triangle& triange) const;
|
|
bool Contains(const Polygon& polygon) const;
|
|
bool Contains(const Frustum& frustum) const;
|
|
bool Contains(const Polyhedron& polyhedron) const;
|
|
bool Contains(const Capsule& capsule) const;
|
|
// Tests whether this AABB and the given object intersect.
|
|
bool Intersects(const Ray& ray, float dNear, float dFar) const;
|
|
bool Intersects(const Capsule& capsule) const;
|
|
bool Intersects(const Triangle& triangle) const;
|
|
bool Intersects(const Polygon& polygon) const;
|
|
bool Intersects(const Frustum& frustum) const;
|
|
bool Intersects(const Polyhedron& polyhedron) const;
|
|
TriangleMesh Triangulate(int numFacesX, int numFacesY, int numFacesZ, bool ccwIsFrontFacing) const;
|
|
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 ¢er, const Vector3 &size);
|
|
|
|
void SetFrom(const OBB &obb);
|
|
|
|
void SetFrom(const Sphere &s);
|
|
|
|
Vector3 GetRandomPointInside() const;
|
|
|
|
void SetNegativeInfinity();
|
|
|
|
void Enclose(const Vector3 &point);
|
|
|
|
void Enclose(const Vector3 &aabbMinPt, const Vector3 &aabbMaxPt);
|
|
|
|
void Enclose(const LineSegment &lineSegment);
|
|
|
|
void Enclose(const OBB &obb);
|
|
};
|
|
}
|