dune-grid  2.3beta2
geometrygrid/geometry.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GEOGRID_GEOMETRY_HH
4 #define DUNE_GEOGRID_GEOMETRY_HH
5 
6 #include <dune/common/nullptr.hh>
7 #include <dune/common/typetraits.hh>
8 
9 #include <dune/geometry/referenceelements.hh>
10 #include <dune/geometry/multilineargeometry.hh>
11 
14 
15 namespace Dune
16 {
17 
18  namespace GeoGrid
19  {
20 
21  // InferHasSingleGeometryType
22  // --------------------------
23 
24  template< class hasSingleGeometryType, int dim, int mydim >
26  {
27  private:
28  static const unsigned int id = hasSingleGeometryType::topologyId;
29  static const unsigned int idMask = (1u << mydim) - 1u;
30 
31  public:
32  static const bool v = hasSingleGeometryType::v && ((mydim == dim) || ((id | 1u) == 1u) || ((id | 1u) == idMask));
33  static const unsigned int topologyId = (v ? id & idMask : ~0u);
34  };
35 
36  template< class hasSingleGeometryType, int dim >
37  struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 1 >
38  {
39  static const bool v = true;
40  static const unsigned int topologyId = GenericGeometry::CubeTopology< 1 >::type::id;
41  };
42 
43  template< class hasSingleGeometryType, int dim >
44  struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 0 >
45  {
46  static const bool v = true;
47  static const unsigned int topologyId = GenericGeometry::CubeTopology< 0 >::type::id;
48  };
49 
50 
51 
52  // GeometryTraits
53  // --------------
54 
55  template< class Grid >
57  {
58  typedef typename remove_const< Grid >::type::Traits Traits;
59 
60  typedef typename Traits::ctype ctype;
61 
62  typedef GenericGeometry::MatrixHelper< GenericGeometry::DuneCoordTraits< ctype > > MatrixHelper;
63 
64  static ctype tolerance () { return 16 * std::numeric_limits< ctype >::epsilon(); }
65 
66  template< int mydim, int cdim >
68  {
70  };
71 
72  template< int mydim >
74  : public InferHasSingleGeometryType< Capabilities::hasSingleGeometryType< Grid >, Traits::dimension, mydim >
75  {};
76  };
77 
78 
79 
80  // Geometry
81  // --------
82 
83  template< int mydim, int cdim, class Grid >
84  class Geometry
85  {
87 
88  typedef typename remove_const< Grid >::type::Traits Traits;
89 
90  template< int, int, class > friend class Geometry;
91 
92  public:
93  typedef typename Traits::ctype ctype;
94 
95  static const int mydimension = mydim;
96  static const int coorddimension = cdim;
97  static const int dimension = Traits::dimension;
98  static const int codimension = dimension - mydimension;
99 
100  protected:
101  typedef CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping;
102 
103  struct Mapping
104  : public BasicMapping
105  {
106  template< class CoordVector >
107  Mapping ( const GeometryType &type, const CoordVector &coords )
108  : BasicMapping( type, coords ),
109  refCount_( 0 )
110  {}
111 
112  void addReference () { ++refCount_; }
113  bool removeReference () { return (--refCount_ == 0); }
114 
115  private:
116  unsigned int refCount_;
117  };
118 
119  public:
120  typedef typename Mapping::LocalCoordinate LocalCoordinate;
121  typedef typename Mapping::GlobalCoordinate GlobalCoordinate;
122 
123  typedef typename Mapping::JacobianTransposed JacobianTransposed;
124  typedef typename Mapping::JacobianInverseTransposed JacobianInverseTransposed;
125 
126  Geometry ( const Grid &grid )
127  : grid_( &grid ),
128  mapping_( nullptr )
129  {}
130 
131  template< class CoordVector >
132  Geometry ( const Grid &grid, const GeometryType &type, const CoordVector &coords )
133  : grid_( &grid )
134  {
135  assert( int( type.dim() ) == mydimension );
136  void *mappingStorage = grid.allocateStorage( sizeof( Mapping ) );
137  mapping_ = new( mappingStorage ) Mapping( type, coords );
138  mapping_->addReference();
139  }
140 
141  Geometry ( const This &other )
142  : grid_( other.grid_ ),
143  mapping_( other.mapping_ )
144  {
145  if( mapping_ )
146  mapping_->addReference();
147  }
148 
150  {
151  if( mapping_ && mapping_->removeReference() )
152  destroyMapping();
153  }
154 
155  const This &operator= ( const This &other )
156  {
157  if( other.mapping_ )
158  other.mapping_->addReference();
159  if( mapping_ && mapping_->removeReference() )
160  destroyMapping();
161  grid_ = other.grid_;
162  mapping_ = other.mapping_;
163  return *this;
164  }
165 
166  operator bool () const { return bool( mapping_ ); }
167 
168  bool affine () const { return mapping_->affine(); }
169  GeometryType type () const { return mapping_->type(); }
170 
171  int corners () const { return mapping_->corners(); }
172  GlobalCoordinate corner ( const int i ) const { return mapping_->corner( i ); }
173  GlobalCoordinate center () const { return mapping_->center(); }
174 
175  GlobalCoordinate global ( const LocalCoordinate &local ) const { return mapping_->global( local ); }
176  LocalCoordinate local ( const GlobalCoordinate &global ) const { return mapping_->local( global ); }
177 
178  ctype integrationElement ( const LocalCoordinate &local ) const { return mapping_->integrationElement( local ); }
179  ctype volume () const { return mapping_->volume(); }
180 
181  const JacobianTransposed &jacobianTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianTransposed( local ); }
182  const JacobianInverseTransposed &jacobianInverseTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianInverseTransposed( local ); }
183 
184  const Grid &grid () const { return *grid_; }
185 
186  private:
187  void destroyMapping ()
188  {
189  mapping_->~Mapping();
190  grid().deallocateStorage( mapping_, sizeof( Mapping ) );
191  }
192 
193  const Grid *grid_;
194  Mapping* mapping_;
195  };
196 
197  } // namespace GeoGrid
198 
199 
200 
201  // FacadeOptions
202  // -------------
203 
204  namespace FacadeOptions
205  {
206 
207  template< int mydim, int cdim, class Grid >
208  struct StoreGeometryReference< mydim, cdim, Grid, GeoGrid::Geometry >
209  {
210  static const bool v = false;
211  };
212 
213  } // namespace FacadeOptions
214 
215 } // namespace Dune
216 
217 #endif // #ifndef DUNE_GEOGRID_GEOMETRY_HH