VTK-m  2.0
VTKDataSetReaderBase.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_VTKDataSetReaderBase_h
11 #define vtk_m_io_VTKDataSetReaderBase_h
12 
13 #include <vtkm/Types.h>
14 #include <vtkm/cont/DataSet.h>
15 #include <vtkm/io/ErrorIO.h>
16 #include <vtkm/io/vtkm_io_export.h>
17 
21 
22 #include <fstream>
23 #include <sstream>
24 
25 namespace vtkm
26 {
27 namespace io
28 {
29 
30 namespace internal
31 {
32 
33 struct VTKDataSetFile
34 {
35  std::string FileName;
36  vtkm::Id2 Version;
37  std::string Title;
38  bool IsBinary;
39  vtkm::io::internal::DataSetStructure Structure;
40  std::ifstream Stream;
41 };
42 
43 inline void parseAssert(bool condition)
44 {
45  if (!condition)
46  {
47  throw vtkm::io::ErrorIO("Parse Error");
48  }
49 }
50 
51 template <typename T>
52 struct StreamIOType
53 {
54  using Type = T;
55 };
56 template <>
57 struct StreamIOType<vtkm::Int8>
58 {
59  using Type = vtkm::Int16;
60 };
61 template <>
62 struct StreamIOType<vtkm::UInt8>
63 {
64  using Type = vtkm::UInt16;
65 };
66 
67 inline vtkm::cont::UnknownCellSet CreateCellSetStructured(const vtkm::Id3& dim)
68 {
69  if (dim[0] > 1 && dim[1] > 1 && dim[2] > 1)
70  {
72  cs.SetPointDimensions(vtkm::make_Vec(dim[0], dim[1], dim[2]));
73  return cs;
74  }
75  else if (dim[0] > 1 && dim[1] > 1 && dim[2] <= 1)
76  {
78  cs.SetPointDimensions(vtkm::make_Vec(dim[0], dim[1]));
79  return cs;
80  }
81  else if (dim[0] > 1 && dim[1] <= 1 && dim[2] <= 1)
82  {
84  cs.SetPointDimensions(dim[0]);
85  return cs;
86  }
87 
88  std::stringstream ss;
89  ss << "Unsupported dimensions: (" << dim[0] << ", " << dim[1] << ", " << dim[2]
90  << "), 2D structured datasets should be on X-Y plane and "
91  << "1D structured datasets should be along X axis";
92  throw vtkm::io::ErrorIO(ss.str());
93 }
94 
95 } // namespace internal
96 
97 class VTKM_IO_EXPORT VTKDataSetReaderBase
98 {
99 protected:
100  std::unique_ptr<internal::VTKDataSetFile> DataFile;
102 
103 private:
104  bool Loaded;
106 
107  friend class VTKDataSetReader;
108 
109 public:
110  explicit VTKM_CONT VTKDataSetReaderBase(const char* fileName);
111 
112  explicit VTKM_CONT VTKDataSetReaderBase(const std::string& fileName);
113 
114  virtual VTKM_CONT ~VTKDataSetReaderBase();
115 
117  void operator=(const VTKDataSetReaderBase&) = delete;
118 
119  const VTKM_CONT vtkm::cont::DataSet& ReadDataSet();
120 
121  const vtkm::cont::DataSet& GetDataSet() const { return this->DataSet; }
122 
123  virtual VTKM_CONT void PrintSummary(std::ostream& out) const;
124 
125 protected:
126  VTKM_CONT void ReadPoints();
127 
128  VTKM_CONT void ReadCells(vtkm::cont::ArrayHandle<vtkm::Id>& connectivity,
130 
131  VTKM_CONT void ReadShapes(vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes);
132 
133  VTKM_CONT void ReadAttributes();
134 
136  {
137  this->CellsPermutation = permutation;
138  }
139 
141  {
142  return this->CellsPermutation;
143  }
144 
146  {
147  reader.DataFile.swap(this->DataFile);
148  this->DataFile.reset(nullptr);
149  }
150 
151  VTKM_CONT virtual void CloseFile();
152 
153  VTKM_CONT virtual void Read() = 0;
154 
155 private:
156  VTKM_CONT void OpenFile();
157  VTKM_CONT void ReadHeader();
158  VTKM_CONT void AddField(const std::string& name,
159  vtkm::cont::Field::Association association,
161  VTKM_CONT void ReadScalars(vtkm::cont::Field::Association association, std::size_t numElements);
162  VTKM_CONT void ReadColorScalars(vtkm::cont::Field::Association association,
163  std::size_t numElements);
164  VTKM_CONT void ReadLookupTable();
165  VTKM_CONT void ReadTextureCoordinates(vtkm::cont::Field::Association association,
166  std::size_t numElements);
167  VTKM_CONT void ReadVectors(vtkm::cont::Field::Association association, std::size_t numElements);
168  VTKM_CONT void ReadTensors(vtkm::cont::Field::Association association, std::size_t numElements);
169  VTKM_CONT void ReadFields(vtkm::cont::Field::Association association,
170  std::size_t expectedNumElements);
171  VTKM_CONT void ReadGlobalOrPedigreeIds(vtkm::cont::Field::Association association,
172  std::size_t numElements);
173 
174 protected:
175  VTKM_CONT void ReadGlobalFields(std::vector<vtkm::Float32>* visitBounds = nullptr);
176 
177 private:
178  class SkipArrayVariant;
179  class ReadArrayVariant;
180 
181  //Make the Array parsing methods protected so that derived classes
182  //can call the methods.
183 protected:
184  VTKM_CONT void DoSkipArrayVariant(std::string dataType,
185  std::size_t numElements,
186  vtkm::IdComponent numComponents);
187  VTKM_CONT vtkm::cont::UnknownArrayHandle DoReadArrayVariant(
188  vtkm::cont::Field::Association association,
189  std::string dataType,
190  std::size_t numElements,
191  vtkm::IdComponent numComponents);
192 
193  template <typename T>
194  VTKM_CONT void ReadArray(std::vector<T>& buffer)
195  {
196  using ComponentType = typename vtkm::VecTraits<T>::ComponentType;
197  constexpr vtkm::IdComponent numComponents = vtkm::VecTraits<T>::NUM_COMPONENTS;
198 
199  std::size_t numElements = buffer.size();
200  if (this->DataFile->IsBinary)
201  {
202  this->DataFile->Stream.read(reinterpret_cast<char*>(&buffer[0]),
203  static_cast<std::streamsize>(numElements * sizeof(T)));
204  if (vtkm::io::internal::IsLittleEndian())
205  {
206  vtkm::io::internal::FlipEndianness(buffer);
207  }
208  }
209  else
210  {
211  for (std::size_t i = 0; i < numElements; ++i)
212  {
213  for (vtkm::IdComponent j = 0; j < numComponents; ++j)
214  {
215  typename internal::StreamIOType<ComponentType>::Type val;
216  this->DataFile->Stream >> val;
217  vtkm::VecTraits<T>::SetComponent(buffer[i], j, static_cast<ComponentType>(val));
218  }
219  }
220  }
221  this->DataFile->Stream >> std::ws;
222  this->SkipArrayMetaData(numComponents);
223  }
224 
225  template <vtkm::IdComponent NumComponents>
228  {
230  "Support for data type 'bit' is not implemented. Skipping.");
231  this->SkipArray(buffer.size(), vtkm::Vec<vtkm::io::internal::DummyBitType, NumComponents>());
232  buffer.clear();
233  }
234 
235  VTKM_CONT void ReadArray(std::vector<vtkm::io::internal::DummyBitType>& buffer);
236 
237  template <typename T>
238  void SkipArray(std::size_t numElements, T)
239  {
240  using ComponentType = typename vtkm::VecTraits<T>::ComponentType;
241  constexpr vtkm::IdComponent numComponents = vtkm::VecTraits<T>::NUM_COMPONENTS;
242 
243  if (this->DataFile->IsBinary)
244  {
245  this->DataFile->Stream.seekg(static_cast<std::streamoff>(numElements * sizeof(T)),
246  std::ios_base::cur);
247  }
248  else
249  {
250  for (std::size_t i = 0; i < numElements; ++i)
251  {
252  for (vtkm::IdComponent j = 0; j < numComponents; ++j)
253  {
254  typename internal::StreamIOType<ComponentType>::Type val;
255  this->DataFile->Stream >> val;
256  }
257  }
258  }
259  this->DataFile->Stream >> std::ws;
260  this->SkipArrayMetaData(numComponents);
261  }
262 
263  template <vtkm::IdComponent NumComponents>
264  void SkipArray(std::size_t numElements,
266  {
267  this->SkipArray(numElements * static_cast<std::size_t>(NumComponents),
268  vtkm::io::internal::DummyBitType(),
269  NumComponents);
270  }
271 
272  VTKM_CONT void SkipArray(std::size_t numElements,
273  vtkm::io::internal::DummyBitType,
274  vtkm::IdComponent numComponents = 1);
275 
276  VTKM_CONT void SkipStringArray(std::size_t numStrings);
277 
278  VTKM_CONT void SkipArrayMetaData(vtkm::IdComponent numComponents);
279 };
280 }
281 } // vtkm::io
282 
283 #endif // vtk_m_io_VTKDataSetReaderBase_h
vtkm::io::VTKDataSetReaderBase::SetCellsPermutation
void SetCellsPermutation(const vtkm::cont::ArrayHandle< vtkm::Id > &permutation)
Definition: VTKDataSetReaderBase.h:135
vtkm::worklet::contourtree_distributed::dataType
vtkm::Float64 dataType
Definition: TreeCompiler.h:72
vtkm::cont::ArrayHandle< vtkm::Id >
vtkm::io::VTKDataSetReaderBase::CellsPermutation
vtkm::cont::ArrayHandle< vtkm::Id > CellsPermutation
Definition: VTKDataSetReaderBase.h:105
vtkm::io::VTKDataSetReaderBase::SkipArray
void SkipArray(std::size_t numElements, T)
Definition: VTKDataSetReaderBase.h:238
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::LogLevel::Warn
@ Warn
Less important user errors, such as out-of-bounds parameters.
Types.h
vtkm::io::VTKDataSetReaderBase
Definition: VTKDataSetReaderBase.h:97
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::CellSetStructured< 3 >
vtkm::io::VTKDataSetReaderBase::GetDataSet
const vtkm::cont::DataSet & GetDataSet() const
Definition: VTKDataSetReaderBase.h:121
vtkm::VecTraits::SetComponent
static VTKM_EXEC_CONT void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
vtkm::cont::Field::Association
Association
Definition: cont/Field.h:34
vtkm::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:406
vtkm::cont::DataSet
Definition: DataSet.h:34
vtkm::Int16
int16_t Int16
Definition: Types.h:158
vtkm::io::VTKDataSetReaderBase::TransferDataFile
VTKM_CONT void TransferDataFile(VTKDataSetReaderBase &reader)
Definition: VTKDataSetReaderBase.h:145
vtkm::cont::UnknownCellSet
A CellSet of an unknown type.
Definition: UnknownCellSet.h:48
vtkm::io::VTKDataSetReaderBase::ReadArray
VTKM_CONT void ReadArray(std::vector< T > &buffer)
Definition: VTKDataSetReaderBase.h:194
vtkm::io::VTKDataSetReaderBase::Loaded
bool Loaded
Definition: VTKDataSetReaderBase.h:104
vtkm::io::VTKDataSetReaderBase::DataSet
vtkm::cont::DataSet DataSet
Definition: VTKDataSetReaderBase.h:101
vtkm::Int8
int8_t Int8
Definition: Types.h:156
vtkm::io::ErrorIO
Definition: ErrorIO.h:22
ErrorIO.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::io::VTKDataSetReaderBase::DataFile
std::unique_ptr< internal::VTKDataSetFile > DataFile
Definition: VTKDataSetReaderBase.h:100
VTKDataSetStructures.h
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::make_Vec
constexpr VTKM_EXEC_CONT vtkm::Vec< T, vtkm::IdComponent(sizeof...(Ts)+1)> make_Vec(T value0, Ts &&... args)
Initializes and returns a Vec containing all the arguments.
Definition: Types.h:1212
VTKDataSetTypes.h
VTKM_LOG_S
#define VTKM_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:261
vtkm::io::VTKDataSetReader
Definition: VTKDataSetReader.h:20
vtkm::io::VTKDataSetReaderBase::SkipArray
void SkipArray(std::size_t numElements, vtkm::Vec< vtkm::io::internal::DummyBitType, NumComponents >)
Definition: VTKDataSetReaderBase.h:264
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::cont::CellSetStructured::SetPointDimensions
void SetPointDimensions(SchedulingRangeType dimensions)
Definition: CellSetStructured.h:49
vtkm::io::VTKDataSetReaderBase::ReadArray
VTKM_CONT void ReadArray(std::vector< vtkm::Vec< vtkm::io::internal::DummyBitType, NumComponents >> &buffer)
Definition: VTKDataSetReaderBase.h:226
vtkm::VecTraits::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:73
vtkm::VecTraits
The VecTraits class gives several static members that define how to use a given type as a vector.
Definition: VecTraits.h:66
vtkm::UInt16
uint16_t UInt16
Definition: Types.h:159
Endian.h
DataSet.h
vtkm::io::VTKDataSetReaderBase::GetCellsPermutation
VTKM_CONT vtkm::cont::ArrayHandle< vtkm::Id > GetCellsPermutation() const
Definition: VTKDataSetReaderBase.h:140