contrib/gel/vsol/vsol_poly_set_2d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_poly_set_2d.h
00002 #ifndef vsol_poly_set_2d_h_
00003 #define vsol_poly_set_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Generic poly_set 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_region_2d.h>
00022 #include <vsol/vsol_point_2d_sptr.h>
00023 #include <vsol/vsol_polygon_2d_sptr.h>
00024 #include <vsl/vsl_binary_io.h>
00025 #include <vcl_vector.h>
00026 #include <vcl_string.h>
00027 #include <vcl_iosfwd.h>
00028 
00029 //: General poly_set class, part of the vsol_curve_2d hierarchy
00030 
00031 class vsol_poly_set_2d : public vsol_region_2d
00032 {
00033  protected:
00034   //***************************************************************************
00035   // Data members
00036   //***************************************************************************
00037 
00038   //---------------------------------------------------------------------------
00039   // Description: List of vsol_polygon_2d
00040   //---------------------------------------------------------------------------
00041   vcl_vector<vsol_polygon_2d_sptr> *storage_;
00042 
00043  public:
00044 
00045   //***************************************************************************
00046   // Initialization
00047   //***************************************************************************
00048 
00049   //---------------------------------------------------------------------------
00050   //: Default Constructor
00051   //---------------------------------------------------------------------------
00052   vsol_poly_set_2d();
00053 
00054   //---------------------------------------------------------------------------
00055   //: Constructor from a vcl_vector of points
00056   //---------------------------------------------------------------------------
00057   vsol_poly_set_2d(vcl_vector<vsol_polygon_2d_sptr> const& new_polys);
00058 
00059   //---------------------------------------------------------------------------
00060   //: Copy constructor
00061   //---------------------------------------------------------------------------
00062   vsol_poly_set_2d(vsol_poly_set_2d const& other);
00063 
00064   //---------------------------------------------------------------------------
00065   //: Destructor
00066   //---------------------------------------------------------------------------
00067   virtual ~vsol_poly_set_2d();
00068 
00069   //---------------------------------------------------------------------------
00070   //: Clone `this': creation of a new object and initialization
00071   // See Prototype pattern
00072   //---------------------------------------------------------------------------
00073   virtual vsol_spatial_object_2d* clone() const;
00074 
00075   //***************************************************************************
00076   // Access
00077   //***************************************************************************
00078 
00079   //---------------------------------------------------------------------------
00080   //: Return vertex `i'
00081   //  REQUIRE: valid_index(i)
00082   //---------------------------------------------------------------------------
00083   vsol_polygon_2d_sptr poly(const int i) const;
00084 
00085   //***************************************************************************
00086   // Comparison
00087   //***************************************************************************
00088 
00089   //---------------------------------------------------------------------------
00090   //: Has `this' the same points than `other' in the same order ?
00091   //---------------------------------------------------------------------------
00092   virtual bool operator==(vsol_poly_set_2d const& other) const;
00093   virtual bool operator==(vsol_spatial_object_2d const& obj) const; // virtual of vsol_spatial_object_2d
00094 
00095   //---------------------------------------------------------------------------
00096   //: Has `this' the same points than `other' in the same order ?
00097   //---------------------------------------------------------------------------
00098   inline bool operator!=(vsol_poly_set_2d const& o) const {return !operator==(o);}
00099 
00100   //---------------------------------------------------------------------------
00101   //: Add another polygon to the curve
00102   //---------------------------------------------------------------------------
00103   void add_poly(vsol_polygon_2d_sptr const& new_p);
00104 
00105   //***************************************************************************
00106   // Status report
00107   //***************************************************************************
00108   //---------------------------------------------------------------------------
00109   //: Return the region type of a polygon.  Its spatial type is a REGION
00110   //---------------------------------------------------------------------------
00111   vsol_region_2d_type region_type(void) const { return vsol_region_2d::POLYGON_SET; }
00112   //---------------------------------------------------------------------------
00113   //: Return `this' if `this' is a poly_set, 0 otherwise
00114   //---------------------------------------------------------------------------
00115   virtual vsol_poly_set_2d const*cast_to_poly_set()const{return this;}
00116   virtual vsol_poly_set_2d *cast_to_poly_set() {return this;}
00117 
00118  public:
00119 
00120   //---------------------------------------------------------------------------
00121   //: Compute the bounding box of `this'
00122   //---------------------------------------------------------------------------
00123   virtual void compute_bounding_box() const;
00124 
00125   //---------------------------------------------------------------------------
00126   //: Return the number of vertices
00127   //---------------------------------------------------------------------------
00128   unsigned int size() const { return storage_->size(); }
00129 
00130   //---------------------------------------------------------------------------
00131   //: Is `i' a valid index for the list of vertices ?
00132   //---------------------------------------------------------------------------
00133   bool valid_index(unsigned int i) const { return i<storage_->size(); }
00134 
00135   //---------------------------------------------------------------------------
00136   //: Return the area of `this'
00137   //---------------------------------------------------------------------------
00138   virtual double area(void) const; // virtual of vsol_region_2d
00139 
00140   //---------------------------------------------------------------------------
00141   //: Return the centroid of `this'
00142   //---------------------------------------------------------------------------
00143   virtual vsol_point_2d_sptr centroid(void) const;
00144 
00145   //---------------------------------------------------------------------------
00146   //: Is `this' convex ?
00147   //---------------------------------------------------------------------------
00148   virtual bool is_convex(void) const;
00149   //***************************************************************************
00150   // Basic operations
00151   //***************************************************************************
00152 
00153   //---------------------------------------------------------------------------
00154   //: output description to stream
00155   //---------------------------------------------------------------------------
00156   void describe(vcl_ostream &strm, int blanking=0) const;
00157 
00158   // ==== Binary IO methods ======
00159 
00160   //: Binary save self to stream.
00161   void b_write(vsl_b_ostream &os) const;
00162 
00163   //: Binary load self from stream.
00164   void b_read(vsl_b_istream &is);
00165 
00166   //: Return IO version number;
00167   short version() const;
00168 
00169   //: Print an ascii summary to the stream
00170   void print_summary(vcl_ostream &os) const;
00171 
00172   //: Return a platform independent string identifying the class
00173   virtual vcl_string is_a() const { return vcl_string("vsol_poly_set_2d"); }
00174 
00175   //: Return true if the argument matches the string identifying the class or any parent class
00176   virtual bool is_class(vcl_string const& cls) const { return cls==is_a(); }
00177 };
00178 
00179 //: Binary save vsol_polyline_2d* to stream.
00180 void vsl_b_write(vsl_b_ostream &os, const vsol_poly_set_2d* p);
00181 
00182 //: Binary load vsol_polyline_2d* from stream.
00183 void vsl_b_read(vsl_b_istream &is, vsol_poly_set_2d* &p);
00184 
00185 #endif // vsol_poly_set_2d_h_