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