dune-grid  2.2.0
common.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 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 
11 #include <dune/common/deprecated.hh>
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  // //! Ouput is compressed and appended to the file.
53  // compressedappended
54  };
56 
65  enum DataMode {
67 
73 
80  };
81 
82  } // namespace VTK
83 
85 
92  struct DUNE_DEPRECATED VTKOptions {
94 
102  typedef DUNE_DEPRECATED VTK::OutputType OutputType;
104 
109 
112  static const VTK::OutputType binary = VTK::base64;
114 
117  static const VTK::OutputType binaryappended = VTK::appendedraw;
119 
131  typedef DUNE_DEPRECATED VTK::DataMode DataMode;
133 
141 
150  };
151 
152  namespace VTK {
153 
155  //
156  // PrintType
157  //
158 
160 
164  template<typename T>
165  struct PrintType {
167  typedef T Type;
168  };
169 
170  template<>
171  struct PrintType<unsigned char> {
172  typedef unsigned Type;
173  };
174 
175  template<>
176  struct PrintType<signed char> {
177  typedef int Type;
178  };
179 
180  template<>
181  struct PrintType<char> {
182  typedef SelectType<std::numeric_limits<char>::is_signed,
183  int, unsigned>::Type
185  };
186 
188  //
189  // TypeName
190  //
191 
193 
196  template<typename T>
197  class TypeName {
198  static std::string getString() {
199  static const unsigned int_sizes[] = { 8, 16, 32, 64, 0 };
200  static const unsigned float_sizes[] = { 32, 64, 0 };
201  const unsigned* sizes;
202 
203  std::ostringstream s;
204  if(std::numeric_limits<T>::is_integer) {
205  if(std::numeric_limits<T>::is_signed)
206  s << "Int";
207  else
208  s << "UInt";
209  sizes = int_sizes;
210  }
211  else {
212  // assume float
213  s << "Float";
214  sizes = float_sizes;
215  }
216 
217  static const unsigned size = 8*sizeof(T);
218  while(*sizes != 0 && *sizes <= size) ++sizes;
219  --sizes;
220  s << *sizes;
221 
222  return s.str();
223  }
224 
225  public:
227 
230  const std::string& operator()() const {
231  static const std::string s = getString();
232  return s;
233  }
234  };
235 
237  //
238  // VTK::GeometryType related stuff
239  //
240 
242 
252  vertex = 1,
253  line = 3,
254  triangle = 5,
258  prism = 13,
259  pyramid = 14
260  };
261 
263 
269  {
270  if (t.isVertex()) return vertex;
271  if (t.isLine()) return line;
272  if (t.isTriangle()) return triangle;
273  if (t.isQuadrilateral()) return quadrilateral;
274  if (t.isTetrahedron()) return tetrahedron;
275  if (t.isPyramid()) return pyramid;
276  if (t.isPrism()) return prism;
277  if (t.isHexahedron()) return hexahedron;
278 
279  DUNE_THROW(IOError,"VTKWriter: unsupported GeometryType " << t);
280  }
281 
283  //
284  // Functions for transforming the index of a corner inside an entity
285  // between Dune and VTK
286  //
287 
289 
297  inline int renumber(const Dune::GeometryType &t, int i)
298  {
299  static const int quadRenumbering[4] = {0,1,3,2};
300  static const int cubeRenumbering[8] = {0,1,3,2,4,5,7,6};
301  static const int prismRenumbering[6] = {0,2,1,3,5,4};
302  static const int pyramidRenumbering[5] = {0,1,3,2,4};
303 
304  if (t.isQuadrilateral()) return quadRenumbering[i];
305  if (t.isPyramid()) return pyramidRenumbering[i];
306  if (t.isPrism()) return prismRenumbering[i];
307  if (t.isHexahedron()) return cubeRenumbering[i];
308 
309  return i;
310  }
311 
313 
327  template<typename T>
328  int renumber(const T& t, int i)
329  {
330  return renumber(t.type(), i);
331  }
332 
334  //
335  // Determine Endianness
336  //
337 
339 
343  inline std::string getEndiannessString()
344  {
345  short i = 1;
346  if (reinterpret_cast<char*>(&i)[1] == 1)
347  return "BigEndian";
348  else
349  return "LittleEndian";
350  }
351 
353  //
354  // which type of vtkfile to write
355  //
356 
358 
363  enum FileType {
368  };
369 
370  } // namespace VTK
371 
373 
374 } // namespace Dune
375 
376 #endif // DUNE_GRID_IO_FILE_VTK_COMMON_HH