dune-grid  2.5-git
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 <utility>
7 
8 #include <dune/common/typetraits.hh>
9 
10 #include <dune/geometry/multilineargeometry.hh>
11 #include <dune/geometry/referenceelements.hh>
12 #include <dune/geometry/type.hh>
13 
16 
17 namespace Dune
18 {
19 
20  namespace GeoGrid
21  {
22 
23  // InferHasSingleGeometryType
24  // --------------------------
25 
26  template< class hasSingleGeometryType, int dim, int mydim >
28  {
29  private:
30  static const unsigned int id = hasSingleGeometryType::topologyId;
31  static const unsigned int idMask = (1u << mydim) - 1u;
32 
33  public:
34  static const bool v = hasSingleGeometryType::v && ((mydim == dim) || ((id | 1u) == 1u) || ((id | 1u) == idMask));
35  static const unsigned int topologyId = (v ? id & idMask : ~0u);
36  };
37 
38  template< class hasSingleGeometryType, int dim >
39  struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 1 >
40  {
41  static const bool v = true;
42  static const unsigned int topologyId = Impl::CubeTopology< 1 >::type::id;
43  };
44 
45  template< class hasSingleGeometryType, int dim >
46  struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 0 >
47  {
48  static const bool v = true;
49  static const unsigned int topologyId = Impl::CubeTopology< 0 >::type::id;
50  };
51 
52 
53 
54  // GeometryTraits
55  // --------------
56 
57  template< class Grid >
59  {
60  typedef typename std::remove_const< Grid >::type::Traits Traits;
61 
62  typedef typename Traits::ctype ctype;
63 
64  typedef Impl::FieldMatrixHelper< ctype > MatrixHelper;
65 
66  static ctype tolerance () { return 16 * std::numeric_limits< ctype >::epsilon(); }
67 
68  template< int mydim, int cdim >
70  {
72  };
73 
74  template< int mydim >
76  : public InferHasSingleGeometryType< Capabilities::hasSingleGeometryType< Grid >, Traits::dimension, mydim >
77  {};
78  };
79 
80 
81 
82  // Geometry
83  // --------
84 
85  template< int mydim, int cdim, class Grid >
86  class Geometry
87  {
89 
90  typedef typename std::remove_const< Grid >::type::Traits Traits;
91 
92  template< int, int, class > friend class Geometry;
93 
94  public:
95  typedef typename Traits::ctype ctype;
96 
97  static const int mydimension = mydim;
98  static const int coorddimension = cdim;
99  static const int dimension = Traits::dimension;
100  static const int codimension = dimension - mydimension;
101 
102  protected:
103  typedef CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping;
104 
105  struct Mapping
106  : public BasicMapping
107  {
108  template< class CoordVector >
109  Mapping ( const GeometryType &type, const CoordVector &coords )
110  : BasicMapping( type, coords ),
111  refCount_( 0 )
112  {}
113 
114  void addReference () { ++refCount_; }
115  bool removeReference () { return (--refCount_ == 0); }
116 
117  private:
118  unsigned int refCount_;
119  };
120 
121  public:
122  typedef typename Mapping::LocalCoordinate LocalCoordinate;
123  typedef typename Mapping::GlobalCoordinate GlobalCoordinate;
124 
125  typedef typename Mapping::JacobianTransposed JacobianTransposed;
126  typedef typename Mapping::JacobianInverseTransposed JacobianInverseTransposed;
127 
128  Geometry () : grid_( nullptr ), mapping_( nullptr ) {}
129 
130  explicit Geometry ( const Grid &grid ) : grid_( &grid ), mapping_( nullptr ) {}
131 
132  template< class CoordVector >
133  Geometry ( const Grid &grid, const GeometryType &type, const CoordVector &coords )
134  : grid_( &grid )
135  {
136  assert( int( type.dim() ) == mydimension );
137  void *mappingStorage = grid.allocateStorage( sizeof( Mapping ) );
138  mapping_ = new( mappingStorage ) Mapping( type, coords );
139  mapping_->addReference();
140  }
141 
142  Geometry ( const This &other )
143  : grid_( other.grid_ ),
144  mapping_( other.mapping_ )
145  {
146  if( mapping_ )
147  mapping_->addReference();
148  }
149 
150  Geometry ( This&& other )
151  : grid_( other.grid_ ),
152  mapping_( other.mapping_ )
153  {
154  other.grid_ = nullptr;
155  other.mapping_ = nullptr;
156  }
157 
159  {
160  if( mapping_ && mapping_->removeReference() )
161  destroyMapping();
162  }
163 
164  const This &operator= ( const This &other )
165  {
166  if( other.mapping_ )
167  other.mapping_->addReference();
168  if( mapping_ && mapping_->removeReference() )
169  destroyMapping();
170  grid_ = other.grid_;
171  mapping_ = other.mapping_;
172  return *this;
173  }
174 
175  const This &operator= ( This&& other )
176  {
177  using std::swap;
178  swap( grid_, other.grid_ );
179  swap( mapping_, other.mapping_ );
180  return *this;
181  }
182 
183  operator bool () const { return bool( mapping_ ); }
184 
185  bool affine () const { return mapping_->affine(); }
186  GeometryType type () const { return mapping_->type(); }
187 
188  int corners () const { return mapping_->corners(); }
189  GlobalCoordinate corner ( const int i ) const { return mapping_->corner( i ); }
190  GlobalCoordinate center () const { return mapping_->center(); }
191 
192  GlobalCoordinate global ( const LocalCoordinate &local ) const { return mapping_->global( local ); }
193  LocalCoordinate local ( const GlobalCoordinate &global ) const { return mapping_->local( global ); }
194 
195  ctype integrationElement ( const LocalCoordinate &local ) const { return mapping_->integrationElement( local ); }
196  ctype volume () const { return mapping_->volume(); }
197 
198  JacobianTransposed jacobianTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianTransposed( local ); }
199  JacobianInverseTransposed jacobianInverseTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianInverseTransposed( local ); }
200 
201  const Grid &grid () const { assert( grid_ ); return *grid_; }
202 
203  private:
204  void destroyMapping ()
205  {
206  mapping_->~Mapping();
207  grid().deallocateStorage( mapping_, sizeof( Mapping ) );
208  }
209 
210  const Grid *grid_;
211  Mapping* mapping_;
212  };
213 
214  } // namespace GeoGrid
215 
216 } // namespace Dune
217 
218 #endif // #ifndef DUNE_GEOGRID_GEOMETRY_HH
bool removeReference()
Definition: geometrygrid/geometry.hh:115
Definition: cornerstorage.hh:20
GlobalCoordinate corner(const int i) const
Definition: geometrygrid/geometry.hh:189
Geometry(const Grid &grid, const GeometryType &type, const CoordVector &coords)
Definition: geometrygrid/geometry.hh:133
Geometry(const This &other)
Definition: geometrygrid/geometry.hh:142
static ctype tolerance()
Definition: geometrygrid/geometry.hh:66
GeometryType
Type representing VTK&#39;s entity geometry types.
Definition: common.hh:178
Geometry(This &&other)
Definition: geometrygrid/geometry.hh:150
bool affine() const
Definition: geometrygrid/geometry.hh:185
Impl::FieldMatrixHelper< ctype > MatrixHelper
Definition: geometrygrid/geometry.hh:64
~Geometry()
Definition: geometrygrid/geometry.hh:158
GlobalCoordinate global(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:192
static const unsigned int topologyId
Definition: geometrygrid/geometry.hh:35
Definition: geometrygrid/geometry.hh:27
Mapping::LocalCoordinate LocalCoordinate
Definition: geometrygrid/geometry.hh:122
Mapping(const GeometryType &type, const CoordVector &coords)
Definition: geometrygrid/geometry.hh:109
ctype volume() const
Definition: geometrygrid/geometry.hh:196
CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping
Definition: geometrygrid/geometry.hh:103
GlobalCoordinate center() const
Definition: geometrygrid/geometry.hh:190
Definition: geometrygrid/geometry.hh:75
JacobianTransposed jacobianTransposed(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:198
Traits::ctype ctype
Definition: geometrygrid/geometry.hh:62
int corners() const
Definition: geometrygrid/geometry.hh:188
A set of traits classes to store static information about grid implementation.
ctype integrationElement(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:195
static const bool v
Definition: geometrygrid/geometry.hh:34
GeoGrid::CornerStorage< mydim, cdim, Grid > Type
Definition: geometrygrid/geometry.hh:71
Include standard header files.
Definition: agrid.hh:59
Geometry(const Grid &grid)
Definition: geometrygrid/geometry.hh:130
Definition: geometrygrid/geometry.hh:105
JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:199
Definition: geometrygrid/geometry.hh:69
Mapping::JacobianInverseTransposed JacobianInverseTransposed
Definition: geometrygrid/geometry.hh:126
Geometry()
Definition: geometrygrid/geometry.hh:128
void addReference()
Definition: geometrygrid/geometry.hh:114
GeometryType type() const
Definition: geometrygrid/geometry.hh:186
Mapping::GlobalCoordinate GlobalCoordinate
Definition: geometrygrid/geometry.hh:123
Definition: cornerstorage.hh:172
LocalCoordinate local(const GlobalCoordinate &global) const
Definition: geometrygrid/geometry.hh:193
const Grid & grid() const
Definition: geometrygrid/geometry.hh:201
Definition: geometrygrid/geometry.hh:86
Grid abstract base classThis class is the base class for all grid implementations. Although no virtual functions are used we call it abstract since its methods do not contain an implementation but forward to the methods of the derived class via the Barton-Nackman trick.
Definition: common/grid.hh:373
Traits::ctype ctype
Definition: geometrygrid/geometry.hh:95
Definition: geometrygrid/geometry.hh:58
std::remove_const< Grid >::type::Traits Traits
Definition: geometrygrid/geometry.hh:60
Mapping::JacobianTransposed JacobianTransposed
Definition: geometrygrid/geometry.hh:125