core/vgl/vgl_line_3d_2_points.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_line_3d_2_points.h
00002 #ifndef vgl_line_3d_2_points_h_
00003 #define vgl_line_3d_2_points_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief non-homogeneous 3D line, represented by 2 points.
00010 // \author Peter Vanroose
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   Gamze Tunali    26 Jan 2007: Deprecated intersection(). Moved into vgl_intersection.
00015 //   Peter Vanroose  30 Mar 2007: Commented out deprecated intersection() functions.
00016 // \endverbatim
00017 
00018 #include <vcl_iosfwd.h>
00019 #include <vcl_cassert.h>
00020 #include <vgl/vgl_point_3d.h> // data member of this class
00021 #include <vgl/vgl_vector_3d.h>
00022 
00023 //: A class to hold a non-homogeneous representation of a 3D line.
00024 // The line is stored as a pair of non-homogeneous 3D points.
00025 template <class Type>
00026 class vgl_line_3d_2_points
00027 {
00028   // Data Members------------------------------------------------------------
00029 
00030   //: Any point on the line
00031   vgl_point_3d<Type> point1_;
00032   //: Any other point on the line
00033   vgl_point_3d<Type> point2_;
00034 
00035  public:
00036   //+**************************************************************************
00037   // Initialization
00038   //+**************************************************************************
00039 
00040   //: Default constructor with (0,0,0) and (1,0,0), which is the line \a y=z=0
00041   inline vgl_line_3d_2_points(void)
00042   : point1_(0,0,0), point2_(1,0,0) {}
00043 
00044   //: Copy constructor
00045   inline vgl_line_3d_2_points(const vgl_line_3d_2_points<Type> &that)
00046   : point1_(that.point1_), point2_(that.point2_) {}
00047 
00048   //: Construct from two points
00049   inline vgl_line_3d_2_points(vgl_point_3d<Type> const& p1,
00050                               vgl_point_3d<Type> const& p2)
00051   : point1_(p1), point2_(p2) {assert(p1!=p2);}
00052 
00053   //: comparison
00054   bool operator==(vgl_line_3d_2_points<Type> const& l) const;
00055   bool operator!=(vgl_line_3d_2_points<Type> const& l) const{return !operator==(l);}
00056 
00057   // Data access
00058 
00059   //: Return the first point representing this line
00060   inline vgl_point_3d<Type> point1() const {return point1_;}
00061   //: Return the second point representing this line
00062   inline vgl_point_3d<Type> point2() const{ return point2_;}
00063 
00064   //: Assignment
00065   inline void set(vgl_point_3d<Type> const& p1, vgl_point_3d<Type> const& p2)
00066   { assert(p1!=p2); point1_ = p1; point2_ = p2; }
00067 
00068   // Utility methods
00069 
00070   //: Return true iff line is at infinity (which is always false)
00071   inline bool ideal(Type  /*tol*/ = (Type)0) const { return false; }
00072 
00073   //: Return the direction vector of this line (not normalised - but perhaps it should be, like other line classes?)
00074   inline vgl_vector_3d<Type> direction() const { return point2()-point1(); }
00075 
00076   //: Return a point on the line defined by a scalar parameter \a t such that \a t=0.0 at point1 and \a t=1.0 at point2.
00077   //\note Assumes that direction() is not normalized.
00078   inline vgl_point_3d<Type> point_t(const double t) const { return point1() + t*direction(); }
00079 };
00080 
00081 #define l vgl_line_3d_2_points<Type>
00082 
00083 //: Return true iff line is at infinity (which is always false)
00084 // \relatesalso vgl_line_3d_2_points
00085 template <class Type>
00086 inline bool is_ideal(l const&, Type=(Type)0) { return false; }
00087 
00088 //: Does a line pass through a point, i.e., are the point and the line collinear?
00089 // \relatesalso vgl_line_3d_2_points
00090 // \relatesalso vgl_point_3d
00091 template <class Type>
00092 inline bool collinear(l const& l1, vgl_point_3d<Type> const& p)
00093 {
00094   return collinear(l1.point1(),l1.point2(),p);
00095 }
00096 
00097 //: Are two lines coplanar, i.e., do they either intersect or are parallel?
00098 // \relatesalso vgl_line_3d_2_points
00099 template <class Type>
00100 inline bool coplanar(l const& l1, l const& l2)
00101 { return coplanar(l1.point1(),l1.point2(),l2.point1(),l2.point2()); }
00102 
00103 //: Are two lines concurrent, i.e., do they intersect in a finite point?
00104 // \relatesalso vgl_line_3d_2_points
00105 template <class Type>
00106 inline bool concurrent(l const& l1, l const& l2)
00107 {
00108   return coplanar(l1,l2) && !parallel(l1.direction(),l2.direction());
00109 }
00110 
00111 //: Are two points coplanar with a line?
00112 // \relatesalso vgl_line_3d_2_points
00113 // \relatesalso vgl_point_3d
00114 template <class Type>
00115 inline bool coplanar(l const& l1, vgl_point_3d<Type> const& p1, vgl_point_3d<Type> const& p2)
00116 { return coplanar(l1.point1(),l1.point2(),p1,p2); }
00117 
00118 //: Are three lines coplanar, i.e., are they in a common plane?
00119 // \relatesalso vgl_line_3d_2_points
00120 template <class Type>
00121 inline bool coplanar(l const& l1, l const& l2, l const& l3)
00122 {
00123   vgl_point_3d<Type> p = l2.point1();
00124   if (collinear(l1,p)) p = l2.point2();
00125   return coplanar(l1,l2) && coplanar(l1,l3) &&
00126          coplanar(l1,p,l3.point1()) && coplanar(l1,p,l3.point2());
00127 }
00128 
00129 #if 0 // deprecated
00130 //: Return the intersection point of two concurrent lines
00131 // \relatesalso vgl_line_3d_2_points
00132 // \deprecated in favour of vgl_intersection.
00133 // Can be removed after the release of VXL 1.8
00134 template <class Type>
00135 vgl_point_3d<Type> intersection(l const& l1, l const& l2)
00136 { return vgl_intersection(l1, l2); }
00137 
00138 //: Return the intersection point of a line and a plane.
00139 // \relatesalso vgl_line_3d_2_points
00140 // \deprecated in favour of vgl_intersection.
00141 // Can be removed after the release of VXL 1.8
00142 template <class Type>
00143 vgl_point_3d<Type> intersection(l const& line, vgl_plane_3d<Type> const& plane)
00144 { return vgl_intersection(line, plane); }
00145 #endif // 0
00146 
00147 //: Are three lines concurrent, i.e., do they pass through a common point?
00148 // \relatesalso vgl_line_3d_2_points
00149 template <class Type>
00150 inline bool concurrent(l const& l1, l const& l2, l const& l3)
00151 {
00152   if (!concurrent(l1,l2) || !concurrent(l1,l3) || !concurrent(l2,l3)) return false;
00153   return vgl_intersection(l1,l2) == vgl_intersection(l1,l3);
00154 }
00155 
00156 //+****************************************************************************
00157 // stream operators
00158 //+****************************************************************************
00159 
00160 //: Write to stream (verbose)
00161 // \relatesalso vgl_line_3d_2_points
00162 template <class Type>
00163 vcl_ostream &operator<<(vcl_ostream&s, l const& );
00164 
00165 //: Read parameters from stream
00166 // \relatesalso vgl_line_3d_2_points
00167 template <class Type>
00168 vcl_istream &operator>>(vcl_istream &is, l &);
00169 
00170 #undef l
00171 
00172 #define VGL_LINE_3D_2_POINTS_INSTANTIATE(T) extern "please include vgl/vgl_line_3d_2_points.txx first"
00173 
00174 #endif // vgl_line_3d_2_points_h_