core/vgl/vgl_line_segment_3d.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_line_segment_3d.h
00002 #ifndef vgl_line_segment_3d_h_
00003 #define vgl_line_segment_3d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief line segment in 3D nonhomogeneous space
00010 // \author  awf@robots.ox.ac.uk
00011 //
00012 // \verbatim
00013 // Modifications
00014 // Peter Vanroose -  9 July 2001 - Inlined constructors
00015 // Peter Vanroose - 27 June 2001 - Added operator==
00016 // Kieran O'Mahony - 13 Aug 2007 - Added contains()
00017 // \endverbatim
00018 
00019 #include <vcl_iosfwd.h>
00020 #include <vgl/vgl_point_3d.h> // data member of this class
00021 
00022 //: Represents a 3D line segment using two points.
00023 template <class Type>
00024 class vgl_line_segment_3d
00025 {
00026   vgl_point_3d<Type> point1_;
00027   vgl_point_3d<Type> point2_;
00028 
00029  public:
00030   //: Default constructor - does not initialise!
00031   inline vgl_line_segment_3d() {}
00032 
00033   //: Copy constructor
00034   inline vgl_line_segment_3d(vgl_line_segment_3d<Type> const& l)
00035     : point1_(l.point1()), point2_(l.point2()) {}
00036 
00037   //: Construct from two end points
00038   inline vgl_line_segment_3d(vgl_point_3d<Type> const& p1,
00039                              vgl_point_3d<Type> const& p2)
00040     : point1_(p1), point2_(p2) {}
00041 
00042   inline ~vgl_line_segment_3d() {}
00043 
00044   inline vgl_point_3d<Type> point1() const { return point1_; } // return a copy
00045   inline vgl_point_3d<Type> point2() const { return point2_; } // return a copy
00046 
00047   //: the comparison operator
00048   inline bool operator==(vgl_line_segment_3d<Type> const& l) const {
00049     return (this==&l) || (point1() == l.point1() && point2() == l.point2())
00050                       || (point1() == l.point2() && point2() == l.point1()); }
00051 
00052   inline bool operator!=(vgl_line_segment_3d<Type>const& other)const{return !operator==(other);}
00053 
00054   //: assignment
00055   inline void set(vgl_point_3d<Type> const& p1, vgl_point_3d<Type> const& p2) { point1_ = p1; point2_ = p2; }
00056 
00057   //: Return the direction vector of this line (not normalised - but perhaps it should be, like other line classes?)
00058   inline vgl_vector_3d<Type> direction() const { return point2()-point1(); }
00059   
00060   //: Return a point on the line defined by a scalar parameter \a t.
00061   // \a t=0.0 corresponds to point1 and \a t=1.0 to point2.
00062   // 0<t<1 for points on the segment between point1 and point2.
00063   // t<0 for points on the (infinite) line, outside the segment, and closer to point1 than to point2.
00064   // t>1 for points on the (infinite) line, outside the segment, and closer to point2 than to point1.
00065   //\note Assumes that direction() is not normalized.
00066   inline vgl_point_3d<Type> point_t(const double t) const { return point1() + t*direction(); }
00067   
00068   //: Check if point \a p is on the line segment
00069   inline bool contains(const vgl_point_3d<Type>& p ) const
00070   {
00071     double r = (point1_ - point2_).length() - ( (point1_ - p).length() + (point2_ - p).length() );    
00072     return r < 1e-8 && r > -1e-8;
00073   }
00074 };
00075 
00076 //: Write to stream
00077 // \relatesalso vgl_line_segment_3d
00078 template <class Type>
00079 vcl_ostream&  operator<<(vcl_ostream& s, const vgl_line_segment_3d<Type>& p);
00080 
00081 //: Read from stream
00082 // \relatesalso vgl_line_segment_3d
00083 template <class Type>
00084 vcl_istream&  operator>>(vcl_istream& is,  vgl_line_segment_3d<Type>& p);
00085 #define VGL_LINE_SEGMENT_3D_INSTANTIATE(T) extern "please include vgl/vgl_line_segment_3d.txx first"
00086 
00087 #endif // vgl_line_segment_3d_h_