17#ifndef IGNITION_MATH_TRIANGLE3_HH_
18#define IGNITION_MATH_TRIANGLE3_HH_
24#include <ignition/math/config.hh>
30 inline namespace IGNITION_MATH_VERSION_NAMESPACE
52 this->
Set(_pt1, _pt2, _pt3);
66 this->pts[
clamp(_index, 0u, 2u)] = _pt;
93 T a = this->
Side(0).Length();
94 T b = this->
Side(1).Length();
95 T c = this->
Side(2).Length();
96 return (a+b) > c && (b+c) > a && (c+a) > b;
109 return Line3<T>(this->pts[0], this->pts[1]);
110 else if (_index == 1)
111 return Line3<T>(this->pts[1], this->pts[2]);
113 return Line3<T>(this->pts[2], this->pts[0]);
134 Vector3d v0 = this->pts[2] - this->pts[0];
135 Vector3d v1 = this->pts[1] - this->pts[0];
138 double dot00 = v0.
Dot(v0);
139 double dot01 = v0.
Dot(v1);
140 double dot02 = v0.
Dot(v2);
141 double dot11 = v1.
Dot(v1);
142 double dot12 = v1.
Dot(v2);
145 double invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
146 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
147 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
150 return (u >= 0) && (v >= 0) && (u + v <= 1);
187 Vector3d dir = (_line[1] - _line[0]).Normalize();
189 double denom = norm.
Dot(dir);
195 double intersection =
196 -norm.
Dot(_line[0] - this->pts[0]) / denom;
199 if (intersection < 1.0 || intersection > _line.
Length())
203 _ipt1 = _line[0] + (dir * intersection);
217 else if (_line.
Intersect(this->Side(0), _ipt1))
222 else if (_line.
Intersect(this->Side(1), _ipt1))
227 else if (_line.
Intersect(this->Side(2), _ipt1))
240 return this->
Side(0).Length() + this->
Side(1).Length() +
241 this->
Side(2).Length();
249 T a = this->
Side(0).Length();
250 T b = this->
Side(1).Length();
251 T c = this->
Side(2).Length();
255 return sqrt(s * (s-a) * (s-b) * (s-c));
A three dimensional line segment.
Definition Line3.hh:35
bool Intersect(const Line3< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition Line3.hh:235
T Length() const
Get the length of the line.
Definition Line3.hh:144
@ NO_SIDE
On the plane.
Definition Plane.hh:50
A 3-dimensional triangle and related functions.
Definition Triangle3.hh:36
Vector3d Normal() const
Get the triangle's normal vector.
Definition Triangle3.hh:160
bool Valid() const
Get whether this triangle is valid, based on triangle inequality: the sum of the lengths of any two s...
Definition Triangle3.hh:91
bool Contains(const Vector3< T > &_pt) const
Get whether this triangle contains the given point.
Definition Triangle3.hh:129
Triangle3()=default
Default constructor.
T Perimeter() const
Get the length of the triangle's perimeter.
Definition Triangle3.hh:238
void Set(const Vector3< T > &_pt1, const Vector3< T > &_pt2, const Vector3< T > &_pt3)
Set all vertices of the triangle.
Definition Triangle3.hh:78
Vector3< T > operator[](const size_t _index) const
Get one of points that define the triangle.
Definition Triangle3.hh:262
double Area() const
Get the area of this triangle.
Definition Triangle3.hh:246
bool Contains(const Line3< T > &_line) const
Check if this triangle completely contains the given line segment.
Definition Triangle3.hh:121
void Set(const unsigned int _index, const Vector3< T > &_pt)
Set one vertex of the triangle.
Definition Triangle3.hh:64
Triangle3(const Vector3< T > &_pt1, const Vector3< T > &_pt2, const Vector3< T > &_pt3)
Constructor.
Definition Triangle3.hh:48
bool Intersects(const Line3< T > &_line, Vector3< T > &_ipt1) const
Get whether the given line intersects an edge of this triangle.
Definition Triangle3.hh:181
Line3< T > Side(const unsigned int _index) const
Get a line segment for one side of the triangle.
Definition Triangle3.hh:106
The Vector3 class represents the generic vector containing 3 elements.
Definition Vector3.hh:40
static Vector3 Normal(const Vector3< T > &_v1, const Vector3< T > &_v2, const Vector3< T > &_v3)
Get a normal vector to a triangle.
Definition Vector3.hh:251
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition Vector3.hh:198
Triangle3< double > Triangle3d
Double specialization of the Triangle class.
Definition Triangle3.hh:275
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition Helpers.hh:395
Plane< double > Planed
Definition Plane.hh:229
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition Helpers.hh:222
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition Helpers.hh:545
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition Helpers.hh:216
Triangle3< float > Triangle3f
Float specialization of the Triangle class.
Definition Triangle3.hh:278
Triangle3< int > Triangle3i
Integer specialization of the Triangle class.
Definition Triangle3.hh:272