113 lines
4.0 KiB
C++
113 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#include <J3ML/Geometry/Common.h>
|
|
#include <J3ML/Geometry/Shape.h>
|
|
#include <vector>
|
|
#include <J3ML/LinearAlgebra/Vector3.h>
|
|
|
|
|
|
namespace J3ML::Geometry
|
|
{
|
|
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;
|
|
std::vector<Face> f;
|
|
|
|
int NumVertices() const {return (int)v.size();}
|
|
int NumFaces() const { return (int)f.size();}
|
|
AABB MinimalEnclosingAABB() const;
|
|
|
|
Vector3 Vertex(int vertexIndex) const;
|
|
|
|
|
|
|
|
bool Contains(const Vector3&) const;
|
|
bool Contains(const LineSegment&) const;
|
|
bool Contains(const Triangle&) const;
|
|
bool Contains(const Polygon&) const;
|
|
bool Contains(const AABB&) const;
|
|
bool Contains(const OBB&) const;
|
|
bool Contains(const Frustum&) const;
|
|
bool Contains(const Polyhedron&) const;
|
|
|
|
bool ContainsConvex(const Vector3&, float epsilon = 1e-4f) const;
|
|
bool ContainsConvex(const LineSegment&) const;
|
|
bool ContainsConvex(const Triangle&) const;
|
|
|
|
bool Intersects(const Line&) const;
|
|
bool Intersects(const LineSegment&) const;
|
|
bool Intersects(const Ray&) const;
|
|
bool Intersects(const Plane&) const;
|
|
bool Intersects(const Polyhedron&) const;
|
|
bool Intersects(const AABB&) const;
|
|
bool Intersects(const OBB&) const;
|
|
bool Intersects(const Triangle&) const;
|
|
bool Intersects(const Polygon&) const;
|
|
bool Intersects(const Frustum&) const;
|
|
bool Intersects(const Sphere&) const;
|
|
bool Intersects(const Capsule&) const;
|
|
|
|
float FaceContainmentDistance2D(int i, Vector3 vector3) const;
|
|
|
|
bool IsClosed() const;
|
|
|
|
Plane FacePlane(int faceIndex) const;
|
|
|
|
std::vector<Polygon> Faces() const;
|
|
|
|
int NumEdges() const;
|
|
|
|
LineSegment Edge(int edgeIndex) const;
|
|
|
|
std::vector<std::pair<int, int>> EdgeIndices() const;
|
|
|
|
std::vector<LineSegment> Edges() const;
|
|
|
|
Polygon FacePolygon(int faceIndex) const;
|
|
|
|
Vector3 FaceNormal(int faceIndex) const;
|
|
|
|
bool IsConvex() const;
|
|
|
|
Vector3 ApproximateConvexCentroid() const;
|
|
|
|
int ExtremeVertex(const Vector3 &direction) const;
|
|
|
|
Vector3 ExtremePoint(const Vector3 &direction) const;
|
|
|
|
/// Tests if the given face of this Polyhedron contains the given point.
|
|
bool FaceContains(int faceIndex, const Vector3 &worldSpacePoint, float polygonThickness = 1e-3f) const;
|
|
|
|
/// A helper for Contains() and FaceContains() tests: Returns a positive value if the given point is contained in the given face,
|
|
/// and a negative value if the given point is outside the face. The magnitude of the return value reports a pseudo-distance
|
|
/// from the point to the nearest edge of the face polygon. This is used as a robustness/stability criterion to estimate how
|
|
/// numerically believable the result is.
|
|
float FaceContainmentDistance2D(int faceIndex, const Vector3 &worldSpacePoint, float polygonThickness) const;
|
|
void ProjectToAxis(const Vector3 &direction, float &outMin, float &outMax) const;
|
|
protected:
|
|
private:
|
|
|
|
|
|
};
|
|
} |