types.hh 5.68 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
#include <dune/geometry/type.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
11
#include <dune/vtk/utility/errors.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
12

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

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

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

42
43
44
    enum CellTypes : std::uint8_t {
      // Linear VTK cell types
      VERTEX         = 1,
45
      /* POLY_VERTEX    = 2, // not supported */
46
      LINE           = 3,
47
      /* POLY_LINE      = 4, // not supported */
48
      TRIANGLE       = 5,
49
50
51
      /* TRIANGLE_STRIP = 6, // not supported */
      POLYGON        = 7,
      /* PIXEL          = 8, // not supported */
52
53
      QUAD           = 9,
      TETRA          = 10,
54
      /* VOXEL          = 11, // not supported */
55
56
57
58
59
60
61
62
      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
63
64
65
66
67
68
69
70
71
      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
72
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
73
    GeometryType to_geometry (std::uint8_t);
Praetorius, Simon's avatar
Praetorius, Simon committed
74
75
76

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

79
80
81
82
83
84
85
86
87
88
89
      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
90
91

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


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    /// 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:
112
          VTK_ASSERT_MSG(false, "Unsupported type " + to_string(t));
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
141
          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
142
143
144
145
    /// Mapping of Dune geometry types to VTK cell types
    class CellType
    {
    public:
146
      CellType (GeometryType const& t, CellParametrization = LINEAR);
Praetorius, Simon's avatar
Praetorius, Simon committed
147

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

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
165
166
167
    private:
      std::uint8_t type_;
      std::vector<int> permutation_;
Praetorius, Simon's avatar
Praetorius, Simon committed
168
      bool noPermutation_ = true;
Praetorius, Simon's avatar
Praetorius, Simon committed
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
196

    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
197
  } // end namespace Vtk
198
} // end namespace Dune