Go to Overview over all GrAL packages.
Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Gral/Grids/Cartesian2D/cartesian-grid2d.h

Go to the documentation of this file.
00001 #ifndef NMWR_GB_CARTESIAN_GRID_2D_H
00002 #define NMWR_GB_CARTESIAN_GRID_2D_H
00003 
00004 
00005 // $LICENSE
00006 
00007 
00008 #include <algobase.h> // for min/max
00009 #include <string>
00010 
00011 
00012 #include "Utility/pre-post-conditions.h"
00013 
00014 #include "Gral/Base/common-grid-basics.h" 
00015 #include "Gral/Base/element-handle.h"
00016 #include "Gral/Iterators/vertex-on-edge-iterator.h"
00017 
00018 #include "Gral/Grids/Cartesian2D/index-map.h"
00019 
00020 
00021 class RegGrid2D;
00022 typedef RegGrid2D CartesianGrid2D;
00023 
00029 class RegGrid2D {
00030 public:
00032   // typedef int dummy;
00034 
00035   typedef xmjr_indexmap2D indexmap_type;
00036   // iteration is done in y-direction.
00037   // replacing this with ymjr_... would yield a regular grid
00038   // in which iteration is done in x-direction.
00039   typedef  indexmap_type::index_type index_type; // 2D-integer index
00040 
00041 private:
00042   // DATA 
00043   index_type ll_,ur_;   // integer coordinates of lower left and upper right vertices
00044                         // ll does not have to be (0,0)
00045   int xpoints, ypoints; // number of vertices in x and y direction
00046   // this is redundant information: 
00047   //   xpoints == ur_.x -ll_.x + 1
00048   //   ypoints == ur_.y -ll_.y + 1
00049 
00050   // maps between 1D and 2D integer indices
00051   // see init_maps for information about their relationship
00052   indexmap_type vertex_index_map;
00053   indexmap_type cell_index_map;
00054   indexmap_type xedge_index_map;
00055   indexmap_type yedge_index_map;
00056 
00057 public:
00058 
00061   RegGrid2D(int pts = 2) // default: 1 cell, 4 vertices
00062     :ll_(0,0), ur_(pts-1,pts-1),
00063      xpoints(pts), ypoints(pts)
00064     { init_maps(ll_,ur_);}
00065  
00066   RegGrid2D(int x, int y) 
00067     : ll_(0,0), ur_(x-1,y-1),
00068       xpoints(x), ypoints(y)
00069     {init_maps(ll_,ur_);}
00070 
00071   RegGrid2D(int llx, int lly,int urx, int ury)
00072     : ll_(llx,lly), ur_(urx,ury),
00073       xpoints(urx - llx +1), 
00074       ypoints(ury - lly +1)
00075     { init_maps(ll_,ur_);}
00076 
00077   RegGrid2D(const index_type& LL, const index_type& UR)
00078     :  ll_(LL), ur_(UR),
00079        xpoints(UR.x - LL.x +1), 
00080        ypoints(UR.y - LL.y +1)
00081     { init_maps(ll_,ur_);}
00083 
00084 private:
00085   void init_maps(const index_type& ll,
00086                  const index_type& ur)
00087     { 
00088       vertex_index_map = indexmap_type(ll,ur);
00089       cell_index_map   = indexmap_type(ll,index_type(ur.x-1,ur.y-1)); 
00090       xedge_index_map  = indexmap_type(ll,index_type(ur.x-1,ur.y  )); 
00091       yedge_index_map  = indexmap_type(ll,index_type(ur.x,  ur.y-1)); 
00092     }
00093 
00094 public:
00097   const index_type& ll() const { return ll_;}
00098   const index_type& ur() const { return ur_;}
00099   int llx() const { return ll_.x;}
00100   int lly() const { return ll_.y;}
00101   int urx() const { return ur_.x;}
00102   int ury() const { return ur_.y;}
00104 
00105 private:
00106   
00107   const indexmap_type& TheVertexMap() const {return vertex_index_map;}
00108   const indexmap_type& TheXEdgeMap()  const {return xedge_index_map;}
00109   const indexmap_type& TheYEdgeMap()  const {return yedge_index_map;}
00110   const indexmap_type& TheCellMap()   const {return cell_index_map;}
00111 
00112 
00113   // static DATA:  2D-increments for local neighbour offsets
00114   static index_type side_offset_  [4];
00115   static index_type corner_offset_[4];  
00116   static index_type direction_    [4];  
00117   static index_type side_vertex_1_[4];
00118   static index_type side_vertex_2_[4];
00119 
00120   // names for I/O
00121   static std::string  side_name_  [4];
00122   static std::string  corner_name_[4];
00123 
00124 public:
00125   // map strings to side-/corner-enum and vice versa
00126   // this recognises different spellings, e.g. "S", "s", "South", "south".
00127   static int get_side(const std::string& nm);
00128   static int get_corner(const std::string& nm);
00129   static std::string side_name(int side) { return side_name_[side-1];} // returns "S", "N" etc.
00130   static std::string corner_name(int corner) { return corner_name_[corner-1];} // returns "SW", "NW" etc.
00131 
00132   static int invalid_side();
00133   static int invalid_corner();
00134 
00135   // c in {SW,SE,NE,NW}
00136   static const index_type& corner_offset(int c) { return corner_offset_[--c];}
00137   static int opposite_corner(int c)  { return (((c +1) % 4) + 1);}
00138 
00139   // s in {S,E,N,W}
00140   static int   opposite_side(int s)   { return (((s +1) % 4) + 1);}
00141   static const index_type& side_offset(int s)   { return side_offset_[--s];}
00142   static const index_type& direction(int s)     { return direction_[--s];}
00143   static const index_type& outer_normal(int s)  { return side_offset_[--s];}
00144 
00145   index_type side_vertex1(int s) const { return index_type(llx()+(xpoints-1)*side_vertex_1_[s-1].x,
00146                                                            lly()+(ypoints-1)*side_vertex_1_[s-1].y);}
00147   index_type side_vertex2(int s) const { return index_type(llx()+(xpoints-1)*side_vertex_2_[s-1].x,
00148                                                            lly()+(ypoints-1)*side_vertex_2_[s-1].y);}
00149 public:
00150   typedef RegGrid2D Grid;
00151   
00152   typedef vertex_handle_int<RegGrid2D> vertex_handle;
00153   typedef edge_handle_int<RegGrid2D>   edge_handle;
00154   typedef cell_handle_int<RegGrid2D>   cell_handle;
00155 
00156   cell_handle invalid_cell()      const {return TheCellMap().n0() -1;}
00157   cell_handle outer_cell_handle() const {return TheCellMap().n0() -1;}
00158  
00159   class Vertex;
00160   class Edge;
00161   class Cell;
00162 
00163   class VertexIterator;
00164   class EdgeIterator;
00165   class CellIterator;
00166   class VertexOnVertexIterator;
00167   class CellOnVertexIterator;
00168 
00169   class VertexOnCellIterator;
00170   class EdgeOnCellIterator;
00171   class CellOnCellIterator;
00172 
00173 
00174   //-----------------------------------------------------------
00175   //
00176   // now the definitions of local classes:
00177   // Elements: Vertex, Edge, Cell,
00178   // Sequence Iterators: VertexIterator, EdgeIterator, CellIterator,
00179   // Adjacency Iterators: VertexOnCellIterator, EdgeOnCellIterator, 
00180   //                      CellOnCellIterator,
00181   //                      VertexOnVertexIterator
00182   //
00183   //-----------------------------------------------------------
00184 
00185 
00186   //-----------------------------------------------------------
00187   //
00188   // ELEMENTS (VERTEX, EDGE, CELL)
00189   //
00190   //-----------------------------------------------------------
00191  
00194   class elem_base { // common base for vertex, edge, cell
00195   public:
00196     typedef RegGrid2D grid_type;
00197     typedef RegGrid2D anchor_type;
00198 
00199     elem_base() : _g((const Grid*)0) {}
00200     elem_base(const Grid* g) :_g(g) {}
00201      
00202     grid_type const& TheGrid() const {
00203       REQUIRE((_g != 0),"No Grid!\n",1);
00204       return (*_g);
00205     }
00206     grid_type const& TheAnchor() const { return TheGrid();}
00207   protected: 
00208     const Grid* _g;
00209   };
00210 
00211   typedef index_type vertex_base;
00212 
00213   //------------------ VERTEX ---------------------
00214 
00215   class Vertex : public elem_base {
00216     typedef Vertex self;
00217     friend class Edge;
00218   public:
00219     typedef  VertexOnVertexIterator VertexIterator;
00220     typedef  VertexOnVertexIterator NeighbourIterator;
00221 
00222     Vertex() {}
00223     Vertex(Grid const& g, vertex_handle v) { *this = g.vertex(v);}
00224 
00225     Vertex(const vertex_base& v, const Grid* g) :  elem_base(g),  _v(v) {}
00226     Vertex(const vertex_base& v, const Grid& g) :  elem_base(&g), _v(v) {}
00227     int x() const { return _v.x;}
00228     int y() const { return _v.y;}
00229  
00230     
00231     inline VertexOnVertexIterator FirstVertex() const;
00232     inline VertexOnVertexIterator EndVertex() const;
00233     inline VertexOnVertexIterator FirstNeighbour() const;
00234     inline VertexOnVertexIterator EndNeighbour() const;
00235     inline CellOnVertexIterator   FirstCell() const;
00236     inline CellOnVertexIterator   EndCell() const;
00237 
00238     int next_neighbour(int nb) const {
00239       if( !TheGrid().IsOnBoundary(*this)) return ++nb;
00240       do { nb++; } while( (nb <= 4) && (! TheGrid().IsValid(get_nb_base(nb))));
00241       return nb;
00242     }
00243 
00244     int next_cell(int c) const {
00245       if( !TheGrid().IsOnBoundary(*this)) return ++c;
00246       do { c++; } while( (c <= 4) && (! TheGrid().IsValidCellBase(get_cell_base(c))));
00247       return c;
00248     }
00249 
00250     vertex_base get_nb_base(int nb) const { // 1 <= nb <= 4, no check if Neighbour exists!
00251       return vertex_base(x() + TheGrid().side_offset((int)nb).x,
00252                          y() + TheGrid().side_offset((int)nb).y);
00253     }
00254 
00255     vertex_base get_cell_base(int c) const { // 1 <= nb <= 4, no check if Neighbour exists!
00256       return vertex_base(x() + TheGrid().corner_offset((int)c).x -1,
00257                          y() + TheGrid().corner_offset((int)c).y -1);
00258     }
00259    
00260     Vertex vertex(int nb) const { return Vertex(get_nb_base(nb),TheGrid());}
00261     inline Cell   cell(int c) const;
00262 
00263     friend bool operator==(const self& lhs, const self& rhs) 
00264     {return (lhs.GlobalNumber() == rhs.GlobalNumber());}
00265     friend bool operator!=(const self& lhs, const self& rhs) 
00266       { return ! (lhs == rhs);}
00267     friend bool operator<(const self& lhs, const self& rhs) 
00268     {return (lhs.GlobalNumber() < rhs.GlobalNumber());}
00269 
00270     vertex_handle GlobalNumber() const {return TheGrid().vertex_num(_v);}
00271     vertex_handle handle      () const {return TheGrid().vertex_num(_v);}
00272 
00273     const vertex_base& base() const {return _v;}
00274   protected:
00275     vertex_base _v;
00276   };
00277 
00278   //-------------------- EDGE ---------------------
00279 
00280   class Edge : public elem_base {
00281   public:
00282     enum   direction {x_dir,y_dir};
00283 
00284     friend class RegGrid2D;
00285     typedef RegGrid2D         Grid;
00286     typedef Edge self;
00287 
00288     Edge()  {}
00289     Edge(Grid const& g, edge_handle e) { *this = g.edge(e);}
00290 
00291     Edge(direction d, const vertex_base& v, const Grid* g)
00292       : elem_base(g), dir(d), v1_(v)  {}
00293     Edge(direction d, const vertex_base& v, const Grid& g)
00294       : elem_base(&g), dir(d), v1_(v)  {}
00295     Edge(const Vertex& w1, const Vertex& w2) : elem_base(&(w1.TheGrid()))
00296       { init(w1._v,w2._v);}
00297     Edge(const vertex_base& w1, const vertex_base& w2, const Grid& g) : elem_base(&g)
00298       { init(w1,w2);}
00299 
00300     void init(const vertex_base& w1, const vertex_base& w2)
00301       {
00302         REQUIRE((    (w1.x == w2.x) && (w1.y == w2.y+1 || w1.y == w2.y-1)
00303                   || (w1.y == w2.y) && (w1.x == w2.x+1 || w1.x == w2.x-1)),
00304                 "Edge(w1,w2): (w1,w2) = (" << w1 << ','  << w2 << ')' << "is no edge!\n",1);
00305         if(w1.y == w2.y) {
00306           dir = x_dir;
00307           v1_ = ( w1.x < w2.x ? w1 : w2);
00308         }
00309         else {
00310           dir = y_dir;
00311           v1_ = ( w1.y < w2.y ? w1 : w2);
00312         }
00313       }
00314 
00315     Edge(const CellIterator&, const CellOnCellIterator& nb); // somewhat obsolete.
00316     Edge(const Cell&,         const CellOnCellIterator& nb);
00317     explicit Edge(const CellOnCellIterator& nb);
00318 
00319     Vertex V1() const { return Vertex(v1_,_g);}
00320     Vertex V2() const { 
00321       return Vertex((dir==x_dir 
00322                      ? vertex_base(v1_.x+1,v1_.y)
00323                      : vertex_base(v1_.x,  v1_.y+1)),TheGrid());
00324     }
00325     vertex_handle v1() const { return V1().handle();}
00326     vertex_handle v2() const { return V2().handle();}
00327 
00328     Vertex V(int i) const {
00329       REQUIRE( (i == 1 || i == 2), "Edge::V(i): i must be 1 or 2! (i = " << i << ")\n",1); 
00330       return ( i == 1 ? V1() : V2()); }  
00331 
00332     void FlipVertex(Vertex& V) const {
00333       REQUIRE( (V == V1() || V == V2()), "FlipVertex(): Vertex not on Edge!\n",1);
00334       V = ( V == V1() ? V2() : V1());
00335     }
00336     Vertex FlippedVertex(const Vertex& V) const {
00337       REQUIRE( (V == V1() || V == V2()), "FlipVertex(): Vertex not on Edge!\n",1);
00338       return ( V == V1() ? V2() : V1());
00339     }
00340     inline void FlipCell(Cell& C) const;
00341     inline Cell FlippedCell(Cell& C) const;
00342     inline Cell C1()   const;  // lower resp. left cell
00343     inline Cell C2()   const;  // upper resp. right cell
00344 
00345 
00346     friend bool operator==(const self& lhs, const self& rhs) 
00347     {return (lhs.GlobalNumber() == rhs.GlobalNumber());}
00348     friend bool operator!=(const self& lhs, const self& rhs) 
00349       { return !(lhs == rhs);}
00350     friend bool operator<(const self& lhs, const self& rhs) 
00351     {return (lhs.GlobalNumber() < rhs.GlobalNumber());}
00352 
00353     edge_handle GlobalNumber() const { return TheGrid().edge_num(*this); }
00354     edge_handle handle      () const { return TheGrid().edge_num(*this); }
00355 
00356   private:
00357     direction dir;
00358     vertex_base v1_;
00359   };
00360 
00361   //----------------- CELL ----------------------
00362 
00363   class Cell : public elem_base {
00364   public:
00365     friend class RegGrid2D;
00366     typedef Cell self;
00367     enum side   { S  = 1, E  = 2, N  = 3, W  = 4, invalid_side   = 5};
00368     enum corner { SW = 1, SE = 2, NE = 3, NW = 4, invalid_corner = 5};
00369 
00370     Cell() : elem_base(0) {}
00371     Cell(const Grid& g, cell_handle c) { *this = g.cell(c); }
00372 
00373     Cell(const vertex_base& b, const Grid* g) : elem_base(g),  llv(b)  {}
00374     Cell(const vertex_base& b, const Grid& g) : elem_base(&g), llv(b)  {}
00375 
00376 
00377     VertexOnCellIterator FirstVertex()    const;
00378     VertexOnCellIterator EndVertex()      const;
00379     EdgeOnCellIterator   FirstEdge()      const;
00380     EdgeOnCellIterator   EndEdge()        const;
00381     EdgeOnCellIterator   FirstFacet()     const;
00382     EdgeOnCellIterator   EndFacet()       const;
00383     CellOnCellIterator   FirstCell()      const;
00384     CellOnCellIterator   EndCell()   const;
00385     CellOnCellIterator   FirstNeighbour() const;
00386     CellOnCellIterator   EndNeighbour()   const;
00387 
00388     int NumOfVertices()   const {return 4;}
00389     int NumOfNeighbours() const {return 4- NumOfBoundaryFacets();}
00390     int NumOfEdges()      const {return 4;}
00391     int NumOfFacets()     const {return 4;}
00392     int NumOfBoundaryFacets() const
00393       {
00394         int n = 0; 
00395         n += ((llv.x   == TheGrid().llx()) ? 1 : 0);
00396         n += ((llv.y   == TheGrid().lly()) ? 1 : 0);
00397         n += ((llv.x+1 == TheGrid().urx()) ? 1 : 0);
00398         n += ((llv.y+1 == TheGrid().ury()) ? 1 : 0);
00399         return n;
00400       }
00401  
00402     friend bool operator==(const self& lhs, const self& rhs) 
00403       {return (lhs.llv == rhs.llv);} // GlobalNumber() == rhs.GlobalNumber());}
00404     friend bool operator!=(const self& lhs, const self& rhs) 
00405       { return !(lhs == rhs);}
00406     friend bool operator<(const self& lhs, const self& rhs) 
00407     {return (lhs.GlobalNumber() < rhs.GlobalNumber());}
00408 
00409     side   opposite(side s  ) const { return side((((int)s +1) % 4) + 1);}
00410     corner opposite(corner c) const { return corner((((int)c +1) % 4) + 1);}
00411     bool IsValid(side s)   const { return ((S <= s) && (s <= W));}
00412     bool IsValid(corner c) const { return ((SW <= c) && (c <= NW));}
00413 
00414     cell_handle GlobalNumber()    const { return TheGrid().cell_num(llv);}
00415     cell_handle handle      ()    const { return TheGrid().cell_num(llv);}
00416     Vertex V(corner i) const { return vertex(i);}
00417     Vertex V(int i)    const { return vertex(corner(i));}
00418 
00421     Vertex vertex(corner v) const
00422     {
00423       REQUIRE(((1<=v) && (v<=4)),"Cartesian2D::Cell::vertex(v) : v = " << (int)v , 1);
00424       return Vertex(vertex_base(llv.x + TheGrid().corner_offset((int)v).x,
00425                                 llv.y + TheGrid().corner_offset((int)v).y),
00426                                 TheGrid());
00427     }
00428 
00429     vertex_base ll() const { return llv;}
00430     
00431     Edge edge(side e) const 
00432       {
00433         REQUIRE(((S<=e) && (e<=W)),"Cartesian2D::Cell::edge(e) : e = " << (int)e , 1);
00434         return ( e == S ? Edge(Edge::x_dir,llv,TheGrid())
00435                  : e == E ? Edge(Edge::y_dir,vertex_base(llv.x+1,llv.y),TheGrid())
00436                  : e == N ? Edge(Edge::x_dir,vertex_base(llv.x,  llv.y+1),TheGrid())
00437                  :          Edge(Edge::y_dir,llv,TheGrid()));
00438       } 
00439     
00440     Cell neighbour(side nb) const {
00441         REQUIRE(((S<=nb) && (nb<=W)),"Cartesian2D::Cell::neighbour(nb) : nb = " << (int) nb , 1);
00442         vertex_base bv = get_nb_base(nb);
00443         REQUIRE(TheGrid().IsValidCellBase(bv),
00444                 "Cell::Neighbour(nb) : nb = " << (int)nb << " not in grid!\n"
00445                 << " llv = " << llv << " bv = " << bv << '\n',1);
00446         return Cell(bv,TheGrid()); 
00447     }
00448     Cell Nb(side nb) const { return neighbour(nb);}
00449 
00450     bool IsOnBoundary(side nb) const { return TheGrid().IsOnBoundary(edge(nb));}
00451     bool IsOnBoundary(int nb) const  { return IsOnBoundary(side(nb));}
00452 
00453     void FlipEdge(const Vertex& v, Edge& e) const;   
00454   protected:
00455     //    cell_base _b;
00456     vertex_base get_nb_base(side nb) const { // no check
00457       return vertex_base(llv.x + TheGrid().side_offset((int)nb).x,
00458                          llv.y + TheGrid().side_offset((int)nb).y);
00459     }
00460 
00462     vertex_base llv; 
00463   };
00465   
00466 
00467   //-----------------------------------------------------------
00468   //
00469   //           ITERATORS
00470   //
00471   //-----------------------------------------------------------
00472  
00473 
00474   //-----------------------------------------------------------
00475   //                                      
00476   //        SEQUENCE ITERATORS             
00477   //                                       
00478   //-----------------------------------------------------------
00479  
00482   class seq_iterator_base {
00483   public:
00484     typedef RegGrid2D grid_type;
00485     typedef RegGrid2D anchor_type;
00486 
00487     typedef seq_iterator_base base;
00488     seq_iterator_base()                   : _g(0) {}
00489     seq_iterator_base(const grid_type* g) : _g(g) {}
00490     seq_iterator_base(const grid_type& g) : _g(&g) {}
00491 
00492      grid_type const& TheGrid() const {
00493       REQUIRE((_g != 0),"No Grid!\n",1);
00494       return (*_g);
00495     }
00496     grid_type const& TheAnchor() const { return TheGrid();}
00497   protected:
00498     const grid_type* _g;
00499   };
00500 
00501   //------------------- VERTEX ITERATOR ----------------------
00502    
00503   class VertexIterator : public seq_iterator_base {
00504     typedef VertexIterator self;
00505   public:
00506     VertexIterator() : base(0) {} 
00507     VertexIterator(const Grid* g) : base(g), v(g->MinVertexNum()) {}
00508     explicit 
00509     VertexIterator(const Grid& g) : base(g), v(g .MinVertexNum()) {}
00510     VertexIterator(const Grid& g, vertex_handle vv) : base(&g), v(vv) {}
00511     VertexIterator(vertex_handle vv, const Grid* g) : base(g), v(vv) {}
00512     self& operator++() {
00513       ++v;
00514       return (*this);
00515     }
00516     self  operator++(int)    { self tmp(*this); ++(*this); return tmp;}
00517     self& operator +=(const index_type& ij) {
00518       v+= TheGrid().TheVertexMap().offset(ij);
00519       return *this;
00520     }
00521 
00522     Vertex   operator*() const { return TheGrid().vertex(v);} 
00523     // random access
00524     Vertex operator()(int i, int j) const { 
00525       return TheGrid().vertex(v + TheGrid().TheVertexMap().offset(i,j));
00526     }
00527 
00528     bool     IsDone()    const { return  (v > TheGrid().MaxVertexNum());}
00529     operator bool() const { return !IsDone();} 
00530     vertex_handle GlobalNumber() const { return v;}
00531     vertex_handle handle      () const { return v;}
00532 
00533     friend bool operator==(self const& lhs, self const& rhs)
00534     {
00535       REQUIRE((&(lhs.TheGrid()) == &(rhs.TheGrid())), "Comparing vertices with different grids!\n",1);
00536       return (lhs.v == rhs.v);
00537     }
00538     friend bool operator!=(const self& lhs, const self& rhs) 
00539       { return !(lhs == rhs);}
00540 
00541   private:
00542     vertex_handle v;
00543   };
00544 
00545 
00546   //-------------------- EDGE  ITERATOR ----------------------
00547 
00548   class EdgeIterator : public seq_iterator_base {
00549     typedef EdgeIterator self;
00550   public:
00551     EdgeIterator() {}
00552     explicit
00553     EdgeIterator(Grid const& g) : base(g), e(g.MinEdgeNum()) {}
00554     EdgeIterator(int ee,const Grid* g) 
00555       : base(g), e(ee)  {}
00556 
00557     self& operator++() { ++e; return(*this);}
00558     self operator++(int) { self tmp(*this); ++(*this); return tmp;}
00559     Edge operator*() const { return TheGrid().get_edge(e);}
00560     bool IsDone()    const { return ( e >= TheGrid().NumOfEdges());}
00561 
00562     //  operator Edge()  const { return (this->operator*());}
00563     //  operator bool() const { return !IsDone();} 
00564     //  edge_handle GlobalNumber() const { return e;}
00565     edge_handle handle      () const { return e;}
00566 
00567     friend bool operator==(self const& lhs, self const& rhs)
00568     {
00569       REQUIRE((&(lhs.TheGrid()) == &(rhs.TheGrid())), "Comparing edges with different grids!\n",1);
00570       return (lhs.e == rhs.e);
00571     }
00572     friend bool operator!=(const self& lhs, const self& rhs) 
00573       { return !(lhs == rhs);}
00574   private:
00575     edge_handle e;
00576   };
00577 
00578 
00579   //-------------------- CELL  ITERATOR ----------------------
00580 
00581   class CellIterator : public seq_iterator_base {
00582     typedef CellIterator self;
00583   public:  
00584     CellIterator() : base(0), c(-1) {}
00585     explicit
00586     CellIterator(const Grid& g) : base(&g), c(g.MinCellNum()) {}
00587     CellIterator(int cc, const Grid* g) : base(g), c(cc) {} 
00588     CellIterator(const Grid& g, int cc) : base(&g), c(cc) {} 
00589  
00590     self& operator++() { ++c; return (*this);}
00591     self  operator++(int)  { self tmp(*this); ++(*this); return tmp;}
00592     Cell  operator*() const { return TheGrid().cell(c);}
00593     // operator Cell()   const { return TheGrid().cell(c);}
00594     bool  IsDone()    const { return (c > TheGrid().MaxCellNum());}
00595     // operator bool() const { return !IsDone();} 
00596     // cell_handle GlobalNumber() const { return c;}
00597     cell_handle handle      () const { return c;}
00598 
00599     friend bool operator==(self const& lhs, self const& rhs)
00600     {
00601       REQUIRE((&(lhs.TheGrid()) == &(rhs.TheGrid())), "Comparing cells with different grids!\n",1);
00602       return (lhs.c == rhs.c);
00603     }
00604     friend bool operator!=(const self& lhs, const self& rhs) 
00605       { return !(lhs == rhs);}
00606 
00607   private:
00608     cell_handle c;
00609   };
00611 
00612 
00614   //                                       //
00615   //       INCIDENCE ITERATORS             //
00616   //                                       //
00618   
00621   class inc_iterator_base {
00622   public:
00623     typedef RegGrid2D grid_type;
00624   
00625     typedef inc_iterator_base base;
00626     inc_iterator_base() : _g(0) {}
00627     inc_iterator_base(const grid_type* g) : _g(g) {}
00628     inc_iterator_base(const grid_type& g) : _g(&g) {}
00629 
00630     grid_type const& TheGrid() const {
00631       REQUIRE((_g != 0),"No Grid!\n",1);
00632       return (*_g);
00633     }
00634   protected:
00635     const grid_type* _g;
00636   };
00637 
00638 
00639   //-------------------- VERTEX ON VERTEX  ITERATOR ----------------------
00640 
00641   class VertexOnVertexIterator : public inc_iterator_base {
00642     typedef VertexOnVertexIterator self;
00643   public:
00644     typedef Vertex anchor_type;
00645     typedef Vertex value_type;
00646     typedef Vertex element_type;
00647 
00648     VertexOnVertexIterator() {}
00649     explicit
00650     VertexOnVertexIterator(Vertex const& vv) { *this = vv.FirstVertex(); }
00651     VertexOnVertexIterator(int v, const Vertex& vv, const Grid* g) 
00652       : base(g), _v(v), _vv(vv)  {}
00653     self& operator++() {_v = _vv.next_neighbour(_v); return (*this);}
00654     self  operator++(int)    { self tmp(*this); ++(*this); return tmp;}
00655     Vertex  operator*() const { return (_vv.vertex(_v));}
00656     // operator Vertex()   const { return this->operator*();}
00657     bool    IsDone()    const { return (_v > 4);}
00658     // operator bool() const { return !IsDone();} 
00659 
00660     Vertex const& TheVertex()  const { return _vv;}
00661     Vertex const& TheAnchor()  const { return _vv;}
00662 
00663     int LocalNumber()   const { return _v;}
00664 
00665     friend bool operator==(self const& lhs, self const& rhs)
00666     {
00667       REQUIRE((lhs.TheVertex() == rhs.TheVertex()), 
00668               "Comparing VertexOnVertexIterator with different vertex anchors!\n",1);
00669       return (lhs._v == rhs._v);
00670     }
00671     friend bool operator!=(const self& lhs, const self& rhs) 
00672       { return !(lhs == rhs);}
00673 
00674   private:
00675     int _v; // [1 , _c.NumOfVertices()]
00676     Vertex _vv;
00677   };
00678 
00679   //-------------------- CELL ON VERTEX  ITERATOR ----------------------
00680 
00681   class CellOnVertexIterator : public inc_iterator_base {
00682     typedef CellOnVertexIterator self;
00683   public:
00684     typedef Vertex anchor_type;
00685     typedef Cell   value_type;
00686     typedef Cell   element_type;
00687 
00688     CellOnVertexIterator() {}
00689     explicit
00690     CellOnVertexIterator(Vertex const& vv) { *this = vv.FirstCell();}
00691     CellOnVertexIterator(int c, const Vertex& vv, const Grid* g) 
00692       :  base(g), _c(c), _vv(vv) {}
00693     self& operator++() {_c = _vv.next_cell(_c); return (*this);}
00694     self  operator++(int)    { self tmp(*this); ++(*this); return tmp;}
00695     Cell operator*() const { return (_vv.cell(_c));}
00696     bool    IsDone()    const { return (_c > 4);}
00697     // operator bool() const { return !IsDone();} 
00698     int LocalNumber()   const { return _c;}
00699 
00700     Vertex const& TheVertex()  const { return _vv;}
00701     Vertex const& TheAnchor()  const { return _vv;}
00702 
00703     friend bool operator==(self const& lhs, self const& rhs)
00704     {
00705       REQUIRE((lhs.TheVertex() == rhs.TheVertex()), 
00706               "Comparing CellOnVertexIterator with different vertex anchors!\n",1);
00707       return (lhs._c == rhs._c);
00708     }
00709     friend bool operator!=(const self& lhs, const self& rhs) 
00710       { return !(lhs == rhs);}
00711   private:
00712     int _c; // [1 , _c.NumOfVertices()]
00713     Vertex _vv;
00714   };
00715 
00716 
00717   //-------------------- VERTEX ON CELL ITERATOR ----------------------
00718 
00719   class VertexOnCellIterator : public inc_iterator_base {
00720     typedef VertexOnCellIterator self;
00721   public:
00722     typedef Cell   anchor_type;
00723     typedef Vertex value_type;
00724     typedef Vertex element_type;
00725 
00726     VertexOnCellIterator() {}
00727     explicit
00728     VertexOnCellIterator(const Cell& cc)
00729       : base(& cc.TheGrid()), v(Cell::SW), c(cc)  {}
00730     VertexOnCellIterator( Cell::corner vv, const Cell& cc, const Grid* g) 
00731       : base(g), v(vv), c(cc)  {}
00732     self& operator++() { ++((int&)v); return (*this);}
00733     self  operator++(int)    { self tmp(*this); ++(*this); return tmp;}
00734     Vertex  operator*() const { return (c.vertex(v));}
00735     // operator Vertex()   const { return this->operator*();}
00736     bool    IsDone()    const { return (v > c.NumOfVertices());}
00737     // operator bool() const { return !IsDone();} 
00738     int LocalNumber()   const { return v;}
00739 
00740     vertex_handle   handle() const { return c.vertex(v).handle();}
00741 
00742     self CyclicSucc() const { self tmp(*this); tmp.v = Cell::corner(tmp.v == 4 ? 1 : tmp.v+1); return tmp;}
00743     self CyclicPred() const { self tmp(*this); tmp.v = Cell::corner(tmp.v == 1 ? 4 : tmp.v-1); return tmp;}
00744 
00745     Cell const& TheCell()     const { return c;}
00746     Cell const& TheAnchor()   const { return c;}
00747 
00748     friend
00749     bool operator==(self const& lhs, self const& rhs)
00750     { 
00751       REQUIRE( (lhs.c == rhs.c), "Comparing VertexOnCellIterators with different anchor cells!\n",1);
00752       return ((int) lhs.v == (int) rhs.v);
00753     }
00754     friend bool operator!=(const self& lhs, const self& rhs) 
00755       { return !(lhs == rhs);}
00756 
00757   private:
00758     Cell::corner v; 
00759     Cell c;
00760   };
00761 
00762   //-------------------- EDGE ON CELL ITERATOR ----------------------
00763 
00764   class EdgeOnCellIterator : public inc_iterator_base {
00765     typedef EdgeOnCellIterator self;
00766     friend class CellOnCellIterator;
00767     friend class RegGrid2D;
00768 
00769   public:
00770     typedef Edge value_type;
00771     typedef Edge element_type;
00772     typedef Cell anchor_type;
00773 
00774     EdgeOnCellIterator() {}
00775     explicit
00776     EdgeOnCellIterator(Cell const& cc) { *this = cc.FirstEdge();}
00777     EdgeOnCellIterator( Cell::side ee, const Cell& cc, const Grid* g)
00778       :  base(g), e(ee), c(cc) {}
00779     self& operator++() { ++((int&)e); return (*this);}
00780     self  operator++(int)    { self tmp(*this); ++(*this); return tmp;}
00781     Edge  operator*() const { return (TheCell().edge(e));}
00782     // FIXME: could be more efficient.
00783     edge_handle handle() const { return TheCell().edge(e).handle();}
00784     bool    IsDone()    const { return (e > c.NumOfEdges());}
00785     operator bool() const { return !IsDone();} 
00786 
00787     int  LocalNumber()  const { return e;}
00788 
00789     Cell    OtherCell() const { return TheCell().Nb(e);}
00790     Vertex V1() const { return c.V(e);}
00791     Vertex V2() const { return (e == 4 ? c.V(1) : c.V( (int)e + 1));}
00792 
00793     self CyclicSucc() const 
00794     { self tmp(*this); tmp.e = Cell::side(tmp.e == 4 ? 1 : tmp.e+1); return tmp;}
00795     self CyclicPred() const 
00796     { self tmp(*this); tmp.e = Cell::side(tmp.e == 1 ? 4 : tmp.e-1); return tmp;}
00797 
00798     Cell const&  TheCell()   const { return c;}
00799     Cell const&  TheAnchor()   const { return c;}
00800 
00801     friend
00802     bool operator==(self const& lhs, self const& rhs)
00803     { 
00804       REQUIRE( (lhs.c == rhs.c), "Comparing EdgeOnCellIterators with different anchor cells!\n",1);
00805       return ((int) lhs.e == (int) rhs.e);
00806     }
00807     friend bool operator!=(const self& lhs, const self& rhs) 
00808       { return !(lhs == rhs);}
00809 
00810   private:
00811     Cell::side e;  
00812     Cell c;
00813   };
00814 
00815 
00816   //-------------------- CELL ON CELL ITERATOR ----------------------
00817 
00818   class CellOnCellIterator : public inc_iterator_base {
00819     friend class RegGrid2D;
00820     typedef CellOnCellIterator self;
00821 
00822   public:
00823     typedef Cell anchor_type;
00824     typedef Cell element_type;
00825     typedef Cell value_type;
00826     
00827     CellOnCellIterator() {}
00828     explicit
00829     CellOnCellIterator(Cell const& cc) { *this = cc.FirstCell();}
00830     CellOnCellIterator(const EdgeOnCellIterator& F) : base(F), nb(F.e), c(F.c) 
00831       {
00832         while( ! IsDone() && c.IsOnBoundary(nb))
00833           ++(*this);
00834       }
00835     CellOnCellIterator( Cell::side nnb, const Cell& cc, const Grid* g) 
00836       : base(g), nb(nnb), c(cc) 
00837       {
00838         while( ! IsDone() && c.IsOnBoundary(nb))
00839           ++(*this);
00840       }
00841     self& operator++() { 
00842       do {
00843         ++((int&)nb);
00844       } while( ! IsDone() && c.IsOnBoundary(nb));
00845       return (*this);
00846     }
00847     self  operator++(int)    { self tmp(*this); ++(*this); return tmp;}
00848     Cell  operator*() const  { return TheCell().neighbour(nb);}
00849 
00850     // operator Cell()   const { return this->operator*();}
00851     Edge facet()      const { return c.edge(LocalNumber());}
00852     bool  IsDone()    const { return (nb > 4);}
00853     // operator bool() const { return !IsDone();} 
00854     Cell::side LocalNumber()  const { return nb;}
00855 
00856     Cell const&  TheCell()   const { return c;}
00857     Cell const&  TheAnchor() const { return c;}
00858 
00859     friend bool operator==(self const& lhs, self const& rhs)  { 
00860       REQUIRE( (lhs.c == rhs.c), "Comparing CellOnCellIterators with different anchor cells!\n",1);
00861       return ((int) lhs.nb == (int) rhs.nb);
00862     }
00863     friend bool operator!=(const self& lhs, const self& rhs) 
00864       { return !(lhs == rhs);}
00865 
00866   private:
00867     Cell::side nb;  // [1 , c.NumOfNeighbours()]
00868     Cell c;
00869   };
00870   typedef CellOnCellIterator CellNeighbourIterator;
00872 
00873   //----------------------------------------------------------------------- 
00874   //---------------  Operations of RegGrid2D  ----------------------------- 
00875   //----------------------------------------------------------------------- 
00876 
00879 
00880   int NumOfVertices() const { return TheVertexMap().range_size();}
00881   int NumOfEdges()    const { return (NumOfXEdges() + NumOfYEdges());}
00882   int NumOfFacets()   const { return (NumOfEdges());}
00883   int NumOfCells()    const { return TheCellMap().range_size();}
00884 
00885   int NumOfXEdges() const { return TheXEdgeMap().range_size();}
00886   int NumOfYEdges() const { return TheYEdgeMap().range_size();}
00887   int NumOfHorizontalEdges() const { return NumOfXEdges();}
00888   int NumOfVerticalEdges()   const { return NumOfYEdges();}
00889 
00890 
00891   int NumOfBoundaryVertices() const { return (2*(xpoints   + ypoints) -4);}
00892   int NumOfBoundaryEdges()    const { return (2*(xpoints-1 + ypoints-1));}
00893   int NumOfBoundaryFacets()   const { return NumOfBoundaryEdges();}
00894   int NumOfBoundaryCells()    const { return (2*(xpoints-1 + ypoints-1) -4);}
00896 
00897   int Offset(const Vertex&) const {return TheVertexMap().n0();}
00898   int Offset(const Edge&  ) const {return TheXEdgeMap().n0();}
00899   int Offset(const Cell&  ) const {return TheCellMap().n0();}
00900 
00901 
00902   int NumXpoints() const { return xpoints;}
00903   int NumYpoints() const { return ypoints;}
00904   
00907   VertexIterator FirstVertex() const { return VertexIterator(MinVertexNum(),this);}
00908   EdgeIterator   FirstEdge()   const { return EdgeIterator(MinEdgeNum(),this);}
00909   EdgeIterator   FirstFacet()  const { return EdgeIterator(MinEdgeNum(),this);}
00910   CellIterator   FirstCell()   const { return CellIterator(MinCellNum(),this);}
00912 
00915   inline bool IsValid(const vertex_base& v) const { return TheVertexMap().IsInRange(v);}
00916   inline bool IsValidCellBase(const vertex_base& v) const { 
00917     return TheCellMap().IsInRange(v);
00918   }
00919   bool IsValid(const Cell& C)     const;
00921 
00924   bool IsOnBoundary(const Vertex& V) const { return IsOnBoundary(V.base());}
00925   inline bool IsOnBoundary(const vertex_base&) const;
00926   inline bool IsOnBoundary(const Edge&   E) const;
00927   inline bool IsOnBoundary(const EdgeOnCellIterator& Nb) const;
00928 
00929   bool IsInside(const CellIterator& C) const;
00930   bool IsInside(const Cell& C)    const;
00932 
00933 
00934   friend class SubrangeReg2D;
00935   friend class Vertex;
00936   friend class Edge;
00937   friend class Cell;
00938 
00939   friend class VertexIterator;
00940   friend class EdgeIterator;
00941   friend class CellIterator;
00942   friend class VertexOnVertexIterator;
00943   friend class VertexOnCellIterator;
00944   friend class EdgeOnCellIterator;
00945   friend class CellOnCellIterator;
00946 
00949   vertex_handle handle(const Vertex& V) const { return vertex_num(V.x(),V.y());} 
00950   vertex_handle handle(const VertexIterator& V) const { return V.GlobalNumber();}
00951   vertex_handle handle(const VertexOnCellIterator& V) const { return handle(*V);}
00952   edge_handle   handle(const Edge& E)   const { return edge_num(E);}
00953   cell_handle   handle(const Cell& C)   const { return cell_num(C);}
00954 
00955   // vertex_handle  <-> Vertex
00956   vertex_handle vertex_num(int x, int y)     const { return TheVertexMap().number(x,y);}
00957   vertex_handle vertex_num(const vertex_base& V) const { return vertex_num(V.x,V.y);} 
00958   vertex_handle vertex_num(const Vertex& V) const { return vertex_num(V.x(),V.y());}
00959   vertex_base get_vertex(vertex_handle v) const { return TheVertexMap().index(v);}
00960   Vertex vertex(vertex_handle v) const { return Vertex(get_vertex(v),this);}
00961   Vertex vertex(const vertex_base& v) const { return Vertex(v,this);}
00962   Vertex vertex(int x, int y) const { return vertex(vertex_base(x,y));}
00963 
00964   // edge_handle <-> Edge
00965   inline edge_handle edge_num(const Edge& E) const;
00966   inline Edge get_edge(edge_handle e)   const;
00967   Edge   edge(edge_handle e)   const { return get_edge(e);}
00968   Edge   xedge(vertex_base leftv)   const { return Edge(Edge::x_dir,leftv,  *this);}
00969   Edge   yedge(vertex_base bottomv) const { return Edge(Edge::y_dir,bottomv,*this);}
00970 
00971   // cell_handle <-> Cell
00972   // (x,y) == lower left corner == int-coordinates of box center
00973   cell_handle cell_num(int llx, int lly) const { return TheCellMap().number(llx,lly);}
00974   cell_handle cell_num(const vertex_base& llv) const {return cell_num(llv.x,llv.y);}
00975   cell_handle cell_num(const Cell& C) const { return cell_num(C.ll());}
00976   vertex_base   get_cell_llv(cell_handle c)   const { return TheCellMap().index(c);}
00977   Cell   cell(cell_handle c)    const { return Cell(get_cell_llv(c),this);}
00978   Cell   cell(vertex_base llv)  const { return Cell(llv,this);}
00979   Cell   cell(int llx, int lly) const { return Cell(vertex_base(llx,lly),this);}
00981  
00984   vertex_handle MinVertexNum() const { return  TheVertexMap().n0();}
00985   vertex_handle MaxVertexNum() const { return  TheVertexMap().nmax();}
00986   vertex_handle MinNum(const Vertex&) const { return MinVertexNum();}
00987   vertex_handle MaxNum(const Vertex&) const { return MaxVertexNum();}
00988 
00989   edge_handle MinEdgeNum() const { return TheXEdgeMap().n0();}
00990   edge_handle MaxEdgeNum() const { return MinEdgeNum() + NumOfEdges() -1;}
00991   edge_handle MinNum(const Edge&) const { return MinEdgeNum();}
00992   edge_handle MaxNum(const Edge&) const { return MaxEdgeNum();}
00993 
00994   cell_handle MinCellNum() const { return TheCellMap().n0();}
00995   cell_handle MaxCellNum() const { return TheCellMap().nmax();}
00996   cell_handle MinNum(const Cell&) const { return MinCellNum();}
00997   cell_handle MaxNum(const Cell&) const { return MaxCellNum();}
00999 
01003   inline void switch_vertex(Vertex& v, Edge const& e) const 
01004     { e.FlipVertex(v);}
01005   inline void switch_edge(Vertex const& v, Edge & e, Cell const& c) const
01006     { c.FlipEdge(v,e);}  
01007   inline void switch_facet(Vertex const& v, Edge & e, Cell const& c) const
01008     { switch_edge(v,e,c);}
01009   inline void switch_cell(Edge const& e, Cell & c) const
01010     { e.FlipCell(c);}
01011   inline Vertex switched_vertex(Vertex const& v, Edge const& e) const
01012     { Vertex sv(v); switch_vertex(sv,e); return sv;}
01013   inline Edge switched_edge(Vertex const& v, Edge const& e, Cell const& c) const
01014     { Edge se(e); switch_edge(v,se,c); return se;}
01015   inline Edge switched_facet(Vertex const& v, Edge const& e, Cell const& c) const
01016     { Edge se(e); switch_edge(v,se,c); return se;}
01017   inline Cell switched_cell(Edge const& e, Cell const& c) const
01018     { Cell sc(c); switch_cell(e,sc); return sc;}
01020 
01021 };
01022 
01023 
01025 //
01026 //  methods of RegGrid2D::Vertex
01027 //
01029 
01030 inline  
01031 RegGrid2D::VertexOnVertexIterator 
01032 RegGrid2D::Vertex::FirstVertex() const {
01033   if( y() > TheGrid().lly()) // not on lower boundary 
01034     return VertexOnVertexIterator(1,*this,&TheGrid());
01035   else if( x() < TheGrid().urx()) //  not on lower right corner
01036     return VertexOnVertexIterator(2,*this,&TheGrid());
01037   else 
01038     return VertexOnVertexIterator(3,*this,&TheGrid());
01039 }
01040 
01041 inline
01042 RegGrid2D::VertexOnVertexIterator 
01043 RegGrid2D::Vertex::EndVertex() const { return VertexOnVertexIterator(5,*this, &TheGrid());}
01044 
01045 inline 
01046 RegGrid2D::VertexOnVertexIterator 
01047 RegGrid2D::Vertex::FirstNeighbour() const 
01048 { return FirstVertex();}
01049 inline 
01050 RegGrid2D::VertexOnVertexIterator 
01051 RegGrid2D::Vertex::EndNeighbour() const 
01052 { return EndVertex();}
01053 
01054 inline 
01055 RegGrid2D::CellOnVertexIterator 
01056 RegGrid2D::Vertex::FirstCell() const 
01057 {
01058   int c = 0;
01059   c = next_cell(c);
01060   return CellOnVertexIterator(c,*this,&TheGrid());
01061 }
01062 
01063 inline 
01064 RegGrid2D::CellOnVertexIterator 
01065 RegGrid2D::Vertex::EndCell() const { return CellOnVertexIterator(5,*this,&TheGrid());}
01066 
01067 
01068 inline 
01069 RegGrid2D::Cell 
01070 RegGrid2D::Vertex::cell(int c) const 
01071 { return Cell(get_cell_base(c),TheGrid());}
01072 
01073 
01074 
01076 //
01077 // methods of  RegGrid2D::Edge
01078 //
01080 
01081 inline 
01082 RegGrid2D::Edge::Edge(const RegGrid2D::CellIterator&, 
01083                       const RegGrid2D::CellOnCellIterator& nb)
01084 { *this = nb.facet(); }
01085 
01086 inline 
01087 RegGrid2D::Edge::Edge(const RegGrid2D::CellOnCellIterator& nb)
01088 { *this = nb.facet(); }
01089 
01090 inline 
01091 RegGrid2D::Edge::Edge(const RegGrid2D::Cell&, const RegGrid2D::CellOnCellIterator& nb)
01092 { *this = nb.facet(); }
01093 
01094 inline 
01095  RegGrid2D::Cell 
01096 RegGrid2D::Edge::C1()   const  { // lower resp. left cell
01097   /*
01098   REQUIRE((dir == x_dir 
01099            ?TheGrid().TheCellMap().IsInRange(index_type(v1.x,  v1.y-1))
01100            :TheGrid().TheCellMap().IsInRange(index_type(v1.x-1,v1.y  ))),
01101           "Edge::C1() : Edge on Boundary",1);
01102           */
01103   return (dir == x_dir 
01104           ? Cell(vertex_base(v1_.x,  v1_.y-1),TheGrid())
01105           : Cell(vertex_base(v1_.x-1,v1_.y  ),TheGrid()));
01106 }    
01107 
01108 inline  
01109  RegGrid2D::Cell 
01110 RegGrid2D::Edge::C2()   const  {  // upper resp. right cell
01111   // REQUIRE(TheGrid().TheCellMap().IsInRange(v1),
01112   //      "Edge::C1() : Edge on Boundary",1);
01113   return Cell(v1_,TheGrid()); 
01114 }
01115 
01116 inline  
01117 void 
01118 RegGrid2D::Edge::FlipCell(RegGrid2D::Cell& C)   const  
01119 { 
01120   REQUIRE( (C == C1() || C == C2()), "FlipCell(): Cell not on Edge!\n",1);
01121   C = ( C == C1() ? C2() : C1());
01122 }
01123 
01124 
01126 //
01127 //  methods of RegGrid2D::Cell
01128 //
01130 
01131 inline 
01132 RegGrid2D::VertexOnCellIterator 
01133 RegGrid2D::Cell::FirstVertex()  const 
01134 { return VertexOnCellIterator(SW,*this,_g);}
01135 
01136 inline 
01137 RegGrid2D::VertexOnCellIterator 
01138 RegGrid2D::Cell::EndVertex()  const 
01139 { return VertexOnCellIterator(invalid_corner,*this,_g);}
01140 
01141 inline 
01142 RegGrid2D::EdgeOnCellIterator   
01143 RegGrid2D::Cell::FirstEdge()  const 
01144 { return EdgeOnCellIterator(S,*this,_g);}
01145 
01146 inline 
01147 RegGrid2D::EdgeOnCellIterator   
01148 RegGrid2D::Cell::EndEdge()  const 
01149 { return EdgeOnCellIterator(invalid_side,*this,_g);}
01150 
01151 inline 
01152 RegGrid2D::EdgeOnCellIterator   
01153 RegGrid2D::Cell::FirstFacet()  const 
01154 { return EdgeOnCellIterator(S,*this,_g);}
01155 
01156 inline 
01157 RegGrid2D::EdgeOnCellIterator   
01158 RegGrid2D::Cell::EndFacet()  const 
01159 { return EdgeOnCellIterator(invalid_side,*this,_g);}
01160 
01161 inline 
01162 RegGrid2D::CellOnCellIterator   
01163 RegGrid2D::Cell::FirstNeighbour() const 
01164 { return CellOnCellIterator(S,*this,_g);}
01165 
01166 inline 
01167 RegGrid2D::CellOnCellIterator   
01168 RegGrid2D::Cell::EndNeighbour() const 
01169 { return CellOnCellIterator(invalid_side,*this,_g);}
01170 
01171 
01172 inline 
01173 RegGrid2D::CellOnCellIterator   
01174 RegGrid2D::Cell::FirstCell() const 
01175 { return FirstNeighbour();}
01176 
01177 inline 
01178 RegGrid2D::CellOnCellIterator   
01179 RegGrid2D::Cell::EndCell() const 
01180 { return EndNeighbour();}
01181 
01182 inline
01183 void RegGrid2D::Cell::FlipEdge(const Vertex& v, Edge& e) const {
01184   REQUIRE(((v == e.V1()) || (v == e.V2())),
01185           "FlipEdge(v,e): v not in {e.V1,e.V2} !\n",1);
01186   VertexOnCellIterator w =FirstVertex();
01187   while (( ! (*w == v)) && ( ! w.IsDone())) 
01188     ++w;
01189   REQUIRE( (*w == v), "FlipEdge(v,e): v not on cell!\n",1);
01190   Vertex v2 = e.FlippedVertex(v);
01191   e =  (*(w.CyclicSucc()) == v2 ? Edge(v,*(w.CyclicPred())) : Edge(v,*(w.CyclicSucc())));
01192 }
01193 
01194 
01196 //
01197 // methods of RegGrid2D
01198 //
01200   
01201 inline 
01202 RegGrid2D::Edge
01203 RegGrid2D::get_edge(edge_handle e) const
01204 {
01205   // offset <= edge < NumOfEdges() + offset ???
01206   REQUIRE( ((0 <= e) && (e < NumOfEdges())),
01207            "RegGrid2D::get_edge(e) : e = " << e
01208            << " not in [0," << NumOfEdges()-1 << "]!", 1);
01209 
01210   return (e < NumOfXEdges()
01211           ? Edge(Edge::x_dir,TheXEdgeMap().index(e),this)
01212           : Edge(Edge::y_dir,TheYEdgeMap().index(e-NumOfXEdges()),this));
01213 }
01214 
01215 
01216 inline 
01217 RegGrid2D::edge_handle 
01218 RegGrid2D::edge_num(const RegGrid2D::Edge& E) const
01219 {
01220   return ( E.dir == Edge::x_dir
01221            ? TheXEdgeMap().number(E.v1_)
01222            : TheYEdgeMap().number(E.v1_) + NumOfXEdges());
01223 }
01224 
01225 
01226 
01227 inline bool RegGrid2D::IsOnBoundary(const RegGrid2D::vertex_base& V) const
01228 {
01229  return (   (V.x == llx()) || (V.x == urx())
01230          || (V.y == lly()) || (V.y == ury()));
01231 }
01232 
01233 inline  bool RegGrid2D::IsOnBoundary(const RegGrid2D::Edge&   E) const
01234 {
01235   return (  (E.dir == Edge::x_dir && (E.v1_.y == lly() || E.v1_.y == ury()))
01236           ||(E.dir == Edge::y_dir && (E.v1_.x == llx() || E.v1_.x == urx())));
01237 }
01238 
01239 inline  bool RegGrid2D::IsOnBoundary(const RegGrid2D::EdgeOnCellIterator& F) const
01240 { 
01241   vertex_base bv = F.TheCell().get_nb_base(F.e);
01242   return ( ! TheCellMap().IsInRange(bv.x,bv.y));
01243 }
01244 
01245 
01246 inline bool RegGrid2D::IsInside(const RegGrid2D::Cell& C) const
01247 {
01248   vertex_base bv = C.ll();
01249   return ( TheCellMap().IsInRange(bv.x,bv.y));
01250 }
01251 
01252 inline bool RegGrid2D::IsValid(const RegGrid2D::Cell& C) const
01253 {
01254   return ((C._g == this) && IsInside(C));
01255 }
01256 
01257 
01258 
01259 
01262 struct grid_types<RegGrid2D> {
01263 
01264   typedef RegGrid2D             Grid;
01265   typedef RegGrid2D             grid_type;
01266   typedef  Grid::Vertex Vertex;
01267   typedef  Grid::Edge   Edge;
01268   typedef  Grid::Edge   Facet;
01269   typedef  Grid::Cell   Cell;
01270   typedef  Grid::Cell   Face;
01271 
01272 
01273   typedef  Grid::vertex_handle vertex_handle;
01274   typedef  Grid::edge_handle   edge_handle;
01275   typedef  Grid::edge_handle   facet_handle;
01276   typedef  Grid::cell_handle   cell_handle;
01277 
01278   typedef  Grid::VertexIterator  VertexIterator;
01279   typedef  Grid::EdgeIterator    EdgeIterator;
01280   typedef  Grid::EdgeIterator    FacetIterator;
01281   typedef  Grid::CellIterator    CellIterator;
01282 
01283   typedef  Grid::VertexOnVertexIterator   VertexOnVertexIterator;
01284   typedef  Grid::VertexOnVertexIterator   VertexNeighbourIterator;
01285   typedef  Grid::CellOnVertexIterator     CellOnVertexIterator;
01286   typedef  vertex_on_edge_iterator<Grid>  VertexOnEdgeIterator;
01287   typedef  VertexOnEdgeIterator           VertexOnFacetIterator;
01288 
01289   typedef  Grid::VertexOnCellIterator   VertexOnCellIterator;
01290   typedef  Grid::EdgeOnCellIterator     EdgeOnCellIterator;
01291   typedef  Grid::EdgeOnCellIterator     FacetOnCellIterator;
01292   typedef  Grid::CellOnCellIterator     CellOnCellIterator;
01293   typedef  Grid::CellOnCellIterator     NeighbourCellIterator;
01294   typedef  Grid::CellOnCellIterator     CellNeighbourIterator;
01295 
01296 
01297   typedef grid_dim_tag<2>   dimension_tag;
01298   static int dimension(const Cell& ) { return 2;}
01299   static int dimension(const Facet&) { return 1;} 
01300   static int dimension(const Vertex&) { return 0;}
01301 
01302   struct hash_facet {
01303     int operator()(const Facet& F) const { return F.GlobalNumber();}
01304   };
01305 
01306   static cell_handle invalid_cell_handle(grid_type const& G) { return G.invalid_cell();}
01307   static cell_handle outer_cell_handle  (grid_type const& G) { return G.outer_cell_handle();}
01308   static bool is_cell_valid (grid_type const& G, cell_handle c) { return (c !=G.invalid_cell());}
01309   static bool is_cell_inside(grid_type const& G, cell_handle c) { return (c !=G.invalid_cell());}
01310   static bool is_cell_valid (grid_type const& G, Cell const& c) { return (G.IsValid(c));}
01311   static bool is_cell_inside(grid_type const& G, Cell const& c) { return (G.IsInside(c));}
01312 };
01313 
01314 #include "Gral/Grids/Cartesian2D/element-traits.h"
01315 
01316 #endif
01317 

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 16:08:09 2002 for GrAL Cartesian2D by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000