core/vgl/vgl_lineseg_test.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_lineseg_test.h
00002 #ifndef vgl_lineseg_test_h_
00003 #define vgl_lineseg_test_h_
00004 //:
00005 // \file
00006 // \author fsm
00007 // \verbatim
00008 //  Modifications
00009 //   Nov.2003 - Peter Vanroose - made all functions templated
00010 //   Sep.2005 - Peter Vanroose - bug fix: collinear line segments always "true"
00011 //   Mar.2008 - Ibrahim Eden - bug fix: bool vgl_lineseg_test_line(vgl_line_2d<T> const& l1,vgl_line_segment_2d<T> const& l2)
00012 //   Mar.2009 - Dirk Steckhan - bug fix in vgl_lineseg_test_point (missing sqrt)
00013 // \endverbatim
00014 
00015 #include <vgl/vgl_line_segment_2d.h>
00016 #include <vgl/vgl_line_2d.h>
00017 #include <vgl/vgl_point_2d.h>
00018 #include <vgl/vgl_tolerance.h>
00019 #include <vcl_cmath.h> // for std::sqrt()
00020 
00021 // The old signature vgl_lineseg_test() was incorrectly documented. Its
00022 // meaning was the same as the new vgl_lineseg_test_line(). Only you can
00023 // decide which one you want.
00024 
00025 //: true if the line joining [1], [2] meets the linesegment joining [3], [4].
00026 // End points are considered to belong to a line segment.
00027 export template <class T>
00028 bool vgl_lineseg_test_line(T x1, T y1, T x2, T y2, T x3, T y3, T x4, T y4);
00029 
00030 //: true if the linesegment joining [1], [2] meets the linesegment joining [3], [4].
00031 // End points are considered to belong to a line segment.
00032 export template <class T>
00033 bool vgl_lineseg_test_lineseg(T x1, T y1, T x2, T y2, T x3, T y3, T x4, T y4);
00034 
00035 //: true if the line meets the linesegment.
00036 // End points are considered to belong to a line segment.
00037 // \relatesalso vgl_line_2d
00038 // \relatesalso vgl_line_segment_2d
00039 template <class T>
00040 inline bool vgl_lineseg_test_line(vgl_line_2d<T> const& l1,
00041                                   vgl_line_segment_2d<T> const& l2)
00042 {
00043   vgl_point_2d<T> l1_p1,l1_p2;
00044   l1.get_two_points(l1_p1,l1_p2);
00045   return vgl_lineseg_test_line(l1_p1.x(),l1_p1.y(),
00046                                l1_p2.x(),l1_p2.y(),
00047                                l2.point1().x(),l2.point1().y(),
00048                                l2.point2().x(),l2.point2().y());
00049 }
00050 
00051 //: true if the point lies on the line segment and is between the endpoints
00052 // \relatesalso vgl_point_2d
00053 // \relatesalso vgl_line_segment_2d
00054 template <class T>
00055 inline bool vgl_lineseg_test_point(vgl_point_2d<T> const& p,
00056                                    vgl_line_segment_2d<T> const& lseg)
00057 {
00058   vgl_point_2d<T> p1 = lseg.point1(), p2 = lseg.point2();
00059   T x1 = p1.x(), y1 = p1.y(),
00060     x2 = p2.x(), y2 = p2.y(),
00061     xp = p.x(),  yp = p.y();
00062   // compute squared distances
00063   T d1p = (xp-x1)*(xp-x1) + (yp-y1)*(yp-y1);
00064   T d2p = (xp-x2)*(xp-x2) + (yp-y2)*(yp-y2);
00065   T d12 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
00066   double diff = vcl_sqrt(d1p) + vcl_sqrt(d2p) - vcl_sqrt(d12);
00067   // diff is always >= 0 (triangle inequality)
00068   return diff <= vgl_tolerance<double>::position;
00069 }
00070 
00071 //: return true if the two linesegments meet.
00072 // End points are considered to belong to a line segment.
00073 // \relatesalso vgl_line_segment_2d
00074 template <class T>
00075 inline bool vgl_lineseg_test_lineseg(vgl_line_segment_2d<T> const& l1,
00076                                      vgl_line_segment_2d<T> const& l2)
00077 {
00078   return vgl_lineseg_test_lineseg(l1.point1().x(),l1.point1().y(),
00079                                   l1.point2().x(),l1.point2().y(),
00080                                   l2.point1().x(),l2.point1().y(),
00081                                   l2.point2().x(),l2.point2().y());
00082 }
00083 
00084 #define VGL_LINESEG_TEST_INSTANTIATE(T) extern "please include vgl/vgl_lineseg_test.txx instead"
00085 
00086 #endif // vgl_lineseg_test_h_