00001 #ifndef GRAL_GB_BASE_POLYGON1D_H
00002 #define GRAL_GB_BASE_POLYGON1D_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "Gral/Base/element-handle.h"
00019 #include "Gral/Base/vertex-iterator-int.h"
00020 #include "Gral/Base/cell-iterator-int.h"
00021 #include "Gral/Base/archetype-0D.h"
00022 #include "Gral/Base/construct-grid.h"
00023
00024 #include "Geometry/point-traits.h"
00025 #include "Utility/ref-ptr.h"
00026
00027 namespace GrAL {
00028
00029 namespace polygon1d {
00030
00031 class VertexOnVertexIterator1d;
00032 class VertexOnCellIterator1d;
00033 class CellOnVertexIterator1d;
00034 class polygon;
00035
00036 struct grid_types_p1d : public grid_types_detail::grid_types_root {
00037 typedef grid_types_p1d self;
00038
00039 typedef polygon grid_type;
00040 typedef grid_dim_tag<1> dimension_tag;
00041
00042 typedef vertex_handle_int<polygon,self> vertex_handle;
00043 typedef cell_handle_int <polygon,self> cell_handle;
00044
00045 typedef vertex_iterator_int<grid_types_p1d> VertexIterator;
00046 typedef cell_iterator_int <grid_types_p1d> CellIterator;
00047 typedef VertexIterator Vertex;
00048 typedef CellIterator Cell;
00049
00050 typedef VertexOnVertexIterator1d VertexOnVertexIterator;
00051 typedef VertexOnCellIterator1d VertexOnCellIterator;
00052 typedef CellOnVertexIterator1d CellOnVertexIterator;
00053
00054
00055 typedef vertex_handle facet_handle;
00056 typedef cell_handle edge_handle;
00057 typedef Vertex Facet;
00058 typedef Cell Edge;
00059 typedef VertexIterator FacetIterator;
00060 typedef CellIterator EdgeIterator;
00061
00062 typedef VertexOnVertexIterator FacetOnVertexIterator;
00063
00064 typedef VertexOnCellIterator FacetOnCellIterator;
00065 typedef VertexOnCellIterator VertexOnEdgeIterator;
00066
00067 typedef CellOnVertexIterator EdgeOnVertexIterator;
00068 typedef CellOnVertexIterator CellOnFacetIterator;
00069 };
00070
00080 class polygon : public grid_types_p1d, public archetype_0D::grid_mixin<polygon, grid_types_p1d> {
00081 private:
00082 size_type nv;
00083 public:
00084 explicit polygon(size_type nnv = 0) : nv(nnv) {}
00085
00086 void set_number_of_vertices(size_type nnv) { nv = nnv;}
00087
00088 size_type NumOfVertices() const { return nv;}
00089 size_type NumOfCells() const { return nv;}
00090
00091 size_type NumOfEdges() const { return NumOfCells();}
00092 size_type NumOfFacets() const { return NumOfVertices();}
00093
00094 inline VertexIterator FirstVertex() const;
00095 inline CellIterator FirstCell() const;
00096
00097 EdgeIterator FirstEdge() const { return FirstCell();}
00098 FacetIterator FirstFacet() const { return FirstVertex();}
00099
00100
00101 size_type NumOfVertices(Vertex const&) const { return 2;}
00102 size_type NumOfEdges (Vertex const&) const { return 2;}
00103 size_type NumOfFaces (Vertex const&) const { return 2;}
00104 size_type NumOfFacets (Vertex const&) const { return 2;}
00105 size_type NumOfCells (Vertex const&) const { return 2;}
00106
00107 size_type NumOfVertices(Cell const&) const { return 2;}
00108 size_type NumOfFacets (Cell const&) const { return 2;}
00109
00110 size_type next_handle(size_type h) const { return (h == NumOfVertices()-1 ? 0 : h+1);}
00111 size_type prev_handle(size_type h) const { return (h == 0 ? NumOfVertices()-1 : h-1);}
00112
00113 inline void switch_vertex(Vertex & v, Cell const& c) const;
00114 inline void switch_cell (Vertex const& v, Cell & c) const;
00115
00116 inline Vertex switched_vertex(Vertex const& v, Cell const& c) const;
00117 inline Cell switched_cell (Vertex const& v, Cell const& c) const;
00118
00119
00120 bool IsOnBoundary(FacetOnCellIterator const&) const { return false;}
00121 bool IsOnBoundary(Facet const&) const { return false;}
00122 };
00123
00124
00125
00126 class VertexOnVertexIterator1d : public grid_types_p1d {
00127 typedef VertexOnVertexIterator1d self;
00128 private:
00129 Vertex v;
00130 int h;
00131 public:
00132 typedef Vertex value_type;
00133 typedef Vertex anchor_type;
00134
00135 VertexOnVertexIterator1d() {}
00136 explicit VertexOnVertexIterator1d(Vertex const& vv, int hh = 0) : v(vv), h(hh) {}
00137
00138 self& operator++() { cv(); ++h; return *this;}
00139 Vertex operator*() const { cv(); return Vertex(TheGrid(),handle());}
00140 vertex_handle handle() const {
00141 cv();
00142 size_type vh = v.handle();
00143 return (h == 0 ? vertex_handle(prev(vh)) : vertex_handle(next(vh)));
00144 }
00145 size_type prev(size_type vh) const { return (vh == 0 ? TheGrid().NumOfVertices() -1 : vh-1);}
00146 size_type next(size_type vh) const { return (vh+1)%TheGrid().NumOfVertices();}
00147
00148 bool IsDone() const { cb(); return h >= 2;}
00149
00150 Vertex const& TheVertex() const { cb(); return v;}
00151 Vertex const& TheAnchor() const { cb(); return v;}
00152 grid_type const& TheGrid() const { cb(); return v.TheGrid();}
00153
00154 friend bool operator==(self const& lhs, self const& rhs) { lhs.cb(); rhs.cb(); return (lhs.h == rhs.h);}
00155 friend bool operator!=(self const& lhs, self const& rhs) { return !(lhs == rhs);}
00156
00157 bool bound() const { return v.valid();}
00158 bool valid() const { return bound() && h < 2;}
00159 void cb() const { REQUIRE(bound(), "", 1);}
00160 void cv() const { REQUIRE(valid(), "h=" << h, 1);}
00161 };
00162
00163
00164
00165
00166
00167 class VertexOnCellIterator1d : public grid_types_p1d {
00168 typedef VertexOnCellIterator1d self;
00169 private:
00170 Cell c;
00171 int h;
00172 public:
00173 typedef Vertex value_type;
00174 typedef Cell anchor_type;
00175
00176 VertexOnCellIterator1d() {}
00177 explicit VertexOnCellIterator1d(Cell const& cc, int hh = 0) : c(cc), h(hh) {}
00178
00179 self& operator++() { cv(); ++h; return *this;}
00180 Vertex operator*() const { cv(); return Vertex(TheGrid(),handle());}
00181 vertex_handle handle() const {
00182 cv();
00183 size_type v = c.handle();
00184 return (h == 0 ? vertex_handle(v) : vertex_handle( (v+1)%TheGrid().NumOfVertices()));
00185 }
00186 bool IsDone() const { cb(); return h >= 2;}
00187
00188 Cell const& TheCell() const { cb(); return c;}
00189 Cell const& TheEdge() const { cb(); return c;}
00190 Cell const& TheAnchor() const { cb(); return c;}
00191 grid_type const& TheGrid() const { cb(); return c.TheGrid();}
00192
00193 friend bool operator==(self const& lhs, self const& rhs) { lhs.cb(); rhs.cb(); return (lhs.h == rhs.h);}
00194 friend bool operator!=(self const& lhs, self const& rhs) { return !(lhs == rhs);}
00195
00196 bool bound() const { return c.valid();}
00197 bool valid() const { return bound() && h < 2;}
00198 void cb() const { REQUIRE(bound(), "", 1);}
00199 void cv() const { REQUIRE(valid(), "h=" << h, 1);}
00200 };
00201
00202
00203
00204
00205 class CellOnVertexIterator1d : public grid_types_p1d {
00206 typedef CellOnVertexIterator1d self;
00207 private:
00208 Vertex v;
00209 int h;
00210 public:
00211 typedef Cell value_type;
00212 typedef Vertex anchor_type;
00213
00214 CellOnVertexIterator1d() {}
00215 explicit CellOnVertexIterator1d(Vertex const& vv, int hh = 0) : v(vv), h(hh) {}
00216
00217 self& operator++() { cv(); ++h; return *this;}
00218 Cell operator*() const { cv(); return Cell(TheGrid(),handle());}
00219 cell_handle handle() const {
00220 cv();
00221 size_type c = v.handle();
00222 return (h == 0 ? cell_handle(c) : cell_handle(c == 0 ? TheGrid().NumOfVertices()-1 : c-1));
00223 }
00224 bool IsDone() const { cb(); return h >= 2;}
00225
00226 Vertex const& TheVertex() const { cb(); return v;}
00227 Vertex const& TheAnchor() const { cb(); return v;}
00228 grid_type const& TheGrid() const { cb(); return v.TheGrid();}
00229
00230 friend bool operator==(self const& lhs, self const& rhs) { lhs.cb(); rhs.cb(); return (lhs.h == rhs.h);}
00231 friend bool operator!=(self const& lhs, self const& rhs) { return !(lhs == rhs);}
00232
00233 bool bound() const { return v.valid();}
00234 bool valid() const { return bound() && h < 2;}
00235 void cb() const { REQUIRE(bound(), "", 1);}
00236 void cv() const { REQUIRE(valid(), "h=" << h, 1);}
00237 };
00238
00239 }
00240
00241 template<>
00242 struct grid_types<polygon1d::polygon>
00243 : public grid_types_base<polygon1d::grid_types_p1d>,
00244 public archetype_0D::grid_types_mixin
00245 {
00246
00247 grid_types() {}
00248 };
00249
00250 template<>
00251 struct element_traits<polygon1d::polygon::Vertex>
00252 : public element_traits_vertex_base<polygon1d::polygon>
00253 {
00254 typedef element_traits_vertex_base<polygon1d::polygon> base;
00255 typedef consecutive_integer_tag<0> consecutive_tag;
00256 typedef base::hasher_type_elem_base hasher_type;
00257 };
00258
00259 template<>
00260 struct element_traits<polygon1d::polygon::Cell>
00261 : public element_traits_cell_base<polygon1d::polygon>
00262 {
00263 typedef element_traits_cell_base<polygon1d::polygon> base;
00264 typedef consecutive_integer_tag<0> consecutive_tag;
00265 typedef base::hasher_type_elem_base hasher_type;
00266 };
00267
00268
00269 template<class T>
00270 class grid_function<polygon1d::polygon::Vertex, T>
00271 : public grid_function_vector<polygon1d::polygon::Vertex, T>
00272 {
00273 typedef grid_function_vector<polygon1d::polygon::Vertex, T> base;
00274 public:
00275 typedef polygon1d::polygon grid_type;
00276 grid_function() {}
00277 grid_function(grid_type const& g) : base(g) {}
00278 grid_function(grid_type const& g, T const& t) : base(g,t) {}
00279 grid_function(ref_ptr<grid_type const> g) : base(g) {}
00280 grid_function(ref_ptr<grid_type const> g, T const& t) : base(g,t) {}
00281 };
00282
00283 template<class T>
00284 class grid_function<polygon1d::polygon::Cell, T>
00285 : public grid_function_vector<polygon1d::polygon::Cell, T>
00286 {
00287 typedef grid_function_vector<polygon1d::polygon::Cell, T> base;
00288 public:
00289 typedef polygon1d::polygon grid_type;
00290 grid_function() {}
00291 grid_function(grid_type const& g) : base(g) {}
00292 grid_function(grid_type const& g, T const& t) : base(g,t) {}
00293 grid_function(ref_ptr<grid_type const> g) : base(g) {}
00294 grid_function(ref_ptr<grid_type const> g, T const& t) : base(g,t) {}
00295 };
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 template<class GSRC, class VCORR, class CCORR>
00322 void ConstructGrid0(polygon1d::polygon & G_dst,
00323 GSRC const& G_src,
00324 VCORR & v_corr,
00325 CCORR & c_corr)
00326 {
00327 typedef grid_types<GSRC> gtsrc;
00328 typedef grid_types<polygon1d::polygon> gtdst;
00329 typedef typename gtsrc::size_type size_type;
00330 G_dst.set_number_of_vertices(G_src.NumOfVertices());
00331
00332 if(G_src.NumOfVertices() > 0) {
00333
00334
00335 typename gtsrc::Cell c_src = * GrAL::begin<typename gtsrc::Cell>(G_src);
00336 typename gtsrc::Vertex v_src = * GrAL::begin<typename gtsrc::Vertex>(c_src);
00337 gtdst ::Cell c_dst = * G_dst.FirstCell();
00338 gtdst ::Vertex v_dst = * c_dst.FirstVertex();
00339 typename gtsrc::Vertex v_src_first = v_src;
00340
00341 v_corr[v_src.handle()] = v_dst.handle();
00342 c_corr[c_src.handle()] = c_dst.handle();
00343 typename gtsrc::size_type cnt = 0;
00344
00345 G_src.switch_vertex(v_src,c_src);
00346 G_src.switch_cell (v_src,c_src);
00347 G_dst.switch_vertex(v_dst,c_dst);
00348 G_dst.switch_cell (v_dst,c_dst);
00349 ++cnt;
00350 while ( v_src != v_src_first) {
00351
00352 v_corr[v_src.handle()] = v_dst.handle();
00353 c_corr[c_src.handle()] = c_dst.handle();
00354 G_src.switch_vertex(v_src,c_src);
00355 G_src.switch_cell (v_src,c_src);
00356 G_dst.switch_vertex(v_dst,c_dst);
00357 G_dst.switch_cell (v_dst,c_dst);
00358 ++cnt;
00359 }
00360
00361 if(cnt < G_src.NumOfVertices() -1) {
00362 ENSURE(false,
00363 "=> G_src is not a simple polygon! cnt=" << cnt
00364 << " G_src.NumOfVertices()=" << G_src.NumOfVertices(), 1);
00365 }
00366
00367 }
00368
00369 }
00370
00371 namespace polygon1d {
00372
00373 typedef grid_types<polygon> gt;
00374
00375 inline gt::VertexIterator gral_begin(gt::grid_type const& R, gt::VertexIterator) { return R.FirstVertex();}
00376 inline gt::VertexIterator gral_end (gt::grid_type const& R, gt::VertexIterator) { return gt::VertexIterator(R, R.NumOfVertices());}
00377 inline gt::size_type gral_size (gt::grid_type const& R, gt::VertexIterator) { return R.NumOfVertices();}
00378
00379 inline gt::CellIterator gral_begin(gt::grid_type const& R, gt::CellIterator) { return R.FirstCell();}
00380 inline gt::CellIterator gral_end (gt::grid_type const& R, gt::CellIterator) { return gt::CellIterator(R, R.NumOfCells());}
00381 inline gt::size_type gral_size (gt::grid_type const& R, gt::CellIterator) { return R.NumOfCells();}
00382
00383 inline gt::VertexOnCellIterator gral_begin(gt::Cell a, gt::VertexOnCellIterator) { return a.FirstVertex();}
00384 inline gt::VertexOnCellIterator gral_end (gt::Cell a, gt::VertexOnCellIterator) { return gt::VertexOnCellIterator(a,a.NumOfVertices());}
00385 inline gt::size_type gral_size (gt::Cell a, gt::VertexOnCellIterator) { return a.NumOfVertices();}
00386
00387 inline gt::CellOnVertexIterator gral_begin(gt::Vertex a, gt::CellOnVertexIterator) { return a.FirstCell();}
00388 inline gt::CellOnVertexIterator gral_end (gt::Vertex a, gt::CellOnVertexIterator) { return gt::CellOnVertexIterator(a, a.NumOfCells());}
00389 inline gt::size_type gral_size (gt::Vertex a, gt::CellOnVertexIterator) { return a.NumOfCells();}
00390
00391
00392
00393 inline polygon::VertexIterator polygon::FirstVertex() const { return VertexIterator(*this);}
00394 inline polygon::CellIterator polygon::FirstCell() const { return CellIterator (*this);}
00395
00396 inline void polygon::switch_vertex(polygon::Vertex & v, polygon::Cell const& c) const
00397 {
00398 v = ( (v == * c.FirstVertex())
00399 ? Vertex(*this, next_handle(v.handle()))
00400 : Vertex(*this, prev_handle(v.handle())));
00401 }
00402
00403 inline void polygon::switch_cell (polygon::Vertex const& v, polygon::Cell & c) const
00404 {
00405 c = ( (v == * c.FirstVertex())
00406 ? Cell(*this, prev_handle(c.handle()))
00407 : Cell(*this, next_handle(c.handle())));
00408 }
00409
00410 inline polygon::Vertex polygon::switched_vertex(polygon::Vertex const& v, polygon::Cell const& c) const
00411 { Vertex res(v); switch_vertex(res,c); return res;}
00412
00413 inline polygon::Cell polygon::switched_cell (polygon::Vertex const& v, polygon::Cell const& c) const
00414 { Cell res(c); switch_cell (v,res); return res;}
00415
00416
00420 template<class COORD, class GT = grid_types<polygon> >
00421 class geometry {
00422 public:
00423 typedef COORD coord_type;
00424 typedef polygon grid_type;
00425 typedef point_traits<coord_type> pt;
00426 typedef typename GT::Vertex Vertex;
00427 typedef typename GT::vertex_handle vertex_handle;
00428
00429 private:
00430 grid_function<Vertex, coord_type> coo;
00431 public:
00432 geometry() {}
00433 geometry(grid_type const& g) : coo(g) {}
00434 template<class IT>
00435 geometry(grid_type const& g, IT b, IT e)
00436 : coo(g)
00437 {
00438 typename GT::VertexIterator v(g);
00439 for( ; ! v.IsDone() && b != e; ++v, ++b)
00440 coo[*v] = *b;
00441 REQUIRE(b==e && v.IsDone(), "", 1);
00442 }
00443
00444 coord_type const& operator()(Vertex const& v) const { return coo(v);}
00445 coord_type & operator()(Vertex const& v) { return coo[v];}
00446 coord_type const& coord (Vertex const& v) const { return coo(v);}
00447 coord_type & coord (Vertex const& v) { return coo[v];}
00448 coord_type const& operator()(vertex_handle const& v) const { return coo(v);}
00449 coord_type & operator()(vertex_handle const& v) { return coo[v];}
00450 coord_type const& coord (vertex_handle const& v) const { return coo(v);}
00451 coord_type & coord (vertex_handle const& v) { return coo[v];}
00452
00453 ref_ptr<grid_type const> TheGrid() const { return ref_ptr<grid_type const>(coo.TheGrid());}
00454 unsigned space_dimension() const { return pt::Dim(coord(*TheGrid()->FirstVertex()));}
00455 };
00456
00457 }
00458
00459 }
00460
00461 #endif
00462