00001 #ifndef NMWR_GB_CARTESIAN_GRID_2D_H
00002 #define NMWR_GB_CARTESIAN_GRID_2D_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00022 #include <string>
00023
00024
00025 #include "Utility/pre-post-conditions.h"
00026
00027 #include "Gral/Base/common-grid-basics.h"
00028 #include "Gral/Base/element-handle.h"
00029 #include "Gral/Iterators/vertex-on-edge-iterator.h"
00030
00031 #include "Gral/Grids/Cartesian2D/index-map.h"
00032 #include "Gral/Base/polygon.h"
00033
00034 namespace GrAL {
00035
00036 namespace cartesian2d {
00037
00038 class RegGrid2D;
00039 class grid_types_cart2d;
00040
00041
00042
00044 typedef RegGrid2D CartesianGrid2D;
00045
00060 class RegGrid2D {
00061 public:
00062 typedef RegGrid2D self;
00063 typedef cartesian_grid_category_d<2> category;
00064 typedef self grid_type;
00065
00066 typedef xmjr_indexmap2D indexmap_type;
00067
00068
00069
00070 typedef indexmap_type::index_type index_type;
00071 typedef index_type vertex_index_type;
00072 typedef index_type cell_index_type;
00073
00074 private:
00075
00076 index_type ll_,ur_;
00077
00078 int xpoints, ypoints;
00079
00080
00081
00082
00083
00084
00085 indexmap_type vertex_index_map;
00086 indexmap_type cell_index_map;
00087 indexmap_type xedge_index_map;
00088 indexmap_type yedge_index_map;
00089
00090 public:
00091 enum { dim = 2};
00092
00094
00099 RegGrid2D(int n = 2)
00100 :ll_(0,0), ur_(n-1,n-1)
00101 { init(); }
00106 RegGrid2D(int nv_x, int nv_y)
00107 : ll_(0,0), ur_(nv_x-1,nv_y-1)
00108 { init(); }
00109
00114 RegGrid2D(const index_type& nv)
00115 : ll_(0,0), ur_(nv-index_type(1,1))
00116 { init(); }
00117
00118
00123 RegGrid2D(int llx, int lly,int urx, int ury)
00124 : ll_(llx,lly), ur_(urx,ury)
00125 { init(); }
00126
00127
00132 RegGrid2D(const index_type& LL, const index_type& UR)
00133 : ll_(LL), ur_(UR)
00134 { init(); }
00136
00137 private:
00138 void init()
00139 {
00140 xpoints = ur_.x() - ll_.x() +1;
00141 ypoints = ur_.y() - ll_.y() +1;
00142 vertex_index_map = indexmap_type(ll_,ur_);
00143 cell_index_map = indexmap_type(ll_,index_type(ur_.x()-1,ur_.y()-1));
00144 xedge_index_map = indexmap_type(ll_,index_type(ur_.x()-1,ur_.y() ));
00145 yedge_index_map = indexmap_type(ll_,index_type(ur_.x(), ur_.y()-1));
00146 }
00147
00148 public:
00150
00153
00154 const index_type& ll() const { return ll_;}
00156 const index_type& ur() const { return ur_;}
00158 int llx() const { return ll_.x();}
00160 int lly() const { return ll_.y();}
00162 int urx() const { return ur_.x();}
00164 int ury() const { return ur_.y();}
00165
00166 index_type vertex_size() const { return ur_ - ll_ + index_type(1);}
00167 index_type cell_size() const { return ur_ - ll_;}
00169
00170
00171 private:
00172
00173 const indexmap_type& TheVertexMap() const {return vertex_index_map;}
00174 const indexmap_type& TheXEdgeMap() const {return xedge_index_map;}
00175 const indexmap_type& TheYEdgeMap() const {return yedge_index_map;}
00176 const indexmap_type& TheCellMap() const {return cell_index_map;}
00177
00178
00179
00180 static index_type side_offset_ [4];
00181 static index_type corner_offset_[4];
00182 static index_type direction_ [4];
00183 static index_type side_vertex_1_[4];
00184 static index_type side_vertex_2_[4];
00185
00186
00187 static std::string side_name_ [4];
00188 static std::string corner_name_[4];
00189
00190 public:
00191 typedef polygon1d::polygon archetype_type;
00192 typedef archetype_type const* archetype_iterator;
00193 typedef unsigned archetype_handle;
00194 private:
00195
00196
00197 class SD {
00198 public:
00199 SD();
00200
00201 typedef RegGrid2D grid_type;
00202 grid_type::archetype_type the_archetype[1];
00203 };
00204 static SD sd;
00205 public:
00206
00207
00208
00209 static int get_side (const std::string& nm);
00210 static int get_corner(const std::string& nm);
00211 static std::string side_name (int side) { return side_name_ [side-1];}
00212 static std::string corner_name(int corner) { return corner_name_[corner-1];}
00213
00214 static int invalid_side();
00215 static int invalid_corner();
00216
00217 static bool valid_v(int c) { return (0 <= c && c < 4);}
00218 static void cv_v (int c) { REQUIRE(valid_v(c), " c=" << c, 1);}
00219
00220 static const index_type& corner_offset(int c) { cv_v(c); return corner_offset_[c];}
00221 static int opposite_corner(int c) { cv_v(c); return ((c+2) % 4);}
00222
00223
00224 static bool valid_e(int c) { return (0 <= c && c < 4);}
00225 static void cv_e (int c) { REQUIRE(valid_e(c), " c=" << c, 1);}
00226
00227 static int opposite_side(int s) { cv_e(s); return ((s+2) % 4);}
00228 static const index_type& side_offset(int s) { cv_e(s); return side_offset_[s];}
00229 static const index_type& direction(int s) { cv_e(s); return direction_[s];}
00230 static const index_type& outer_normal(int s) { cv_e(s); return side_offset_[s];}
00231
00232 index_type side_vertex1(int s) const { return index_type(llx()+(xpoints-1)*side_vertex_1_[s].x(),
00233 lly()+(ypoints-1)*side_vertex_1_[s].y());}
00234 index_type side_vertex2(int s) const { return index_type(llx()+(xpoints-1)*side_vertex_2_[s].x(),
00235 lly()+(ypoints-1)*side_vertex_2_[s].y());}
00236 public:
00237 typedef RegGrid2D Grid;
00238
00239 typedef GrAL::signed_size_t size_type;
00240 typedef vertex_handle_int<RegGrid2D,RegGrid2D> vertex_handle;
00241 typedef edge_handle_int <RegGrid2D,RegGrid2D> edge_handle;
00242 typedef cell_handle_int <RegGrid2D,RegGrid2D> cell_handle;
00243
00244 cell_handle invalid_cell() const {return TheCellMap().n0() -1;}
00245 cell_handle outer_cell_handle() const {return TheCellMap().n0() -1;}
00246
00247 class Vertex;
00248 class Edge;
00249 class Cell;
00250
00251 class VertexIterator;
00252 class EdgeIterator;
00253 class CellIterator;
00254 class VertexOnVertexIterator;
00255 class CellOnVertexIterator;
00256
00257 class VertexOnCellIterator;
00258 class EdgeOnCellIterator;
00259 class CellOnCellIterator;
00260
00261 typedef vertex_on_edge_iterator<RegGrid2D,grid_types_cart2d> VertexOnEdgeIterator;
00262 typedef Cell Face;
00263 typedef Edge Facet;
00264
00266 unsigned dimension() const { return 2;}
00267
00272 void cv(vertex_handle e) const { REQUIRE(valid_handle(e), "",1);}
00273 bool valid_handle(vertex_handle e) const { return (0 <= e && e < (int)NumOfVertices()); }
00274 void cv(edge_handle e) const { REQUIRE(valid_handle(e), "",1);}
00275 bool valid_handle(edge_handle e) const { return (0 <= e && e < (int)NumOfEdges()); }
00276 void cv(cell_handle e) const { REQUIRE(valid_handle(e), "",1);}
00277 bool valid_handle(cell_handle e) const { return (0 <= e && e < (int)NumOfCells()); }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00301 class elem_base {
00302 public:
00303 typedef RegGrid2D grid_type;
00304 typedef RegGrid2D anchor_type;
00305 typedef grid_type::index_type index_type;
00306
00307 elem_base() : _g((const Grid*)0) {}
00308 elem_base(const Grid* g) :_g(g) {}
00309
00310 grid_type const& TheGrid() const {
00311 REQUIRE((_g != 0),"No Grid!\n",1);
00312 return (*_g);
00313 }
00314 grid_type const& TheAnchor() const { return TheGrid();}
00315
00316 bool bound() const { return _g != 0;}
00317 protected:
00318 const Grid* _g;
00319 };
00320
00321 typedef index_type vertex_base;
00322
00323
00324
00325 class Vertex : public elem_base {
00326 typedef Vertex self;
00327 friend class Edge;
00328 public:
00329 typedef vertex_type_tag element_type_tag;
00330 typedef grid_vertex_category category;
00331 typedef VertexOnVertexIterator VertexIterator;
00332 typedef VertexOnVertexIterator NeighbourIterator;
00333
00334 Vertex() {}
00335 Vertex(Grid const& g, vertex_handle v) { *this = g.vertex(v);}
00336 Vertex(ref_ptr<Grid const> g, vertex_handle v) { *this = g->vertex(v);}
00337
00338 Vertex(const vertex_base& v, const Grid* g) : elem_base(g), _v(v) {}
00339 Vertex(const vertex_base& v, const Grid& g) : elem_base(&g), _v(v) {}
00340 Vertex(Grid const& g, int xx, int yy) : elem_base(&g), _v(xx,yy) {}
00341 int x() const { return _v.x();}
00342 int y() const { return _v.y();}
00343
00344 inline VertexOnVertexIterator FirstVertex() const;
00345 inline VertexOnVertexIterator EndVertex() const;
00346 inline VertexOnVertexIterator FirstNeighbour() const;
00347 inline VertexOnVertexIterator EndNeighbour() const;
00348 inline CellOnVertexIterator FirstCell() const;
00349 inline CellOnVertexIterator EndCell() const;
00350
00351 int NumOfVertices() const {
00352 return 4 - ( (x() > _g->ll().x() ? 0 : 1) +
00353 (x() < _g->ur().x() ? 0 : 1) +
00354 (y() > _g->ll().y() ? 0 : 1) +
00355 (y() < _g->ur().y() ? 0 : 1));
00356 }
00357 int NumOfCells() const {
00358 return 4 - ( (x() > _g->llx() && y() > _g->lly() ? 0 : 1) +
00359 (x() < _g->urx() && y() > _g->lly() ? 0 : 1) +
00360 (x() > _g->llx() && y() < _g->ury() ? 0 : 1) +
00361 (x() < _g->urx() && y() < _g->ury() ? 0 : 1));
00362 }
00363 int next_neighbour(int nb) const {
00364 if( !TheGrid().IsOnBoundary(*this)) return ++nb;
00365 do { nb++; } while( (nb <= 3) && (! TheGrid().IsValid(get_nb_base(nb))));
00366 return nb;
00367 }
00368
00369 int next_cell(int c) const {
00370 if( !TheGrid().IsOnBoundary(*this)) return ++c;
00371 do { c++; } while( (c <= 3) && (! TheGrid().IsValidCellBase(get_cell_base(c))));
00372 return c;
00373 }
00374
00375 vertex_base get_nb_base(int nb) const {
00376 return vertex_base(x() + TheGrid().side_offset((int)nb).x(),
00377 y() + TheGrid().side_offset((int)nb).y());
00378 }
00379
00380 vertex_base get_cell_base(int c) const {
00381 return vertex_base(x() + TheGrid().corner_offset((int)c).x() -1,
00382 y() + TheGrid().corner_offset((int)c).y() -1);
00383 }
00384
00385 Vertex vertex(int nb) const { return Vertex(get_nb_base(nb),TheGrid());}
00386 inline Cell cell(int c) const;
00387
00388 friend bool operator==(const self& lhs, const self& rhs)
00389 {return (lhs.GlobalNumber() == rhs.GlobalNumber());}
00390 friend bool operator!=(const self& lhs, const self& rhs)
00391 { return ! (lhs == rhs);}
00392 friend bool operator<(const self& lhs, const self& rhs)
00393 {return (lhs.GlobalNumber() < rhs.GlobalNumber());}
00394
00395 vertex_handle GlobalNumber() const {return TheGrid().vertex_num(_v);}
00396 vertex_handle handle () const {return TheGrid().vertex_num(_v);}
00397
00398 const index_type& index() const { return _v;}
00399
00400 bool valid() const { return bound() && TheGrid().IsValid(_v);}
00401 protected:
00402 index_type _v;
00403 };
00404
00405
00406
00407 class Edge : public elem_base {
00408 public:
00409 typedef edge_type_tag element_type_tag;
00410 typedef grid_edge_category category;
00411
00412 enum direction {x_dir,y_dir};
00413
00414 friend class RegGrid2D;
00415 typedef RegGrid2D Grid;
00416 typedef Edge self;
00417
00418 Edge() {}
00419 Edge(Grid const& g, edge_handle e) { *this = g.edge(e);}
00420 Edge(ref_ptr<Grid const> g, edge_handle e) { *this = g->edge(e);}
00421
00422 Edge(direction d, const vertex_base& v, const Grid* g)
00423 : elem_base(g), dir(d), v1_(v) {}
00424 Edge(direction d, const vertex_base& v, const Grid& g)
00425 : elem_base(&g), dir(d), v1_(v) {}
00426 Edge(const Vertex& w1, const Vertex& w2) : elem_base(&(w1.TheGrid()))
00427 { init(w1._v,w2._v);}
00428 Edge(const vertex_base& w1, const vertex_base& w2, const Grid& g) : elem_base(&g)
00429 { init(w1,w2);}
00430
00431 void init(const vertex_base& w1, const vertex_base& w2)
00432 {
00433 REQUIRE(( (w1.x() == w2.x()) && (w1.y() == w2.y()+1 || w1.y() == w2.y()-1)
00434 || (w1.y() == w2.y()) && (w1.x() == w2.x()+1 || w1.x() == w2.x()-1)),
00435 "Edge(w1,w2): (w1,w2) = (" << w1 << ',' << w2 << ')' << "is no edge!\n",1);
00436 if(w1.y() == w2.y()) {
00437 dir = x_dir;
00438 v1_ = ( w1.x() < w2.x() ? w1 : w2);
00439 }
00440 else {
00441 dir = y_dir;
00442 v1_ = ( w1.y() < w2.y() ? w1 : w2);
00443 }
00444 }
00445
00446 Edge(const CellIterator&, const CellOnCellIterator& nb);
00447 Edge(const Cell&, const CellOnCellIterator& nb);
00448 explicit Edge(const CellOnCellIterator& nb);
00449
00450 unsigned NumOfVertices() const { return 2;}
00451 Vertex V1() const { return Vertex(v1_,_g);}
00452 Vertex V2() const {
00453 return Vertex((dir==x_dir
00454 ? vertex_base(v1_.x()+1,v1_.y())
00455 : vertex_base(v1_.x(), v1_.y()+1)),TheGrid());
00456 }
00457 vertex_handle v1() const { return V1().handle();}
00458 vertex_handle v2() const { return V2().handle();}
00459
00460 Vertex V(int i) const {
00461 REQUIRE( (i == 0 || i == 1), "Edge::V(i): i must be 0 or 1! (i = " << i << ")\n",1);
00462 return ( i == 0 ? V1() : V2()); }
00463
00464 void FlipVertex(Vertex& V) const {
00465 REQUIRE( (V == V1() || V == V2()), "FlipVertex(): Vertex not on Edge!\n",1);
00466 V = ( V == V1() ? V2() : V1());
00467 }
00468 Vertex FlippedVertex(const Vertex& V) const {
00469 REQUIRE( (V == V1() || V == V2()), "FlipVertex(): Vertex not on Edge!\n",1);
00470 return ( V == V1() ? V2() : V1());
00471 }
00472 VertexOnEdgeIterator FirstVertex() const;
00473 VertexOnEdgeIterator EndVertex() const;
00474
00475 index_type index() const { return v1_;}
00476 index_type low_vertex_index() const { return index();}
00477 index_type high_vertex_index() const { return index() + (dir == x_dir ? index_type(1,0) : index_type(0,1));}
00478
00479 inline void FlipCell(Cell& C) const;
00480 inline Cell FlippedCell(Cell& C) const;
00481 inline Cell C1() const;
00482 inline Cell C2() const;
00483
00484
00485 friend bool operator==(const self& lhs, const self& rhs)
00486 {return (lhs.GlobalNumber() == rhs.GlobalNumber());}
00487 friend bool operator!=(const self& lhs, const self& rhs)
00488 { return !(lhs == rhs);}
00489 friend bool operator<(const self& lhs, const self& rhs)
00490 {return (lhs.GlobalNumber() < rhs.GlobalNumber());}
00491
00492 edge_handle GlobalNumber() const { return TheGrid().edge_num(*this); }
00493 edge_handle handle () const { return TheGrid().edge_num(*this); }
00494
00495 bool valid() const { return bound() && TheGrid().IsValid(low_vertex_index()) && TheGrid().IsValid(high_vertex_index()) ;}
00496 private:
00497 direction dir;
00498 vertex_base v1_;
00499 };
00500
00501
00502
00503 class Cell : public elem_base {
00504 public:
00505 typedef cell_type_tag element_type_tag;
00506 typedef grid_cell_category category;
00507
00508 friend class RegGrid2D;
00509 typedef Cell self;
00510 typedef Cell Face;
00511 typedef Edge Facet;
00512 typedef grid_types<Grid::archetype_type> archgt;
00513 typedef index_type local_index_type;
00514
00515 enum side { S = 0, E = 1, N = 2, W = 3, invalid_side = 4};
00516 enum corner { SW = 0, SE = 1, NE = 2, NW = 3, invalid_corner = 4};
00517
00518 Cell() : elem_base(0) {}
00519 Cell(const Grid& g, cell_handle c) { *this = g.cell(c); }
00520 Cell(ref_ptr<Grid const> g, cell_handle c) { *this = g->cell(c); }
00521
00522 Cell(const Grid* g, const vertex_base& b) : elem_base(g), llv(b) {}
00523 Cell(const Grid& g, const vertex_base& b) : elem_base(&g), llv(b) {}
00524 Cell(const Grid& g, int x, int y) : elem_base(&g), llv(x,y) {}
00525
00526 inline VertexOnCellIterator FirstVertex() const;
00527 inline VertexOnCellIterator EndVertex() const;
00528 inline EdgeOnCellIterator FirstEdge() const;
00529 inline EdgeOnCellIterator EndEdge() const;
00530 inline EdgeOnCellIterator FirstFacet() const;
00531 inline EdgeOnCellIterator EndFacet() const;
00532 inline CellOnCellIterator FirstCell() const;
00533 inline CellOnCellIterator EndCell() const;
00534 inline CellOnCellIterator FirstFace() const;
00535 inline CellOnCellIterator EndFace() const;
00536 inline CellOnCellIterator FirstNeighbour() const;
00537 inline CellOnCellIterator EndNeighbour() const;
00538
00539 int NumOfVertices() const {return 4;}
00540 int NumOfNeighbours() const {return 4- NumOfBoundaryFacets();}
00541 int NumOfEdges() const {return 4;}
00542 int NumOfFacets() const {return 4;}
00543 int NumOfBoundaryFacets() const
00544 {
00545 int n = 0;
00546 n += ((llv.x() == TheGrid().llx()) ? 1 : 0);
00547 n += ((llv.y() == TheGrid().lly()) ? 1 : 0);
00548 n += ((llv.x()+1 == TheGrid().urx()) ? 1 : 0);
00549 n += ((llv.y()+1 == TheGrid().ury()) ? 1 : 0);
00550 return n;
00551 }
00552 int NumOfCells() const { return 4 - NumOfBoundaryFacets();}
00553 int NumOfFaces() const { return NumOfCells();}
00554
00555
00556 VertexOnCellIterator FirstElement(tp<Vertex>) const { return FirstVertex();}
00557 VertexOnCellIterator EndElement (tp<Vertex>) const { return EndVertex();}
00558 EdgeOnCellIterator FirstElement(tp<Edge >) const { return FirstEdge();}
00559 EdgeOnCellIterator EndElement (tp<Edge >) const { return EndEdge();}
00560 CellOnCellIterator FirstElement(tp<Cell >) const { return FirstCell();}
00561 CellOnCellIterator EndElement (tp<Cell >) const { return EndCell();}
00562
00563 template<class EE>
00564 typename incidence_iterator<grid_type, typename EE::element_type_tag, cell_type_tag>::type
00565 FirstElement() const { return FirstElement(tp<EE>());}
00566 template<class EE>
00567 typename incidence_iterator<grid_type, typename EE::element_type_tag, cell_type_tag>::type
00568 EndElement() const { return EndElement(tp<EE>());}
00569
00570
00571 int NumOf(tp<Vertex>) const { return NumOfVertices();}
00572 int NumOf(tp<Edge >) const { return NumOfEdges ();}
00573 int NumOf(tp<Cell >) const { return NumOfCells ();}
00574 template<class EE> int NumOf() const { return NumOf(tp<EE>());}
00575
00576
00577 friend bool operator==(const self& lhs, const self& rhs)
00578 {return (lhs.llv == rhs.llv);}
00579 friend bool operator!=(const self& lhs, const self& rhs)
00580 { return !(lhs == rhs);}
00581 friend bool operator<(const self& lhs, const self& rhs)
00582 {return (lhs.GlobalNumber() < rhs.GlobalNumber());}
00583
00584 side opposite(side s ) const { return side ((s+2)%4);}
00585 corner opposite(corner c) const { return corner((c+2)%4);}
00586 bool IsValid(side s) const { return ((S <= s) && (s <= W));}
00587 bool IsValid(corner c) const { return ((SW <= c) && (c <= NW));}
00588
00589 cell_handle GlobalNumber() const { return TheGrid().cell_num(llv);}
00590 cell_handle handle () const { return TheGrid().cell_num(llv);}
00591
00592
00593
00594 Vertex V(corner i) const { return vertex(i);}
00595 Vertex V(int i) const { return vertex(corner(i));}
00596 Vertex V (archgt::Vertex const& lV) const { return V(lV.handle());}
00597 vertex_handle v(archgt::Vertex const& lV) const { return V(lV).handle();}
00598 vertex_handle v(archgt::vertex_handle lv) const { return V(lv).handle();}
00599
00602 Vertex vertex(corner v) const
00603 {
00604 REQUIRE(((0<=v) && (v<4)),"Cartesian2D::Cell::vertex(v) : v = " << (int)v , 1);
00605 return Vertex(vertex_base(llv.x() + TheGrid().corner_offset((int)v).x(),
00606 llv.y() + TheGrid().corner_offset((int)v).y()),
00607 TheGrid());
00608 }
00609
00610 vertex_base ll() const { return llv;}
00611 index_type index() const { return llv;}
00612
00613 Edge edge(side e) const
00614 {
00615 REQUIRE(((S<=e) && (e<=W)),"Cartesian2D::Cell::edge(e) : e = " << (int)e , 1);
00616 return ( e == S ? Edge(Edge::x_dir,llv,TheGrid())
00617 : e == E ? Edge(Edge::y_dir,vertex_base(llv.x()+1,llv.y()),TheGrid())
00618 : e == N ? Edge(Edge::x_dir,vertex_base(llv.x(), llv.y()+1),TheGrid())
00619 : Edge(Edge::y_dir,llv,TheGrid()));
00620 }
00621
00622 Cell neighbour(side nb) const {
00623 REQUIRE(((S<=nb) && (nb<=W)),"Cartesian2D::Cell::neighbour(nb) : nb = " << (int) nb , 1);
00624 vertex_base bv = get_nb_base(nb);
00625 REQUIRE(TheGrid().IsValidCellBase(bv),
00626 "Cell::Neighbour(nb) : nb = " << (int)nb << " not in grid!\n"
00627 << " llv = " << llv << " bv = " << bv << '\n',1);
00628 return Cell(TheGrid(),bv);
00629 }
00630 Cell Nb(side nb) const { return neighbour(nb);}
00631
00632 bool IsOnBoundary(side nb) const { return TheGrid().IsOnBoundary(edge(nb));}
00633 bool IsOnBoundary(int nb) const { return IsOnBoundary(side(nb));}
00634
00635 inline void FlipEdge(const Vertex& v, Edge& e) const;
00636
00637 typedef grid_type::archetype_type archetype_type;
00638 archetype_type const& TheArchetype() const { return TheGrid().ArchetypeOf(*this);}
00639 protected:
00640
00641 vertex_base get_nb_base(side nb) const {
00642 return vertex_base(llv.x() + TheGrid().side_offset((int)nb).x(),
00643 llv.y() + TheGrid().side_offset((int)nb).y());
00644 }
00645
00647 vertex_base llv;
00648 };
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00667 class seq_iterator_base {
00668 public:
00669 typedef RegGrid2D grid_type;
00670 typedef RegGrid2D anchor_type;
00671
00672 typedef seq_iterator_base base;
00673 seq_iterator_base() : _g(0) {}
00674 seq_iterator_base(const grid_type* g) : _g(g) {}
00675 seq_iterator_base(const grid_type& g) : _g(&g) {}
00676
00677 grid_type const& TheGrid() const { cb(); return (*_g); }
00678 grid_type const& TheAnchor() const { return TheGrid();}
00679
00680 bool bound() const { return _g != 0;}
00681 void cb() const { REQUIRE(bound(), "", 1);}
00682 protected:
00683 const grid_type* _g;
00684 };
00685
00686
00687
00688 class VertexIterator : public seq_iterator_base {
00689 typedef VertexIterator self;
00690 public:
00691 typedef grid_vertex_iterator_category category;
00692 typedef Vertex value_type;
00693 typedef Vertex element_type;
00694
00695 VertexIterator() : base(0) {}
00696 VertexIterator(const Grid* g) : base(g), v(g->MinVertexNum()) {}
00697 explicit
00698 VertexIterator(const Grid& g) : base(g), v(g .MinVertexNum()) {}
00699 VertexIterator(const Grid& g, vertex_handle vv) : base(&g), v(vv) {}
00700
00701
00702 self& operator++() { ++v; return (*this); }
00703 self operator++(int) { cv(); self tmp(*this); ++(*this); return tmp;}
00704 self& operator +=(const index_type& ij) {
00705 cv(); v+= TheGrid().TheVertexMap().offset(ij);
00706 return *this;
00707 }
00708
00709 Vertex operator*() const { cv(); return TheGrid().vertex(v);}
00710
00711
00712
00713
00714
00715
00716 bool IsDone() const { cb(); return (v > TheGrid().MaxVertexNum());}
00717
00718 vertex_handle GlobalNumber() const { cv(); return v;}
00719 vertex_handle handle () const { cv(); return v;}
00720
00721 friend bool operator==(self const& lhs, self const& rhs)
00722 {
00723 lhs.cb(); rhs.cb();
00724 REQUIRE((&(lhs.TheGrid()) == &(rhs.TheGrid())), "Comparing vertices with different grids!\n",1);
00725 return (lhs.v == rhs.v);
00726 }
00727 friend bool operator!=(const self& lhs, const self& rhs)
00728 { return !(lhs == rhs);}
00729
00730 bool valid() const { return bound() && TheGrid().valid_handle(v);}
00731 void cv() const { REQUIRE(valid(), "handle= " << v, 1);}
00732
00733 static self begin(anchor_type const& a) { return self(a,0);}
00734 static self end (anchor_type const& a) { return self(a,a.NumOf<value_type>());}
00735 static int size (anchor_type const& a) { return a.NumOf<value_type>();}
00736 private:
00737 vertex_handle v;
00738 };
00739
00740
00741
00742
00743 class EdgeIterator : public seq_iterator_base {
00744 typedef EdgeIterator self;
00745 public:
00746 typedef grid_edge_iterator_category category;
00747 typedef Edge value_type;
00748 typedef Edge element_type;
00749
00750 EdgeIterator() {}
00751 explicit
00752 EdgeIterator(Grid const& g) : base(g), e(g.MinEdgeNum()) {}
00753 EdgeIterator(Grid const& g, int ee) : base(g), e(ee) {}
00754
00755 self& operator++() { cv(); ++e; return(*this);}
00756 Edge operator*() const { cv(); return TheGrid().get_edge(e);}
00757 bool IsDone() const { cb(); return ( e >= TheGrid().NumOfEdges());}
00758
00759
00760
00761
00762 edge_handle handle () const { return e;}
00763
00764 friend bool operator==(self const& lhs, self const& rhs)
00765 {
00766 lhs.cb(); rhs.cb();
00767 REQUIRE((&(lhs.TheGrid()) == &(rhs.TheGrid())), "Comparing edges with different grids!\n",1);
00768 return (lhs.e == rhs.e);
00769 }
00770 friend bool operator!=(const self& lhs, const self& rhs)
00771 { return !(lhs == rhs);}
00772
00773 bool valid() const { return bound() && TheGrid().valid_handle(e);}
00774 void cv() const { REQUIRE(valid(), "handle= " << e, 1);}
00775
00776 static self begin(anchor_type const& a) { return self(a,0);}
00777 static self end (anchor_type const& a) { return self(a,a.NumOf<value_type>());}
00778 static int size (anchor_type const& a) { return a.NumOf<value_type>();}
00779
00780 private:
00781 edge_handle e;
00782 };
00783
00784
00785
00786
00787 class CellIterator : public seq_iterator_base {
00788 typedef CellIterator self;
00789 public:
00790 typedef grid_cell_iterator_category category;
00791 typedef Cell value_type;
00792 typedef Cell element_type;
00793
00794 CellIterator() : base(0), c(-1) {}
00795 explicit
00796 CellIterator(const Grid& g) : base(&g), c(g.MinCellNum()) {}
00797
00798 CellIterator(const Grid& g, int cc) : base(&g), c(cc) {}
00799
00800 self& operator++() { cv(); ++c; return (*this);}
00801 Cell operator*() const { cv(); return TheGrid().cell(c);}
00802
00803 bool IsDone() const { cb(); return (c > TheGrid().MaxCellNum());}
00804
00805
00806 cell_handle handle () const { cv(); return c;}
00807
00808 friend bool operator==(self const& lhs, self const& rhs)
00809 {
00810 lhs.cb(); rhs.cb();
00811 REQUIRE((&(lhs.TheGrid()) == &(rhs.TheGrid())), "Comparing cells with different grids!\n",1);
00812 return (lhs.c == rhs.c);
00813 }
00814 friend bool operator!=(const self& lhs, const self& rhs)
00815 { return !(lhs == rhs);}
00816
00817 bool valid() const { return bound() && TheGrid().valid_handle(c);}
00818 void cv() const { REQUIRE(valid(), "handle= " << c, 1);}
00819
00820 static self begin(anchor_type const& a) { return self(a,0);}
00821 static self end (anchor_type const& a) { return self(a,a.NumOf<value_type>());}
00822 static int size (anchor_type const& a) { return a.NumOf<value_type>();}
00823 private:
00824 cell_handle c;
00825 };
00827
00828
00830
00831
00832
00834
00837 class inc_iterator_base {
00838 public:
00839 typedef RegGrid2D grid_type;
00840
00841 typedef inc_iterator_base base;
00842 inc_iterator_base() : _g(0) {}
00843 inc_iterator_base(const grid_type* g) : _g(g) {}
00844 inc_iterator_base(const grid_type& g) : _g(&g) {}
00845
00846 grid_type const& TheGrid() const { cb(); return *_g; }
00847
00848 bool bound() const { return _g != 0;}
00849 void cb() const { REQUIRE(bound(), "", 1);}
00850 protected:
00851 const grid_type* _g;
00852 };
00853
00854
00855
00856
00857 class VertexOnVertexIterator : public inc_iterator_base {
00858 typedef VertexOnVertexIterator self;
00859 public:
00860 typedef grid_incidence_iterator_category_d<0,0> category;
00861 typedef Vertex anchor_type;
00862 typedef Vertex value_type;
00863 typedef Vertex element_type;
00864
00865 VertexOnVertexIterator() {}
00866 explicit
00867 VertexOnVertexIterator(Vertex const& vv) { *this = vv.FirstVertex(); }
00868 VertexOnVertexIterator(int v, const Vertex& vv, const Grid* g)
00869 : base(g), _v(v), _vv(vv) {}
00870 self& operator++() { cv(); _v = _vv.next_neighbour(_v); return (*this);}
00871 self operator++(int) { cv(); self tmp(*this); ++(*this); return tmp;}
00872
00873 Vertex operator*() const { cv(); return (_vv.vertex(_v));}
00874
00875 bool IsDone() const { cb(); return (_v >= 4);}
00876
00877
00878 Vertex const& TheVertex() const { cb(); return _vv;}
00879 Vertex const& TheAnchor() const { cb(); return _vv;}
00880
00881 int LocalNumber() const { cv(); return _v;}
00882
00883 friend bool operator==(self const& lhs, self const& rhs)
00884 {
00885 lhs.cb(); rhs.cb();
00886 REQUIRE((lhs.TheVertex() == rhs.TheVertex()),
00887 "Comparing VertexOnVertexIterator with different vertex anchors!\n",1);
00888 return (lhs._v == rhs._v);
00889 }
00890 friend bool operator!=(const self& lhs, const self& rhs) { return !(lhs == rhs);}
00891
00892 bool valid() const { return bound() && ! IsDone();}
00893 void cv() const { REQUIRE(valid(), "vv=" << _vv.index() << " v=" << _v, 1);}
00894 private:
00895 int _v;
00896 Vertex _vv;
00897 };
00898
00899
00900
00901 class CellOnVertexIterator : public inc_iterator_base {
00902 typedef CellOnVertexIterator self;
00903 public:
00904 typedef grid_incidence_iterator_category_d<2,0> category;
00905 typedef Vertex anchor_type;
00906 typedef Cell value_type;
00907 typedef Cell element_type;
00908
00909 CellOnVertexIterator() {}
00910 explicit
00911 CellOnVertexIterator(Vertex const& vv) { *this = vv.FirstCell();}
00912 CellOnVertexIterator(int c, const Vertex& vv, const Grid* g)
00913 : base(g), _c(c), _vv(vv) {}
00914 self& operator++() {cv(); _c = _vv.next_cell(_c); return (*this);}
00915 self operator++(int) { cv(); self tmp(*this); ++(*this); return tmp;}
00916 Cell operator*() const { cv(); return (_vv.cell(_c));}
00917 bool IsDone() const { cb(); return (_c >= 4);}
00918
00919 int LocalNumber() const { cv(); return _c;}
00920
00921 Vertex const& TheVertex() const { cb(); return _vv;}
00922 Vertex const& TheAnchor() const { cb(); return _vv;}
00923
00924 friend bool operator==(self const& lhs, self const& rhs)
00925 {
00926 lhs.cb(); rhs.cb();
00927 REQUIRE((lhs.TheVertex() == rhs.TheVertex()),
00928 "Comparing CellOnVertexIterator with different vertex anchors!\n",1);
00929 return (lhs._c == rhs._c);
00930 }
00931 friend bool operator!=(const self& lhs, const self& rhs) { return !(lhs == rhs);}
00932
00933 bool valid() const { return bound() && ! IsDone();}
00934 void cv() const { REQUIRE(valid(), "vv=" << _vv.index() << " c=" << _c, 1);}
00935 private:
00936 int _c;
00937 Vertex _vv;
00938 };
00939
00940
00941
00942
00943 class VertexOnCellIterator : public inc_iterator_base {
00944 typedef VertexOnCellIterator self;
00945 public:
00946 typedef grid_incidence_iterator_category_d<0,2> category;
00947 typedef Cell anchor_type;
00948 typedef Vertex value_type;
00949 typedef Vertex element_type;
00950
00951 VertexOnCellIterator() {}
00952 explicit
00953 VertexOnCellIterator(const Cell& cc)
00954 : base(& cc.TheGrid()), v(Cell::SW), c(cc) {}
00955 VertexOnCellIterator(const Cell& cc, int vv) : base(& cc.TheGrid()), v(vv), c(cc) {}
00956
00957 self& operator++() { cv(); ++v; return (*this);}
00958 self operator++(int) { cv(); self tmp(*this); ++(*this); return tmp;}
00959 Vertex operator*() const { cv(); return (c.vertex(Cell::corner(v)));}
00960
00961 bool IsDone() const { cb(); return (v >= c.NumOfVertices());}
00962
00963 int LocalNumber() const { cv(); return v;}
00964 int local_handle() const { cv(); return v;}
00965
00966 vertex_handle handle() const { cv(); return c.vertex(Cell::corner(v)).handle();}
00967
00968 self CyclicSucc() const { cv(); self tmp(*this); tmp.v = Cell::corner(tmp.v == 3 ? 0 : tmp.v+1); return tmp;}
00969 self CyclicPred() const { cv(); self tmp(*this); tmp.v = Cell::corner(tmp.v == 0 ? 3 : tmp.v-1); return tmp;}
00970
00971 Cell const& TheCell() const { cb(); return c;}
00972 Cell const& TheAnchor() const { cb(); return c;}
00973
00974 friend
00975 bool operator==(self const& lhs, self const& rhs)
00976 {
00977 lhs.cb(); rhs.cb();
00978 REQUIRE( (lhs.c == rhs.c), "Comparing VertexOnCellIterators with different anchor cells!\n",1);
00979 return ((int) lhs.v == (int) rhs.v);
00980 }
00981 friend bool operator!=(const self& lhs, const self& rhs) { return !(lhs == rhs);}
00982
00983 bool valid() const { return bound() && ! IsDone();}
00984 void cv() const { REQUIRE(valid(), "c=" << c.index() << " v=" << v, 1);}
00985
00986 static self begin(anchor_type const& a) { return self(a,0);}
00987 static self end (anchor_type const& a) { return self(a,a.NumOf<value_type>());}
00988 static int size (anchor_type const& a) { return a.NumOf<value_type>();}
00989
00990 private:
00991 int v;
00992 Cell c;
00993 };
00994
00995
00996
00997 class EdgeOnCellIterator : public inc_iterator_base {
00998 typedef EdgeOnCellIterator self;
00999 friend class CellOnCellIterator;
01000 friend class RegGrid2D;
01001 typedef grid_types<RegGrid2D::archetype_type> archgt;
01002 typedef archgt::Cell archetype_cell;
01003 public:
01004 typedef grid_incidence_iterator_category_d<1,2> category;
01005 typedef Edge value_type;
01006 typedef Edge element_type;
01007 typedef Cell anchor_type;
01008
01009 EdgeOnCellIterator() {}
01010 explicit
01011 EdgeOnCellIterator(Cell const& cc) { *this = cc.FirstEdge();}
01012 EdgeOnCellIterator(Cell const& cc, int ee) : base(cc.TheGrid()), e(ee), c(cc) {}
01013
01014 self& operator++() { ++e; return (*this);}
01015 self operator++(int) { self tmp(*this); ++(*this); return tmp;}
01016 Edge operator*() const { return (TheCell().edge(Cell::side(e)));}
01017
01018 edge_handle handle() const { return TheCell().edge(Cell::side(e)).handle();}
01019 bool IsDone() const { return (e >= c.NumOfEdges());}
01020 operator bool() const { return !IsDone();}
01021
01022 int LocalNumber() const { return e;}
01023 archgt::edge_handle local_handle() const { cv(); return e;}
01024
01025 Cell OtherCell() const { return TheCell().Nb(Cell::side(e));}
01026 Vertex V1() const { return c.V(e);}
01027 Vertex V2() const { return c.V((e+1)%4);}
01028
01029 self CyclicSucc() const
01030 { self tmp(*this); tmp.e = (tmp.e == 3 ? 0 : tmp.e+1); return tmp;}
01031 self CyclicPred() const
01032 { self tmp(*this); tmp.e = (tmp.e == 0 ? 3 : tmp.e-1); return tmp;}
01033
01034 Cell const& TheCell() const { cb(); return c;}
01035 Cell const& TheAnchor() const { cb(); return c;}
01036 archetype_cell ArchetypeCell() const { return archetype_cell(TheGrid().ArchetypeOf(c), e);}
01037
01038 friend
01039 bool operator==(self const& lhs, self const& rhs)
01040 {
01041 lhs.cb(); rhs.cb();
01042 REQUIRE( (lhs.c == rhs.c), "Comparing EdgeOnCellIterators with different anchor cells!\n",1);
01043 return ((int) lhs.e == (int) rhs.e);
01044 }
01045 friend bool operator!=(const self& lhs, const self& rhs) { return !(lhs == rhs);}
01046
01047 bool valid() const { return bound() && ! IsDone();}
01048 void cv() const { REQUIRE(valid(), "c=" << c.index() << " e=" << e, 1);}
01049
01050 static self begin(anchor_type const& a) { return self(a,0);}
01051 static self end (anchor_type const& a) { return self(a,a.NumOf<value_type>());}
01052 static int size (anchor_type const& a) { return a.NumOf<value_type>();}
01053 private:
01054 int e;
01055 Cell c;
01056 };
01057
01058
01059
01060
01061 class CellOnCellIterator : public inc_iterator_base {
01062 friend class RegGrid2D;
01063 typedef CellOnCellIterator self;
01064
01065 public:
01066 typedef grid_incidence_iterator_category_d<2,2> category;
01067 typedef Cell anchor_type;
01068 typedef Cell element_type;
01069 typedef Cell value_type;
01070
01071 CellOnCellIterator() {}
01072 explicit
01073 CellOnCellIterator(Cell const& cc) { *this = cc.FirstCell();}
01074 CellOnCellIterator(Cell const& cc, int n) : base(cc.TheGrid()), nb(n), c(cc)
01075 { advance_till_valid(); }
01076 CellOnCellIterator(const EdgeOnCellIterator& F) : base(F), nb(F.e), c(F.c)
01077 { advance_till_valid(); }
01078 self& operator++() {
01079 ++nb;
01080 advance_till_valid();
01081 return (*this);
01082 }
01083 self operator++(int) { self tmp(*this); ++(*this); return tmp;}
01084 Cell operator*() const { return TheCell().neighbour(Cell::side(nb));}
01085 cell_handle handle() const { return (operator*()).handle();}
01086
01087
01088 Edge facet() const { return c.edge(Cell::side(LocalNumber()));}
01089 Edge TheFacet() const { return facet();}
01090
01091 bool IsDone() const { return (nb >= 4);}
01092
01093 int LocalNumber() const { return nb;}
01094
01095 Cell const& TheCell() const { return c;}
01096 Cell const& TheAnchor() const { return c;}
01097
01098 friend bool operator==(self const& lhs, self const& rhs) {
01099 lhs.cb(); rhs.cb();
01100 REQUIRE( (lhs.c == rhs.c), "Comparing CellOnCellIterators with different anchor cells!\n",1);
01101 return ((int) lhs.nb == (int) rhs.nb);
01102 }
01103 friend bool operator!=(const self& lhs, const self& rhs) { return !(lhs == rhs);}
01104
01105 bool valid() const { return bound() && ! IsDone();}
01106 void cv() const { REQUIRE(valid(), "c=" << c.index() << " nb=" << nb, 1);}
01107
01108
01109
01110
01111 static self begin(anchor_type const& a) { return self(a,0);}
01112 static self end (anchor_type const& a) { return self(a,a.NumOf<value_type>());}
01113 static int size (anchor_type const& a) { return a.NumOf<value_type>();}
01114 private:
01115 int nb;
01116 Cell c;
01117 void advance_till_valid() {
01118 while( ! IsDone() && c.IsOnBoundary(nb))
01119 ++(*this);
01120 }
01121 };
01122 typedef CellOnCellIterator CellNeighbourIterator;
01124
01125
01126
01127
01128
01130
01133
01134 size_type NumOfXVertices() const { return xpoints;}
01135 size_type NumOfYVertices() const { return ypoints;}
01136 size_type NumOfXEdges() const { return xpoints-1;}
01137 size_type NumOfYEdges() const { return ypoints-1;}
01138 size_type NumOfXCells() const { return xpoints-1;}
01139 size_type NumOfYCells() const { return ypoints-1;}
01140
01142 size_type NumOfHorizontalEdges() const { return TheXEdgeMap().range_size();}
01144 size_type NumOfVerticalEdges() const { return TheYEdgeMap().range_size();}
01146
01147 size_type Offset(const Vertex&) const {return TheVertexMap().n0();}
01148 size_type Offset(const Edge& ) const {return TheXEdgeMap().n0();}
01149 size_type Offset(const Cell& ) const {return TheCellMap().n0();}
01150
01151
01152
01154
01157 VertexIterator FirstVertex() const { return VertexIterator(*this,MinVertexNum());}
01158 EdgeIterator FirstEdge() const { return EdgeIterator (*this,MinEdgeNum());}
01159 EdgeIterator FirstFacet() const { return EdgeIterator (*this,MinEdgeNum());}
01160 CellIterator FirstFace() const { return FirstCell();}
01161 CellIterator FirstCell() const { return CellIterator (*this,MinCellNum());}
01162 VertexIterator EndVertex() const { return VertexIterator(*this,MaxVertexNum()+1);}
01163 EdgeIterator EndEdge() const { return EdgeIterator (*this,MaxEdgeNum()+1);}
01164 EdgeIterator EndFacet() const { return EdgeIterator (*this,MaxEdgeNum()+1);}
01165 CellIterator EndFace() const { return EndCell();}
01166 CellIterator EndCell() const { return CellIterator (*this,MaxCellNum()+1);}
01167
01168 VertexIterator FirstElement(tp<Vertex>) const { return FirstVertex();}
01169 VertexIterator EndElement (tp<Vertex>) const { return EndVertex();}
01170 EdgeIterator FirstElement(tp<Edge>) const { return FirstEdge();}
01171 EdgeIterator EndElement (tp<Edge>) const { return EndEdge();}
01172 CellIterator FirstElement(tp<Cell>) const { return FirstCell();}
01173 CellIterator EndElement (tp<Cell>) const { return EndCell();}
01174
01175 template<class E>
01176 typename sequence_iterator<self, typename E::element_type_tag>::type
01177 FirstElement() const { return FirstElement(tp<E>());}
01178 template<class E>
01179 typename sequence_iterator<self, typename E::element_type_tag>::type
01180 EndElement() const { return EndElement(tp<E>());}
01181
01182
01183 size_type NumOfVertices() const { return TheVertexMap().range_size();}
01184 size_type NumOfEdges() const { return NumOfHorizontalEdges()+NumOfVerticalEdges();}
01185 size_type NumOfFacets() const { return NumOfEdges();}
01186 size_type NumOfFaces() const { return NumOfCells();}
01187 size_type NumOfCells() const { return TheCellMap().range_size();}
01188
01189 size_type NumOf(tp<Vertex>) const { return NumOfVertices();}
01190 size_type NumOf(tp<Edge >) const { return NumOfEdges ();}
01191 size_type NumOf(tp<Cell >) const { return NumOfCells ();}
01192 template<class E> size_type NumOf() const { return NumOf(tp<E>());}
01194
01196
01199 inline bool IsValid(const vertex_base& v) const { return TheVertexMap().IsInRange(v);}
01200 inline bool IsValidCellBase(const vertex_base& v) const {
01201 return TheCellMap().IsInRange(v);
01202 }
01203 inline bool IsValid(const Cell& C) const;
01205
01207
01210 bool IsOnBoundary(const Vertex& V) const { return IsOnBoundary(V.index());}
01211 inline bool IsOnBoundary(const vertex_base&) const;
01212 inline bool IsOnBoundary(const Edge& E) const;
01213 inline bool IsOnBoundary(const EdgeOnCellIterator& Nb) const;
01214
01215 inline bool IsInside(const CellIterator& C) const;
01216 inline bool IsInside(const Cell& C) const;
01217
01218 size_type NumOfBoundaryVertices() const { return (2*(xpoints + ypoints) -4);}
01219 size_type NumOfBoundaryEdges() const { return (2*(xpoints-1 + ypoints-1));}
01220 size_type NumOfBoundaryFacets() const { return NumOfBoundaryEdges();}
01221 size_type NumOfBoundaryFaces() const { return NumOfBoundaryCells();}
01222 size_type NumOfBoundaryCells() const { return (2*(xpoints-1 + ypoints-1) -4);}
01224
01225
01226 friend class SubrangeReg2D;
01227 friend class Vertex;
01228 friend class Edge;
01229 friend class Cell;
01230
01231 friend class VertexIterator;
01232 friend class EdgeIterator;
01233 friend class CellIterator;
01234 friend class VertexOnVertexIterator;
01235 friend class VertexOnCellIterator;
01236 friend class EdgeOnCellIterator;
01237 friend class CellOnCellIterator;
01238
01244 vertex_handle handle(const Vertex& V) const { return vertex_num(V.x(),V.y());}
01245 vertex_handle handle(const VertexIterator& V) const { return V.GlobalNumber();}
01246 vertex_handle handle(const VertexOnCellIterator& V) const { return handle(*V);}
01247 edge_handle handle(const Edge& E) const { return edge_num(E);}
01248 cell_handle handle(const Cell& C) const { return cell_num(C);}
01249
01250
01251 vertex_handle get_vertex_handle(index_type I) const { return vertex_num(I);}
01252 index_type get_index (vertex_handle h) const { return get_vertex(h);}
01253
01254 vertex_handle vertex_num(int x, int y) const { return TheVertexMap().number(x,y);}
01255 vertex_handle vertex_num(const vertex_base& V) const { return vertex_num(V.x(),V.y());}
01256 vertex_handle vertex_num(const Vertex& V) const { return vertex_num(V.x(),V.y());}
01257 vertex_base get_vertex(vertex_handle v) const { return TheVertexMap().index(v);}
01258 Vertex vertex(vertex_handle v) const { return Vertex(get_vertex(v),this);}
01259 Vertex vertex(const vertex_base& v) const { return Vertex(v,this);}
01260 Vertex vertex(int x, int y) const { return vertex(vertex_base(x,y));}
01261
01262
01263 inline edge_handle edge_num(const Edge& E) const;
01264 inline Edge get_edge(edge_handle e) const;
01265 Edge edge(edge_handle e) const { return get_edge(e);}
01266 Edge xedge(vertex_base leftv) const { return Edge(Edge::x_dir,leftv, *this);}
01267 Edge yedge(vertex_base bottomv) const { return Edge(Edge::y_dir,bottomv,*this);}
01268
01269
01270
01271 cell_handle get_cell_handle(index_type I) const { return cell_num(I);}
01272 index_type get_index (cell_handle h) const { return get_cell_llv(h);}
01273
01274 cell_handle cell_num(int llx, int lly) const { return TheCellMap().number(llx,lly);}
01275 cell_handle cell_num(const vertex_base& llv) const {return cell_num(llv.x(),llv.y());}
01276 cell_handle cell_num(const Cell& C) const { return cell_num(C.ll());}
01277 vertex_base get_cell_llv(cell_handle c) const { return TheCellMap().index(c);}
01278 Cell cell(cell_handle c) const { return Cell(this,get_cell_llv(c));}
01279 Cell cell(vertex_base llv) const { return Cell(this,llv);}
01280 Cell cell(int llx, int lly) const { return Cell(this,vertex_base(llx,lly));}
01282
01287 vertex_handle MinVertexNum() const { return TheVertexMap().n0();}
01288 vertex_handle MaxVertexNum() const { return TheVertexMap().nmax();}
01289 vertex_handle MinNum(const Vertex&) const { return MinVertexNum();}
01290 vertex_handle MaxNum(const Vertex&) const { return MaxVertexNum();}
01291
01292 edge_handle MinEdgeNum() const { return TheXEdgeMap().n0();}
01293 edge_handle MaxEdgeNum() const { return MinEdgeNum() + NumOfEdges() -1;}
01294 edge_handle MinNum(const Edge&) const { return MinEdgeNum();}
01295 edge_handle MaxNum(const Edge&) const { return MaxEdgeNum();}
01296
01297 cell_handle MinCellNum() const { return TheCellMap().n0();}
01298 cell_handle MaxCellNum() const { return TheCellMap().nmax();}
01299 cell_handle MinNum(const Cell&) const { return MinCellNum();}
01300 cell_handle MaxNum(const Cell&) const { return MaxCellNum();}
01301
01302 bool valid(vertex_handle v) const { return MinVertexNum() <= v && v <= MaxVertexNum();}
01303 bool valid(edge_handle e) const { return MinEdgeNum() <= e && e <= MaxEdgeNum();}
01304 bool valid(cell_handle c) const { return MinCellNum() <= c && c <= MaxCellNum();}
01305 bool valid_vertex(vertex_handle v) const { return valid(v);}
01306 bool valid_edge (edge_handle e) const { return valid(e);}
01307 bool valid_cell (cell_handle c) const { return valid(c);}
01308
01310
01316 inline void switch_vertex(Vertex& v, Edge const& e) const
01317 { e.FlipVertex(v);}
01318 inline void switch_edge(Vertex const& v, Edge & e, Cell const& c) const
01319 { c.FlipEdge(v,e);}
01320 inline void switch_facet(Vertex const& v, Edge & e, Cell const& c) const
01321 { switch_edge(v,e,c);}
01322 inline void switch_cell(Edge const& e, Cell & c) const
01323 { e.FlipCell(c);}
01324 inline Vertex switched_vertex(Vertex const& v, Edge const& e) const
01325 { Vertex sv(v); switch_vertex(sv,e); return sv;}
01326 inline Edge switched_edge(Vertex const& v, Edge const& e, Cell const& c) const
01327 { Edge se(e); switch_edge(v,se,c); return se;}
01328 inline Edge switched_facet(Vertex const& v, Edge const& e, Cell const& c) const
01329 { Edge se(e); switch_edge(v,se,c); return se;}
01330 inline Cell switched_cell(Edge const& e, Cell const& c) const
01331 { Cell sc(c); switch_cell(e,sc); return sc;}
01333
01338 static archetype_iterator BeginArchetype() { return sd.the_archetype; }
01339 static archetype_iterator EndArchetype() { return sd.the_archetype+1; }
01340 static archetype_handle handle(archetype_iterator it) { return it - BeginArchetype();}
01341 static archetype_type const& Archetype(archetype_handle = 0) { return *BeginArchetype();}
01342 static archetype_type const& ArchetypeOf (Cell const&)
01343 { return *BeginArchetype();}
01344 static archetype_type const& ArchetypeOf (cell_handle)
01345 { return *BeginArchetype();}
01346 static archetype_handle archetype_of(cell_handle)
01347 { return 0;}
01348 static archetype_handle archetype_of(Cell const&)
01349 { return 0;}
01350 static unsigned NumOfArchetypes() { return 1;}
01351
01354 };
01355
01356
01357
01358 class SubrangeReg2D;
01359
01360
01361 struct grid_types_cart2d : public grid_types_detail::grid_types_root {
01362 typedef grid_types_cart2d self;
01363
01364 typedef cartesian2d::RegGrid2D Grid;
01365 typedef cartesian2d::RegGrid2D grid_type;
01366 typedef Grid::index_type index_type;
01367 typedef Grid::size_type size_type;
01368
01369 typedef Grid::vertex_index_type vertex_index_type;
01370 typedef Grid::cell_index_type cell_index_type;
01371
01372 typedef Grid::Vertex Vertex;
01373 typedef Grid::Edge Edge;
01374 typedef Grid::Edge Facet;
01375 typedef Grid::Cell Cell;
01376 typedef Grid::Cell Face;
01377
01378
01379 typedef Grid::vertex_handle vertex_handle;
01380 typedef Grid::edge_handle edge_handle;
01381 typedef Grid::edge_handle facet_handle;
01382 typedef Grid::cell_handle cell_handle;
01383
01384 typedef Grid::VertexIterator VertexIterator;
01385 typedef Grid::EdgeIterator EdgeIterator;
01386 typedef Grid::EdgeIterator FacetIterator;
01387 typedef Grid::CellIterator CellIterator;
01388
01389 typedef Grid::VertexOnVertexIterator VertexOnVertexIterator;
01390 typedef Grid::VertexOnVertexIterator VertexNeighbourIterator;
01391 typedef Grid::CellOnVertexIterator CellOnVertexIterator;
01392 typedef vertex_on_edge_iterator<Grid,self> VertexOnEdgeIterator;
01393 typedef VertexOnEdgeIterator VertexOnFacetIterator;
01394
01395 typedef Grid::VertexOnCellIterator VertexOnCellIterator;
01396 typedef Grid::EdgeOnCellIterator EdgeOnCellIterator;
01397 typedef Grid::EdgeOnCellIterator FacetOnCellIterator;
01398 typedef Grid::CellOnCellIterator CellOnCellIterator;
01399 typedef Grid::CellOnCellIterator NeighbourCellIterator;
01400 typedef Grid::CellOnCellIterator CellNeighbourIterator;
01401
01402
01403 typedef cell_handle face_handle;
01404 typedef CellIterator FaceIterator;
01405 typedef VertexOnCellIterator VertexOnFaceIterator;
01406 typedef CellOnVertexIterator FaceOnVertexIterator;
01407 typedef EdgeOnCellIterator EdgeOnFaceIterator;
01408
01409
01410
01411 typedef grid_dim_tag<2> dimension_tag;
01412 static int dimension(const Cell& ) { return 2;}
01413 static int dimension(const Facet&) { return 1;}
01414 static int dimension(const Vertex&) { return 0;}
01415
01416 struct hash_facet {
01417 int operator()(const Facet& F) const { return F.GlobalNumber();}
01418 };
01419
01420 static cell_handle invalid_cell_handle(grid_type const& G) { return G.invalid_cell();}
01421 static cell_handle outer_cell_handle (grid_type const& G) { return G.outer_cell_handle();}
01422 static bool is_cell_valid (grid_type const& G, cell_handle c) { return (c !=G.invalid_cell());}
01423 static bool is_cell_inside(grid_type const& G, cell_handle c) { return (c !=G.invalid_cell());}
01424 static bool is_cell_valid (grid_type const& G, Cell const& c) { return (G.IsValid(c));}
01425 static bool is_cell_inside(grid_type const& G, Cell const& c) { return (G.IsInside(c));}
01426
01427
01428 typedef grid_type::archetype_type archetype_type;
01429 typedef grid_type::archetype_handle archetype_handle;
01430 typedef grid_type::archetype_iterator archetype_iterator;
01431 typedef grid_types<archetype_type> archgt;
01432
01433 typedef cartesian2d::SubrangeReg2D cartesian_subrange_type;
01434
01435
01436 template<class E, class A>
01437 static VertexOnEdgeIterator begin_aux(A const& a, grid_vertex_category, grid_edge_category) { return VertexOnEdgeIterator(a);}
01438 template<class E, class A>
01439 static VertexOnEdgeIterator end_aux (A const& a, grid_vertex_category, grid_edge_category) { return VertexOnEdgeIterator(a.EndVertex());}
01440 template<class E, class A>
01441 static int size_aux (A const& a, grid_vertex_category, grid_edge_category) { return a.NumOfVertices();}
01442
01443
01444 };
01445
01446 }
01447
01448
01449
01450
01453 template<>
01454 struct grid_types<cartesian2d::RegGrid2D>
01455 : public grid_types_base<cartesian2d::grid_types_cart2d>
01456 { };
01457
01458 }
01459
01460 #include "Gral/Grids/Cartesian2D/element-traits.h"
01461
01462 namespace GrAL {
01463
01464 namespace cartesian2d {
01465
01467
01468
01469
01471
01472 inline
01473 RegGrid2D::VertexOnVertexIterator
01474 RegGrid2D::Vertex::FirstVertex() const {
01475 if( y() > TheGrid().lly())
01476 return VertexOnVertexIterator(0,*this,&TheGrid());
01477 else if( x() < TheGrid().urx())
01478 return VertexOnVertexIterator(1,*this,&TheGrid());
01479 else if(y() < TheGrid().ury())
01480 return VertexOnVertexIterator(2,*this,&TheGrid());
01481 else if(x() > TheGrid().llx())
01482 return VertexOnVertexIterator(3,*this,&TheGrid());
01483 return EndVertex();
01484 }
01485
01486 inline
01487 RegGrid2D::VertexOnVertexIterator
01488 RegGrid2D::Vertex::EndVertex() const { return VertexOnVertexIterator(4,*this, &TheGrid());}
01489
01490 inline
01491 RegGrid2D::VertexOnVertexIterator
01492 RegGrid2D::Vertex::FirstNeighbour() const
01493 { return FirstVertex();}
01494 inline
01495 RegGrid2D::VertexOnVertexIterator
01496 RegGrid2D::Vertex::EndNeighbour() const
01497 { return EndVertex();}
01498
01499 inline
01500 RegGrid2D::CellOnVertexIterator
01501 RegGrid2D::Vertex::FirstCell() const
01502 {
01503 int c = -1;
01504 c = next_cell(c);
01505 return CellOnVertexIterator(c,*this,&TheGrid());
01506 }
01507
01508 inline
01509 RegGrid2D::CellOnVertexIterator
01510 RegGrid2D::Vertex::EndCell() const { return CellOnVertexIterator(4,*this,&TheGrid());}
01511
01512
01513 inline
01514 RegGrid2D::Cell
01515 RegGrid2D::Vertex::cell(int c) const
01516 { return Cell(TheGrid(),get_cell_base(c));}
01517
01518
01519
01521
01522
01523
01525
01526 inline
01527 RegGrid2D::Edge::Edge(const RegGrid2D::CellIterator&,
01528 const RegGrid2D::CellOnCellIterator& nb)
01529 { *this = nb.facet(); }
01530
01531 inline
01532 RegGrid2D::Edge::Edge(const RegGrid2D::CellOnCellIterator& nb)
01533 { *this = nb.facet(); }
01534
01535 inline
01536 RegGrid2D::Edge::Edge(const RegGrid2D::Cell&, const RegGrid2D::CellOnCellIterator& nb)
01537 { *this = nb.facet(); }
01538
01539
01540 inline
01541 RegGrid2D::VertexOnEdgeIterator RegGrid2D::Edge::FirstVertex() const { return VertexOnEdgeIterator(*this);}
01542
01543 inline
01544 RegGrid2D::VertexOnEdgeIterator RegGrid2D::Edge::EndVertex() const { return VertexOnEdgeIterator(*this,2);}
01545
01546 inline
01547 RegGrid2D::Cell
01548 RegGrid2D::Edge::C1() const {
01549 return (dir == x_dir
01550 ? Cell(TheGrid(),vertex_base(v1_.x(), v1_.y()-1))
01551 : Cell(TheGrid(),vertex_base(v1_.x()-1,v1_.y() )));
01552 }
01553
01554 inline
01555 RegGrid2D::Cell
01556 RegGrid2D::Edge::C2() const {
01557 return Cell(TheGrid(),v1_);
01558 }
01559
01560 inline
01561 void
01562 RegGrid2D::Edge::FlipCell(RegGrid2D::Cell& C) const
01563 {
01564 REQUIRE( (C == C1() || C == C2()), "FlipCell(): Cell not on Edge!\n",1);
01565 C = ( C == C1() ? C2() : C1());
01566 }
01567
01568
01570
01571
01572
01574
01575 inline
01576 RegGrid2D::VertexOnCellIterator
01577 RegGrid2D::Cell::FirstVertex() const
01578 { return VertexOnCellIterator(*this,SW);}
01579
01580 inline
01581 RegGrid2D::VertexOnCellIterator
01582 RegGrid2D::Cell::EndVertex() const
01583 { return VertexOnCellIterator(*this,invalid_corner);}
01584
01585 inline
01586 RegGrid2D::EdgeOnCellIterator
01587 RegGrid2D::Cell::FirstEdge() const
01588 { return EdgeOnCellIterator(*this,S);}
01589
01590 inline
01591 RegGrid2D::EdgeOnCellIterator
01592 RegGrid2D::Cell::EndEdge() const
01593 { return EdgeOnCellIterator(*this,invalid_side);}
01594
01595 inline
01596 RegGrid2D::EdgeOnCellIterator
01597 RegGrid2D::Cell::FirstFacet() const
01598 { return EdgeOnCellIterator(*this,S);}
01599
01600 inline
01601 RegGrid2D::EdgeOnCellIterator
01602 RegGrid2D::Cell::EndFacet() const
01603 { return EdgeOnCellIterator(*this,invalid_side);}
01604
01605 inline
01606 RegGrid2D::CellOnCellIterator
01607 RegGrid2D::Cell::FirstNeighbour() const
01608 { return CellOnCellIterator(*this,S);}
01609
01610 inline
01611 RegGrid2D::CellOnCellIterator
01612 RegGrid2D::Cell::EndNeighbour() const
01613 { return CellOnCellIterator(*this,invalid_side);}
01614
01615
01616 inline
01617 RegGrid2D::CellOnCellIterator
01618 RegGrid2D::Cell::FirstCell() const
01619 { return FirstNeighbour();}
01620
01621 inline
01622 RegGrid2D::CellOnCellIterator
01623 RegGrid2D::Cell::EndCell() const
01624 { return EndNeighbour();}
01625
01626 inline
01627 RegGrid2D::CellOnCellIterator
01628 RegGrid2D::Cell::FirstFace() const
01629 { return FirstNeighbour();}
01630
01631 inline
01632 RegGrid2D::CellOnCellIterator
01633 RegGrid2D::Cell::EndFace() const
01634 { return EndNeighbour();}
01635
01636 inline
01637 void RegGrid2D::Cell::FlipEdge(const Vertex& v, Edge& e) const {
01638 REQUIRE(((v == e.V1()) || (v == e.V2())),
01639 "FlipEdge(v,e): v not in {e.V1,e.V2} !\n",1);
01640 VertexOnCellIterator w =FirstVertex();
01641 while (( ! (*w == v)) && ( ! w.IsDone()))
01642 ++w;
01643 REQUIRE( (*w == v), "FlipEdge(v,e): v not on cell!\n",1);
01644 Vertex v2 = e.FlippedVertex(v);
01645 e = (*(w.CyclicSucc()) == v2 ? Edge(v,*(w.CyclicPred())) : Edge(v,*(w.CyclicSucc())));
01646 }
01647
01648
01650
01651
01652
01654
01655 inline
01656 RegGrid2D::Edge
01657 RegGrid2D::get_edge(edge_handle e) const
01658 {
01659
01660 REQUIRE( ((0 <= e) && (e < NumOfEdges())),
01661 "RegGrid2D::get_edge(e) : e = " << e
01662 << " not in [0," << NumOfEdges()-1 << "]!", 1);
01663
01664 return (e < NumOfHorizontalEdges()
01665 ? Edge(Edge::x_dir,TheXEdgeMap().index(e),this)
01666 : Edge(Edge::y_dir,TheYEdgeMap().index(e-NumOfHorizontalEdges()),this));
01667 }
01668
01669
01670 inline
01671 RegGrid2D::edge_handle
01672 RegGrid2D::edge_num(const RegGrid2D::Edge& E) const
01673 {
01674 return ( E.dir == Edge::x_dir
01675 ? TheXEdgeMap().number(E.v1_)
01676 : TheYEdgeMap().number(E.v1_) + NumOfHorizontalEdges());
01677 }
01678
01679
01680
01681 inline bool RegGrid2D::IsOnBoundary(const RegGrid2D::vertex_base& V) const
01682 {
01683 return ( (V.x() == llx()) || (V.x() == urx())
01684 || (V.y() == lly()) || (V.y() == ury()));
01685 }
01686
01687 inline bool RegGrid2D::IsOnBoundary(const RegGrid2D::Edge& E) const
01688 {
01689 return ( (E.dir == Edge::x_dir && (E.v1_.y() == lly() || E.v1_.y() == ury()))
01690 ||(E.dir == Edge::y_dir && (E.v1_.x() == llx() || E.v1_.x() == urx())));
01691 }
01692
01693 inline bool RegGrid2D::IsOnBoundary(const RegGrid2D::EdgeOnCellIterator& F) const
01694 {
01695 vertex_base bv = F.TheCell().get_nb_base(Cell::side(F.e));
01696 return ( ! TheCellMap().IsInRange(bv.x(),bv.y()));
01697 }
01698
01699
01700 inline bool RegGrid2D::IsInside(const RegGrid2D::Cell& C) const
01701 {
01702 vertex_base bv = C.ll();
01703 return ( TheCellMap().IsInRange(bv.x(),bv.y()));
01704 }
01705
01706 inline bool RegGrid2D::IsValid(const RegGrid2D::Cell& C) const
01707 {
01708 return ((C._g == this) && IsInside(C));
01709 }
01710
01711
01712 typedef grid_types<RegGrid2D> gt;
01713
01714 inline gt::VertexIterator gral_begin(RegGrid2D const& R, gt::VertexIterator) { return R.FirstVertex();}
01715 inline gt::VertexIterator gral_end (RegGrid2D const& R, gt::VertexIterator) { return R.EndVertex();}
01716 inline gt::size_type gral_size (RegGrid2D const& R, gt::VertexIterator) { return R.NumOfVertices();}
01717
01718 inline gt::EdgeIterator gral_begin(RegGrid2D const& R, gt::EdgeIterator) { return R.FirstEdge();}
01719 inline gt::EdgeIterator gral_end (RegGrid2D const& R, gt::EdgeIterator) { return R.EndEdge();}
01720 inline gt::size_type gral_size (RegGrid2D const& R, gt::EdgeIterator) { return R.NumOfEdges();}
01721
01722 inline gt::CellIterator gral_begin(RegGrid2D const& R, gt::CellIterator) { return R.FirstCell();}
01723 inline gt::CellIterator gral_end (RegGrid2D const& R, gt::CellIterator) { return R.EndCell();}
01724 inline gt::size_type gral_size (RegGrid2D const& R, gt::CellIterator) { return R.NumOfCells();}
01725
01726
01727 inline gt::VertexOnCellIterator gral_begin(gt::Cell a, gt::VertexOnCellIterator) { return a.FirstVertex();}
01728 inline gt::VertexOnCellIterator gral_end (gt::Cell a, gt::VertexOnCellIterator) { return a.EndVertex();}
01729 inline gt::size_type gral_size (gt::Cell a, gt::VertexOnCellIterator) { return a.NumOfVertices();}
01730 inline gt::VertexOnEdgeIterator gral_begin(gt::Edge a, gt::VertexOnEdgeIterator) { return a.FirstVertex();}
01731 inline gt::VertexOnEdgeIterator gral_end (gt::Edge a, gt::VertexOnEdgeIterator) { return a.EndVertex();}
01732 inline gt::size_type gral_size (gt::Edge a, gt::VertexOnEdgeIterator) { return a.NumOfVertices();}
01733 inline gt::VertexOnVertexIterator gral_begin(gt::Vertex a, gt::VertexOnVertexIterator) { return a.FirstVertex();}
01734 inline gt::VertexOnVertexIterator gral_end (gt::Vertex a, gt::VertexOnVertexIterator) { return a.EndVertex();}
01735 inline gt::size_type gral_size (gt::Vertex a, gt::VertexOnVertexIterator) { return a.NumOfVertices();}
01736
01737 inline gt::EdgeOnCellIterator gral_begin(gt::Cell a, gt::EdgeOnCellIterator) { return a.FirstEdge();}
01738 inline gt::EdgeOnCellIterator gral_end (gt::Cell a, gt::EdgeOnCellIterator) { return a.EndEdge();}
01739 inline gt::size_type gral_size (gt::Cell a, gt::EdgeOnCellIterator) { return a.NumOfEdges();}
01740
01741
01742
01743
01744
01745
01746 inline gt::CellOnVertexIterator gral_begin(gt::Vertex a, gt::CellOnVertexIterator) { return a.FirstCell();}
01747 inline gt::CellOnVertexIterator gral_end (gt::Vertex a, gt::CellOnVertexIterator) { return a.EndCell();}
01748 inline gt::size_type gral_size (gt::Vertex a, gt::CellOnVertexIterator) { return a.NumOfCells();}
01749
01750
01751
01752
01753
01754 inline gt::CellOnCellIterator gral_begin(gt::Cell a, gt::CellOnCellIterator) { return a.FirstCell();}
01755 inline gt::CellOnCellIterator gral_end (gt::Cell a, gt::CellOnCellIterator) { return a.EndCell();}
01756 inline gt::size_type gral_size (gt::Cell a, gt::CellOnCellIterator) { return a.NumOfCells();}
01757
01758
01759
01760
01761 }
01762
01763 }
01764
01765
01766 #endif
01767