143 lines
7.5 KiB
C++
143 lines
7.5 KiB
C++
#pragma once
|
|
|
|
#include "J3ML/LinearAlgebra/Vector3.hpp"
|
|
#include <J3ML/Geometry/Common.hpp>
|
|
#include <J3ML/LinearAlgebra.hpp>
|
|
#include <cfloat>
|
|
|
|
namespace J3ML::Geometry
|
|
{
|
|
class Triangle
|
|
{
|
|
public:
|
|
Vector3 V0;
|
|
Vector3 V1;
|
|
Vector3 V2;
|
|
public:
|
|
|
|
public:
|
|
static int NumFaces() { return 1; }
|
|
static int NumEdges() { return 3; }
|
|
static int NumVertices() { return 3; }
|
|
public:
|
|
|
|
float DistanceSq(const Vector3 &point) const;
|
|
|
|
/// Returns a new triangle, translated with a direction vector
|
|
Triangle Translated(const Vector3& translation) const;
|
|
/// Returns a new triangle, scaled from 3D factors
|
|
Triangle Scaled(const Vector3& scaled) const;
|
|
|
|
bool Intersects(const AABB& aabb) const;
|
|
bool Intersects(const Capsule& capsule) const;
|
|
bool Intersects(const Triangle& rhs) const;
|
|
friend bool Intersects(const Triangle& lhs, const Triangle &rhs);
|
|
|
|
AABB BoundingAABB() const;
|
|
|
|
Vector3 Centroid() const;
|
|
|
|
Vector3 CenterPoint() const;
|
|
|
|
/// Tests if the given object is fully contained inside this triangle.
|
|
/** @param triangleThickness An epsilon threshold value to use for this test. triangleThicknessSq is the squared version of this parameter.
|
|
This specifies the maximum distance the given object can lie from the plane defined by this triangle.
|
|
@see Distance(), Intersects(), ClosestPoint().
|
|
@todo Add Triangle::Contains(Circle) and Triangle::Contains(Disc). */
|
|
bool Contains(const Vector3& point, float triangleThicknessSq = 1e-5f) const;
|
|
bool Contains(const LineSegment& lineSeg, float triangleThickness = 1e-3f) const;
|
|
bool Contains(const Triangle& triangle, float triangleThickness = 1e-3f) const;
|
|
|
|
|
|
bool Intersects(const LineSegment& lineSegment, float* d = 0, Vector3* intersectionPoint = 0) const;
|
|
|
|
/// Project the triangle onto an axis, and returns the min and max value with the axis as a unit
|
|
Interval ProjectionInterval(const Vector3& axis) const;
|
|
void ProjectToAxis(const Vector3 &axis, float &dMin, float &dMax) const;
|
|
|
|
/// Quickly returns an arbitrary point inside this Triangle. Used in GJK intersection test.
|
|
inline Vector3 AnyPointFast() const { return V0; }
|
|
|
|
/// Computes an extreme point of this Triangle in the given direction.
|
|
/** An extreme point is a farthest point of this Triangle in the given direction. Given a direction,
|
|
this point is not necessarily unique.
|
|
@param direction The direction vector of the direction to find the extreme point. This vector may
|
|
be unnormalized, but may not be null.
|
|
@return An extreme point of this Triangle in the given direction. The returned point is always a
|
|
vertex of this Triangle.
|
|
@see Vertex(). */
|
|
Vector3 ExtremePoint(const Vector3 &direction) const;
|
|
Vector3 ExtremePoint(const Vector3 &direction, float &projectionDistance) const;
|
|
|
|
|
|
static float IntersectLineTri(const Vector3 &linePos, const Vector3 &lineDir, const Vector3 &v0, const Vector3 &v1,
|
|
const Vector3 &v2, float &u, float &v);
|
|
|
|
/// Computes the closest point on the edge of this triangle to the given object.
|
|
/** @param outU [out] If specified, receives the barycentric U coordinate of the returned point (in the UV convention).
|
|
This pointer may be null.
|
|
@param outV [out] If specified, receives the barycentric V coordinate of the returned point (in the UV convention).
|
|
This pointer may be null.
|
|
@param outD [out] If specified, receives the distance along the line of the closest point on the line to the edge of this triangle.
|
|
@return The closest point on the edge of this triangle to the given object.
|
|
@todo Add ClosestPointToTriangleEdge(Point/Ray/Triangle/Plane/Polygon/Circle/Disk/AABB/OBB/Sphere/Capsule/Frustum/Polyhedron).
|
|
@see Distance(), Contains(), Intersects(), ClosestPointToTriangleEdge(), Line::GetPoint. */
|
|
Vector3 ClosestPointToTriangleEdge(const Line &line, float *outU, float *outV, float *outD) const;
|
|
Vector3 ClosestPointToTriangleEdge(const LineSegment &lineSegment, float *outU, float *outV, float *outD) const;
|
|
|
|
Vector3 ClosestPoint(const LineSegment &lineSegment, Vector3 *otherPt = 0) const;
|
|
/// Returns the point at the given barycentric coordinates.
|
|
/** This function computes the vector space point at the given barycentric coordinates.
|
|
@param uvw The barycentric UVW coordinate triplet. The condition u+v+w == 1 should hold for the input coordinate.
|
|
If 0 <= u,v,w <= 1, the returned point lies inside this triangle.
|
|
@return u*a + v*b + w*c. */
|
|
Vector3 Point(const Vector3 &uvw) const;
|
|
Vector3 Point(float u, float v, float w) const;
|
|
/** These functions are an alternate form of Point(u,v,w) for the case when the barycentric coordinates are
|
|
represented as a (u,v) pair and not as a (u,v,w) triplet. This function is provided for convenience
|
|
and effectively just computes Point(1-u-v, u, v).
|
|
@param uv The barycentric UV coordinates. If 0 <= u,v <= 1 and u+v <= 1, then the returned point lies inside
|
|
this triangle.
|
|
@return a + (b-a)*u + (c-a)*v.
|
|
@see BarycentricUV(), BarycentricUVW(), BarycentricInsideTriangle(). */
|
|
Vector3 Point(const Vector2 &uv) const;
|
|
Vector3 Point(float u, float v) const;
|
|
|
|
/// Expresses the given point in barycentric (u,v,w) coordinates.
|
|
/** @note There are two different conventions for representing barycentric coordinates. One uses
|
|
a (u,v,w) triplet with the equation pt == u*a + v*b + w*c, and the other uses a (u,v) pair
|
|
with the equation pt == a + u*(b-a) + v*(c-a). These two are equivalent. Use the mappings
|
|
(u,v) -> (1-u-v, u, v) and (u,v,w)->(v,w) to convert between these two representations.
|
|
@param point The point of the vector space to express in barycentric coordinates. This point should
|
|
lie in the plane formed by this triangle.
|
|
@return The factors (u,v,w) that satisfy the weighted sum equation point == u*a + v*b + w*c.
|
|
@see BarycentricUV(), BarycentricInsideTriangle(), Point(), http://mathworld.wolfram.com/BarycentricCoordinates.html */
|
|
Vector3 BarycentricUVW(const Vector3 &point) const;
|
|
|
|
/// Expresses the given point in barycentric (u,v) coordinates.
|
|
/** @note There are two different conventions for representing barycentric coordinates. One uses
|
|
a (u,v,w) triplet with the equation pt == u*a + v*b + w*c, and the other uses a (u,v) pair
|
|
with the equation pt == a + u*(b-a) + v*(c-a). These two are equivalent. Use the mappings
|
|
(u,v) -> (1-u-v, u, v) and (u,v,w)->(v,w) to convert between these two representations.
|
|
@param point The point to express in barycentric coordinates. This point should lie in the plane
|
|
formed by this triangle.
|
|
@return The factors (u,v) that satisfy the weighted sum equation point == a + u*(b-a) + v*(c-a).
|
|
@see BarycentricUVW(), BarycentricInsideTriangle(), Point(). */
|
|
Vector2 BarycentricUV(const Vector3 &point) const;
|
|
|
|
|
|
Vector3 ClosestPoint(const Vector3 &p) const;
|
|
|
|
Plane PlaneCCW() const;
|
|
|
|
Plane PlaneCW() const;
|
|
|
|
Vector3 FaceNormal() const;
|
|
|
|
Vector3 Vertex(int i) const;
|
|
|
|
LineSegment Edge(int i) const;
|
|
};
|
|
|
|
|
|
} |