contrib/gel/vsol/vsol_line_2d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_line_2d.h
00002 #ifndef vsol_line_2d_h_
00003 #define vsol_line_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Straight line segment in a 2D space.
00008 //
00009 // The direction gives the orientation and the length of the segment
00010 //
00011 // \author François BERTEL
00012 // \date   2000-04-28
00013 //
00014 // \verbatim
00015 //  Modifications
00016 //   2000-04-28 François BERTEL Creation
00017 //   2000-06-17 Peter Vanroose  Implemented all operator==()s and type info
00018 //   2001-07-03 Peter Vanroose  Added constructor from vgl_line_segment_2d
00019 //   2001-07-03 Peter Vanroose  Replaced vnl_double_2 by vgl_vector_2d
00020 //   2004-05-14 Peter Vanroose  Added describe()
00021 // \endverbatim
00022 //*****************************************************************************
00023 
00024 #include <vsol/vsol_curve_2d.h>
00025 #include <vsol/vsol_point_2d_sptr.h>
00026 #include <vgl/vgl_fwd.h> // vgl_line_segment_2d, vgl_homg_line_2d, vgl_point_2d
00027 #include <vgl/vgl_homg_line_2d.h>
00028 #include <vgl/vgl_line_segment_2d.h>
00029 #include <vsl/vsl_binary_io.h>
00030 #include <vcl_string.h>
00031 #include <vcl_iosfwd.h>
00032 
00033 class vsol_line_2d : public vsol_curve_2d
00034 {
00035   //***************************************************************************
00036   // Data members
00037   //***************************************************************************
00038 
00039   //---------------------------------------------------------------------------
00040   // Description: First point of the straight line segment
00041   //---------------------------------------------------------------------------
00042   vsol_point_2d_sptr p0_;
00043 
00044   //---------------------------------------------------------------------------
00045   // Description: Last point of the straight line segment
00046   //---------------------------------------------------------------------------
00047   vsol_point_2d_sptr p1_;
00048 
00049  public:
00050   //***************************************************************************
00051   // Initialization
00052   //***************************************************************************
00053 
00054   //---------------------------------------------------------------------------
00055   //: Default Constructor
00056   //---------------------------------------------------------------------------
00057   vsol_line_2d();
00058 
00059   //---------------------------------------------------------------------------
00060   //: Constructor from the direction and the middle point
00061   //---------------------------------------------------------------------------
00062   vsol_line_2d(vgl_vector_2d<double> const& new_direction,
00063                const vsol_point_2d_sptr &new_middle);
00064 
00065   //---------------------------------------------------------------------------
00066   //: Constructor from the direction and the middle point
00067   //---------------------------------------------------------------------------
00068   vsol_line_2d(vgl_vector_2d<double> const& new_direction,
00069                const vgl_point_2d<double> &new_middle);
00070 
00071   //---------------------------------------------------------------------------
00072   //: Constructor from the first and the last point of the straight line
00073   //---------------------------------------------------------------------------
00074   vsol_line_2d(vsol_point_2d_sptr const& new_p0,
00075                vsol_point_2d_sptr const& new_p1)
00076     : vsol_curve_2d(), p0_(new_p0), p1_(new_p1) {}
00077 
00078   //---------------------------------------------------------------------------
00079   //: Constructor from two vgl_point_2d (end points)
00080   //---------------------------------------------------------------------------
00081   vsol_line_2d(vgl_point_2d<double> const& p0, vgl_point_2d<double> const& p1);
00082 
00083   //---------------------------------------------------------------------------
00084   //: Constructor from a vgl_line_segment_2d
00085   //---------------------------------------------------------------------------
00086   vsol_line_2d(vgl_line_segment_2d<double> const& l);
00087 
00088   //---------------------------------------------------------------------------
00089   //: Copy constructor
00090   //  no duplication of the points
00091   //---------------------------------------------------------------------------
00092   vsol_line_2d(vsol_line_2d const& other)
00093     : vsol_curve_2d(other), p0_(other.p0_), p1_(other.p1_) {}
00094 
00095   //---------------------------------------------------------------------------
00096   //: Destructor
00097   //---------------------------------------------------------------------------
00098   virtual ~vsol_line_2d() {}
00099 
00100   //---------------------------------------------------------------------------
00101   //: Return `this' if `this' is a line_2d, 0 otherwise
00102   //---------------------------------------------------------------------------
00103   virtual vsol_line_2d const*cast_to_line()const{return this;}
00104   virtual vsol_line_2d *cast_to_line() {return this;}
00105 
00106  private: // has been superseded by is_a()
00107   //: Return the curve type
00108   virtual vsol_curve_2d_type curve_type() const { return vsol_curve_2d::LINE; }
00109 
00110  public:
00111   //---------------------------------------------------------------------------
00112   //: Clone `this': creation of a new object and initialization
00113   //  See Prototype pattern
00114   //---------------------------------------------------------------------------
00115   virtual vsol_spatial_object_2d* clone() const;
00116 
00117   //***************************************************************************
00118   // Access
00119   //***************************************************************************
00120 
00121   //---------------------------------------------------------------------------
00122   //: Middle point of the straight line segment
00123   //---------------------------------------------------------------------------
00124   vsol_point_2d_sptr middle() const;
00125 
00126   //---------------------------------------------------------------------------
00127   //: direction of the straight line segment.
00128   //---------------------------------------------------------------------------
00129   vgl_vector_2d<double> direction() const;
00130 
00131   //---------------------------------------------------------------------------
00132   //: First point of the straight line segment
00133   //---------------------------------------------------------------------------
00134   virtual vsol_point_2d_sptr p0() const { return p0_; }
00135 
00136   //---------------------------------------------------------------------------
00137   //: Last point of the straight line segment
00138   //---------------------------------------------------------------------------
00139   virtual vsol_point_2d_sptr p1() const { return p1_; }
00140 
00141   //---------------------------------------------------------------------------
00142   //: Get an unbounded vgl_homg_line_2d
00143   //---------------------------------------------------------------------------
00144   vgl_homg_line_2d<double> vgl_hline_2d() const;
00145 
00146   //---------------------------------------------------------------------------
00147   //: Get a vgl_line_segment_2d
00148   //---------------------------------------------------------------------------
00149   vgl_line_segment_2d<double> vgl_seg_2d() const;
00150 
00151   //***************************************************************************
00152   // Comparison
00153   //***************************************************************************
00154 
00155   //---------------------------------------------------------------------------
00156   //: Has `this' the same points than `other' ?
00157   //---------------------------------------------------------------------------
00158   virtual bool operator==(vsol_line_2d const& other) const;
00159   virtual bool operator==(vsol_spatial_object_2d const& obj) const; // virtual of vsol_spatial_object_2d
00160 
00161   //---------------------------------------------------------------------------
00162   //: Has `this' not the same points than `other' ?
00163   //---------------------------------------------------------------------------
00164   inline bool operator!=(vsol_line_2d const& o) const {return !operator==(o);}
00165 
00166   //***************************************************************************
00167   // Status report
00168   //***************************************************************************
00169 
00170   //---------------------------------------------------------------------------
00171   //: Compute the bounding box of `this'
00172   //---------------------------------------------------------------------------
00173   virtual void compute_bounding_box() const;
00174 
00175   //---------------------------------------------------------------------------
00176   //: Return the length of `this'
00177   //---------------------------------------------------------------------------
00178   virtual double length() const;
00179 
00180   //---------------------------------------------------------------------------
00181   //: Return the tangent angle (in degrees) of `this'. 0<angle<360
00182   //---------------------------------------------------------------------------
00183   double tangent_angle() const;
00184 
00185   //***************************************************************************
00186   // Status setting
00187   //***************************************************************************
00188 
00189   //---------------------------------------------------------------------------
00190   //: Set the first point of the straight line segment
00191   //---------------------------------------------------------------------------
00192   virtual void set_p0(vsol_point_2d_sptr const& new_p0);
00193 
00194   //---------------------------------------------------------------------------
00195   //: Set the last point of the straight line segment
00196   //---------------------------------------------------------------------------
00197   virtual void set_p1(vsol_point_2d_sptr const& new_p1);
00198 
00199   //---------------------------------------------------------------------------
00200   //: Set the length of `this'. Doesn't change middle point and orientation.
00201   //  If p0 and p1 are equal then the direction is set to (1,0)
00202   //  REQUIRE: new_length>=0
00203   //---------------------------------------------------------------------------
00204   void set_length(const double new_length);
00205 
00206   //***************************************************************************
00207   // Basic operations
00208   //***************************************************************************
00209 
00210   //---------------------------------------------------------------------------
00211   //: Is `p' in `this' ?
00212   //---------------------------------------------------------------------------
00213   virtual bool in(vsol_point_2d_sptr const& p) const;
00214 
00215   //---------------------------------------------------------------------------
00216   //: Return the tangent to `this' at `p'. Has to be deleted manually
00217   //  REQUIRE: in(p)
00218   //---------------------------------------------------------------------------
00219   virtual vgl_homg_line_2d<double>* tangent_at_point(vsol_point_2d_sptr const& p) const;
00220 
00221   // ==== Binary IO methods ======
00222 
00223   //: Binary save self to stream.
00224   void b_write(vsl_b_ostream &os) const;
00225 
00226   //: Binary load self from stream.
00227   void b_read(vsl_b_istream &is);
00228 
00229   //: Return IO version number;
00230   short version() const;
00231 
00232   //: Print an ascii summary to the stream
00233   void print_summary(vcl_ostream &os) const;
00234 
00235   //: Return a platform independent string identifying the class
00236   virtual vcl_string is_a() const { return vcl_string("vsol_line_2d"); }
00237 
00238   //: Return true if the argument matches the string identifying the class or any parent class
00239   virtual bool is_class(vcl_string const& cls) const { return cls==is_a(); }
00240 
00241   //---------------------------------------------------------------------------
00242   //: output description to stream
00243   //---------------------------------------------------------------------------
00244   void describe(vcl_ostream &strm, int blanking=0) const;
00245 };
00246 
00247 //: Binary save vsol_line_2d* to stream.
00248 void vsl_b_write(vsl_b_ostream &os, const vsol_line_2d* p);
00249 
00250 //: Binary load vsol_line_2d* from stream.
00251 void vsl_b_read(vsl_b_istream &is, vsol_line_2d* &p);
00252 
00253 #endif // vsol_line_2d_h_