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/Complex2D/complex2d.h

Go to the documentation of this file.
00001 #ifndef NMWR_GB_GRIDS_COMPLEX2D_H
00002 #define NMWR_GB_GRIDS_COMPLEX2D_H
00003 
00004 
00005 
00006 // $LICENSE
00007 
00008 
00009 #include <list.h>
00010 #include <vector.h> 
00011 
00012 #include "Utility/pre-post-conditions.h"
00013 #include "Gral/Grids/Complex2D/point.h"
00014 
00015 #include "Gral/Base/common-grid-basics.h"
00016 #include "Gral/Base/element-handle.h"
00017 
00018 // many, many forward declarations ...
00019 
00020 
00021 class Complex2D;
00022 class Vertex2D;
00023 class Edge2D;
00024 class Cell2D;
00025  
00026 class Vertex2D_Iterator;
00027 class Edge2D_Iterator;
00028 class Cell2D_Iterator;
00029 class BoundaryFacet2D_Iterator;
00030 class BoundaryComponent_Vertex2D_Iterator;
00031 
00032 class VertexOnEdge2D_Iterator;
00033 class CellOnEdge2D_Iterator;
00034 
00035 class VertexOnCell2D_Iterator;
00036 class EdgeOnCell2D_Iterator;
00037 class CellOnCell2D_Iterator;
00038 
00039 
00040 class CellOnVertex2D_Iterator;
00041 
00042 
00043 // the next two classes (vertex_base and cell_connectivity) are
00044 // the *internal* representations of vertices and cells and are
00045 // *not* intended for public use
00046 
00047 
00048 
00049 class vertex_base {
00050 public:
00051   typedef point2 CoordType;
00052 
00053   typedef vector<int> cell_list;
00054 private:
00055 
00056   //--- DATA ------
00057   CoordType _coord;       // this could be moved to external geometry.
00058   cell_list _cells;       // information about adjacent cells.
00059                           // no ordering of the cells is assumed, albeit possible in 2D.
00060   void reserve(unsigned n) { _cells.reserve(n);}
00061 
00062   typedef vertex_base self;
00063 public:
00064   //-------- constructors ----------------------
00065   vertex_base() : _coord(0.0), _cells() {}
00066   vertex_base(unsigned n) : _coord(0.0), _cells() { reserve(n);} // NumOfCells() == 0
00067   vertex_base(const CoordType& coord)                 // NumOfCells() == 0
00068     : _coord(coord), _cells() {}
00069  
00070   // copying 
00071   vertex_base(const vertex_base& rs) : _coord(rs._coord), _cells(rs._cells) {}
00072   vertex_base& operator=(const vertex_base& rs) { 
00073     if (this != &rs) { 
00074       _coord = rs._coord; 
00075       _cells = rs._cells;
00076     }
00077     return *this;
00078   }
00079 
00080 private:
00081   friend class Complex2D;
00082   friend class Vertex2D;
00083 };
00084 
00085 
00086 
00087 class cell2d_connectivity {
00088 public:
00089   typedef vector<int> vertex_list;
00090   typedef vector<int> cell_list;
00091 
00092   //private:
00093   //-------------- DATA -----------
00094   // the assumption is that the 2 vertices corresponding to
00095   // the edge separating (*this) from nb_i (at postion i in _neighbours)
00096   // are at pos. i and i+1 (mod Length(_vertices)) in _vertices:
00097   // v1 --------- v2 ---------- v3 --- ... --- vn [ -------- v1 ]
00098   //      nb1            nb2         ...              nb_n
00099   cell_list      _neighbours;
00100   vertex_list    _vertices; 
00101 
00102   void resize( unsigned n) {
00103     _neighbours = cell_list(n);
00104     _vertices   = vertex_list(n);
00105   }
00106 
00107 
00108   typedef cell2d_connectivity self;
00109 public:
00110   //------------ constructors -----------------------
00111   cell2d_connectivity()  {}
00112   cell2d_connectivity(unsigned n) : _neighbours(n), _vertices(n) {} // NumOfVertices() == n
00113   ~cell2d_connectivity() {}
00114 
00115   // copying
00116   cell2d_connectivity(const self& rs) 
00117     : _neighbours(rs._neighbours), _vertices(rs._vertices) {}
00118   self& operator=(const self& rs) {
00119     if(this != &rs) {
00120       _neighbours = rs._neighbours;
00121       _vertices   = rs._vertices;
00122     }
00123    return (*this);
00124   }
00125   
00126 private:
00127   friend class friend_for_input; // work around, see below.
00128   friend class EdgeOnCell2D_Iterator;
00129   friend class Complex2D;
00130   friend class Cell2D;
00131 };
00132 
00133 
00134 
00135 
00136 typedef vector<cell2d_connectivity>   cell_list_complex2d;
00137 typedef vector<vertex_base>           vertex_list_complex2d;
00138 
00139 struct edge_handle_complex2d {
00140   int c;  // cell
00141   int le; // local position in cell
00142   edge_handle_complex2d() {}
00143   edge_handle_complex2d(int cc, int lle) : c(cc), le(lle) {}
00144 
00145   int local_facet() const { return le;}
00146 
00147   typedef edge_handle_complex2d self;
00148   friend bool operator==(const self& ls, const self& rs)
00149     { return ((ls.c == rs.c) && (ls.le == rs.le)); }
00150   friend bool operator< (const self& ls, const self& rs)
00151     { return ((ls.c <rs.c)  || ((ls.c == rs.c) &&  (ls.le < rs.le))); }
00152 
00153   friend ostream& operator<< (ostream& out, const self& e)
00154     { return (out << e.c << ' ' << e.le);}
00155   friend istream& operator>>(istream& in,         self& e)
00156     { return (in >> e.c >> e.le);}
00157 };
00158 
00159 
00160 namespace std {
00161   template<class T> class hash;
00162   struct hash<edge_handle_complex2d> {
00163   public:
00164     typedef edge_handle_complex2d argument_type;
00165     typedef size_t                result_type;
00166     
00167     size_t operator()(const edge_handle_complex2d& e) const
00168       { return (6*e.c + e.le);}
00169   };
00170   
00171 } // namespace std
00172 
00173 struct complex2d_types { 
00174   typedef Complex2D Complex;
00175   typedef Complex2D grid_type;
00176   typedef const Complex2D* ComplexPtr;
00177 
00178   typedef Vertex2D  Vertex;
00179   typedef Edge2D    Edge;
00180   typedef Edge2D    Facet;
00181   typedef Cell2D    Cell;
00182   typedef Cell2D    Face;
00183 
00184 
00185   typedef vertex_handle_int<Complex2D> vertex_handle;
00186   typedef cell_handle_int<Complex2D>   cell_handle;
00187   typedef edge_handle_complex2d        edge_handle;
00188   typedef edge_handle_complex2d        facet_handle;
00189 
00190   // typedef int vertex_handle;
00191   //typedef int cell_handle;
00192 
00193   // sequence iterators
00194   typedef Vertex2D_Iterator       GridVertexIterator;
00195   typedef Edge2D_Iterator         GridEdgeIterator;
00196   typedef Edge2D_Iterator         GridFacetIterator;
00197   typedef Cell2D_Iterator         GridCellIterator;
00198 
00199   typedef VertexOnEdge2D_Iterator VertexOnEdgeIterator;
00200   typedef VertexOnEdge2D_Iterator VertexOnFacetIterator;
00201   typedef CellOnEdge2D_Iterator   CellOnEdgeIterator;
00202   typedef CellOnEdge2D_Iterator   CellOnFacetIterator;
00203 
00204 
00205   // X-on-Cell incidence
00206   typedef VertexOnCell2D_Iterator VertexOnCellIterator;
00207   typedef EdgeOnCell2D_Iterator   EdgeOnCellIterator;
00208   typedef EdgeOnCell2D_Iterator   FacetOnCellIterator;
00209   typedef CellOnCell2D_Iterator   CellNeighbourIterator;
00210   typedef CellOnCell2D_Iterator   CellOnCellIterator;
00211 
00212   typedef CellOnVertex2D_Iterator CellOnVertexIterator;
00213 };
00214 
00215 
00216 
00217 
00218 #include "Gral/Grids/Complex2D/internal/vertex2d.h"
00219 #include "Gral/Grids/Complex2D/internal/cell2d.h"
00220 #include "Gral/Grids/Complex2D/internal/cell-on-cell2d-it.h"
00221 #include "Gral/Grids/Complex2D/internal/cell-on-vertex2d-it.h"
00222 #include "Gral/Grids/Complex2D/internal/edge-on-cell2d-it.h"
00223 
00256 class Complex2D : public complex2d_types  {
00257   typedef list<EdgeOnCell2D_Iterator>  boundary_facet_list;
00258   typedef vertex_list_complex2d        v_list;
00259   typedef cell_list_complex2d          c_list;
00260 
00261 
00262   //----- DATA -------------------------------------
00263 public: // for benchmark only
00264   cell_list_complex2d            _cells;
00265   vertex_list_complex2d          _vertices;
00266   boundary_facet_list            _boundary;
00267 
00268 public:
00269   //--------- types --------------------
00270 
00271   // sequence iterators
00272   typedef Vertex2D_Iterator       VertexIterator;
00273   typedef Edge2D_Iterator         EdgeIterator;
00274   typedef Edge2D_Iterator         FacetIterator;
00275   typedef Cell2D_Iterator         CellIterator;
00276 
00277   typedef BoundaryFacet2D_Iterator            BoundaryFacetIterator;
00278   typedef BoundaryComponent_Vertex2D_Iterator BoundaryVertexIterator;
00279 
00280   typedef vertex_base::CoordType    CoordType;
00281  
00282   //--------------- constructors --------------
00284   Complex2D() {}
00285   Complex2D(const Complex2D& rhs);
00286   Complex2D& operator=(const Complex2D& rhs);
00287   ~Complex2D();
00289 private:
00290   void clear();
00291 public:
00292   //-------------- iteration ------------------
00293 
00295   inline VertexIterator   FirstVertex() const;
00296   inline VertexIterator   EndVertex()   const;
00297 
00298   inline EdgeIterator     FirstEdge()    const;
00299   inline EdgeIterator     EndEdge()      const;
00300 
00301   inline FacetIterator    FirstFacet()   const;
00302   inline FacetIterator    EndFacet()     const;
00303 
00304   inline CellIterator     FirstCell()    const;
00305   inline CellIterator     EndCell()      const;
00307 
00309   inline void switch_vertex(Vertex& v, Edge const& e) const;
00310   inline void switch_edge(Vertex const& v, Edge & e, Cell const& c) const;
00311   inline void switch_facet(Vertex const& v, Edge & e, Cell const& c) const
00312     { switch_edge(v,e,c);}
00313   inline void switch_cell(Edge const& e, Cell & c) const;
00314 
00315   inline Vertex switched_vertex(Vertex const& v, Edge const& e) const;
00316   inline Edge   switched_edge  (Vertex const& v, Edge const& e, Cell const& c) const;
00317   inline Facet  switched_facet (Vertex const& v, Edge const& e, Cell const& c) const;
00318   inline Cell   switched_cell  (Edge   const& e, Cell const& c) const;
00320 
00321 
00323   // boundary iteration -- still somewhat incomplete.
00324   // there could be access to the 1D boundary grid as a whole,
00325   // and to its connected components as entities of their own.
00326   inline BoundaryFacetIterator  FirstBoundaryFacet() const;
00327   inline BoundaryFacetIterator  FirstBoundaryEdge () const;
00328   inline BoundaryVertexIterator FirstBoundaryVertex() const; // works only for one bd. component!
00329 
00330   int NumOfBoundaryVertices() const { return NumOfBoundaryFacets();}
00331   int NumOfBoundaryFacets() const { return (_boundary.size());}
00332 
00333   // now here some work has still to be done ...
00334   // int NumOfBoundaryCells() const 
00335   //   { return NumOfBoundaryFacets() - # cells w. multiple bd}
00336 
00337   // FIXME: only correct for genus = genus(S^2) / genus [0,1]^2
00338   int NumOfBoundaryComponents() const { return (NumOfBoundaryFacets() > 0 ? 1 : 0);} 
00340 
00341   //---------------- size information ------------
00342 
00344   int NumOfVertices() const  {return (_vertices.size());}
00345   int NumOfEdges()    const  
00346     {return (NumOfCells() == 0 ? 0 : -2 + NumOfVertices() + NumOfCells() + NumOfBoundaryComponents());}
00347   int NumOfFacets()   const { return NumOfEdges();}
00348   int NumOfFaces()    const  {return NumOfCells();}
00349   int NumOfCells()    const  {return (_cells.size());}
00351   
00352   //-----------------------------------------------------
00354 
00355   cell_handle outer_cell_handle() const { return cell_handle(-1);}
00356 
00357   //  inline bool IsOnBoundary(CellNeighbourIterator const& nb) const;
00358   inline bool IsOnBoundary(EdgeOnCellIterator    const& e ) const;
00359   inline bool IsOnBoundary(Edge                  const& e ) const;
00360 
00361   bool IsInside(cell_handle c) const { return (c != outer_cell_handle());}
00362   bool IsInside(const Cell& c) const { return IsInside(c.handle());}
00364 
00369   inline const CoordType& Coord(const Vertex& v)   const;
00370   inline       CoordType& Coord(const Vertex& v);
00372   
00373 private:
00374   friend class friend_for_input; // work-around class
00375   friend class Vertex2D;
00376   friend class Edge2D;
00377   friend class Cell2D;
00378 
00379   friend class VertexOnCell2D_Iterator;
00380   friend class EdgeOnCell2D_Iterator;
00381   friend class BoundaryFacet2D_Iterator; 
00382   friend class CellOnCell2D_Iterator;
00383   friend class CellOnVertex2D_Iterator;
00384 
00385   friend class Vertex2D_Iterator;
00386   friend class Edge2D_Iterator;
00387   friend class Cell2D_Iterator;
00388   
00389    
00390 
00391   inline cell_handle   _new_cell(int num_of_v);
00392   inline vertex_handle _new_vertex(const CoordType& coo = CoordType());
00393 
00394   // make n1_it point to n2
00395   inline void set_neighbour(const FacetOnCellIterator& n1_it, const Cell& n2);
00396   inline void add_cell_on_vertex(const Vertex& V, const Cell& C);
00397   inline void add_cell_on_vertex(const Vertex& V, const cell_handle& C);
00398 
00399   // calculate required connectivity information
00400   void  calculate_neighbour_cells();
00401   void  calculate_vertex_cells();
00402   void  calculate_adjacencies() {
00403     calculate_neighbour_cells();
00404     calculate_vertex_cells();
00405   }
00406 
00407 public:
00408 
00409   //------------------------- elements -> handles ------------------
00410 
00412   inline cell_handle   handle(Cell                  const& C) const;
00413   inline cell_handle   handle(CellIterator          const& c) const;
00414   inline cell_handle   handle(CellNeighbourIterator const& c) const;
00415 
00416   inline vertex_handle handle(Vertex               const& V)  const;
00417   inline vertex_handle handle(VertexIterator       const& v)  const;
00418   inline vertex_handle handle(VertexOnCellIterator const& v)  const;
00419 
00420   inline bool is_valid_vertex(vertex_handle v) const { return ((0 <= v) && (v < (int)_vertices.size()));}
00421   inline bool is_valid_cell  (cell_handle c)   const { return ((0 <= c) && (c < (int)_cells.size()));}
00422 
00423   inline edge_handle handle(const Edge& E) const;
00424 
00425   //------------------------- handles -> elements  ------------------
00426 
00427   Vertex vertex(const vertex_handle& v) const { return Vertex(*this,v);}
00428   Cell   cell  (const cell_handle&   c) const { return Cell  (*this,c);}
00429   inline Edge   edge  (const edge_handle& e) const;
00430   inline Edge   facet (const edge_handle& e) const;
00432 };
00433 
00434 
00435 //-----------------------------------------------------------------------------
00436 //-----------------------------------------------------------------------------
00437 
00438   // an ugly hack to workaround the lack of template
00439   // member functions or template friends.
00440   // the sole purpose of this class is to make public
00441   // some private functions/data of Complex2D, that are
00442   // needed to build it from some connectivity information
00443   
00444 class friend_for_input : public complex2d_types {
00445 public:
00446   //  typedef  Complex2D::v_list vertex_list;
00447   //  typedef  Complex2D::c_list   cell_list;
00448   typedef  Complex2D::CoordType CoordType;
00449   typedef  Complex2D::boundary_facet_list boundary_facet_list;
00450 
00451   friend_for_input(Complex2D& cc) : _cc(cc) {}
00452 
00453   void clear() { _cc.clear();}
00454   boundary_facet_list&    boundary()  {return _cc._boundary;} 
00455   vertex_list_complex2d& _vertices()  {return _cc._vertices;}
00456   cell_list_complex2d&   _cells()     {return _cc._cells;}
00457 
00458   void calculate_neighbour_cells()    {_cc.calculate_neighbour_cells();}
00459   void calculate_vertex_cells()       {_cc.calculate_vertex_cells();}
00460 
00461   cell_handle   _new_cell(int i)                                { return _cc._new_cell(i);}
00462   vertex_handle _new_vertex(const CoordType& coo = CoordType()) { return _cc._new_vertex(coo);}
00463 
00464   CoordType& coord(const Vertex2D& V) { return _cc.Coord(V);}
00465 
00466   void set_neighbour(const EdgeOnCell2D_Iterator& n1_it, const Cell2D& n2)
00467     { _cc.set_neighbour(n1_it,n2);}
00468   void add_cell_on_vertex(const Vertex2D& V, const Cell2D& C) 
00469     {_cc.add_cell_on_vertex(V,C);}
00470   void add_cell_on_vertex(const Vertex2D& V, const Complex2D::cell_handle& c)
00471     {_cc.add_cell_on_vertex(V,c);}
00472 
00473 
00474   cell_handle  outer_cell_handle() const { return _cc.outer_cell_handle();}
00475   
00476   cell2d_connectivity::vertex_list& _cell_vertices  (const cell_handle& c) 
00477   { return (_cc._cells[c]._vertices);}
00478   cell2d_connectivity::cell_list&   _cell_neighbours(const cell_handle& c) 
00479   { return (_cc._cells[c]._neighbours);}
00480 
00481 
00482 private:
00483   Complex2D& _cc;
00484 };
00485 
00486 //-----------------------------------------------------------------------------
00487 //-----------------------------------------------------------------------------
00488 
00489 #include "Gral/Grids/Complex2D/internal/vertex-on-cell2d-it.h" 
00490 #include "Gral/Grids/Complex2D/internal/vertex2d-it.h"
00491 #include "Gral/Grids/Complex2D/internal/cell2d-it.h"
00492 
00493 #include "Gral/Grids/Complex2D/internal/edge2d.h"
00494 #include "Gral/Grids/Complex2D/internal/edge2d-it.h"
00495 #include "Gral/Grids/Complex2D/internal/vertex-on-edge2d-it.h"
00496 #include "Gral/Grids/Complex2D/internal/cell-on-edge2d-it.h"
00497 #include "Gral/Grids/Complex2D/internal/boundary-facet-it.h"
00498 #include "Gral/Grids/Complex2D/internal/boundary-vertex-iterator.h"
00499 
00500 #include "Gral/Grids/Complex2D/internal/cell-on-cell2d-it.C"
00501 #include "Gral/Grids/Complex2D/internal/edge-on-cell2d-it.C"
00502 #include "Gral/Grids/Complex2D/internal/vertex2d.C"
00503 #include "Gral/Grids/Complex2D/internal/edge2d.C"
00504 #include "Gral/Grids/Complex2D/internal/cell2d.C"
00505 #include "Gral/Grids/Complex2D/internal/complex2d.C"
00506 
00507 
00508 //-----------------------------------------------------------------------------
00509 //------------  parameterized namespace specialization for Complex2D  ---------
00510 //-----------------------------------------------------------------------------
00511 
00512 struct hash_vertex2d;
00513 struct hash_edge2d;
00514 struct hash_cell2d;
00515 
00516 struct grid_types<Complex2D>  : public complex2d_types {
00517 
00518   typedef Vertex2D_Iterator       VertexIterator;
00519   typedef Edge2D_Iterator         EdgeIterator;
00520   typedef Edge2D_Iterator         FacetIterator;
00521   typedef Cell2D_Iterator         CellIterator;
00522  
00523   typedef  Complex2D::BoundaryFacetIterator  BoundaryFacetIterator;
00524   typedef  Complex2D::BoundaryVertexIterator BoundaryVertexIterator;
00525 
00526 
00527   static int hash(const Vertex& V) { return V.handle();}
00528   static int hash(const Cell&   C) { return C.handle();}
00529   static int hash(const Edge&   E) 
00530   {
00531     // problems if E is on boundary: one Cell does not exist.
00532     // bug detected by  __STL_DEBUG
00533     int c1 =  hash(E.V1()); // hash(E.C1());
00534     int c2 = hash(E.V2()); // hash(E.C2());
00535     return (c1 > c2 ? c1 * 4 /*GridOf(E).NumOfCells()*/ + c2 // besser : local_number(c2)
00536                     : c2 * 4 /*GridOf(E).NumOfCells()*/ + c1);
00537   }
00538  
00539   typedef hash_vertex2d hash_vertex;
00540   typedef hash_edge2d   hash_edge;
00541   typedef hash_edge2d   hash_facet;
00542   typedef hash_cell2d   hash_cell;
00543 
00544   typedef  vertex_base::CoordType CoordType;
00545 
00546   typedef grid_dim_tag<2> dimension_tag;
00547   static int dimension(const Cell& ) { return 2;}
00548   static int dimension(const Facet&) { return 1;} 
00549    
00550   static grid_type const&  GridOf(const Cell& C) {return C.TheGrid();}
00551   static grid_type const&  GridOf(const Edge& E) {return E.TheGrid();}
00552   static grid_type const&  GridOf(const Vertex& V) {return V.TheGrid();}
00553 
00554   static CoordType Coord(const Vertex& V) { return GridOf(V).Coord(V);}
00555 
00556   static cell_handle outer_cell_handle(grid_type const& G) 
00557   { return G.outer_cell_handle();}
00558   static cell_handle invalid_cell_handle(grid_type const& G) 
00559   { return G.outer_cell_handle();}
00560 };
00561 
00562 
00563 struct hash_vertex2d {
00564   typedef grid_types<Complex2D> gt;
00565   typedef  gt::Vertex   Vertex;
00566   int operator()(const Vertex& V) const { return gt::hash(V);}
00567 
00568   void operator=(hash_vertex2d const&) {} // suppress warnings about statement w/o effect
00569 };
00570 
00571 struct hash_edge2d {
00572   typedef grid_types<Complex2D> gt;
00573   typedef  gt::Edge     Edge;
00574   int operator()(const Edge& E) const { return gt::hash(E);}
00575 
00576   void operator=(hash_edge2d const&) {} // suppress warnings about statement w/o effect
00577 };
00578 
00579 struct hash_cell2d {
00580   typedef grid_types<Complex2D> gt;
00581   typedef gt::Cell     Cell;
00582   int operator()(const Cell& C) const { return gt::hash(C);}
00583 
00584   void operator=(hash_cell2d const&) {} // suppress warnings about statement w/o effect
00585 };
00586 
00587 namespace std {
00588   template<class T>
00589     struct hash;
00590   
00591   struct hash<Vertex2D> 
00592     : public hash_vertex2d {};
00593   struct hash<Edge2D> 
00594     : public hash_edge2d {};
00595   struct hash<Cell2D> 
00596     : public hash_cell2d {};
00597 }
00598 
00599 #include "Gral/Grids/Complex2D/element-traits.h"
00600 
00601 #endif
00602 

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

Generated at Tue Feb 26 16:06:42 2002 for GrAL Complex2D by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000