VTK-m  2.2
VTKDataSetTypes.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_io_internal_VTKDataSetTypes_h
11 #define vtk_m_io_internal_VTKDataSetTypes_h
12 
13 #include <vtkm/Types.h>
14 #include <vtkm/VecTraits.h>
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <string>
19 
20 namespace vtkm
21 {
22 namespace io
23 {
24 namespace internal
25 {
26 
27 enum DataType
28 {
29  DTYPE_UNKNOWN = 0,
30  DTYPE_BIT,
31  DTYPE_UNSIGNED_CHAR,
32  DTYPE_CHAR,
33  DTYPE_UNSIGNED_SHORT,
34  DTYPE_SHORT,
35  DTYPE_UNSIGNED_INT,
36  DTYPE_INT,
37  DTYPE_UNSIGNED_LONG,
38  DTYPE_LONG,
39  DTYPE_FLOAT,
40  DTYPE_DOUBLE,
41  DTYPE_UNSIGNED_LONG_LONG,
42  DTYPE_LONG_LONG,
43 
44  DTYPE_COUNT
45 };
46 
47 inline const char* DataTypeString(int id)
48 {
49  static const char* strings[] = {
50  "", "bit", "unsigned_char", "char", "unsigned_short",
51  "short", "unsigned_int", "int", "unsigned_long", "long",
52  "float", "double", "vtktypeuint64", "vtktypeint64"
53  };
54  return strings[id];
55 }
56 
57 inline DataType DataTypeId(const std::string& str)
58 {
59  DataType type = DTYPE_UNKNOWN;
60  for (int id = 1; id < DTYPE_COUNT; ++id)
61  {
62  if (str == DataTypeString(id))
63  {
64  type = static_cast<DataType>(id);
65  }
66  }
67 
68  return type;
69 }
70 
71 struct DummyBitType
72 {
73  // Needs to work with streams' << operator
74  operator bool() const { return false; }
75 };
76 
77 class ColorChannel8
78 {
79 public:
80  ColorChannel8()
81  : Data()
82  {
83  }
84  ColorChannel8(vtkm::UInt8 val)
85  : Data(val)
86  {
87  }
88  ColorChannel8(vtkm::Float32 val)
89  : Data(static_cast<vtkm::UInt8>(std::min(std::max(val, 1.0f), 0.0f) * 255))
90  {
91  }
92  operator vtkm::Float32() const { return static_cast<vtkm::Float32>(this->Data) / 255.0f; }
93  operator vtkm::UInt8() const { return this->Data; }
94 
95 private:
96  vtkm::UInt8 Data;
97 };
98 
99 inline std::ostream& operator<<(std::ostream& out, const ColorChannel8& val)
100 {
101  return out << static_cast<vtkm::Float32>(val);
102 }
103 
104 inline std::istream& operator>>(std::istream& in, ColorChannel8& val)
105 {
106  vtkm::Float32 fval;
107  in >> fval;
108  val = ColorChannel8(fval);
109  return in;
110 }
111 
112 template <typename T>
113 struct DataTypeName
114 {
115  static const char* Name() { return "unknown"; }
116 };
117 template <>
118 struct DataTypeName<DummyBitType>
119 {
120  static const char* Name() { return "bit"; }
121 };
122 template <>
123 struct DataTypeName<vtkm::Int8>
124 {
125  static const char* Name() { return "char"; }
126 };
127 template <>
128 struct DataTypeName<vtkm::UInt8>
129 {
130  static const char* Name() { return "unsigned_char"; }
131 };
132 template <>
133 struct DataTypeName<vtkm::Int16>
134 {
135  static const char* Name() { return "short"; }
136 };
137 template <>
138 struct DataTypeName<vtkm::UInt16>
139 {
140  static const char* Name() { return "unsigned_short"; }
141 };
142 template <>
143 struct DataTypeName<vtkm::Int32>
144 {
145  static const char* Name() { return "int"; }
146 };
147 template <>
148 struct DataTypeName<vtkm::UInt32>
149 {
150  static const char* Name() { return "unsigned_int"; }
151 };
152 template <>
153 struct DataTypeName<vtkm::Int64>
154 {
155  static const char* Name() { return "long"; }
156 };
157 template <>
158 struct DataTypeName<vtkm::UInt64>
159 {
160  static const char* Name() { return "unsigned_long"; }
161 };
162 template <>
163 struct DataTypeName<vtkm::Float32>
164 {
165  static const char* Name() { return "float"; }
166 };
167 template <>
168 struct DataTypeName<vtkm::Float64>
169 {
170  static const char* Name() { return "double"; }
171 };
172 
173 template <typename Functor>
174 inline void SelectTypeAndCall(DataType dtype, Functor&& functor)
175 {
176  switch (dtype)
177  {
178  case DTYPE_BIT:
179  functor(DummyBitType());
180  break;
181  case DTYPE_UNSIGNED_CHAR:
182  functor(vtkm::UInt8());
183  break;
184  case DTYPE_CHAR:
185  functor(vtkm::Int8());
186  break;
187  case DTYPE_UNSIGNED_SHORT:
188  functor(vtkm::UInt16());
189  break;
190  case DTYPE_SHORT:
191  functor(vtkm::Int16());
192  break;
193  case DTYPE_UNSIGNED_INT:
194  functor(vtkm::UInt32());
195  break;
196  case DTYPE_INT:
197  functor(vtkm::Int32());
198  break;
199  case DTYPE_UNSIGNED_LONG:
200  case DTYPE_UNSIGNED_LONG_LONG:
201  functor(vtkm::UInt64());
202  break;
203  case DTYPE_LONG:
204  case DTYPE_LONG_LONG:
205  functor(vtkm::Int64());
206  break;
207  case DTYPE_FLOAT:
208  functor(vtkm::Float32());
209  break;
210  case DTYPE_DOUBLE:
211  functor(vtkm::Float64());
212  break;
213  default:
214  assert(false);
215  }
216 }
217 
218 }
219 }
220 } // namespace vtkm::io::internal
221 
222 #endif // vtk_m_io_internal_VTKDataSetTypes_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::operator<<
std::ostream & operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:248
vtkm::Int16
int16_t Int16
Base type to use for 16-bit signed integer numbers.
Definition: Types.h:173
vtkm::Int8
int8_t Int8
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:165
vtkm::Int64
signed long long Int64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:204
vtkm::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:169
vtkm::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:185
vtkm::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:157
vtkm::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
vtkm::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:181
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
vtkm::UInt16
uint16_t UInt16
Base type to use for 16-bit unsigned integer numbers.
Definition: Types.h:177
VecTraits.h