types.hh 5.64 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
#pragma once

#include <cstdint>
#include <map>
#include <string>
#include <vector>

8
#include <dune/common/ftraits.hh>
9
#include <dune/common/typelist.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
10
11
#include <dune/geometry/type.hh>

12
namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
13
14
15
16
17
18
19
20
21
{
  namespace Vtk
  {
    enum FormatTypes {
      ASCII      = 1<<0,
      BINARY     = 1<<1,
      COMPRESSED = 1<<2,
      APPENDED = BINARY | COMPRESSED
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
22
    std::string to_string (FormatTypes);
Praetorius, Simon's avatar
Praetorius, Simon committed
23
24
25
26
27
28
29
30
31
32

    enum DataTypes {
      UNKNOWN = 0,
      INT8, UINT8,
      INT16, UINT16,
      INT32, UINT32,
      INT64, UINT64,
      FLOAT32 = 32,
      FLOAT64 = 64
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
33
    std::string to_string (DataTypes);
Praetorius, Simon's avatar
Praetorius, Simon committed
34

35
36
    enum CellParametrization {
      LINEAR,
Praetorius, Simon's avatar
Praetorius, Simon committed
37
38
      QUADRATIC,
      LAGRANGE
Praetorius, Simon's avatar
Praetorius, Simon committed
39
40
    };

41
42
43
    enum CellTypes : std::uint8_t {
      // Linear VTK cell types
      VERTEX         = 1,
44
      /* POLY_VERTEX    = 2, // not supported */
45
      LINE           = 3,
46
      /* POLY_LINE      = 4, // not supported */
47
      TRIANGLE       = 5,
48
49
50
      /* TRIANGLE_STRIP = 6, // not supported */
      POLYGON        = 7,
      /* PIXEL          = 8, // not supported */
51
52
      QUAD           = 9,
      TETRA          = 10,
53
      /* VOXEL          = 11, // not supported */
54
55
56
57
58
59
60
61
      HEXAHEDRON     = 12,
      WEDGE          = 13,
      PYRAMID        = 14,
      // Quadratic VTK cell types
      QUADRATIC_EDGE       = 21,
      QUADRATIC_TRIANGLE   = 22,
      QUADRATIC_QUAD       = 23,
      QUADRATIC_TETRA      = 24,
Praetorius, Simon's avatar
Praetorius, Simon committed
62
63
64
65
66
67
68
69
70
      QUADRATIC_HEXAHEDRON = 25,
      // Arbitrary order Lagrange elements
      LAGRANGE_CURVE = 68,
      LAGRANGE_TRIANGLE = 69,
      LAGRANGE_QUADRILATERAL = 70,
      LAGRANGE_TETRAHEDRON = 71,
      LAGRANGE_HEXAHEDRON = 72,
      LAGRANGE_WEDGE = 73,
      LAGRANGE_PYRAMID = 74,
Praetorius, Simon's avatar
Praetorius, Simon committed
71
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
72
    GeometryType to_geometry (std::uint8_t);
Praetorius, Simon's avatar
Praetorius, Simon committed
73
74
75

    struct Map
    {
76
      static std::map<std::string, DataTypes> to_datatype; // String -> DataTypes
Praetorius, Simon's avatar
Praetorius, Simon committed
77

78
79
80
81
82
83
84
85
86
87
88
      static constexpr DataTypes typeImpl (MetaType<std::int8_t>)   { return INT8; }
      static constexpr DataTypes typeImpl (MetaType<std::uint8_t>)  { return UINT8; }
      static constexpr DataTypes typeImpl (MetaType<std::int16_t>)  { return INT16; }
      static constexpr DataTypes typeImpl (MetaType<std::uint16_t>) { return UINT16; }
      static constexpr DataTypes typeImpl (MetaType<std::int32_t>)  { return INT32; }
      static constexpr DataTypes typeImpl (MetaType<std::uint32_t>) { return UINT32; }
      static constexpr DataTypes typeImpl (MetaType<std::int64_t>)  { return INT64; }
      static constexpr DataTypes typeImpl (MetaType<std::uint64_t>) { return UINT64; }
      static constexpr DataTypes typeImpl (MetaType<float>)       { return FLOAT32; }
      static constexpr DataTypes typeImpl (MetaType<double>)      { return FLOAT64; }
      static constexpr DataTypes typeImpl (MetaType<long double>) { return FLOAT64; }
Praetorius, Simon's avatar
Praetorius, Simon committed
89
90

      template <class T>
91
      static constexpr DataTypes type () { return typeImpl(MetaType<typename FieldTraits<T>::field_type>{}); }
Praetorius, Simon's avatar
Praetorius, Simon committed
92
93
94
    };


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    /// Map a given enum DataType to a type passed to Caller as \ref MetaType
    template <class Caller>
    void mapDataTypes (Vtk::DataTypes t, Caller caller)
    {
      switch (t) {
        case INT8:    caller(MetaType<std::int8_t>{});   break;
        case UINT8:   caller(MetaType<std::uint8_t>{});  break;
        case INT16:   caller(MetaType<std::int16_t>{});  break;
        case UINT16:  caller(MetaType<std::uint16_t>{}); break;
        case INT32:   caller(MetaType<std::int32_t>{});  break;
        case UINT32:  caller(MetaType<std::uint32_t>{}); break;
        case INT64:   caller(MetaType<std::int64_t>{});  break;
        case UINT64:  caller(MetaType<std::uint64_t>{}); break;
        case FLOAT32: caller(MetaType<float>{});         break;
        case FLOAT64: caller(MetaType<double>{});        break;
        default:
          VTK_ASSERT_MSG(false, "Unsupported type " << to_string(t));
          break;
      }
    }

    /// Map two DataTypes as type parameters to the Caller
    template <class Caller>
    void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Caller caller)
    {
      mapDataTypes(t1, [&](auto type1) {
        mapDataTypes(t2, [&](auto type2) {
          caller(type1, type2);
        });
      });
    }

    /// Map three DataTypes as type parameters to the Caller
    template <class Caller>
    void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Vtk::DataTypes t3, Caller caller)
    {
      mapDataTypes(t1, [&](auto type1) {
        mapDataTypes(t2, [&](auto type2) {
          mapDataTypes(t3, [&](auto type3) {
            caller(type1, type2, type3);
          });
        });
      });
    }


Praetorius, Simon's avatar
Praetorius, Simon committed
141
142
143
144
    /// Mapping of Dune geometry types to VTK cell types
    class CellType
    {
    public:
145
      CellType (GeometryType const& t, CellParametrization = LINEAR);
Praetorius, Simon's avatar
Praetorius, Simon committed
146

147
      /// Return VTK Cell type
Praetorius, Simon's avatar
Praetorius, Simon committed
148
149
150
151
152
      std::uint8_t type () const
      {
        return type_;
      }

153
      /// Return a permutation of Dune elemenr vertices to conform to VTK element numbering
154
      int permutation (int idx) const
Praetorius, Simon's avatar
Praetorius, Simon committed
155
156
157
158
      {
        return permutation_[idx];
      }

159
160
161
162
163
      bool noPermutation () const
      {
        return noPermutation_;
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
164
165
166
    private:
      std::uint8_t type_;
      std::vector<int> permutation_;
Praetorius, Simon's avatar
Praetorius, Simon committed
167
      bool noPermutation_ = true;
Praetorius, Simon's avatar
Praetorius, Simon committed
168
169
    };

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

    class FieldInfo
    {
    public:
      FieldInfo (std::string name, int ncomps)
        : name_(std::move(name))
        , ncomps_(ncomps)
      {}

      /// The name of the data field
      std::string const& name () const
      {
        return name_;
      }

      /// The number of components in the data field.
      int ncomps () const
      {
        return ncomps_;
      }

    private:
      std::string name_;
      int ncomps_;
    };

Praetorius, Simon's avatar
Praetorius, Simon committed
196
  } // end namespace Vtk
197
} // end namespace Dune