18 #ifndef _IGNITION_TRIANGLE_HH_
19 #define _IGNITION_TRIANGLE_HH_
46 this->
Set(_pt1, _pt2, _pt3);
58 this->pts[_index] = _pt;
80 T a = this->
Side(0).Length();
81 T b = this->
Side(1).Length();
82 T c = this->
Side(2).Length();
83 return (a+b) > c && (b+c) > a && (c+a) > b;
98 return Line2<T>(this->pts[0], this->pts[1]);
100 return Line2<T>(this->pts[1], this->pts[2]);
102 return Line2<T>(this->pts[2], this->pts[0]);
126 double dot00 = v0.
Dot(v0);
127 double dot01 = v0.
Dot(v1);
128 double dot02 = v0.
Dot(v2);
129 double dot11 = v1.
Dot(v1);
130 double dot12 = v1.
Dot(v2);
133 double invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
134 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
135 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
138 return (u >= 0) && (v >= 0) && (u + v <= 1);
159 Line2<T> line1(this->pts[0], this->pts[1]);
160 Line2<T> line2(this->pts[1], this->pts[2]);
161 Line2<T> line3(this->pts[2], this->pts[0]);
164 std::set<math::Vector2<T> > points;
179 else if (points.size() == 1)
181 typename std::set<math::Vector2<T> >::iterator iter = points.begin();
193 typename std::set<math::Vector2<T> >::iterator iter = points.begin();
205 return this->
Side(0).Length() + this->
Side(1).Length() +
206 this->
Side(2).Length();
214 T a = this->
Side(0).Length();
215 T b = this->
Side(1).Length();
216 T c = this->
Side(2).Length();
220 return sqrt(s * (s-a) * (s-b) * (s-c));
230 return this->pts[_index];
bool Contains(const Line2< T > &_line) const
Check if this triangle completely contains the given line segment.
Definition: Triangle.hh:110
Line2< T > Side(unsigned int _index) const
Get a line segment for one side of the triangle.
Definition: Triangle.hh:93
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
A two dimensional line segment.
Definition: Line2.hh:32
math::Vector2< T > operator[](size_t _index) const
Get one of points that define the triangle.
Definition: Triangle.hh:226
void Set(unsigned int _index, const math::Vector2< T > &_pt)
Set one vertex of the triangle.
Definition: Triangle.hh:53
bool Valid() const
Get whether this triangle is valid, based on triangle inequality: the sum of the lengths of any two s...
Definition: Triangle.hh:78
Triangle< int > Trianglei
Integer specialization of the Triangle class.
Definition: Triangle.hh:238
Triangle< double > Triangled
Double specialization of the Triangle class.
Definition: Triangle.hh:241
bool Contains(const math::Vector2< T > &_pt) const
Get whether this triangle contains the given point.
Definition: Triangle.hh:118
double Area() const
Get the area of this triangle.
Definition: Triangle.hh:211
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:89
void Set(const math::Vector2< T > &_pt1, const math::Vector2< T > &_pt2, const math::Vector2< T > &_pt3)
Set all vertices of the triangle.
Definition: Triangle.hh:65
Triangle()=default
Default constructor.
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:30
Triangle class and related functions.
Definition: Triangle.hh:33
T Perimeter() const
Get the length of the triangle's perimeter.
Definition: Triangle.hh:203
bool Intersect(const Line2< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line2.hh:177
Triangle< float > Trianglef
Float specialization of the Triangle class.
Definition: Triangle.hh:244
bool Intersects(const Line2< T > &_line, math::Vector2< T > &_ipt1, math::Vector2< T > &_ipt2) const
Get whether the given line intersects this triangle.
Definition: Triangle.hh:148
Triangle(const math::Vector2< T > &_pt1, const math::Vector2< T > &_pt2, const math::Vector2< T > &_pt3)
Constructor.
Definition: Triangle.hh:42