Ignition Math

API Reference

6.8.0
Triangle3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_TRIANGLE3_HH_
18 #define IGNITION_MATH_TRIANGLE3_HH_
19 
20 #include <ignition/math/Helpers.hh>
21 #include <ignition/math/Line3.hh>
22 #include <ignition/math/Plane.hh>
23 #include <ignition/math/Vector3.hh>
24 #include <ignition/math/config.hh>
25 
26 namespace ignition
27 {
28  namespace math
29  {
30  // Inline bracket to help doxygen filtering.
31  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
32  //
35  template<typename T>
36  class Triangle3
37  {
39  public: Triangle3() = default;
40 
49  public: Triangle3(const Vector3<T> &_pt1,
50  const Vector3<T> &_pt2,
51  const Vector3<T> &_pt3)
52  {
53  this->Set(_pt1, _pt2, _pt3);
54  }
55 
65  public: void Set(const unsigned int _index, const Vector3<T> &_pt)
66  {
67  this->pts[clamp(_index, 0u, 2u)] = _pt;
68  }
69 
79  public: void Set(const Vector3<T> &_pt1,
80  const Vector3<T> &_pt2,
81  const Vector3<T> &_pt3)
82  {
83  this->pts[0] = _pt1;
84  this->pts[1] = _pt2;
85  this->pts[2] = _pt3;
86  }
87 
92  public: bool Valid() const
93  {
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;
98  }
99 
107  public: Line3<T> Side(const unsigned int _index) const
108  {
109  if (_index == 0)
110  return Line3<T>(this->pts[0], this->pts[1]);
111  else if (_index == 1)
112  return Line3<T>(this->pts[1], this->pts[2]);
113  else
114  return Line3<T>(this->pts[2], this->pts[0]);
115  }
116 
122  public: bool Contains(const Line3<T> &_line) const
123  {
124  return this->Contains(_line[0]) && this->Contains(_line[1]);
125  }
126 
130  public: bool Contains(const Vector3<T> &_pt) const
131  {
132  // Make sure the point is on the same plane as the triangle
133  if (Planed(this->Normal()).Side(_pt) == Planed::NO_SIDE)
134  {
135  Vector3d v0 = this->pts[2] - this->pts[0];
136  Vector3d v1 = this->pts[1] - this->pts[0];
137  Vector3d v2 = _pt - this->pts[0];
138 
139  double dot00 = v0.Dot(v0);
140  double dot01 = v0.Dot(v1);
141  double dot02 = v0.Dot(v2);
142  double dot11 = v1.Dot(v1);
143  double dot12 = v1.Dot(v2);
144 
145  // Compute barycentric coordinates
146  double invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
147  double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
148  double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
149 
150  // Check if point is in triangle
151  return (u >= 0) && (v >= 0) && (u + v <= 1);
152  }
153  else
154  {
155  return false;
156  }
157  }
158 
161  public: Vector3d Normal() const
162  {
163  return Vector3d::Normal(this->pts[0], this->pts[1], this->pts[2]);
164  }
165 
182  public: bool Intersects(const Line3<T> &_line, Vector3<T> &_ipt1) const
183  {
184  // Triangle normal
185  Vector3d norm = this->Normal();
186 
187  // Ray direction to intersect with triangle
188  Vector3d dir = (_line[1] - _line[0]).Normalize();
189 
190  double denom = norm.Dot(dir);
191 
192  // Handle the case when the line is not co-planar with the triangle
193  if (!math::equal(denom, 0.0))
194  {
195  // Distance from line start to triangle intersection
196  double intersection =
197  -norm.Dot(_line[0] - this->pts[0]) / denom;
198 
199  // Make sure the ray intersects the triangle
200  if (intersection < 1.0 || intersection > _line.Length())
201  return false;
202 
203  // Return point of intersection
204  _ipt1 = _line[0] + (dir * intersection);
205 
206  return true;
207  }
208  // Line co-planar with triangle
209  else
210  {
211  // If the line is completely inside the triangle
212  if (this->Contains(_line))
213  {
214  _ipt1 = _line[0];
215  return true;
216  }
217  // If the line intersects the first side
218  else if (_line.Intersect(this->Side(0), _ipt1))
219  {
220  return true;
221  }
222  // If the line intersects the second side
223  else if (_line.Intersect(this->Side(1), _ipt1))
224  {
225  return true;
226  }
227  // If the line intersects the third side
228  else if (_line.Intersect(this->Side(2), _ipt1))
229  {
230  return true;
231  }
232  }
233 
234  return false;
235  }
236 
239  public: T Perimeter() const
240  {
241  return this->Side(0).Length() + this->Side(1).Length() +
242  this->Side(2).Length();
243  }
244 
247  public: double Area() const
248  {
249  double s = this->Perimeter() / 2.0;
250  T a = this->Side(0).Length();
251  T b = this->Side(1).Length();
252  T c = this->Side(2).Length();
253 
254  // Heron's formula
255  // http://en.wikipedia.org/wiki/Heron%27s_formula
256  return sqrt(s * (s-a) * (s-b) * (s-c));
257  }
258 
263  public: Vector3<T> operator[](const size_t _index) const
264  {
265  return this->pts[clamp(_index, IGN_ZERO_SIZE_T, IGN_TWO_SIZE_T)];
266  }
267 
269  private: Vector3<T> pts[3];
270  };
271 
274 
277 
280  }
281  }
282 }
283 #endif
Vector3d Normal() const
Get the triangle&#39;s normal vector.
Definition: Triangle3.hh:161
On the plane.
Definition: Plane.hh:51
Triangle3< double > Triangle3d
Double specialization of the Triangle class.
Definition: Triangle3.hh:276
Plane< double > Planed
Definition: Plane.hh:236
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:258
bool Intersects(const Line3< T > &_line, Vector3< T > &_ipt1) const
Get whether the given line intersects an edge of this triangle.
Definition: Triangle3.hh:182
double Area() const
Get the area of this triangle.
Definition: Triangle3.hh:247
Line3< T > Side(const unsigned int _index) const
Get a line segment for one side of the triangle.
Definition: Triangle3.hh:107
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:556
Triangle3< float > Triangle3f
Float specialization of the Triangle class.
Definition: Triangle3.hh:279
Triangle3< int > Triangle3i
Integer specialization of the Triangle class.
Definition: Triangle3.hh:273
bool Contains(const Line3< T > &_line) const
Check if this triangle completely contains the given line segment.
Definition: Triangle3.hh:122
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:205
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:227
Vector3< T > operator[](const size_t _index) const
Get one of points that define the triangle.
Definition: Triangle3.hh:263
Triangle3(const Vector3< T > &_pt1, const Vector3< T > &_pt2, const Vector3< T > &_pt3)
Constructor.
Definition: Triangle3.hh:49
void Set(const Vector3< T > &_pt1, const Vector3< T > &_pt2, const Vector3< T > &_pt3)
Set all vertices of the triangle.
Definition: Triangle3.hh:79
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:41
A 3-dimensional triangle and related functions.
Definition: Triangle3.hh:36
T Perimeter() const
Get the length of the triangle&#39;s perimeter.
Definition: Triangle3.hh:239
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: Helpers.hh:233
A three dimensional line segment. The line is defined by a start and end point.
Definition: Line3.hh:35
T Length() const
Get the length of the line.
Definition: Line3.hh:145
Definition: Angle.hh:42
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
bool Contains(const Vector3< T > &_pt) const
Get whether this triangle contains the given point.
Definition: Triangle3.hh:130
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:406
bool Intersect(const Line3< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line3.hh:237
void Set(const unsigned int _index, const Vector3< T > &_pt)
Set one vertex of the triangle.
Definition: Triangle3.hh:65