core/vgl/vgl_intersection.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_intersection.h
00002 #ifndef vgl_intersection_h_
00003 #define vgl_intersection_h_
00004 //:
00005 // \file
00006 // \brief Set of intersection functions
00007 // \author Jan 25, 2007 Gamze Tunali
00008 //
00009 // For intersections of "homogeneous coordinates" objects like vgl_homg_line_2d<T>,
00010 // see the static methods of vgl/algo/vgl_homg_operators_2d<T> and _3d.
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   01 Mar 2007 - Gamze Tunali - split up into vgl/algo and vgl parts
00015 //   21 Jul 2009 - Peter Vanroose - added box intersection (2d and 3d)
00016 //   21 Jul 2009 - Peter Vanroose - added inlined point intersection functions
00017 // \endverbatim
00018 
00019 #include <vgl/vgl_fwd.h> // forward declare various vgl classes
00020 #include <vgl/vgl_box_2d.h> // method "contains()"
00021 #include <vgl/vgl_box_3d.h> // method "contains()"
00022 #include <vgl/vgl_point_2d.h> // method "operator==()"
00023 #include <vgl/vgl_point_3d.h> // method "operator==()"
00024 #include <vgl/vgl_line_3d_2_points.h>
00025 #include <vgl/vgl_line_segment_3d.h>
00026 #include <vgl/vgl_infinite_line_3d.h>
00027 #include <vcl_vector.h>
00028 
00029 //: Return true if the two points intersect, i.e., coincide
00030 // \relatesalso vgl_point_2d
00031 template <class T>
00032 inline bool vgl_intersection(vgl_point_2d<T> const& p0,
00033                              vgl_point_2d<T> const& p1)
00034 { return p0 == p1; }
00035 
00036 //: Return true if the two points intersect, i.e., coincide
00037 // \relatesalso vgl_point_2d
00038 template <class T>
00039 inline bool vgl_intersection(vgl_point_3d<T> const& p0,
00040                              vgl_point_3d<T> const& p1)
00041 { return p0 == p1; }
00042 
00043 //: Return true if line intersects box. If so, compute intersection points.
00044 // \relatesalso vgl_line_2d
00045 template <class T>
00046 bool vgl_intersection(vgl_box_2d<T> const& box,
00047                       vgl_line_2d<T> const& line,
00048                       vgl_point_2d<T>& p0,
00049                       vgl_point_2d<T>& p1);
00050 
00051 //: Returns the number of intersections of a line segment with a box, up to two are returned in p0 and p1.
00052 // \relatesalso vgl_line_segment_2d
00053 template <class T>
00054 unsigned vgl_intersection(vgl_box_2d<T> const& box,
00055                           vgl_line_segment_2d<T> const& line,
00056                           vgl_point_2d<T>& p0,
00057                           vgl_point_2d<T>& p1);
00058 
00059 //: Return the intersection point of two concurrent lines
00060 // \relatesalso vgl_line_3d_2_points
00061 //Allows intersection points outside the line segments
00062 //Throws an assertion if lines not concurrent
00063 template <class T>
00064 vgl_point_3d<T> vgl_intersection(vgl_line_3d_2_points<T> const& l1,
00065                                  vgl_line_3d_2_points<T> const& l2);
00066 
00067 //: Return the intersection point of segments of two concurrent lines. Returns false if the intersection point is not inside both line segments
00068 // \relatesalso vgl_line_segment_3d. 
00069 // 
00070 template <class T>
00071 bool vgl_intersection(vgl_line_segment_3d<T> const& l1,
00072                       vgl_line_segment_3d<T> const& l2,
00073                       vgl_point_3d<T>& i_pnt);
00074 
00075 //: Return the intersection point of segments of a concurrent line and line segment pair. Returns false if the intersection point is not inside both line segments
00076 // \relatesalso vgl_line_segment_3d
00077 // \relatesalso vgl_line_3d_2_points
00078 template <class T>
00079 bool vgl_intersection(vgl_line_3d_2_points<T> const& l1,
00080                       vgl_line_segment_3d<T> const& l2,
00081                       vgl_point_3d<T>& i_pnt);
00082 
00083 template <class T> inline
00084 bool vgl_intersection(vgl_line_segment_3d<T> const& l1,
00085                       vgl_line_3d_2_points<T> const& l2,
00086                       vgl_point_3d<T>& i_pnt)
00087 {
00088   return vgl_intersection(l2, l1, i_pnt);
00089 }
00090 
00091 //: Return the intersection point of infinite lines, if concurrent.
00092 // \relatesalso vgl_infinite_line_3d
00093 template <class T>
00094 bool vgl_intersection(vgl_infinite_line_3d<T> const& l1,
00095                       vgl_infinite_line_3d<T> const& l2,
00096                       vgl_point_3d<T>& i_pnt);
00097 
00098 //: Return the intersection point of rays. Returns false if rays are parallel or intersect outside of positive ray domain
00099 // \relatesalso vgl_ray_3d
00100 template <class T>
00101 bool vgl_intersection(vgl_ray_3d<T> const& r1,
00102                       vgl_ray_3d<T> const& r2,
00103                       vgl_point_3d<T>& i_pnt);
00104 
00105 //: Return the intersection point of two lines. Return false if lines are parallel
00106 // \relatesalso vgl_line_2d
00107 template <class T>
00108 bool vgl_intersection(vgl_line_2d<T>  const& line0,
00109                       vgl_line_2d<T>  const& line1,
00110                       vgl_point_2d<T>       &intersection_point );
00111 
00112 
00113 //: Return the intersection point of a line and a plane.
00114 // \relatesalso vgl_line_3d_2_points
00115 // \relatesalso vgl_plane_3d
00116 template <class T>
00117 vgl_point_3d<T> vgl_intersection(vgl_line_3d_2_points<T> const& line,
00118                                  vgl_plane_3d<T> const& plane);
00119 
00120 //: Return the intersection point of a line and a plane.
00121 // \relatesalso vgl_line_segment_3d
00122 // \relatesalso vgl_plane_3d
00123 template <class T>
00124 bool vgl_intersection(vgl_line_segment_3d<T> const& line,
00125                       vgl_plane_3d<T> const& plane,
00126                       vgl_point_3d<T> & i_pt);
00127 
00128 
00129 //: Return the intersection point of a line and a plane.
00130 // \relatesalso vgl_line_segment_3d
00131 // \relatesalso vgl_plane_3d
00132 template <class T>
00133 bool vgl_intersection(vgl_infinite_line_3d<T> const& line,
00134                       vgl_plane_3d<T> const& plane,
00135                       vgl_point_3d<T> & i_pt);
00136 
00137 //: Return the intersection point of a ray and a plane.
00138 // \relatesalso vgl_line_segment_3d
00139 // \relatesalso vgl_plane_3d
00140 template <class T>
00141 bool vgl_intersection(vgl_ray_3d<T> const& ray,
00142                       vgl_plane_3d<T> const& plane,
00143                       vgl_point_3d<T> & i_pt);
00144 
00145 //: Return the intersection line of two planes.
00146 // Returns false if planes are effectively parallel
00147 // \relatesalso vgl_line_segment_3d
00148 // \relatesalso vgl_plane_3d
00149 template <class T>
00150 bool vgl_intersection(vgl_plane_3d<T> const& plane0,
00151                       vgl_plane_3d<T> const& plane1,
00152                       vgl_line_segment_3d<T> & line){
00153   vgl_infinite_line_3d<T> inf_l;
00154   bool status = vgl_intersection(plane0, plane1, inf_l);
00155   if (status)
00156     line.set(inf_l.point_t(T(0)), inf_l.point_t(T(1)));
00157   return status;
00158 }
00159 
00160 template <class T>
00161 bool vgl_intersection(vgl_plane_3d<T> const& plane0,
00162                       vgl_plane_3d<T> const& plane1,
00163                       vgl_line_3d_2_points<T> & line){
00164   vgl_infinite_line_3d<T> inf_l;
00165   bool status = vgl_intersection(plane0, plane1, inf_l);
00166   if (status)
00167     line.set(inf_l.point_t(T(0)), inf_l.point_t(T(1)));
00168   return status;
00169 }
00170 
00171 template <class T>
00172 bool vgl_intersection(vgl_plane_3d<T> const& plane0,
00173                       vgl_plane_3d<T> const& plane1,
00174                       vgl_infinite_line_3d<T> & line);
00175 
00176 //: Return the intersection point of three planes.
00177 // \relatesalso vgl_plane_3d
00178 template <class T>
00179 vgl_point_3d<T> vgl_intersection(vgl_plane_3d<T> const& p1,
00180                                  vgl_plane_3d<T> const& p2,
00181                                  vgl_plane_3d<T> const& p3);
00182 
00183 //: Return true if any point on [p1,p2] is within tol of [q1,q2]
00184 //  Tests two line segments for intersection or near intersection
00185 //  (within given tolerance).
00186 // \author Dan jackson
00187 // \relatesalso vgl_point_2d
00188 template <class T>
00189 bool vgl_intersection(vgl_point_2d<T> const& p1,
00190                       vgl_point_2d<T> const& p2,
00191                       vgl_point_2d<T> const& q1,
00192                       vgl_point_2d<T> const& q2,
00193                       double tol = 1e-6);
00194 
00195 //: Return true if the point lies inside the box
00196 // \relatesalso vgl_point_2d
00197 // \relatesalso vgl_box_2d
00198 template <class T>
00199 inline bool vgl_intersection(vgl_box_2d<T> const& b, vgl_point_2d<T> const& p)
00200 { return b.contains(p); }
00201 
00202 //: Return true if the point lies inside the box
00203 // \relatesalso vgl_point_2d
00204 // \relatesalso vgl_box_2d
00205 template <class T>
00206 inline bool vgl_intersection(vgl_point_2d<T> const& p, vgl_box_2d<T> const& b)
00207 { return b.contains(p); }
00208 
00209 //: Return true if the point lies inside the box
00210 // \relatesalso vgl_point_3d
00211 // \relatesalso vgl_box_3d
00212 template <class T>
00213 inline bool vgl_intersection(vgl_box_3d<T> const& b, vgl_point_3d<T> const& p)
00214 { return b.contains(p); }
00215 
00216 //: Return true if the point lies inside the box
00217 // \relatesalso vgl_point_3d
00218 // \relatesalso vgl_box_3d
00219 template <class T>
00220 inline bool vgl_intersection(vgl_point_3d<T> const& p, vgl_box_3d<T> const& b)
00221 { return b.contains(p); }
00222 
00223 //: Return true if line intersects box. If so, compute intersection points.
00224 // \relatesalso vgl_infinite_line_3d
00225 template <class T>
00226 bool vgl_intersection(vgl_box_3d<T> const& box,
00227                       vgl_infinite_line_3d<T> const& line,
00228                       vgl_point_3d<T>& p0,
00229                       vgl_point_3d<T>& p1);
00230 //: Return true if ray intersects box. If so, compute intersection points.
00231 // If ray origin is inside box then p0==p1
00232 // \relatesalso vgl_ray_3d
00233 template <class T>
00234 bool vgl_intersection(vgl_box_3d<T> const& box,
00235                       vgl_ray_3d<T> const& ray,
00236                       vgl_point_3d<T>& p0,
00237                       vgl_point_3d<T>& p1);
00238 //: Return true if a box and plane intersect in 3D
00239 // \relatesalso vgl_plane_3d
00240 // \relatesalso vgl_box_3d
00241 template <class T>
00242 bool vgl_intersection(vgl_box_3d<T> const& b, vgl_plane_3d<T> const& plane);
00243 
00244 
00245 //: Return the intersection of two boxes (which is itself either a box, or empty)
00246 // \relatesalso vgl_box_2d
00247 template <class T>
00248 vgl_box_2d<T> vgl_intersection(vgl_box_2d<T> const&,vgl_box_2d<T> const&);
00249 
00250 //: Return the intersection of two boxes (which is itself either a box, or empty)
00251 // \relatesalso vgl_box_3d
00252 template <class T>
00253 vgl_box_3d<T> vgl_intersection(vgl_box_3d<T> const&,vgl_box_3d<T> const&);
00254 
00255 //: Return true if the box and polygon regions intersect, regions include boundaries
00256 // \relatesalso vgl_polygon
00257 // \relatesalso vgl_box_2d
00258 template <class T>
00259 bool vgl_intersection(vgl_box_2d<T> const& b, vgl_polygon<T> const& poly);
00260 
00261 //: Return the points from the list that lie inside the box
00262 // \relatesalso vgl_point_2d
00263 // \relatesalso vgl_box_2d
00264 template <class T>
00265 vcl_vector<vgl_point_2d<T> > vgl_intersection(vgl_box_2d<T> const& b, vcl_vector<vgl_point_2d<T> > const& p);
00266 
00267 //: Return the points from the list that lie inside the box
00268 // \relatesalso vgl_point_2d
00269 // \relatesalso vgl_box_2d
00270 template <class T>
00271 vcl_vector<vgl_point_2d<T> > vgl_intersection(vcl_vector<vgl_point_2d<T> > const& p, vgl_box_2d<T> const& b);
00272 
00273 //: Return the points from the list that lie inside the box
00274 // \relatesalso vgl_point_3d
00275 // \relatesalso vgl_box_3d
00276 template <class T>
00277 vcl_vector<vgl_point_3d<T> > vgl_intersection(vgl_box_3d<T> const& b, vcl_vector<vgl_point_3d<T> > const& p);
00278 
00279 //: Return the points from the list that lie inside the box
00280 // \relatesalso vgl_point_3d
00281 // \relatesalso vgl_box_3d
00282 template <class T>
00283 vcl_vector<vgl_point_3d<T> > vgl_intersection(vcl_vector<vgl_point_3d<T> > const& p, vgl_box_3d<T> const& b);
00284 
00285 //: Find the intersections of a line with a polygon( can have multiple sheets)
00286 // \relatesalso vgl_line_2d
00287 // \relatesalso vgl_point_2d
00288 template <class T>
00289 vcl_vector<vgl_point_2d<T> > vgl_intersection(vgl_polygon<T> const& poly,
00290                                               vgl_line_2d<T> const& line);
00291 template <class T>
00292 vcl_vector<vgl_point_2d<T> > vgl_intersection(vgl_line_2d<T> const& line,
00293                                               vgl_polygon<T> const& poly){
00294   return vgl_intersection(poly, line);
00295 }
00296 #define VGL_INTERSECTION_INSTANTIATE(T) extern "please include vgl/vgl_intersection.txx first"
00297 #define VGL_INTERSECTION_BOX_INSTANTIATE(T) extern "please include vgl/vgl_intersection.txx first"
00298 
00299 #endif // vgl_intersection_h_