VTK-m  2.0
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 T, typename Functor>
174 inline void SelectVecTypeAndCall(T, vtkm::IdComponent numComponents, const Functor& functor)
175 {
176  switch (numComponents)
177  {
178  case 1:
179  functor(T());
180  break;
181  case 2:
182  functor(vtkm::Vec<T, 2>());
183  break;
184  case 3:
185  functor(vtkm::Vec<T, 3>());
186  break;
187  case 4:
188  functor(vtkm::Vec<T, 4>());
189  break;
190  case 9:
191  functor(vtkm::Vec<T, 9>());
192  break;
193  default:
194  functor(numComponents, T());
195  break;
196  }
197 }
198 
199 template <typename Functor>
200 inline void SelectTypeAndCall(DataType dtype,
201  vtkm::IdComponent numComponents,
202  const Functor& functor)
203 {
204  switch (dtype)
205  {
206  case DTYPE_BIT:
207  SelectVecTypeAndCall(DummyBitType(), numComponents, functor);
208  break;
209  case DTYPE_UNSIGNED_CHAR:
210  SelectVecTypeAndCall(vtkm::UInt8(), numComponents, functor);
211  break;
212  case DTYPE_CHAR:
213  SelectVecTypeAndCall(vtkm::Int8(), numComponents, functor);
214  break;
215  case DTYPE_UNSIGNED_SHORT:
216  SelectVecTypeAndCall(vtkm::UInt16(), numComponents, functor);
217  break;
218  case DTYPE_SHORT:
219  SelectVecTypeAndCall(vtkm::Int16(), numComponents, functor);
220  break;
221  case DTYPE_UNSIGNED_INT:
222  SelectVecTypeAndCall(vtkm::UInt32(), numComponents, functor);
223  break;
224  case DTYPE_INT:
225  SelectVecTypeAndCall(vtkm::Int32(), numComponents, functor);
226  break;
227  case DTYPE_UNSIGNED_LONG:
228  case DTYPE_UNSIGNED_LONG_LONG:
229  SelectVecTypeAndCall(vtkm::UInt64(), numComponents, functor);
230  break;
231  case DTYPE_LONG:
232  case DTYPE_LONG_LONG:
233  SelectVecTypeAndCall(vtkm::Int64(), numComponents, functor);
234  break;
235  case DTYPE_FLOAT:
236  SelectVecTypeAndCall(vtkm::Float32(), numComponents, functor);
237  break;
238  case DTYPE_DOUBLE:
239  SelectVecTypeAndCall(vtkm::Float64(), numComponents, functor);
240  break;
241  default:
242  assert(false);
243  }
244 }
245 }
246 }
247 } // namespace vtkm::io::internal
248 
249 VTKM_BASIC_TYPE_VECTOR(vtkm::io::internal::ColorChannel8)
250 VTKM_BASIC_TYPE_VECTOR(vtkm::io::internal::DummyBitType)
251 
252 #endif // vtk_m_io_internal_VTKDataSetTypes_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::Int16
int16_t Int16
Definition: Types.h:158
vtkm::Vec< T, 4 >
Definition: Types.h:1093
vtkm::Int8
int8_t Int8
Definition: Types.h:156
vtkm::Vec< T, 2 >
Definition: Types.h:859
vtkm::operator<<
VTKM_CONT std::ostream & operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:237
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::Vec< T, 3 >
Definition: Types.h:975
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::UInt16
uint16_t UInt16
Definition: Types.h:159
vtkm::worklet::contourtree_distributed::operator>>
std::istream & operator>>(std::istream &inStream, SupernodeOnSuperarc &node)
Definition: TreeCompiler.h:215
VecTraits.h
VTKM_BASIC_TYPE_VECTOR
#define VTKM_BASIC_TYPE_VECTOR(type)
Definition: VecTraits.h:598