contrib/gel/vsol/vsol_polygon_2d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_polygon_2d.h
00002 #ifndef vsol_polygon_2d_h_
00003 #define vsol_polygon_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Polygon in 2D space
00008 //
00009 // The vertices are to be defined in counterclockwise order.
00010 //
00011 // \author François BERTEL
00012 // \date   2000-05-09
00013 //
00014 // \verbatim
00015 //  Modifications
00016 //   2000-05-09 François BERTEL Creation
00017 //   2000-06-17 Peter Vanroose  Implemented all operator==()s and type info
00018 //   2001-07-03 Peter Vanroose  Corrected the implementation of is_convex()
00019 //   2003-11-05 Amir Tamrakar   Added Safe casting methods
00020 //   2004-05-01 Joseph Mundy    Added binary I/O
00021 //   2004-05-14 Peter Vanroose  Added describe()
00022 // \endverbatim
00023 //*****************************************************************************
00024 
00025 //*****************************************************************************
00026 // External declarations for values
00027 //*****************************************************************************
00028 #include <vsol/vsol_region_2d.h>
00029 #include <vsol/vsol_point_2d_sptr.h>
00030 #include <vsl/vsl_binary_io.h>
00031 #include <vcl_vector.h>
00032 #include <vcl_string.h>
00033 #include <vcl_iosfwd.h>
00034 class vsol_triangle_2d;
00035 class vsol_rectangle_2d;
00036 
00037 class vsol_polygon_2d : public vsol_region_2d
00038 {
00039  protected:
00040   //***************************************************************************
00041   // Data members
00042   //***************************************************************************
00043 
00044   //---------------------------------------------------------------------------
00045   // Description: List of vertices
00046   //---------------------------------------------------------------------------
00047   vcl_vector<vsol_point_2d_sptr> *storage_;
00048 
00049   //***************************************************************************
00050   // Initialization
00051   //***************************************************************************
00052  public:
00053   //---------------------------------------------------------------------------
00054   //: Default constructor.
00055   //---------------------------------------------------------------------------
00056   vsol_polygon_2d(void);
00057 
00058   //---------------------------------------------------------------------------
00059   //: Constructor from a vcl_vector (not a geometric vector but a list of points)
00060   //  REQUIRE: new_vertices.size()>=3
00061   //---------------------------------------------------------------------------
00062   explicit vsol_polygon_2d(const vcl_vector<vsol_point_2d_sptr> &new_vertices);
00063 
00064   //---------------------------------------------------------------------------
00065   //: Copy constructor
00066   //---------------------------------------------------------------------------
00067   vsol_polygon_2d(const vsol_polygon_2d &other);
00068 
00069   //---------------------------------------------------------------------------
00070   //: Destructor
00071   //---------------------------------------------------------------------------
00072   virtual ~vsol_polygon_2d();
00073 
00074   //---------------------------------------------------------------------------
00075   //: Clone `this': creation of a new object and initialization
00076   //  See Prototype pattern
00077   //---------------------------------------------------------------------------
00078   virtual vsol_spatial_object_2d* clone(void) const;
00079 
00080   //---------------------------------------------------------------------------
00081   //: Safe casting
00082   //---------------------------------------------------------------------------
00083 
00084   virtual vsol_polygon_2d* cast_to_polygon(void);
00085   virtual const vsol_polygon_2d* cast_to_polygon(void) const;
00086 
00087   virtual vsol_triangle_2d* cast_to_triangle(void);
00088   virtual const vsol_triangle_2d* cast_to_triangle(void) const;
00089 
00090   virtual vsol_rectangle_2d* cast_to_rectangle(void);
00091   virtual const vsol_rectangle_2d* cast_to_rectangle(void) const;
00092 
00093   //***************************************************************************
00094   // Access
00095   //***************************************************************************
00096 
00097   //---------------------------------------------------------------------------
00098   //: Return vertex `i'
00099   //  REQUIRE: valid_index(i)
00100   //---------------------------------------------------------------------------
00101   vsol_point_2d_sptr vertex(const int i) const;
00102 
00103   //***************************************************************************
00104   // Comparison
00105   //***************************************************************************
00106 
00107   //---------------------------------------------------------------------------
00108   //: Has `this' the same points than `other' in the same order ?
00109   //---------------------------------------------------------------------------
00110   virtual bool operator==(const vsol_polygon_2d &other) const;
00111   virtual bool operator==(const vsol_spatial_object_2d& obj) const; // virtual of vsol_spatial_object_2d
00112 
00113   //---------------------------------------------------------------------------
00114   //: Has `this' not the same points than `other' in the same order ?
00115   //---------------------------------------------------------------------------
00116   inline bool operator!=(const vsol_polygon_2d &o)const{return !operator==(o);}
00117 
00118   //***************************************************************************
00119   // Status report
00120   //***************************************************************************
00121 
00122   //---------------------------------------------------------------------------
00123   //: Return the region type of a polygon.  Its spatial type is a REGION
00124   //---------------------------------------------------------------------------
00125   vsol_region_2d_type region_type(void) const { return vsol_region_2d::POLYGON; }
00126 
00127   //---------------------------------------------------------------------------
00128   //: Compute the bounding box of `this'
00129   //---------------------------------------------------------------------------
00130   virtual void compute_bounding_box(void) const;
00131 
00132   //---------------------------------------------------------------------------
00133   //: Return the number of vertices
00134   //---------------------------------------------------------------------------
00135   unsigned int size(void) const { return storage_->size(); }
00136 
00137   //---------------------------------------------------------------------------
00138   //: Return the area of `this'
00139   //---------------------------------------------------------------------------
00140   virtual double area(void) const; // virtual of vsol_region_2d
00141 
00142   //---------------------------------------------------------------------------
00143   //: Return the centroid of `this'
00144   //---------------------------------------------------------------------------
00145   virtual vsol_point_2d_sptr centroid(void) const;
00146 
00147   //---------------------------------------------------------------------------
00148   //: Is `this' convex ?
00149   //---------------------------------------------------------------------------
00150   virtual bool is_convex(void) const;
00151 
00152   //---------------------------------------------------------------------------
00153   //: Is `i' a valid index for the list of vertices ?
00154   //---------------------------------------------------------------------------
00155   bool valid_index(unsigned int i) const { return i<storage_->size(); }
00156 
00157   //---------------------------------------------------------------------------
00158   //: Are `new_vertices' valid vertices to build a polygon of the current type?
00159   //  All vertex sets are valid for a general polygon.
00160   //---------------------------------------------------------------------------
00161   virtual bool valid_vertices(const vcl_vector<vsol_point_2d_sptr> ) const;
00162 
00163 
00164   // ==== Binary IO methods ======
00165 
00166   //: Binary save self to stream.
00167   void b_write(vsl_b_ostream &os) const;
00168 
00169   //: Binary load self from stream.
00170   void b_read(vsl_b_istream &is);
00171 
00172   //: Return IO version number;
00173   short version() const;
00174 
00175   //: Print an ascii summary to the stream
00176   void print_summary(vcl_ostream &os) const;
00177 
00178   //: Return a platform independent string identifying the class
00179   virtual vcl_string is_a() const { return vcl_string("vsol_polygon_2d"); }
00180 
00181   //: Return true if the argument matches the string identifying the class or any parent class
00182   virtual bool is_class(vcl_string const& cls) const
00183   { return cls==is_a() || vsol_region_2d::is_class(cls); }
00184 
00185   //---------------------------------------------------------------------------
00186   //: output description to stream
00187   //---------------------------------------------------------------------------
00188   void describe(vcl_ostream &strm, int blanking=0) const;
00189 };
00190 
00191 //: Binary save vsol_polygon_2d* to stream.
00192 void vsl_b_write(vsl_b_ostream &os, const vsol_polygon_2d* p);
00193 
00194 //: Binary load vsol_polygon_2d* from stream.
00195 void vsl_b_read(vsl_b_istream &is, vsol_polygon_2d* &p);
00196 
00197 #endif // vsol_polygon_2d_h_