core/vgl/vgl_line_segment_2d.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_line_segment_2d.h
00002 #ifndef vgl_line_segment_2d_h_
00003 #define vgl_line_segment_2d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author mccane@cs.otago.ac.nz: but copied from vgl_line_segment_3d
00010 //
00011 // \verbatim
00012 // Modifications
00013 // Peter Vanroose -  9 July 2001 - Inlined constructors
00014 // Peter Vanroose - 27 June 2001 - Added operator==
00015 // J.L. Mundy     - 13 April 2003 - Added angle and line coefficient functions
00016 // \endverbatim
00017 
00018 #include <vcl_iosfwd.h>
00019 #include <vgl/vgl_point_2d.h> // data member of this class
00020 
00021 //: Represents a 2D line segment using two points.
00022 template <class Type>
00023 class vgl_line_segment_2d
00024 {
00025   //: One end of line segment
00026   vgl_point_2d<Type> point1_;
00027   //: The other end of the line segment
00028   vgl_point_2d<Type> point2_;
00029 
00030  public:
00031   //: Default constructor - does not initialise!
00032   inline vgl_line_segment_2d() {}
00033 
00034   //: Copy constructor
00035   inline vgl_line_segment_2d(vgl_line_segment_2d<Type> const& l)
00036     : point1_(l.point1_), point2_(l.point2_) {}
00037 
00038   //: Construct from two end points
00039   inline vgl_line_segment_2d(vgl_point_2d<Type> const& p1,
00040                              vgl_point_2d<Type> const& p2)
00041     : point1_(p1), point2_(p2) {}
00042 
00043   //: Destructor
00044   inline ~vgl_line_segment_2d() {}
00045 
00046   //: One end-point of the line segment.
00047   inline vgl_point_2d<Type> point1() const { return point1_; } // return a copy
00048 
00049   //: The other end-point of the line segment.
00050   inline vgl_point_2d<Type> point2() const { return point2_; } // return a copy
00051 
00052   //: The equality comparison operator
00053   inline bool operator==(vgl_line_segment_2d<Type> const& l) const {
00054     return (this==&l) || (point1() == l.point1() && point2() == l.point2())
00055                       || (point1() == l.point2() && point2() == l.point1()); }
00056 
00057   //: The inequality comparison operator.
00058   inline bool operator!=(vgl_line_segment_2d<Type>const& other)const{return !operator==(other);}
00059 
00060   // A consistent interface with vgl_line_2d:
00061 
00062   //: Parameter a of line a*x + b*y + c = 0
00063   Type a() const;
00064 
00065   //: Parameter b of line a*x + b*y + c = 0
00066   Type b() const;
00067 
00068   //: Parameter c of line a*x + b*y + c = 0
00069   Type c() const;
00070 
00071   //: unit vector describing line direction
00072   vgl_vector_2d<Type> direction() const;
00073 
00074   //: unit vector orthogonal to line
00075   vgl_vector_2d<Type> normal() const;
00076 
00077   //: angle with the oriented horizontal line y=0, measured in radians.
00078   //  Returns values between -pi and pi.
00079   double slope_radians() const;
00080 
00081   //: angle with the oriented horizontal line y=0, measured in 360-degrees.
00082   //  Returns values between -180 and 180.
00083   double slope_degrees() const;
00084 
00085   //: Assignment
00086   inline void set(vgl_point_2d<Type> const& p1, vgl_point_2d<Type> const& p2) {
00087     point1_ = p1; point2_ = p2; }
00088 
00089   //: Return a point on the line defined by a scalar parameter \a t.
00090   // \a t=0.0 corresponds to point1 and \a t=1.0 to point2.
00091   // 0<t<1 for points on the segment between point1 and point2.
00092   // t<0 for points on the (infinite) line, outside the segment, and closer to point1 than to point2.
00093   // t>1 for points on the (infinite) line, outside the segment, and closer to point2 than to point1.
00094   inline vgl_point_2d<Type> point_t(const double t) const { return point1() + t*(point2_-point1_); }
00095 };
00096 
00097 //: Write to stream
00098 // \relatesalso vgl_line_segment_2d
00099 template <class Type>
00100 vcl_ostream&  operator<<(vcl_ostream& s, const vgl_line_segment_2d<Type>& p);
00101 
00102 //: Read from stream
00103 // \relatesalso vgl_line_segment_2d
00104 template <class Type>
00105 vcl_istream&  operator>>(vcl_istream& is,  vgl_line_segment_2d<Type>& p);
00106 
00107 #define VGL_LINE_SEGMENT_2D_INSTANTIATE(T) extern "please include vgl/vgl_line_segment_2d.txx first"
00108 
00109 #endif // vgl_line_segment_2d_h_