Implement Triangle::NumFaces NumEdges NumVertices CenterPoint Intersects(LineSegment)

This commit is contained in:
2024-07-10 14:13:51 -04:00
parent 2195752e1e
commit a8dd46efc3
2 changed files with 38 additions and 1 deletions

View File

@@ -14,6 +14,13 @@ namespace J3ML::Geometry
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
@@ -28,6 +35,10 @@ namespace J3ML::Geometry
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.
@@ -37,6 +48,9 @@ namespace J3ML::Geometry
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;

View File

@@ -1,12 +1,29 @@
#include <J3ML/Geometry/Triangle.h>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/Geometry/AABB.h>
#include <J3ML/Geometry/AABB.hpp>
#include <J3ML/Geometry/LineSegment.h>
#include <J3ML/Geometry/Line.h>
#include <J3ML/Geometry/Capsule.h>
namespace J3ML::Geometry
{
bool Triangle::Intersects(const LineSegment &l, float *d, Vector3 *intersectionPoint) const {
/** The Triangle-Line/LineSegment/Ray intersection tests are based on M&ouml;ller-Trumbore method:
"T. M&ouml;ller, B. Trumbore. Fast, Minimum Storage Ray/Triangle Intersection. 2005."
http://jgt.akpeters.com/papers/MollerTrumbore97/. */
float u,v;
float t = IntersectLineTri(l.A, l.B - l.A, V0, V1, V2, u, v);
bool success = (t >= 0.f && t <= 1.f);
if (!success)
return false;
if (d)
*d = t;
if (intersectionPoint)
*intersectionPoint = l.GetPoint(t);
return true;
}
Interval Triangle::ProjectionInterval(const Vector3& axis) const {
// https://gdbooks.gitbooks.io/3dcollisions/content/Chapter4/generic_sat.html
float min = axis.Dot(V0);
@@ -105,6 +122,12 @@ namespace J3ML::Geometry
return aabb;
}
Vector3 Triangle::Centroid() const {
return (V0 + V1 + V2) * (1.f/3.f);
}
Vector3 Triangle::CenterPoint() const { return Centroid();}
bool Triangle::Intersects(const AABB &aabb) const {
return aabb.Intersects(*this);
}