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 <vcl_vector.h> 00031 #include <vsl/vsl_binary_io.h> 00032 class vsol_triangle_2d; 00033 class vsol_rectangle_2d; 00034 00035 class vsol_polygon_2d : public vsol_region_2d 00036 { 00037 protected: 00038 //*************************************************************************** 00039 // Data members 00040 //*************************************************************************** 00041 00042 //--------------------------------------------------------------------------- 00043 // Description: List of vertices 00044 //--------------------------------------------------------------------------- 00045 vcl_vector<vsol_point_2d_sptr> *storage_; 00046 00047 //*************************************************************************** 00048 // Initialization 00049 //*************************************************************************** 00050 public: 00051 //--------------------------------------------------------------------------- 00052 //: Default constructor. 00053 //--------------------------------------------------------------------------- 00054 vsol_polygon_2d(void); 00055 00056 //--------------------------------------------------------------------------- 00057 //: Constructor from a vcl_vector (not a geometric vector but a list of points) 00058 // REQUIRE: new_vertices.size()>=3 00059 //--------------------------------------------------------------------------- 00060 explicit vsol_polygon_2d(const vcl_vector<vsol_point_2d_sptr> &new_vertices); 00061 00062 //--------------------------------------------------------------------------- 00063 //: Copy constructor 00064 //--------------------------------------------------------------------------- 00065 vsol_polygon_2d(const vsol_polygon_2d &other); 00066 00067 //--------------------------------------------------------------------------- 00068 //: Destructor 00069 //--------------------------------------------------------------------------- 00070 virtual ~vsol_polygon_2d(); 00071 00072 //--------------------------------------------------------------------------- 00073 //: Clone `this': creation of a new object and initialization 00074 // See Prototype pattern 00075 //--------------------------------------------------------------------------- 00076 virtual vsol_spatial_object_2d* clone(void) const; 00077 00078 //--------------------------------------------------------------------------- 00079 //: Safe casting 00080 //--------------------------------------------------------------------------- 00081 00082 virtual vsol_polygon_2d* cast_to_polygon(void); 00083 virtual const vsol_polygon_2d* cast_to_polygon(void) const; 00084 00085 virtual vsol_triangle_2d* cast_to_triangle(void); 00086 virtual const vsol_triangle_2d* cast_to_triangle(void) const; 00087 00088 virtual vsol_rectangle_2d* cast_to_rectangle(void); 00089 virtual const vsol_rectangle_2d* cast_to_rectangle(void) const; 00090 00091 //*************************************************************************** 00092 // Access 00093 //*************************************************************************** 00094 00095 //--------------------------------------------------------------------------- 00096 //: Return vertex `i' 00097 // REQUIRE: valid_index(i) 00098 //--------------------------------------------------------------------------- 00099 vsol_point_2d_sptr vertex(const int i) const; 00100 00101 //*************************************************************************** 00102 // Comparison 00103 //*************************************************************************** 00104 00105 //--------------------------------------------------------------------------- 00106 //: Has `this' the same points than `other' in the same order ? 00107 //--------------------------------------------------------------------------- 00108 virtual bool operator==(const vsol_polygon_2d &other) const; 00109 virtual bool operator==(const vsol_spatial_object_2d& obj) const; // virtual of vsol_spatial_object_2d 00110 00111 //--------------------------------------------------------------------------- 00112 //: Has `this' not the same points than `other' in the same order ? 00113 //--------------------------------------------------------------------------- 00114 inline bool operator!=(const vsol_polygon_2d &o)const{return !operator==(o);} 00115 00116 //*************************************************************************** 00117 // Status report 00118 //*************************************************************************** 00119 00120 //--------------------------------------------------------------------------- 00121 //: Return the region type of a polygon. Its spatial type is a REGION 00122 //--------------------------------------------------------------------------- 00123 vsol_region_2d_type region_type(void) const { return vsol_region_2d::POLYGON; } 00124 00125 //--------------------------------------------------------------------------- 00126 //: Compute the bounding box of `this' 00127 //--------------------------------------------------------------------------- 00128 virtual void compute_bounding_box(void) const; 00129 00130 //--------------------------------------------------------------------------- 00131 //: Return the number of vertices 00132 //--------------------------------------------------------------------------- 00133 unsigned int size(void) const { return 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 //--------------------------------------------------------------------------- 00151 //: Is `i' a valid index for the list of vertices ? 00152 //--------------------------------------------------------------------------- 00153 bool valid_index(unsigned int i) const { return i<storage_->size(); } 00154 00155 //--------------------------------------------------------------------------- 00156 //: Are `new_vertices' valid vertices to build a polygon of the current type? 00157 // All vertex sets are valid for a general polygon. 00158 //--------------------------------------------------------------------------- 00159 virtual bool valid_vertices(const vcl_vector<vsol_point_2d_sptr> ) const; 00160 00161 00162 // ==== Binary IO methods ====== 00163 00164 //: Binary save self to stream. 00165 void b_write(vsl_b_ostream &os) const; 00166 00167 //: Binary load self from stream. 00168 void b_read(vsl_b_istream &is); 00169 00170 //: Return IO version number; 00171 short version() const; 00172 00173 //: Print an ascii summary to the stream 00174 void print_summary(vcl_ostream &os) const; 00175 00176 //: Return a platform independent string identifying the class 00177 virtual vcl_string is_a() const { return vcl_string("vsol_polygon_2d"); } 00178 00179 //: Return true if the argument matches the string identifying the class or any parent class 00180 virtual bool is_class(vcl_string const& cls) const 00181 { return cls==is_a() || vsol_region_2d::is_class(cls); } 00182 00183 //--------------------------------------------------------------------------- 00184 //: output description to stream 00185 //--------------------------------------------------------------------------- 00186 void describe(vcl_ostream &strm, int blanking=0) const; 00187 }; 00188 00189 //: Binary save vsol_polygon_2d* to stream. 00190 void vsl_b_write(vsl_b_ostream &os, const vsol_polygon_2d* p); 00191 00192 //: Binary load vsol_polygon_2d* from stream. 00193 void vsl_b_read(vsl_b_istream &is, vsol_polygon_2d* &p); 00194 00195 #endif // vsol_polygon_2d_h_
1.5.1