vgl_triangle_3d.h
Go to the documentation of this file.
1 #ifndef VGL_TRIANGLE_3D_H_
2 #define VGL_TRIANGLE_3D_H_
3 //:
4 // \file
5 // \brief Some helpful functions when working with triangles
6 // \author Kieran O'Mahony
7 // \date 21 June 2007
8 
9 #include <algorithm>
10 #include <utility>
11 #include <cmath>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 
17 #include <vgl/vgl_point_3d.h>
18 
20 {
21  None=0,
24 };
25 
26 //: Check for coincident edges of triangles a and b
27 // \return a vector of the coincident edges
28 std::vector<std::pair<unsigned,unsigned> > vgl_triangle_3d_coincident_edges(
29  const vgl_point_3d<double>& a_p1,
30  const vgl_point_3d<double>& a_p2,
31  const vgl_point_3d<double>& a_p3,
32  const vgl_point_3d<double>& b_p1,
33  const vgl_point_3d<double>& b_p2,
34  const vgl_point_3d<double>& b_p3);
35 
36 //: Check if the given point is inside the triangle
37 // The triangle is represented by its vertices \a p1, \a p2, \a p3
38 // \return true if point is inside
40  const vgl_point_3d<double>& i_pnt,
41  const vgl_point_3d<double>& p1,
42  const vgl_point_3d<double>& p2,
43  const vgl_point_3d<double>& p3);
44 
45 //: Check if the given point is inside the triangle
46 // The triangle is represented by its vertices \a p1, \a p2, \a p3
47 // \return true if point is inside
48 // \param coplanar_tolerance is used to dismiss points because they are
49 // outside the plane. This doesn't widen the triangle, just thickens it.
51  const vgl_point_3d<double>& p1,
52  const vgl_point_3d<double>& p2,
53  const vgl_point_3d<double>& p3,
54  double coplanar_tolerance );
55 
56 //: Check if point \a i_pnt is inside the triangle
57 // The triangle is represented by its vertices \a p1, \a p2, \a p3
58 // \return true if point is inside
59 // \note this method uses the less efficient 'angles' method which requires 3 calls to acos()
61  const vgl_point_3d<double>& i_pnt,
62  const vgl_point_3d<double>& p1,
63  const vgl_point_3d<double>& p2,
64  const vgl_point_3d<double>& p3 );
65 
66 
67 //: Compute the intersection point between the line segment and triangle
68 // The triangle is represented by its vertices \a p1, \a p2, \a p3
69 // \return true if line intersects triangle
71  const vgl_line_segment_3d<double>& line,
72  const vgl_point_3d<double>& p1,
73  const vgl_point_3d<double>& p2,
74  const vgl_point_3d<double>& p3,
75  vgl_point_3d<double>& i_pnt,
76  bool ignore_coplanar = false);
77 
78 //: Compute if the given triangles a and b intersect
79 // The triangles are represented by their respective vertices \a a_p1, \a a_p2, \a a_p3
80 // and \a b_p1, \a b_p2, \a b_p3
81 // \return intersection type
83  const vgl_point_3d<double>& a_p1,
84  const vgl_point_3d<double>& a_p2,
85  const vgl_point_3d<double>& a_p3,
86  const vgl_point_3d<double>& b_p1,
87  const vgl_point_3d<double>& b_p2,
88  const vgl_point_3d<double>& b_p3);
89 
90 //: Compute the intersection line of the given triangles
91 // \see vgl_triangle_3d_triangle_intersection()
92 // \note an intersection line is not computed for a coplanar intersection
94  const vgl_point_3d<double>& a_p1,
95  const vgl_point_3d<double>& a_p2,
96  const vgl_point_3d<double>& a_p3,
97  const vgl_point_3d<double>& b_p1,
98  const vgl_point_3d<double>& b_p2,
99  const vgl_point_3d<double>& b_p3,
101 
102 //: compute the intersection line of the given triangles
103 // \see vgl_triangle_3d_triangle_intersection()
104 // \note an intersection line is not computed for a coplanar intersection
105 // \retval i_line_point1_edge A number [0-5] indicating which edge of the two triangles
106 // point1 of i_line lies on. 0 indicates [a_p1,a_p2], 1 - [a_p2,a_p3], 2 - [a_p3,a_p1],
107 // 3 - [b_p1,b_p2], 4 - [b_p2,b_p3], 5 - [b_p3,b_p1]
108 // \retval i_line_point2_edge. As i_line_point1_edge, but for the other end of the intersection.
109 // \note if i_line_point1_edge==i_line_point2_edge, this indicates that due to coplanarity, or
110 // some other corner case, there were more than two edges involved in the intersection
111 // boundaries. The returned edge is one of those edges.
113  const vgl_point_3d<double>& a_p1,
114  const vgl_point_3d<double>& a_p2,
115  const vgl_point_3d<double>& a_p3,
116  const vgl_point_3d<double>& b_p1,
117  const vgl_point_3d<double>& b_p2,
118  const vgl_point_3d<double>& b_p3,
120  unsigned &i_line_point1_edge,
121  unsigned &i_line_point2_edge);
122 
123 //: Compute the line of intersection of the given triangle and plane
124 // The triangle is represented by its vertices \a p1, \a p2, \a p3
125 // \return intersection type
126 // \note an intersection line is not defined (NaN) for a coplanar intersection
128  const vgl_point_3d<double>& p1,
129  const vgl_point_3d<double>& p2,
130  const vgl_point_3d<double>& p3,
131  const vgl_plane_3d<double>& i_plane,
133 
134 //: Compute the longest side of the given triangle
135 // The triangle is represented by its vertices \a p1, \a p2, \a p3
136 // \return length of the longest side
138  const vgl_point_3d<double>& p1,
139  const vgl_point_3d<double>& p2,
140  const vgl_point_3d<double>& p3)
141 {
142  double side_length_max = std::max( (p2 - p1).sqr_length(), (p3 - p2).sqr_length());
143  side_length_max = std::max( side_length_max, (p1 - p3).sqr_length());
144  return std::sqrt(side_length_max);
145 }
146 
147 //: Compute the shortest side of the given triangle
148 // The triangle is represented by its vertices \a p1, \a p2, \a p3
149 // \return length of the longest side
151  const vgl_point_3d<double>& p1,
152  const vgl_point_3d<double>& p2,
153  const vgl_point_3d<double>& p3)
154 {
155  double side_length_min = std::min( (p2 - p1).sqr_length(), (p3 - p2).sqr_length());
156  side_length_min = std::min( side_length_min, (p1 - p3).sqr_length());
157  return std::sqrt(side_length_min);
158 }
159 
160 //: Compute the closest point on a triangle to a reference point
161 // The triangle is represented by its vertices \a p1, \a p2, \a p3.
162 // \param q The reference point.
163 // \return The closest point on the triangle. This may be inside the triangle, or it may be a point on one of the triangle edges.
165  const vgl_point_3d<double>& q,
166  const vgl_point_3d<double>& p1,
167  const vgl_point_3d<double>& p2,
168  const vgl_point_3d<double>& p3);
169 
170 //: Compute the distance to the closest point on a triangle from a reference point.
171 // The triangle is represented by its vertices \a p1, \a p2, \a p3.
172 // \param q The reference point.
173 // \return The distance to the closest point on the triangle. (The closest point may be inside the triangle, or it may be a point on one of the triangle edges.)
175  const vgl_point_3d<double>& q,
176  const vgl_point_3d<double>& p1,
177  const vgl_point_3d<double>& p2,
178  const vgl_point_3d<double>& p3);
179 
180 //: Check if the two triangles are coplanar
181 // The triangles are represented by their respective vertices \a a_p1, \a a_p2, \a a_p3
182 // and \a b_p1, \a b_p2, \a b_p3
184  const vgl_point_3d<double>& a_p1,
185  const vgl_point_3d<double>& a_p2,
186  const vgl_point_3d<double>& a_p3,
187  const vgl_point_3d<double>& b_p1,
188  const vgl_point_3d<double>& b_p2,
189  const vgl_point_3d<double>& b_p3);
190 
191 
192 //=======================================================================
193 //: Compute the area of a triangle
194 // The triangle is represented by its vertices \a p1, \a p2, \a p3
195 double vgl_triangle_3d_area(
196  const vgl_point_3d<double> &p0,
197  const vgl_point_3d<double> &p1,
198  const vgl_point_3d<double> &p2 );
199 
200 //=======================================================================
201 //: Compute the aspect ratio of a triangle
202 // The triangle is represented by its vertices \a p1, \a p2, \a p3
204  const vgl_point_3d<double> &p0,
205  const vgl_point_3d<double> &p1,
206  const vgl_point_3d<double> &p2 );
207 
208 #endif // VGL_TRIANGLE_3D_H_
double vgl_triangle_3d_distance(const vgl_point_3d< double > &q, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3)
Compute the distance to the closest point on a triangle from a reference point.
std::vector< std::pair< unsigned, unsigned > > vgl_triangle_3d_coincident_edges(const vgl_point_3d< double > &a_p1, const vgl_point_3d< double > &a_p2, const vgl_point_3d< double > &a_p3, const vgl_point_3d< double > &b_p1, const vgl_point_3d< double > &b_p2, const vgl_point_3d< double > &b_p3)
Check for coincident edges of triangles a and b.
double vgl_triangle_3d_aspect_ratio(const vgl_point_3d< double > &p0, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2)
Compute the aspect ratio of a triangle.
Represents a cartesian 3D point.
Definition: vgl_fwd.h:11
bool vgl_triangle_3d_test_inside_simple(const vgl_point_3d< double > &i_pnt, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3)
Check if point i_pnt is inside the triangle.
bool vgl_triangle_3d_test_inside(const vgl_point_3d< double > &i_pnt, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3)
Check if the given point is inside the triangle.
double vgl_triangle_3d_longest_side(const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3)
Compute the longest side of the given triangle.
vgl_triangle_3d_intersection_t
double vgl_triangle_3d_area(const vgl_point_3d< double > &p0, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2)
Compute the area of a triangle.
a point in 3D nonhomogeneous space
line segment in 3D nonhomogeneous space
vgl_triangle_3d_intersection_t vgl_triangle_3d_line_intersection(const vgl_line_segment_3d< double > &line, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3, vgl_point_3d< double > &i_pnt, bool ignore_coplanar=false)
Compute the intersection point between the line segment and triangle.
Represents a Euclidean 3D plane.
Definition: vgl_fwd.h:23
Represents a 3D line segment using two points.
Definition: vgl_fwd.h:19
bool vgl_triangle_3d_triangle_coplanar(const vgl_point_3d< double > &a_p1, const vgl_point_3d< double > &a_p2, const vgl_point_3d< double > &a_p3, const vgl_point_3d< double > &b_p1, const vgl_point_3d< double > &b_p2, const vgl_point_3d< double > &b_p3)
Check if the two triangles are coplanar.
T sqr_length(v const &a)
Return the squared length of a vector.
Definition: vgl_vector_2d.h:98
vgl_triangle_3d_intersection_t vgl_triangle_3d_plane_intersection(const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3, const vgl_plane_3d< double > &i_plane, vgl_line_segment_3d< double > &i_line)
Compute the line of intersection of the given triangle and plane.
double vgl_triangle_3d_shortest_side(const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3)
Compute the shortest side of the given triangle.
vgl_point_3d< double > vgl_triangle_3d_closest_point(const vgl_point_3d< double > &q, const vgl_point_3d< double > &p1, const vgl_point_3d< double > &p2, const vgl_point_3d< double > &p3)
Compute the closest point on a triangle to a reference point.
vgl_triangle_3d_intersection_t vgl_triangle_3d_triangle_intersection(const vgl_point_3d< double > &a_p1, const vgl_point_3d< double > &a_p2, const vgl_point_3d< double > &a_p3, const vgl_point_3d< double > &b_p1, const vgl_point_3d< double > &b_p2, const vgl_point_3d< double > &b_p3)
Compute if the given triangles a and b intersect.