dune-grid  2.8.0
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 
12 #include <dune/common/exceptions.hh>
13 #include <dune/geometry/type.hh>
14 #include <dune/common/typetraits.hh>
15 
23 namespace Dune
24 {
27 
28  namespace VTK {
29 
31  //
32  // VTKOptions
33  //
34 
36 
41  enum OutputType {
50  // //! Output to the file is compressed inline binary.
51  // binarycompressed,
52  // //! Output is compressed and appended to the file.
53  // compressedappended
54  };
56 
65  enum DataMode {
67 
73 
80  };
81 
83  //
84  // PrintType
85  //
86 
88 
92  template<typename T>
93  struct PrintType {
95  typedef T Type;
96  };
97 
98  template<>
99  struct PrintType<unsigned char> {
100  typedef unsigned Type;
101  };
102 
103  template<>
104  struct PrintType<signed char> {
105  typedef int Type;
106  };
107 
108  template<>
109  struct PrintType<char> {
110  typedef std::conditional<std::numeric_limits<char>::is_signed,
111  int, unsigned>::type
113  };
114 
116  //
117  // VTK::GeometryType related stuff
118  //
119 
121 
131  vertex = 1,
132  line = 3,
133  triangle = 5,
134  polygon = 7,
138  prism = 13,
139  pyramid = 14,
140  polyhedron = 42
141  };
142 
144 
150  {
151  if (t.isVertex()) return vertex;
152  if (t.isLine()) return line;
153  if (t.isTriangle()) return triangle;
154  if (t.isQuadrilateral()) return quadrilateral;
155  if (t.isTetrahedron()) return tetrahedron;
156  if (t.isPyramid()) return pyramid;
157  if (t.isPrism()) return prism;
158  if (t.isHexahedron()) return hexahedron;
159 
160  if (t.isNone() )
161  {
162  if( t.dim() == 2 ) return polygon;
163  if( t.dim() == 3 ) return polyhedron;
164  }
165 
166  DUNE_THROW(IOError,"VTKWriter: unsupported GeometryType " << t);
167  }
168 
170  //
171  // Functions for transforming the index of a corner inside an entity
172  // between Dune and VTK
173  //
174 
176 
184  inline int renumber(const Dune::GeometryType &t, int i)
185  {
186  static const int quadRenumbering[4] = {0,1,3,2};
187  static const int cubeRenumbering[8] = {0,1,3,2,4,5,7,6};
188  static const int prismRenumbering[6] = {0,2,1,3,5,4};
189  static const int pyramidRenumbering[5] = {0,1,3,2,4};
190 
191  if (t.isQuadrilateral()) return quadRenumbering[i];
192  if (t.isPyramid()) return pyramidRenumbering[i];
193  if (t.isPrism()) return prismRenumbering[i];
194  if (t.isHexahedron()) return cubeRenumbering[i];
195 
196  return i;
197  }
198 
200 
214  template<typename T>
215  int renumber(const T& t, int i)
216  {
217  return renumber(t.type(), i);
218  }
219 
221  //
222  // Determine Endianness
223  //
224 
226 
230  inline std::string getEndiannessString()
231  {
232  short i = 1;
233  if (reinterpret_cast<char*>(&i)[1] == 1)
234  return "BigEndian";
235  else
236  return "LittleEndian";
237  }
238 
240  //
241  // which type of vtkfile to write
242  //
243 
245 
250  enum FileType {
255  };
256 
257 
259  //
260  // which precision to use when writing out data
261  //
262 
264 
269  enum class Precision {
270  int32,
271  uint8,
272  uint32,
273  float32,
274  float64
275  };
276 
278  inline std::string toString(Precision p)
279  {
280  switch(p)
281  {
282  case Precision::float32:
283  return "Float32";
284  case Precision::float64:
285  return "Float64";
286  case Precision::uint32:
287  return "UInt32";
288  case Precision::uint8:
289  return "UInt8";
290  case Precision::int32:
291  return "Int32";
292  default:
293  DUNE_THROW(Dune::NotImplemented, "Unknown precision type");
294  }
295  }
296 
298  inline std::size_t typeSize(Precision p)
299  {
300  switch(p)
301  {
302  case Precision::float32:
303  return sizeof(float);
304  case Precision::float64:
305  return sizeof(double);
306  case Precision::uint32:
307  return sizeof(std::uint32_t);
308  case Precision::uint8:
309  return sizeof(std::uint8_t);
310  case Precision::int32:
311  return sizeof(std::int32_t);
312  default:
313  DUNE_THROW(Dune::NotImplemented, "Unknown precision type");
314  }
315  }
316 
318 
325  class FieldInfo
326  {
327 
328  public:
329 
331  enum class Type {
334  scalar,
336  vector,
338  tensor
339  };
340 
342  FieldInfo(std::string name, Type type, std::size_t size, Precision prec = Precision::float32)
343  : _name(name)
344  , _type(type)
345  , _size(size)
346  , _prec(prec)
347  {}
348 
350  std::string name() const
351  {
352  return _name;
353  }
354 
356  Type type() const
357  {
358  return _type;
359  }
360 
362  std::size_t size() const
363  {
364  return _size;
365  }
366 
369  {
370  return _prec;
371  }
372 
373  private:
374 
375  std::string _name;
376  Type _type;
377  std::size_t _size;
378  Precision _prec;
379 
380  };
381 
382 
383  } // namespace VTK
384 
386 
387 } // namespace Dune
388 
389 #endif // DUNE_GRID_IO_FILE_VTK_COMMON_HH
Include standard header files.
Definition: agrid.hh:58
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:269
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:41
@ ascii
Output to the file is in ascii.
Definition: common.hh:43
@ appendedraw
Output is to the file is appended raw binary.
Definition: common.hh:47
@ appendedbase64
Output is to the file is appended base64 binary.
Definition: common.hh:49
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:45
int renumber(const Dune::GeometryType &t, int i)
renumber VTK <-> Dune
Definition: common.hh:184
FileType
which type of VTK file to write
Definition: common.hh:250
@ polyData
for .vtp files (PolyData)
Definition: common.hh:252
@ unstructuredGrid
for .vtu files (UnstructuredGrid)
Definition: common.hh:254
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:278
DataMode
Whether to produce conforming or non-conforming output.
Definition: common.hh:65
@ conforming
Output conforming data.
Definition: common.hh:71
@ nonconforming
Output non-conforming data.
Definition: common.hh:79
std::size_t typeSize(Precision p)
map precision to byte size
Definition: common.hh:298
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:149
std::string getEndiannessString()
determine endianness of this C++ implementation
Definition: common.hh:230
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:130
@ line
Definition: common.hh:132
@ pyramid
Definition: common.hh:139
@ polyhedron
Definition: common.hh:140
@ quadrilateral
Definition: common.hh:135
@ vertex
Definition: common.hh:131
@ tetrahedron
Definition: common.hh:136
@ prism
Definition: common.hh:138
@ hexahedron
Definition: common.hh:137
@ triangle
Definition: common.hh:133
@ polygon
Definition: common.hh:134
determine a type to safely put another type into a stream
Definition: common.hh:93
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:95
unsigned Type
Definition: common.hh:100
int Type
Definition: common.hh:105
std::conditional< std::numeric_limits< char >::is_signed, int, unsigned >::type Type
Definition: common.hh:112
Descriptor struct for VTK fields.
Definition: common.hh:326
std::size_t size() const
The number of components in the data field.
Definition: common.hh:362
Precision precision() const
The precision used for the output of the data field.
Definition: common.hh:368
Type
VTK data type.
Definition: common.hh:331
@ 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:342
Type type() const
The type of the data field.
Definition: common.hh:356
std::string name() const
The name of the data field.
Definition: common.hh:350