17 #ifndef IGNITION_MATH_TRIANGLE_HH_ 18 #define IGNITION_MATH_TRIANGLE_HH_ 24 #include <ignition/math/config.hh> 30 inline namespace IGNITION_MATH_VERSION_NAMESPACE
45 const math::Vector2<T> &_pt2,
46 const math::Vector2<T> &_pt3)
48 this->
Set(_pt1, _pt2, _pt3);
56 public:
void Set(
const unsigned int _index,
const math::Vector2<T> &_pt)
58 this->pts[
clamp(_index, 0u, 2u)] = _pt;
65 public:
void Set(
const math::Vector2<T> &_pt1,
66 const math::Vector2<T> &_pt2,
67 const math::Vector2<T> &_pt3)
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;
96 return Line2<T>(this->pts[0], this->pts[1]);
98 return Line2<T>(this->pts[1], this->pts[2]);
100 return Line2<T>(this->pts[2], this->pts[0]);
116 public:
bool Contains(
const math::Vector2<T> &_pt)
const 119 math::Vector2<T> v0 = this->pts[2] -this->pts[0];
120 math::Vector2<T> v1 = this->pts[1] -this->pts[0];
121 math::Vector2<T> v2 = _pt - this->pts[0];
124 double dot00 = v0.Dot(v0);
125 double dot01 = v0.Dot(v1);
126 double dot02 = v0.Dot(v2);
127 double dot11 = v1.Dot(v1);
128 double dot12 = v1.Dot(v2);
131 double invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
132 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
133 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
136 return (u >= 0) && (v >= 0) && (u + v <= 1);
147 math::Vector2<T> &_ipt1,
148 math::Vector2<T> &_ipt2)
const 157 Line2<T> line1(this->pts[0], this->pts[1]);
158 Line2<T> line2(this->pts[1], this->pts[2]);
159 Line2<T> line3(this->pts[2], this->pts[0]);
162 std::set<math::Vector2<T> > points;
177 else if (points.size() == 1)
179 auto iter = points.begin();
191 auto iter = points.begin();
203 return this->
Side(0).Length() + this->
Side(1).Length() +
204 this->
Side(2).Length();
212 T a = this->
Side(0).Length();
213 T b = this->
Side(1).Length();
214 T c = this->
Side(2).Length();
218 return sqrt(s * (s-a) * (s-b) * (s-c));
226 public: math::Vector2<T>
operator[](
const size_t _index)
const 232 private: math::Vector2<T> pts[3];
Triangle()=default
Default constructor.
Triangle(const math::Vector2< T > &_pt1, const math::Vector2< T > &_pt2, const math::Vector2< T > &_pt3)
Constructor.
Definition: Triangle.hh:44
Triangle< double > Triangled
Double specialization of the Triangle class.
Definition: Triangle.hh:239
Triangle class and related functions.
Definition: Triangle.hh:35
T Perimeter() const
Get the length of the triangle's perimeter.
Definition: Triangle.hh:201
A two dimensional line segment.
Definition: Line2.hh:34
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:216
Triangle< float > Trianglef
Float specialization of the Triangle class.
Definition: Triangle.hh:242
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:146
Triangle< int > Trianglei
Integer specialization of the Triangle class.
Definition: Triangle.hh:236
bool Contains(const Line2< T > &_line) const
Check if this triangle completely contains the given line segment.
Definition: Triangle.hh:108
Line2< T > Side(const unsigned int _index) const
Get a line segment for one side of the triangle.
Definition: Triangle.hh:93
double Area() const
Get the area of this triangle.
Definition: Triangle.hh:209
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: Helpers.hh:222
bool Contains(const math::Vector2< T > &_pt) const
Get whether this triangle contains the given point.
Definition: Triangle.hh:116
math::Vector2< T > operator[](const size_t _index) const
Get one of points that define the triangle.
Definition: Triangle.hh:226
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
bool Intersect(const Line2< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line2.hh:179
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
void Set(const unsigned int _index, const math::Vector2< T > &_pt)
Set one vertex of the triangle.
Definition: Triangle.hh:56
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:395