Dune Core Modules (2.7.1)

common.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 
4 #ifndef DUNE_GRID_IO_FILE_VTK_COMMON_HH
5 #define DUNE_GRID_IO_FILE_VTK_COMMON_HH
6 
7 #include <limits>
8 #include <sstream>
9 #include <string>
10 #include <cstdint>
11 
14 #include <dune/geometry/type.hh>
16 
24 namespace Dune
25 {
28 
29  namespace VTK {
30 
32  //
33  // VTKOptions
34  //
35 
37 
42  enum OutputType {
51  // //! Output to the file is compressed inline binary.
52  // binarycompressed,
53  // //! Output is compressed and appended to the file.
54  // compressedappended
55  };
57 
66  enum DataMode {
68 
74 
81  };
82 
84  //
85  // PrintType
86  //
87 
89 
93  template<typename T>
94  struct PrintType {
96  typedef T Type;
97  };
98 
99  template<>
100  struct PrintType<unsigned char> {
101  typedef unsigned Type;
102  };
103 
104  template<>
105  struct PrintType<signed char> {
106  typedef int Type;
107  };
108 
109  template<>
110  struct PrintType<char> {
111  typedef std::conditional<std::numeric_limits<char>::is_signed,
112  int, unsigned>::type
113  Type;
114  };
115 
117  //
118  // TypeName
119  //
120 
122 
125  template<typename T>
126  class DUNE_DEPRECATED_MSG("TypeName will be removed after Dune 2.7. Look at VTK::toString and VTK::Precision for substitution.") TypeName {
127  static std::string getString() {
128  static const unsigned int_sizes[] = { 8, 16, 32, 64, 0 };
129  static const unsigned float_sizes[] = { 32, 64, 0 };
130  const unsigned* sizes;
131 
132  std::ostringstream s;
133  if(std::numeric_limits<T>::is_integer) {
134  if(std::numeric_limits<T>::is_signed)
135  s << "Int";
136  else
137  s << "UInt";
138  sizes = int_sizes;
139  }
140  else {
141  // assume float
142  s << "Float";
143  sizes = float_sizes;
144  }
145 
146  static const unsigned size = 8*sizeof(T);
147  while(*sizes != 0 && *sizes <= size) ++sizes;
148  --sizes;
149  s << *sizes;
150 
151  return s.str();
152  }
153 
154  public:
156 
159  const std::string& operator()() const {
160  static const std::string s = getString();
161  return s;
162  }
163  };
164 
166  //
167  // VTK::GeometryType related stuff
168  //
169 
171 
181  vertex = 1,
182  line = 3,
183  triangle = 5,
184  polygon = 7,
185  quadrilateral = 9,
186  tetrahedron = 10,
187  hexahedron = 12,
188  prism = 13,
189  pyramid = 14,
190  polyhedron = 42
191  };
192 
194 
200  {
201  if (t.isVertex()) return vertex;
202  if (t.isLine()) return line;
203  if (t.isTriangle()) return triangle;
204  if (t.isQuadrilateral()) return quadrilateral;
205  if (t.isTetrahedron()) return tetrahedron;
206  if (t.isPyramid()) return pyramid;
207  if (t.isPrism()) return prism;
208  if (t.isHexahedron()) return hexahedron;
209 
210  if (t.isNone() )
211  {
212  if( t.dim() == 2 ) return polygon;
213  if( t.dim() == 3 ) return polyhedron;
214  }
215 
216  DUNE_THROW(IOError,"VTKWriter: unsupported GeometryType " << t);
217  }
218 
220  //
221  // Functions for transforming the index of a corner inside an entity
222  // between Dune and VTK
223  //
224 
226 
234  inline int renumber(const Dune::GeometryType &t, int i)
235  {
236  static const int quadRenumbering[4] = {0,1,3,2};
237  static const int cubeRenumbering[8] = {0,1,3,2,4,5,7,6};
238  static const int prismRenumbering[6] = {0,2,1,3,5,4};
239  static const int pyramidRenumbering[5] = {0,1,3,2,4};
240 
241  if (t.isQuadrilateral()) return quadRenumbering[i];
242  if (t.isPyramid()) return pyramidRenumbering[i];
243  if (t.isPrism()) return prismRenumbering[i];
244  if (t.isHexahedron()) return cubeRenumbering[i];
245 
246  return i;
247  }
248 
250 
264  template<typename T>
265  int renumber(const T& t, int i)
266  {
267  return renumber(t.type(), i);
268  }
269 
271  //
272  // Determine Endianness
273  //
274 
276 
280  inline std::string getEndiannessString()
281  {
282  short i = 1;
283  if (reinterpret_cast<char*>(&i)[1] == 1)
284  return "BigEndian";
285  else
286  return "LittleEndian";
287  }
288 
290  //
291  // which type of vtkfile to write
292  //
293 
295 
300  enum FileType {
305  };
306 
307 
309  //
310  // which precision to use when writing out data
311  //
312 
314 
319  enum class Precision {
320  int32,
321  uint8,
322  uint32,
323  float32,
324  float64
325  };
326 
328  inline std::string toString(Precision p)
329  {
330  switch(p)
331  {
332  case Precision::float32:
333  return "Float32";
334  case Precision::float64:
335  return "Float64";
336  case Precision::uint32:
337  return "UInt32";
338  case Precision::uint8:
339  return "UInt8";
340  case Precision::int32:
341  return "Int32";
342  default:
343  DUNE_THROW(Dune::NotImplemented, "Unknown precision type");
344  }
345  }
346 
348  inline std::size_t typeSize(Precision p)
349  {
350  switch(p)
351  {
352  case Precision::float32:
353  return sizeof(float);
354  case Precision::float64:
355  return sizeof(double);
356  case Precision::uint32:
357  return sizeof(std::uint32_t);
358  case Precision::uint8:
359  return sizeof(std::uint8_t);
360  case Precision::int32:
361  return sizeof(std::int32_t);
362  default:
363  DUNE_THROW(Dune::NotImplemented, "Unknown precision type");
364  }
365  }
366 
368 
375  class FieldInfo
376  {
377 
378  public:
379 
381  enum class Type {
384  scalar,
386  vector,
388  tensor
389  };
390 
392  FieldInfo(std::string name, Type type, std::size_t size, Precision prec = Precision::float32)
393  : _name(name)
394  , _type(type)
395  , _size(size)
396  , _prec(prec)
397  {}
398 
400  std::string name() const
401  {
402  return _name;
403  }
404 
406  Type type() const
407  {
408  return _type;
409  }
410 
412  std::size_t size() const
413  {
414  return _size;
415  }
416 
419  {
420  return _prec;
421  }
422 
423  private:
424 
425  std::string _name;
426  Type _type;
427  std::size_t _size;
428  Precision _prec;
429 
430  };
431 
432 
433  } // namespace VTK
434 
436 
437 } // namespace Dune
438 
439 #endif // DUNE_GRID_IO_FILE_VTK_COMMON_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:279
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:614
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:609
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:619
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:589
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:644
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:599
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:594
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:604
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:639
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:624
Default exception class for I/O errors.
Definition: exceptions.hh:229
Default exception for dummy implementations.
Definition: exceptions.hh:261
Descriptor struct for VTK fields.
Definition: common.hh:376
std::size_t size() const
The number of components in the data field.
Definition: common.hh:412
Precision precision() const
The precision used for the output of the data field.
Definition: common.hh:418
Type
VTK data type.
Definition: common.hh:381
@ tensor
tensor field (always 3x3)
@ vector
vector-valued field (always 3D, will be padded if necessary)
FieldInfo(std::string name, Type type, std::size_t size, Precision prec=Precision::float32)
Create a FieldInfo instance with the given name, type and size.
Definition: common.hh:392
Type type() const
The type of the data field.
Definition: common.hh:406
std::string name() const
The name of the data field.
Definition: common.hh:400
map type to its VTK name in data array
Definition: common.hh:126
const std::string & operator()() const
return VTK name of the type
Definition: common.hh:159
Definition of the DUNE_DEPRECATED macro for the case that config.h is not available.
A few common exception classes.
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:319
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:42
@ ascii
Output to the file is in ascii.
Definition: common.hh:44
@ appendedraw
Output is to the file is appended raw binary.
Definition: common.hh:48
@ appendedbase64
Output is to the file is appended base64 binary.
Definition: common.hh:50
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:46
int renumber(const Dune::GeometryType &t, int i)
renumber VTK <-> Dune
Definition: common.hh:234
FileType
which type of VTK file to write
Definition: common.hh:300
@ polyData
for .vtp files (PolyData)
Definition: common.hh:302
@ unstructuredGrid
for .vtu files (UnstructuredGrid)
Definition: common.hh:304
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:328
DataMode
Whether to produce conforming or non-conforming output.
Definition: common.hh:66
@ conforming
Output conforming data.
Definition: common.hh:72
@ nonconforming
Output non-conforming data.
Definition: common.hh:80
std::size_t typeSize(Precision p)
map precision to byte size
Definition: common.hh:348
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:199
std::string getEndiannessString()
determine endianness of this C++ implementation
Definition: common.hh:280
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:180
#define DUNE_DEPRECATED_MSG(text)
Mark some entity as deprecated.
Definition: deprecated.hh:169
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:14
determine a type to safely put another type into a stream
Definition: common.hh:94
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:96
A unique label for each type of element that can occur in a grid.
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 15, 22:30, 2024)