1 // This is gel/vsol/vsol_polygon_2d.h 2 #ifndef vsol_polygon_2d_h_ 3 #define vsol_polygon_2d_h_ 4 //***************************************************************************** 5 //: 6 // \file 7 // \brief Polygon in 2D space 8 // 9 // The vertices are to be defined in counterclockwise order. 10 // 11 // \author Francois BERTEL 12 // \date 2000-05-09 13 // 14 // \verbatim 15 // Modifications 16 // 2000-05-09 Francois BERTEL Creation 17 // 2000-06-17 Peter Vanroose Implemented all operator==()s and type info 18 // 2001-07-03 Peter Vanroose Corrected the implementation of is_convex() 19 // 2003-11-05 Amir Tamrakar Added Safe casting methods 20 // 2004-05-01 Joseph Mundy Added binary I/O 21 // 2004-05-14 Peter Vanroose Added describe() 22 // \endverbatim 23 //***************************************************************************** 24 25 //***************************************************************************** 26 // External declarations for values 27 //***************************************************************************** 28 #include <vector> 29 #include <string> 30 #include <iostream> 31 #include <iosfwd> 32 #include <vsol/vsol_region_2d.h> 33 #include <vsol/vsol_point_2d_sptr.h> 34 #include <vsl/vsl_binary_io.h> 35 #ifdef _MSC_VER 36 # include <vcl_msvc_warnings.h> 37 #endif 38 class vsol_triangle_2d; 39 class vsol_rectangle_2d; 40 41 class vsol_polygon_2d : public vsol_region_2d 42 { 43 protected: 44 //*************************************************************************** 45 // Data members 46 //*************************************************************************** 47 48 //--------------------------------------------------------------------------- 49 // Description: List of vertices 50 //--------------------------------------------------------------------------- 51 std::vector<vsol_point_2d_sptr> *storage_; 52 53 //*************************************************************************** 54 // Initialization 55 //*************************************************************************** 56 public: 57 //--------------------------------------------------------------------------- 58 //: Default constructor. 59 //--------------------------------------------------------------------------- 60 vsol_polygon_2d(); 61 62 //--------------------------------------------------------------------------- 63 //: Constructor from a std::vector (not a geometric vector but a list of 64 //points) 65 // REQUIRE: new_vertices.size()>=3 66 //--------------------------------------------------------------------------- 67 explicit vsol_polygon_2d( 68 const std::vector<vsol_point_2d_sptr> &new_vertices); 69 70 //--------------------------------------------------------------------------- 71 //: Copy constructor 72 //--------------------------------------------------------------------------- 73 vsol_polygon_2d(const vsol_polygon_2d &other); 74 75 //--------------------------------------------------------------------------- 76 //: Destructor 77 //--------------------------------------------------------------------------- 78 ~vsol_polygon_2d() override; 79 80 //--------------------------------------------------------------------------- 81 //: Clone `this': creation of a new object and initialization 82 // See Prototype pattern 83 //--------------------------------------------------------------------------- 84 vsol_spatial_object_2d *clone() const override; 85 86 //--------------------------------------------------------------------------- 87 //: Safe casting 88 //--------------------------------------------------------------------------- 89 90 vsol_polygon_2d *cast_to_polygon() override; 91 const vsol_polygon_2d *cast_to_polygon() const override; 92 93 virtual vsol_triangle_2d *cast_to_triangle(); 94 virtual const vsol_triangle_2d *cast_to_triangle() const; 95 96 virtual vsol_rectangle_2d *cast_to_rectangle(); 97 virtual const vsol_rectangle_2d *cast_to_rectangle() const; 98 99 //*************************************************************************** 100 // Access 101 //*************************************************************************** 102 103 //--------------------------------------------------------------------------- 104 //: Return vertex `i' 105 // REQUIRE: valid_index(i) 106 //--------------------------------------------------------------------------- 107 vsol_point_2d_sptr vertex(const int i) const; 108 109 //*************************************************************************** 110 // Comparison 111 //*************************************************************************** 112 113 //--------------------------------------------------------------------------- 114 //: Has `this' the same points than `other' in the same order ? 115 //--------------------------------------------------------------------------- 116 virtual bool operator==(const vsol_polygon_2d &other) const; 117 bool operator==(const vsol_spatial_object_2d &obj) 118 const override; // virtual of vsol_spatial_object_2d 119 120 //--------------------------------------------------------------------------- 121 //: Has `this' not the same points than `other' in the same order ? 122 //--------------------------------------------------------------------------- 123 inline bool operator!=(const vsol_polygon_2d &o) const { 124 return !operator==(o);} 125 126 //*************************************************************************** 127 // Status report 128 //*************************************************************************** 129 130 //--------------------------------------------------------------------------- 131 //: Return the region type of a polygon. Its spatial type is a REGION 132 //--------------------------------------------------------------------------- region_type()133 vsol_region_2d_type region_type() const override { 134 return vsol_region_2d::POLYGON; 135 } 136 137 //--------------------------------------------------------------------------- 138 //: Compute the bounding box of `this' 139 //--------------------------------------------------------------------------- 140 void compute_bounding_box() const override; 141 142 //--------------------------------------------------------------------------- 143 //: Return the number of vertices 144 //--------------------------------------------------------------------------- size()145 unsigned int size() const { return static_cast<int>(storage_->size()); } 146 147 //--------------------------------------------------------------------------- 148 //: Return the area of `this' 149 //--------------------------------------------------------------------------- 150 double area() const override; // virtual of vsol_region_2d 151 152 //--------------------------------------------------------------------------- 153 //: Return the centroid of `this' 154 //--------------------------------------------------------------------------- 155 vsol_point_2d_sptr centroid() const override; 156 157 //--------------------------------------------------------------------------- 158 //: Is `this' convex ? 159 //--------------------------------------------------------------------------- 160 bool is_convex() const override; 161 162 //--------------------------------------------------------------------------- 163 //: Is `i' a valid index for the list of vertices ? 164 //--------------------------------------------------------------------------- valid_index(unsigned int i)165 bool valid_index(unsigned int i) const { return i < storage_->size(); } 166 167 //--------------------------------------------------------------------------- 168 //: Are `new_vertices' valid vertices to build a polygon of the current type? 169 // All vertex sets are valid for a general polygon. 170 //--------------------------------------------------------------------------- 171 virtual bool valid_vertices(const std::vector<vsol_point_2d_sptr>) const; 172 173 // ==== Binary IO methods ====== 174 175 //: Binary save self to stream. 176 void b_write(vsl_b_ostream &os) const override; 177 178 //: Binary load self from stream. 179 void b_read(vsl_b_istream &is) override; 180 181 //: Return IO version number; 182 short version() const; 183 184 //: Print an ascii summary to the stream 185 void print_summary(std::ostream &os) const; 186 187 //: Return a platform independent string identifying the class is_a()188 std::string is_a() const override { return std::string("vsol_polygon_2d"); } 189 190 //: Return true if the argument matches the string identifying the class or 191 //any parent class is_class(std::string const & cls)192 bool is_class(std::string const &cls) const override { 193 return cls == is_a() || vsol_region_2d::is_class(cls); } 194 195 //--------------------------------------------------------------------------- 196 //: output description to stream 197 //--------------------------------------------------------------------------- 198 void describe(std::ostream &strm, int blanking=0) const override; 199 }; 200 201 //: Binary save vsol_polygon_2d* to stream. 202 void vsl_b_write(vsl_b_ostream &os, const vsol_polygon_2d* p); 203 204 //: Binary load vsol_polygon_2d* from stream. 205 void vsl_b_read(vsl_b_istream &is, vsol_polygon_2d* &p); 206 207 #endif // vsol_polygon_2d_h_ 208