xc
PolygonMap.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 //PolygonMap.h
22 
23 #ifndef MAP_POLYGONS_H
24 #define MAP_POLYGONS_H
25 
26 #include <deque>
27 #include <CGAL/iterator.h>
28 #include "../../cgal_types.h"
29 #include <CGAL/Polyhedron_incremental_builder_3.h>
30 #include <iostream>
31 #include <map>
32 #include "utility/geom/pos_vec/Pos3d.h"
33 
34 
36 //
40  {
41  protected:
42  std::deque<size_t> ind_vertices;
43 
44  public:
45  PolygonVertexStorage(const size_t &sz,const size_t &V);
46  inline size_t getNumVertices(void) const
47  { return ind_vertices.size(); }
48  inline const size_t &Vertice(const size_t &i) const
49  { return ind_vertices[(i-1)]; }
50  inline size_t &Vertice(const size_t &i)
51  { return ind_vertices[(i-1)]; }
52  void Print(std::ostream &os) const;
53  };
54 
55 template <class TPOL>
57  {
58  public:
59  typedef typename TPOL::Vertex_const_iterator VCI;
60  typedef CGAL::Inverse_index<VCI> Index;
61  typedef typename TPOL::Halfedge_around_facet_const_circulator HFCC;
62 
63  public:
64  PolygonVerticesIndices(const Index &index,const HFCC &f);
65  };
66 
67 template <class TPOL>
68 PolygonVerticesIndices<TPOL>::PolygonVerticesIndices(const PolygonVerticesIndices<TPOL>::Index &index,const PolygonVerticesIndices<TPOL>::HFCC &ihc)
69  : PolygonVertexStorage(circulator_size(ihc),0)
70  {
71  HFCC hc= ihc;
72  HFCC hc_end = hc;
73  CGAL_assertion(circulator_size( hc) >= 3);
74  size_t i= 0;
75  do
76  {
77  ind_vertices[i]= index[VCI(hc->vertex())];
78  ++hc; i++;
79  }
80  while(hc != hc_end);
81  }
82 
83 template <class TPOS>
84 class MapPosVertices: protected std::map<size_t,TPOS>
85  {
86 
87  public:
88  typedef std::map<size_t,TPOS> map_p3d;
89  typedef typename map_p3d::iterator iterator;
90  typedef typename map_p3d::const_iterator const_iterator;
91 
92  inline const_iterator begin(void) const
93  { return map_p3d::begin(); }
94  inline const_iterator end(void) const
95  { return map_p3d::end(); }
96  inline iterator begin(void)
97  { return map_p3d::begin(); }
98  inline iterator end(void)
99  { return map_p3d::end(); }
100  void insert(const size_t &i,const TPOS &p);
101  void Print(std::ostream &os) const;
102  };
103 
104 template <class TPOS>
105 void MapPosVertices<TPOS>::insert(const size_t &i,const TPOS &p)
106  { (*this)[i]= p; }
107 template <class TPOS>
108 void MapPosVertices<TPOS>::Print(std::ostream &os) const
109  {
110  for(const_iterator i= begin();i!=end();i++)
111  os << "Vertice: " << (*i).first << ": " << (*i).second << std::endl;
112  }
113 
114 template <class TPOL>
116  {
117  public:
119  typedef typename IndVertices::VCI VCI;
120  typedef typename IndVertices::Index Index;
121  typedef typename IndVertices::HFCC HFCC;
122  typedef typename TPOL::Vertex::Point Point;
124  typedef typename map_pos_vertices::const_iterator vertices_const_iterator;
125  typedef typename std::deque<IndVertices >::const_iterator caras_const_iterator;
126 
127  private:
128  map_pos_vertices mv;
129  std::deque<IndVertices> caras;
130  public:
131 
132  PolygonMap(const TPOL &pol);
133 
134  vertices_const_iterator VerticesBegin(void) const
135  { return mv.begin(); }
136  vertices_const_iterator VerticesEnd(void) const
137  { return mv.end(); }
138  caras_const_iterator CarasBegin(void) const
139  { return caras.begin(); }
140  caras_const_iterator CarasEnd(void) const
141  { return caras.end(); }
142 
143  void Print(std::ostream &os) const;
144  };
145 
146 template <class TPOL>
147 PolygonMap<TPOL>::PolygonMap(const TPOL &pol)
148  {
149  typedef typename TPOL::Facet_const_iterator FCI;
150  size_t cont= 0;
151  for(VCI vi = pol.vertices_begin();
152  vi != pol.vertices_end(); ++vi)
153  {
154  mv.insert(cont,vi->point());
155  cont++;
156  }
157  const Index index(pol.vertices_begin(),pol.vertices_end());
158  for(FCI fi = pol.facets_begin(); fi != pol.facets_end(); ++fi)
159  {
160  HFCC hc = fi->facet_begin();
161  caras.push_back(IndVertices(index,hc));
162  }
163  }
164 
165 template <class TPOL>
166 void PolygonMap<TPOL>::Print(std::ostream &os) const
167  {
168  os << "vertex list: " << std::endl;
169  mv.Print(os);
170  os << "face list: " << std::endl;
171  for(caras_const_iterator i= caras.begin();i!=caras.end();i++)
172  {
173  (*i).Print(os);
174  os << std::endl;
175  }
176  }
177 
178 
179 template <class TPOL>
180 std::ostream &operator<<(std::ostream &os, const PolygonMap<TPOL> &mt)
181  {
182  mt.Print(os);
183  return os;
184  }
185 
186 template <class TPOL>
187 PolygonMap<TPOL> getPolygonMap(const TPOL &pol)
188  { return PolygonMap<TPOL>(pol); }
189 
190 template <class TPOLORG,class HDS,class CVPOS>
191 class Build_tdest_polyhedron: public CGAL::Modifier_base<HDS>
192  {
193  const TPOLORG &sf;
194 
195  public:
196  typedef typename HDS::Vertex Vertex;
197  typedef typename Vertex::Point Point;
198  typedef typename PolygonMap<TPOLORG>::vertices_const_iterator vconst_iter;
199  typedef typename PolygonMap<TPOLORG>::caras_const_iterator cconst_iter;
200 
201  public:
202  Build_tdest_polyhedron(const TPOLORG &pol)
203  : sf(pol) {}
204  void operator()(HDS& hds);
205  };
206 
207 template <class TPOLORG,class HDS,class CVPOS>
209  {
210  const size_t num_facetas= sf.size_of_facets();
211  const size_t num_edges= sf.size_of_halfedges();
212  const size_t num_vertices= sf.size_of_vertices();
213  PolygonMap<TPOLORG> mt= getPolygonMap(sf);
214  // Postcondition: `hds' is a valid polyhedral surface.
215  CGAL::Polyhedron_incremental_builder_3<HDS> B(hds, true);
216 
217  B.begin_surface(num_vertices,num_facetas,num_edges);
218  for(vconst_iter i= mt.VerticesBegin();i!=mt.VerticesEnd();i++)
219  {
220  auto p= (*i).second;
221  B.add_vertex(CVPOS()(p));
222  }
223  for(cconst_iter j= mt.CarasBegin();j!=mt.CarasEnd();j++)
224  {
225  B.begin_facet();
226  const size_t nv= (*j).getNumVertices();
227  for(size_t i=0;i<nv;i++)
228  B.add_vertex_to_facet((*j).Vertice(i+1));
229  B.end_facet();
230  }
231  B.end_surface();
232  }
233 
234 #endif
Definition: PolygonMap.h:115
Definition: PolygonMap.h:56
Definition: PolygonMap.h:84
Almacena los indices de los vértices de un polígono.
Definition: PolygonMap.h:39
Definition: PolygonMap.h:191