contrib/gel/vsol/vsol_polyline_2d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_polyline_2d.h
00002 #ifndef vsol_polyline_2d_h_
00003 #define vsol_polyline_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Generic polyline in 2D for drawing simple curves
00008 //
00009 // This class inherits from vsol_curve_2d.
00010 //
00011 // \author Amir Tamrakar
00012 // \date   2002-04-22
00013 //
00014 // \verbatim
00015 //  Modifications
00016 //   2002-04-22 Amir Tamrakar Creation
00017 //   2004-05-09 Joseph Mundy Added Binary I/O
00018 // \endverbatim
00019 //*****************************************************************************
00020 
00021 #include <vsol/vsol_curve_2d.h>
00022 #include <vsol/vsol_point_2d_sptr.h>
00023 #include <vsl/vsl_binary_io.h>
00024 #include <vcl_vector.h>
00025 #include <vcl_string.h>
00026 #include <vcl_iosfwd.h>
00027 
00028 //: General Polyline class, part of the vsol_curve_2d hierarchy
00029 
00030 class vsol_polyline_2d : public vsol_curve_2d
00031 {
00032  protected:
00033   //***************************************************************************
00034   // Data members
00035   //***************************************************************************
00036 
00037   //---------------------------------------------------------------------------
00038   // Description: List of vsol_point_2d
00039   //---------------------------------------------------------------------------
00040   vcl_vector<vsol_point_2d_sptr> *storage_;
00041 
00042     //---------------------------------------------------------------------------
00043   //: First point of the curve : just to conform to vsol_curve_2d standard
00044   //---------------------------------------------------------------------------
00045   vsol_point_2d_sptr p0_;
00046 
00047   //---------------------------------------------------------------------------
00048   //: Last point of the curve
00049   //---------------------------------------------------------------------------
00050   vsol_point_2d_sptr p1_;
00051 
00052  public:
00053 
00054   //***************************************************************************
00055   // Initialization
00056   //***************************************************************************
00057 
00058   //---------------------------------------------------------------------------
00059   //: Default Constructor
00060   //---------------------------------------------------------------------------
00061   vsol_polyline_2d();
00062 
00063   //---------------------------------------------------------------------------
00064   //: Constructor from a vcl_vector of points
00065   //---------------------------------------------------------------------------
00066   vsol_polyline_2d(vcl_vector<vsol_point_2d_sptr> const& new_vertices);
00067 
00068   //---------------------------------------------------------------------------
00069   //: Copy constructor
00070   //---------------------------------------------------------------------------
00071   vsol_polyline_2d(vsol_polyline_2d const& other);
00072 
00073   //---------------------------------------------------------------------------
00074   //: Destructor
00075   //---------------------------------------------------------------------------
00076   virtual ~vsol_polyline_2d();
00077 
00078   //---------------------------------------------------------------------------
00079   //: Clone `this': creation of a new object and initialization
00080   // See Prototype pattern
00081   //---------------------------------------------------------------------------
00082   virtual vsol_spatial_object_2d* clone() const;
00083 
00084   //***************************************************************************
00085   // Access
00086   //***************************************************************************
00087 
00088   //---------------------------------------------------------------------------
00089   //: Return the first point of `this';  pure virtual of vsol_curve_2d
00090   //---------------------------------------------------------------------------
00091   virtual vsol_point_2d_sptr p0() const { return p0_; }
00092 
00093   //---------------------------------------------------------------------------
00094   //: Return the last point of `this';  pure virtual of vsol_curve_2d
00095   //---------------------------------------------------------------------------
00096   virtual vsol_point_2d_sptr p1() const { return p1_; }
00097 
00098   //---------------------------------------------------------------------------
00099   //: Return vertex `i'
00100   //  REQUIRE: valid_index(i)
00101   //---------------------------------------------------------------------------
00102   vsol_point_2d_sptr vertex(const int i) const;
00103 
00104   //***************************************************************************
00105   // Comparison
00106   //***************************************************************************
00107 
00108   //---------------------------------------------------------------------------
00109   //: Has `this' the same points than `other' in the same order ?
00110   //---------------------------------------------------------------------------
00111   virtual bool operator==(vsol_polyline_2d const& other) const;
00112   virtual bool operator==(vsol_spatial_object_2d const& obj) const; // virtual of vsol_spatial_object_2d
00113 
00114   //---------------------------------------------------------------------------
00115   //: Has `this' the same points than `other' in the same order ?
00116   //---------------------------------------------------------------------------
00117   inline bool operator!=(vsol_polyline_2d const& o) const {return !operator==(o);}
00118 
00119 
00120   //***************************************************************************
00121   // Status setting
00122   //***************************************************************************
00123 
00124   //---------------------------------------------------------------------------
00125   //: Set the first point of the curve
00126   //  REQUIRE: in(new_p0)
00127   //---------------------------------------------------------------------------
00128   virtual void set_p0(vsol_point_2d_sptr const& new_p0);
00129 
00130   //---------------------------------------------------------------------------
00131   //: Set the last point of the curve
00132   //  REQUIRE: in(new_p1)
00133   //---------------------------------------------------------------------------
00134   virtual void set_p1(vsol_point_2d_sptr const& new_p1);
00135 
00136   //---------------------------------------------------------------------------
00137   //: Add another point to the curve
00138   //---------------------------------------------------------------------------
00139   void add_vertex(vsol_point_2d_sptr const& new_p);
00140 
00141   //***************************************************************************
00142   // Status report
00143   //***************************************************************************
00144 
00145   //---------------------------------------------------------------------------
00146   //: Return `this' if `this' is a polyline, 0 otherwise
00147   //---------------------------------------------------------------------------
00148   virtual vsol_polyline_2d const*cast_to_polyline()const{return this;}
00149   virtual vsol_polyline_2d *cast_to_polyline() {return this;}
00150 
00151  private: // has been superseded by is_a()
00152   //: Return the curve type
00153   virtual vsol_curve_2d_type curve_type() const { return vsol_curve_2d::POLYLINE; }
00154 
00155  public:
00156   //---------------------------------------------------------------------------
00157   //: Return the length of `this'
00158   //---------------------------------------------------------------------------
00159   virtual double length() const; // pure virtual of vsol_curve_2d
00160 
00161   //---------------------------------------------------------------------------
00162   //: Compute the bounding box of `this'
00163   //---------------------------------------------------------------------------
00164   virtual void compute_bounding_box() const;
00165 
00166   //---------------------------------------------------------------------------
00167   //: Return the number of vertices
00168   //---------------------------------------------------------------------------
00169   unsigned int size() const { return storage_->size(); }
00170 
00171   //---------------------------------------------------------------------------
00172   //: Is `i' a valid index for the list of vertices ?
00173   //---------------------------------------------------------------------------
00174   bool valid_index(unsigned int i) const { return i<storage_->size(); }
00175 
00176   //***************************************************************************
00177   // Basic operations
00178   //***************************************************************************
00179 
00180   //---------------------------------------------------------------------------
00181   //: output description to stream
00182   //---------------------------------------------------------------------------
00183   void describe(vcl_ostream &strm, int blanking=0) const;
00184 
00185   // ==== Binary IO methods ======
00186 
00187   //: Binary save self to stream.
00188   void b_write(vsl_b_ostream &os) const;
00189 
00190   //: Binary load self from stream.
00191   void b_read(vsl_b_istream &is);
00192 
00193   //: Return IO version number;
00194   short version() const;
00195 
00196   //: Print an ascii summary to the stream
00197   void print_summary(vcl_ostream &os) const;
00198 
00199   //: Return a platform independent string identifying the class
00200   virtual vcl_string is_a() const { return vcl_string("vsol_polyline_2d"); }
00201 
00202   //: Return true if the argument matches the string identifying the class or any parent class
00203   virtual bool is_class(vcl_string const& cls) const { return cls==is_a(); }
00204 };
00205 
00206 //: Binary save vsol_polyline_2d* to stream.
00207 void vsl_b_write(vsl_b_ostream &os, const vsol_polyline_2d* p);
00208 
00209 //: Binary load vsol_polyline_2d* from stream.
00210 void vsl_b_read(vsl_b_istream &is, vsol_polyline_2d* &p);
00211 
00212 #endif // vsol_polyline_2d_h_