00001 // This is brl/bbas/imesh/algo/imesh_intersect.h 00002 #ifndef imesh_intersect_h_ 00003 #define imesh_intersect_h_ 00004 //: 00005 // \file 00006 // \brief Functions for mesh intersections 00007 // \author Matt Leotta (mleotta@lems.brown.edu) 00008 // \date May 9, 2008 00009 // 00010 // \verbatim 00011 // Modifications 00012 // <none yet> 00013 // \endverbatim 00014 00015 00016 #include <imesh/imesh_mesh.h> 00017 #include <vgl/vgl_vector_3d.h> 00018 #include <vcl_limits.h> 00019 00020 00021 //: Intersect the ray from point p with direction d and the triangle defined by a,b,c 00022 // \returns true if intersection occurs 00023 // \param dist is the distance to the triangle (returned by reference) 00024 // \param u and \param v are the barycentric coordinates of the intersection 00025 // Barycentric coordinates are u and v such that (1-u-v)*a + u*b + v*c = p+dist*d 00026 bool imesh_intersect_triangle(const vgl_point_3d<double>& p, 00027 const vgl_vector_3d<double>& d, 00028 const vgl_point_3d<double>& a, 00029 const vgl_point_3d<double>& b, 00030 const vgl_point_3d<double>& c, 00031 double& dist, 00032 double& u, double& v); 00033 00034 00035 //: Intersect the ray from point p with direction d and the triangle defined by a,b,c 00036 // The un-normalized normal vector (b-a)x(c-a) is precomputed and also passed in 00037 // \returns true if intersection occurs 00038 // \param dist is the distance to the triangle (returned by reference) 00039 // \param u and \param v are the barycentric coordinates of the intersection 00040 bool imesh_intersect_triangle(const vgl_point_3d<double>& p, 00041 const vgl_vector_3d<double>& d, 00042 const vgl_point_3d<double>& a, 00043 const vgl_point_3d<double>& b, 00044 const vgl_point_3d<double>& c, 00045 const vgl_vector_3d<double>& n, 00046 double& dist, 00047 double& u, double& v); 00048 00049 00050 //: Intersect the ray from point p with direction d and the triangle defined by a,b,c 00051 // The un-normalized normal vector (b-a)x(c-a) is precomputed and also passed in 00052 // \returns true if intersection occurs and the new dist is less than the old distance (but > 0) 00053 // \param dist is the distance to the triangle (returned by reference) 00054 // \param u and \param v are the barycentric coordinates of the intersection 00055 bool imesh_intersect_triangle_min_dist(const vgl_point_3d<double>& p, 00056 const vgl_vector_3d<double>& d, 00057 const vgl_point_3d<double>& a, 00058 const vgl_point_3d<double>& b, 00059 const vgl_point_3d<double>& c, 00060 const vgl_vector_3d<double>& n, 00061 double& dist, 00062 double& u, double& v); 00063 00064 //: Intersect the ray from point p with direction d and the triangulated mesh 00065 // \returns the face index of the closest intersecting triangle 00066 // \param dist is the distance to the triangle (returned by reference) 00067 // \param u and \param v (optional) are the barycentric coordinates of the intersection 00068 int imesh_intersect_min_dist(const vgl_point_3d<double>& p, 00069 const vgl_vector_3d<double>& d, 00070 const imesh_mesh& mesh, 00071 double& dist, double* u=0, double* v=0); 00072 00073 00074 //: Find the closest point on the triangle a,b,c to point p 00075 // The un-normalized normal vector (b-a)x(c-a) is precomputed and also passed in 00076 // \returns a code indicating that the closest point: 00077 // - 0 does not exist (should not occur) 00078 // - 1 is \a a 00079 // - 2 is \a b 00080 // - 3 is on the edge from \a a to \a b 00081 // - 4 is \a c 00082 // - 5 is on the edge from \a a to \a c 00083 // - 6 is on the edge from \a b to \a c 00084 // - 7 is on the face of the triangle 00085 // \param dist is the distance to the triangle (returned by reference) 00086 // \param u and \param v are the barycentric coordinates of the closest point 00087 unsigned char 00088 imesh_triangle_closest_point(const vgl_point_3d<double>& p, 00089 const vgl_point_3d<double>& a, 00090 const vgl_point_3d<double>& b, 00091 const vgl_point_3d<double>& c, 00092 const vgl_vector_3d<double>& n, 00093 double& dist, 00094 double& u, double& v); 00095 00096 00097 //: Find the closest point on the triangle a,b,c to point p 00098 // \returns a code same as other version of this function 00099 // \param dist is the distance to the triangle (returned by reference) 00100 // \param u and \param v are the barycentric coordinates of the closest point 00101 unsigned char 00102 imesh_triangle_closest_point(const vgl_point_3d<double>& p, 00103 const vgl_point_3d<double>& a, 00104 const vgl_point_3d<double>& b, 00105 const vgl_point_3d<double>& c, 00106 double& dist, 00107 double& u, double& v); 00108 00109 00110 //: Find the closest point on the triangle a,b,c to point p 00111 // \param dist is the distance to the triangle (returned by reference) 00112 vgl_point_3d<double> 00113 imesh_triangle_closest_point(const vgl_point_3d<double>& p, 00114 const vgl_point_3d<double>& a, 00115 const vgl_point_3d<double>& b, 00116 const vgl_point_3d<double>& c, 00117 double& dist); 00118 00119 00120 //: Find the closest point on the triangulated mesh to point p 00121 // \returns the face index of the closest triangle (one of them if on an edge or vertex) 00122 // \param cp is the closest point on the mesh (returned by reference) 00123 // \param u and \param v (optional) are the barycentric coordinates of the closest point 00124 int imesh_closest_point(const vgl_point_3d<double>& p, 00125 const imesh_mesh& mesh, 00126 vgl_point_3d<double>& cp, 00127 double* u=0, double* v=0); 00128 00129 00130 //: Find the closest intersection point from p along d with triangle a,b,c 00131 // \returns a code indicating that the intersection point: 00132 // - 0 does not exist 00133 // - 1 is \a a 00134 // - 2 is \a b 00135 // - 3 is on the edge from \a a to \a b 00136 // - 4 is \a c 00137 // - 5 is on the edge from \a a to \a c 00138 // - 6 is on the edge from \a b to \a c 00139 // - 7 could not be computed (error) 00140 // \param u and \param v are the barycentric coordinates of the intersection 00141 unsigned char 00142 imesh_triangle_intersect(const vgl_point_2d<double>& p, 00143 const vgl_vector_2d<double>& d, 00144 const vgl_point_2d<double>& a, 00145 const vgl_point_2d<double>& b, 00146 const vgl_point_2d<double>& c, 00147 double& u, double& v); 00148 00149 00150 //: Find the closest intersection point along the vector (du,dv) 00151 // Both the vector (du,dv) and the intersection point are in barycentric coordinates 00152 // \returns a code indicating that the intersection point: 00153 // - 0 does not exist 00154 // - 1 is at corner (0,0) 00155 // - 2 is at corner (1,0) 00156 // - 3 is on the edge v=0 00157 // - 4 is at corner (0,1) 00158 // - 5 is on the edge from u=0 00159 // - 6 is on the edge from u+v=1 00160 // - 7 could not be computed (error) 00161 // \param u and \param v are updated to the coordinates of the intersection 00162 unsigned char 00163 imesh_triangle_intersect(double& u, double& v, 00164 const double& du, const double& dv, 00165 const double& eps = vcl_numeric_limits<double>::epsilon()); 00166 00167 #endif // imesh_intersect_h_