xc
Polygon2d.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. Pérez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //Polygon2d.h
22 
23 #ifndef POLYGON2D_H
24 #define POLYGON2D_H
25 
26 #include "utility/geom/d2/2d_polygons/PolygonalSurface2d.h"
27 
28 class Trf2d;
29 class HalfPlane2d;
30 class Line2d;
31 class Segment2d;
32 class Pos2dList;
33 
34 
36 //
39  {
40  CGPolygon_2 cgpol; //CGAL polygon.
41  template <class inputIterator>
42  bool Overlap(inputIterator begin, inputIterator end) const;
43  protected:
44  inline bool In(const CGPoint_2 &p) const
45  { return In(Pos2d(p)); }
46  inline bool Overlap(const CGPoint_2 &p) const
47  { return Overlap(Pos2d(p)); }
48  public:
49 
50  typedef CGPolygon_2::Vertex_iterator vertex_iterator;
51  typedef CGPolygon_2::Vertex_circulator vertex_circulator;
52  typedef CGPolygon_2::Edge_const_iterator edge_const_iterator;
53  typedef CGPolygon_2::Edge_const_circulator edge_const_circulator;
54 
55  Polygon2d(void);
56  Polygon2d(const GeomObj::list_Pos2d &lv);
57  explicit Polygon2d(const CGPolygon_2 &);
58  explicit Polygon2d(const std::list<Polygon2d> &lp);
59  explicit Polygon2d(const Polyline2d &);
60  explicit Polygon2d(const boost::python::list &);
61  virtual GeomObj *getCopy(void) const;
62  const CGPolygon_2 &ToCGAL(void) const
63  { return cgpol; }
64 
65  inline virtual unsigned int getNumVertices(void) const
66  { return cgpol.size(); }
67  inline void push_back(const Pos2d &p)
68  { cgpol.push_back(p.ToCGAL()); }
70  inline vertex_iterator insert(vertex_iterator i,const Pos2d &q)
71  { return cgpol.insert(i,q.ToCGAL()); }
74  template <class InputIterator>
75  inline void insert(vertex_iterator i,
76  InputIterator first,
77  InputIterator last)
78  { cgpol.insert(i,first,last); }
81  void set(vertex_iterator i,const Pos2d &p)
82  { cgpol.set(i,p.ToCGAL()); }
83  void erase(vertex_iterator i)
84  //Elimina el vértice al que se refiere i.
85  { cgpol.erase(i); }
87  void erase(vertex_iterator first,vertex_iterator last)
88  { cgpol.erase(first,last); }
90  void erase(void)
91  { cgpol.erase(cgpol.vertices_begin(),cgpol.vertices_end()); }
92 
93  vertex_iterator vertices_begin(void) const
94  //Returns a constant iterator that allows to traverse
95  //the vertices of the polygon p.
96  { return cgpol.vertices_begin(); }
97  vertex_iterator vertices_end(void) const
98  //Returns the corresponding past-the-end iterator.
99  { return cgpol.vertices_end(); }
100  vertex_circulator vertices_circulator(void)
101  //Returns a mutable circulator that allows to
102  //traverse the vertices of the polygon p.
103  { return cgpol.vertices_circulator(); }
104  edge_const_iterator edges_begin(void) const
105  //Returns a non-mutable iterator that allows to
106  //traverse the edges of the polygon p.
107  { return cgpol.edges_begin(); }
108  edge_const_iterator edges_end(void)
109  //Returns the corresponding past-the-end iterator.
110  { return cgpol.edges_end(); }
111  edge_const_circulator edges_circulator(void) const
112  //Returns a non-mutable circulator that allows to
113  //traverse the edges of the polygon p.
114  { return cgpol.edges_circulator(); }
115 
116  Polygon2d getSwap(void) const
117  {
118  Polygon2d retval(*this);
119  retval.swap();
120  return retval;
121  }
122  inline bool clockwise(void) const
123  { return (cgpol.is_clockwise_oriented());}
124  inline bool counterclockwise(void) const
125  { return (cgpol.is_counterclockwise_oriented()); }
126  inline void swap(void)
127  { cgpol.reverse_orientation(); }
128  void makeCounterClockWise(void);
129 
130  Polygon2d offset(const GEOM_FT &d) const;
131  Polygon2d buffer(const GEOM_FT &d) const;
132 
133  inline GEOM_FT AreaSigno(void) const
134  { return cgpol.area(); }
135  virtual GEOM_FT getArea(void) const;
136  std::vector<Polygon2d> getTributaryPolygons(void) const;
137  std::vector<double> getTributaryAreas(void) const;
138  virtual GEOM_FT GetMax(unsigned short int i) const;
139  virtual GEOM_FT GetMin(unsigned short int i) const;
140  virtual bool In(const Pos2d &p, const double &tol= 0.0) const;
141  template <class inputIterator>
142  bool In(inputIterator begin, inputIterator end) const;
143  bool In(const Polyline2d &) const;
144  bool In(const Polygon2d &) const;
145  bool Overlap(const Pos2d &) const;
146  bool Overlap(const Line2d &r) const;
147  bool Overlap(const Ray2d &sr) const;
148  bool Overlap(const Segment2d &sg) const;
149  bool Overlap(const BND2d &) const;
150  bool Overlap(const Polyline2d &) const;
151  bool Overlap(const Polygon2d &) const;
152  bool Overlap(const std::list<Polygon2d> &) const;
153  template <class T>
154  bool intersects(const T &t) const
155  { return Overlap(t); }
157  inline Pos2d Vertice(unsigned int i) const
158  { return Vertice0(i-1); }
161  inline Pos2d Vertice0(unsigned int j) const
162  { return Pos2d(cgpol.vertex(j)); }
163  GeomObj::list_Pos2d getVertexList(void) const;
164  boost::python::list getVertexListPy(void) const;
165 
166  void Transform(const Trf2d &trf2d);
167 
168  std::deque<GEOM_FT> &getCovers(const Pos2dList &) const;
169 
170  Polygon2d getUnion(const Polygon2d &other) const;
171  void une(const Polygon2d &);
172  void une(const std::list<Polygon2d> &l);
173  void clipBy(const Polygon2d &);
174  Segment2d Clip(const Line2d &) const;
175  Segment2d Clip(const Ray2d &) const;
176  Segment2d Clip(const Segment2d &) const;
177  std::list<Polygon2d> Clip(const BND2d &bnd) const;
178  boost::python::list ClipPy(const BND2d &) const;
179  std::list<Polygon2d> Clip(const Polygon2d &) const;
180  boost::python::list ClipPy(const Polygon2d &) const;
181 
182  Segment2d getIntersection(const Line2d &) const;
183  Segment2d getIntersection(const Ray2d &) const;
184  Segment2d getIntersection(const Segment2d &) const;
185  std::list<Polygon2d> getIntersection(const HalfPlane2d &) const;
186  boost::python::list getIntersectionPy(const HalfPlane2d &) const;
187  std::list<Polygon2d> getIntersection(const Polygon2d &) const;
188  boost::python::list getIntersectionPy(const Polygon2d &) const;
189 
190  std::list<Polygon2d> getBayazitDecomposition(void) const;
191 
192 
193  //std::list<Polygon2d> create_polygon2d_list(const std::string &str) const;
194  };
195 
196 Pos2d at(const Polygon2d &p, int i);
197 
198 
199 inline Polygon2d transform(const Trf2d &trf2d,const Polygon2d &pol2d)
200  { return getTransformed(pol2d,trf2d); }
201 
202 Polygon2d append_mid_points(const Polygon2d &);
203 
204 std::list<Polygon2d> intersection(const Polygon2d &p1,const Polygon2d &p2);
205 std::list<Polygon2d> intersection(const Polygon2d &p,const HalfPlane2d &r);
206 std::list<Polygon2d> corta(const Polygon2d &p,const Line2d &r);
207 
208 Pos2d center_of_mass(const std::list<Polygon2d> &);
209 
212 template <class inputIterator>
213 bool Polygon2d::In(inputIterator begin, inputIterator end) const
214  {
215  bool retval= true;
216  for(inputIterator i= begin;i!=end;i++)
217  if(!In(Pos2d(*i)))
218  {
219  retval= false;
220  break;
221  }
222  return retval;
223  }
224 
227 template <class inputIterator>
228 bool Polygon2d::Overlap(inputIterator begin, inputIterator end) const
229  {
230  bool retval= false;
231  for(inputIterator i= begin;i!=end;i++)
232  if(Overlap(*i))
233  {
234  retval= true;
235  break;
236  }
237  return retval;
238  }
239 
240 #endif
241 
"boundary" en dos dimensiones.
Definition: BND2d.h:38
Base class for position lists.
Definition: PolyPos.h:35
Posición en dos dimensiones.
Definition: Pos2d.h:41
Pos2d Vertice(unsigned int i) const
Return the position of the i-th vertex.
Definition: Polygon2d.h:157
Line in a two-dimensional space.
Definition: Line2d.h:61
Base class for two-dimensional transformations.
Definition: Trf2d.h:40
vertex_iterator insert(vertex_iterator i, const Pos2d &q)
Insert vertex q before the vertex pointed by i.
Definition: Polygon2d.h:70
Pos2d Vertice0(unsigned int j) const
Return the position of the i-th vertex (0 based: j=0..getNumVertices()-1).
Definition: Polygon2d.h:161
void clipBy(const Polygon2d &)
Clip this polygont with the polygon argument.
Definition: Polygon2d.cc:499
std::list< Polygon2d > getBayazitDecomposition(void) const
Return the decomposition obtained using the Mark Bayazit algorithm.
Definition: Polygon2d.cc:543
boost::python::list ClipPy(const BND2d &) const
Return the polygons that result from clipping this one with the BND argument in a Python list...
Definition: Polygon2d.cc:473
Base class for the polygonal surfaces.
Definition: PolygonalSurface2d.h:38
void Transform(const Trf2d &trf2d)
Aplica a los vértices la transformación que se pasa como parámetro.
Definition: Polygon2d.cc:362
void erase(vertex_iterator first, vertex_iterator last)
Elimina el vértice los vértices del rango [first,last).
Definition: Polygon2d.h:87
const Pos2d center_of_mass(InputIter begin, InputIter end)
Return the center of mass of the objects.
Definition: center_of_mass.h:30
Polygon2d getUnion(const Polygon2d &other) const
Return the union of this polygon with the argument.
Definition: Polygon2d.cc:551
Polígono en dos dimensiones.
Definition: Polygon2d.h:38
Segment in a two-dimensional space.
Definition: Segment2d.h:38
boost::python::list getVertexListPy(void) const
Return a Python list containing the positions of the polygon vertices.
Definition: Polygon2d.cc:351
std::vector< Polygon2d > getTributaryPolygons(void) const
Return the polygons that form the tributary area of each vertex.
Definition: Polygon2d.cc:387
std::deque< GEOM_FT > & getCovers(const Pos2dList &) const
Return the cover of the positions in the argument.
Definition: Polygon2d.cc:426
void makeCounterClockWise(void)
Makes the polygon counter clockwise.
Definition: Polygon2d.cc:215
Polygon2d(void)
Default constructor.
Definition: Polygon2d.cc:52
virtual GEOM_FT GetMax(unsigned short int i) const
Return the maximum value of the i-th coordinate.
Definition: Polygon2d.cc:315
std::vector< double > getTributaryAreas(void) const
Return the areas of the tributary polygons (one for each vertex).
Definition: Polygon2d.cc:415
virtual GeomObj * getCopy(void) const
Constructor virtual.
Definition: Polygon2d.cc:99
virtual GEOM_FT GetMin(unsigned short int i) const
Return the minimum value of the i-th coordinate.
Definition: Polygon2d.cc:328
Half plane in a two-dimensional space.
Definition: HalfPlane2d.h:38
Segment2d Clip(const Line2d &) const
Return the intersection of the polygon with the line.
Definition: Polygon2d.cc:455
Polygon2d buffer(const GEOM_FT &d) const
Returns the buffer (a polygon being the spatial point set collection within a specified maximum dista...
Definition: Polygon2d.cc:134
Polygon2d offset(const GEOM_FT &d) const
Return a polygon parallel to this one at the given distance.
Definition: Polygon2d.cc:104
void insert(vertex_iterator i, InputIterator first, InputIterator last)
Insert the vertices between [first,last) before the vertex pointed by i.
Definition: Polygon2d.h:75
void erase(void)
Elimina el vértice los vértices del rango [first,last).
Definition: Polygon2d.h:90
Segment2d getIntersection(const Line2d &) const
Return the intersection of the polygon with the given line.
Definition: Polygon2d.cc:503
Polyline in a two-dimensional space.
Definition: Polyline2d.h:41
virtual GEOM_FT getArea(void) const
Return the polygon area.
Definition: Polygon2d.cc:383
boost::python::list getIntersectionPy(const HalfPlane2d &) const
Return the intersection with the given half plane in a Python list.
Definition: Polygon2d.cc:519
Clase base para las entidades geométricas.
Definition: GeomObj.h:40
Ray in a two-dimensional space.
Definition: Ray2d.h:35
List of 2D positions.
Definition: Pos2dList.h:33