core/vgl/vgl_triangle_3d.h
Go to the documentation of this file.
00001 #ifndef VGL_TRIANGLE_3D_H_
00002 #define VGL_TRIANGLE_3D_H_
00003 //:
00004 // \file
00005 // \brief Some helpful functions when working with triangles
00006 // \author Kieran O'Mahony
00007 // \date 21 June 2007
00008 
00009 #include <vcl_algorithm.h>
00010 #include <vcl_utility.h>
00011 #include <vcl_cmath.h>
00012 
00013 #include <vgl/vgl_line_segment_3d.h>
00014 #include <vgl/vgl_point_3d.h>
00015 
00016 enum vgl_triangle_3d_intersection_t
00017 {
00018   None=0,
00019   Skew,
00020   Coplanar
00021 };
00022 
00023 //: Check for coincident edges of triangles a and b
00024 //  \return a vector of the coincident edges
00025 vcl_vector<vcl_pair<unsigned,unsigned> > vgl_triangle_3d_coincident_edges(
00026   const vgl_point_3d<double>& a_p1,
00027   const vgl_point_3d<double>& a_p2,
00028   const vgl_point_3d<double>& a_p3,
00029   const vgl_point_3d<double>& b_p1,
00030   const vgl_point_3d<double>& b_p2,
00031   const vgl_point_3d<double>& b_p3);
00032 
00033 //: Check if the given point is inside the triangle
00034 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00035 //  \return true if point is inside
00036 bool vgl_triangle_3d_test_inside(
00037   const vgl_point_3d<double>& i_pnt,
00038   const vgl_point_3d<double>& p1,
00039   const vgl_point_3d<double>& p2,
00040   const vgl_point_3d<double>& p3);
00041 
00042 //: Check if point \a i_pnt is inside the triangle
00043 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00044 //  \return true if point is inside
00045 //  \note this method uses the less efficient 'angles' method which requires 3 calls to acos()
00046 bool vgl_triangle_3d_test_inside_simple(
00047   const vgl_point_3d<double>& i_pnt,
00048   const vgl_point_3d<double>& p1,
00049   const vgl_point_3d<double>& p2,
00050   const vgl_point_3d<double>& p3 );
00051 
00052 
00053 //: Compute the intersection point between the line segment and triangle
00054 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00055 //  \return true if line intersects triangle
00056 vgl_triangle_3d_intersection_t vgl_triangle_3d_line_intersection(
00057   const vgl_line_segment_3d<double>& line,
00058   const vgl_point_3d<double>& p1,
00059   const vgl_point_3d<double>& p2,
00060   const vgl_point_3d<double>& p3,
00061   vgl_point_3d<double>& i_pnt,
00062   bool ignore_coplanar = false);
00063 
00064 //: Compute if the given triangles a and b intersect
00065 //  The triangles are represented by their respective vertices \a a_p1, \a a_p2, \a a_p3
00066 //  and \a b_p1, \a b_p2, \a b_p3
00067 //  \return intersection type
00068 vgl_triangle_3d_intersection_t vgl_triangle_3d_triangle_intersection(
00069   const vgl_point_3d<double>& a_p1,
00070   const vgl_point_3d<double>& a_p2,
00071   const vgl_point_3d<double>& a_p3,
00072   const vgl_point_3d<double>& b_p1,
00073   const vgl_point_3d<double>& b_p2,
00074   const vgl_point_3d<double>& b_p3);
00075 
00076 //: Compute the intersection line of the given triangles
00077 //  \see vgl_triangle_3d_triangle_intersection()
00078 //  \note an intesection line is not computed for a coplanar intersection
00079 vgl_triangle_3d_intersection_t vgl_triangle_3d_triangle_intersection(
00080   const vgl_point_3d<double>& a_p1,
00081   const vgl_point_3d<double>& a_p2,
00082   const vgl_point_3d<double>& a_p3,
00083   const vgl_point_3d<double>& b_p1,
00084   const vgl_point_3d<double>& b_p2,
00085   const vgl_point_3d<double>& b_p3,
00086   vgl_line_segment_3d<double>& i_line);
00087 
00088 //: Compute the line of intersection of the given triangle and plane
00089 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00090 //  \return intersection type
00091 //  \note an intersection line is not defined (NaN) for a coplanar intersection
00092 vgl_triangle_3d_intersection_t vgl_triangle_3d_plane_intersection(
00093   const vgl_point_3d<double>& p1,
00094   const vgl_point_3d<double>& p2,
00095   const vgl_point_3d<double>& p3,
00096   const vgl_plane_3d<double>& i_plane,
00097   vgl_line_segment_3d<double>& i_line);
00098 
00099 //: Compute the longest side of the given triangle
00100 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00101 //  \return length of the longest side
00102 inline double vgl_triangle_3d_longest_side(
00103   const vgl_point_3d<double>& p1,
00104   const vgl_point_3d<double>& p2,
00105   const vgl_point_3d<double>& p3)
00106 {
00107   double side_length_max = vcl_max( (p2 - p1).sqr_length(), (p3 - p2).sqr_length());
00108   side_length_max = vcl_max( side_length_max, (p1 - p3).sqr_length());
00109   return vcl_sqrt(side_length_max);
00110 }
00111 
00112 //: Compute the shortest side of the given triangle
00113 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00114 //  \return length of the longest side
00115 inline double vgl_triangle_3d_shortest_side(
00116   const vgl_point_3d<double>& p1,
00117   const vgl_point_3d<double>& p2,
00118   const vgl_point_3d<double>& p3)
00119 {
00120   double side_length_min = vcl_min( (p2 - p1).sqr_length(), (p3 - p2).sqr_length());
00121   side_length_min = vcl_min( side_length_min, (p1 - p3).sqr_length());
00122   return vcl_sqrt(side_length_min);
00123 }
00124 
00125 //: Compute the closest point on a triangle to a reference point
00126 //  The triangle is represented by its vertices \a p1, \a p2, \a p3.
00127 //  \param q The reference point.
00128 //  \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.
00129 vgl_point_3d<double> vgl_triangle_3d_closest_point(
00130   const vgl_point_3d<double>& q,
00131   const vgl_point_3d<double>& p1,
00132   const vgl_point_3d<double>& p2,
00133   const vgl_point_3d<double>& p3);
00134 
00135 //: Compute the distance to the closest point on a triangle from a reference point.
00136 //  The triangle is represented by its vertices \a p1, \a p2, \a p3.
00137 //  \param q The reference point.
00138 //  \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.)
00139 double vgl_triangle_3d_distance(
00140   const vgl_point_3d<double>& q,
00141   const vgl_point_3d<double>& p1,
00142   const vgl_point_3d<double>& p2,
00143   const vgl_point_3d<double>& p3);
00144 
00145 //: Check if the two triangles are coplanar
00146 //  The triangles are represented by their respective vertices \a a_p1, \a a_p2, \a a_p3
00147 //  and \a b_p1, \a b_p2, \a b_p3
00148 bool vgl_triangle_3d_triangle_coplanar(
00149   const vgl_point_3d<double>& a_p1,
00150   const vgl_point_3d<double>& a_p2,
00151   const vgl_point_3d<double>& a_p3,
00152   const vgl_point_3d<double>& b_p1,
00153   const vgl_point_3d<double>& b_p2,
00154   const vgl_point_3d<double>& b_p3);
00155 
00156 
00157 //=======================================================================
00158 //: Compute the area of a triangle
00159 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00160 double vgl_triangle_3d_area(
00161   const vgl_point_3d<double> &p0,
00162   const vgl_point_3d<double> &p1,
00163   const vgl_point_3d<double> &p2 );
00164 
00165 //=======================================================================
00166 //: Compute the aspect ratio of a triangle
00167 //  The triangle is represented by its vertices \a p1, \a p2, \a p3
00168 double vgl_triangle_3d_aspect_ratio(
00169   const vgl_point_3d<double> &p0,
00170   const vgl_point_3d<double> &p1,
00171   const vgl_point_3d<double> &p2 );
00172 
00173 #endif // VGL_TRIANGLE_3D_H_