18 #ifndef _IGNITION_TRIANGLE3_HH_
19 #define _IGNITION_TRIANGLE3_HH_
50 this->
Set(_pt1, _pt2, _pt3);
67 this->pts[_index] = _pt;
94 T a = this->
Side(0).Length();
95 T b = this->
Side(1).Length();
96 T c = this->
Side(2).Length();
97 return (a+b) > c && (b+c) > a && (c+a) > b;
111 else if (_index == 0)
112 return Line3<T>(this->pts[0], this->pts[1]);
113 else if (_index == 1)
114 return Line3<T>(this->pts[1], this->pts[2]);
116 return Line3<T>(this->pts[2], this->pts[0]);
137 Vector3d v0 = this->pts[2] - this->pts[0];
138 Vector3d v1 = this->pts[1] - this->pts[0];
141 double dot00 = v0.
Dot(v0);
142 double dot01 = v0.
Dot(v1);
143 double dot02 = v0.
Dot(v2);
144 double dot11 = v1.
Dot(v1);
145 double dot12 = v1.
Dot(v2);
148 double invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
149 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
150 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
153 return (u >= 0) && (v >= 0) && (u + v <= 1);
190 Vector3d dir = (_line[1] - _line[0]).Normalize();
192 double denom = norm.
Dot(dir);
198 double intersection =
199 -norm.
Dot(_line[0] - this->pts[0]) / denom;
202 if (intersection < 1.0 || intersection > _line.
Length())
206 _ipt1 = _line[0] + (dir * intersection);
220 else if (_line.
Intersect(this->Side(0), _ipt1))
225 else if (_line.
Intersect(this->Side(1), _ipt1))
230 else if (_line.
Intersect(this->Side(2), _ipt1))
243 return this->
Side(0).Length() + this->
Side(1).Length() +
244 this->
Side(2).Length();
252 T a = this->
Side(0).Length();
253 T b = this->
Side(1).Length();
254 T c = this->
Side(2).Length();
258 return sqrt(s * (s-a) * (s-b) * (s-c));
268 return this->pts[_index];
T Length() const
Get the length of the line.
Definition: Line3.hh:142
Triangle3< int > Triangle3i
Integer specialization of the Triangle class.
Definition: Triangle3.hh:276
bool Intersect(const Line3< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line3.hh:233
double Area() const
Get the area of this triangle.
Definition: Triangle3.hh:249
Vector3d Normal() const
Get the triangle's normal vector.
Definition: Triangle3.hh:163
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:92
Plane< double > Planed
Definition: Plane.hh:228
On the plane.
Definition: Plane.hh:48
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:187
bool Intersects(const Line3< T > &_line, Vector3< T > &_ipt1) const
Get whether the given line intersects an edge of this triangle.
Definition: Triangle3.hh:184
Line3< T > Side(const unsigned int _index) const
Get a line segment for one side of the triangle.
Definition: Triangle3.hh:107
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:37
T Perimeter() const
Get the length of the triangle's perimeter.
Definition: Triangle3.hh:241
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
Triangle3< double > Triangle3d
Double specialization of the Triangle class.
Definition: Triangle3.hh:279
static Vector3 Normal(const Vector3< double > &_v1, const Vector3< double > &_v2, const Vector3< double > &_v3)
Get a normal vector to a triangle.
Definition: Vector3.hh:240
A 3-dimensional triangle and related functions.
Definition: Triangle3.hh:33
Triangle3()=default
Default constructor.
void Set(const Vector3< T > &_pt1, const Vector3< T > &_pt2, const Vector3< T > &_pt3)
Set all vertices of the triangle.
Definition: Triangle3.hh:79
Triangle3< float > Triangle3f
Float specialization of the Triangle class.
Definition: Triangle3.hh:282
A three dimensional line segment.
Definition: Line3.hh:32
Vector3< T > operator[](size_t _index) const
Get one of points that define the triangle.
Definition: Triangle3.hh:264
bool Contains(const Line3< T > &_line) const
Check if this triangle completely contains the given line segment.
Definition: Triangle3.hh:124
Definition: AffineException.hh:30
Triangle3(const Vector3< T > &_pt1, const Vector3< T > &_pt2, const Vector3< T > &_pt3)
Constructor.
Definition: Triangle3.hh:46
void Set(const unsigned int _index, const Vector3< T > &_pt)
Set one vertex of the triangle.
Definition: Triangle3.hh:62
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:257
bool Contains(const Vector3< T > &_pt) const
Get whether this triangle contains the given point.
Definition: Triangle3.hh:132