VTK-m  2.2
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 
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 
120  VTKM_CONT const vtkm::cont::DataSet& ReadDataSet();
121 
122  const vtkm::cont::DataSet& GetDataSet() const { return this->DataSet; }
123 
124  virtual VTKM_CONT void PrintSummary(std::ostream& out) const;
125 
126 protected:
127  VTKM_CONT void ReadPoints();
128 
129  VTKM_CONT void ReadCells(vtkm::cont::ArrayHandle<vtkm::Id>& connectivity,
131 
132  VTKM_CONT void ReadShapes(vtkm::cont::ArrayHandle<vtkm::UInt8>& shapes);
133 
134  VTKM_CONT void ReadAttributes();
135 
137  {
138  this->CellsPermutation = permutation;
139  }
140 
142  {
143  return this->CellsPermutation;
144  }
145 
147  {
148  reader.DataFile.swap(this->DataFile);
149  this->DataFile.reset(nullptr);
150  }
151 
152  VTKM_CONT virtual void CloseFile();
153 
154  VTKM_CONT virtual void Read() = 0;
155 
156 private:
157  VTKM_CONT void OpenFile();
158  VTKM_CONT void ReadHeader();
159  VTKM_CONT void AddField(const std::string& name,
160  vtkm::cont::Field::Association association,
162  VTKM_CONT void ReadScalars(vtkm::cont::Field::Association association, std::size_t numElements);
163  VTKM_CONT void ReadColorScalars(vtkm::cont::Field::Association association,
164  std::size_t numElements);
165  VTKM_CONT void ReadLookupTable();
166  VTKM_CONT void ReadTextureCoordinates(vtkm::cont::Field::Association association,
167  std::size_t numElements);
168  VTKM_CONT void ReadVectors(vtkm::cont::Field::Association association, std::size_t numElements);
169  VTKM_CONT void ReadTensors(vtkm::cont::Field::Association association, std::size_t numElements);
170  VTKM_CONT void ReadFields(vtkm::cont::Field::Association association,
171  std::size_t expectedNumElements);
172  VTKM_CONT void ReadGlobalOrPedigreeIds(vtkm::cont::Field::Association association,
173  std::size_t numElements);
174 
175 protected:
176  VTKM_CONT void ReadGlobalFields(std::vector<vtkm::Float32>* visitBounds = nullptr);
177 
178 private:
179  class SkipArrayVariant;
180  class ReadArrayVariant;
181 
182  //Make the Array parsing methods protected so that derived classes
183  //can call the methods.
184 protected:
185  VTKM_CONT void DoSkipArrayVariant(std::string dataType,
186  std::size_t numElements,
187  vtkm::IdComponent numComponents);
188  VTKM_CONT vtkm::cont::UnknownArrayHandle DoReadArrayVariant(
189  vtkm::cont::Field::Association association,
190  std::string dataType,
191  std::size_t numElements,
192  vtkm::IdComponent numComponents);
193 
194  template <typename T>
195  VTKM_CONT void ReadArray(std::vector<T>& buffer)
196  {
197  using ComponentType = typename vtkm::VecTraits<T>::ComponentType;
198  constexpr vtkm::IdComponent numComponents = vtkm::VecTraits<T>::NUM_COMPONENTS;
199 
200  std::size_t numElements = buffer.size();
201  if (this->DataFile->IsBinary)
202  {
203  this->DataFile->Stream.read(reinterpret_cast<char*>(&buffer[0]),
204  static_cast<std::streamsize>(numElements * sizeof(T)));
205  if (vtkm::io::internal::IsLittleEndian())
206  {
207  vtkm::io::internal::FlipEndianness(buffer);
208  }
209  }
210  else
211  {
212  for (std::size_t i = 0; i < numElements; ++i)
213  {
214  for (vtkm::IdComponent j = 0; j < numComponents; ++j)
215  {
216  typename internal::StreamIOType<ComponentType>::Type val;
217  this->DataFile->Stream >> val;
218  vtkm::VecTraits<T>::SetComponent(buffer[i], j, static_cast<ComponentType>(val));
219  }
220  }
221  }
222  this->DataFile->Stream >> std::ws;
223  this->SkipArrayMetaData(numComponents);
224  }
225 
226  template <vtkm::IdComponent NumComponents>
229  {
231  "Support for data type 'bit' is not implemented. Skipping.");
232  this->SkipArray(buffer.size(), vtkm::Vec<vtkm::io::internal::DummyBitType, NumComponents>());
233  buffer.clear();
234  }
235 
236  VTKM_CONT void ReadArray(std::vector<vtkm::io::internal::DummyBitType>& buffer);
237 
238  template <typename T>
239  void SkipArray(std::size_t numElements, T)
240  {
241  using ComponentType = typename vtkm::VecTraits<T>::ComponentType;
242  constexpr vtkm::IdComponent numComponents = vtkm::VecTraits<T>::NUM_COMPONENTS;
243 
244  if (this->DataFile->IsBinary)
245  {
246  this->DataFile->Stream.seekg(static_cast<std::streamoff>(numElements * sizeof(T)),
247  std::ios_base::cur);
248  }
249  else
250  {
251  for (std::size_t i = 0; i < numElements; ++i)
252  {
253  for (vtkm::IdComponent j = 0; j < numComponents; ++j)
254  {
255  typename internal::StreamIOType<ComponentType>::Type val;
256  this->DataFile->Stream >> val;
257  }
258  }
259  }
260  this->DataFile->Stream >> std::ws;
261  this->SkipArrayMetaData(numComponents);
262  }
263 
264  template <vtkm::IdComponent NumComponents>
265  void SkipArray(std::size_t numElements,
267  {
268  this->SkipArray(numElements * static_cast<std::size_t>(NumComponents),
269  vtkm::io::internal::DummyBitType(),
270  NumComponents);
271  }
272 
273  VTKM_CONT void SkipArray(std::size_t numElements,
274  vtkm::io::internal::DummyBitType,
275  vtkm::IdComponent numComponents = 1);
276 
277  VTKM_CONT void SkipStringArray(std::size_t numStrings);
278 
279  VTKM_CONT void SkipArrayMetaData(vtkm::IdComponent numComponents);
280 };
281 }
282 } // vtkm::io
283 
284 #endif // vtk_m_io_VTKDataSetReaderBase_h
vtkm::io::VTKDataSetReaderBase::SetCellsPermutation
void SetCellsPermutation(const vtkm::cont::ArrayHandle< vtkm::Id > &permutation)
Definition: VTKDataSetReaderBase.h:136
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:239
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::make_Vec
constexpr 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:1250
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::CellSetStructured
Defines a 1-, 2-, or 3-dimensional structured grid of points.
Definition: CastAndCall.h:32
vtkm::io::VTKDataSetReaderBase::GetDataSet
const vtkm::cont::DataSet & GetDataSet() const
Definition: VTKDataSetReaderBase.h:122
vtkm::cont::Field::Association
Association
Identifies what elements of a data set a field is associated with.
Definition: Field.h:38
vtkm::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:71
vtkm::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:430
vtkm::io::VTKDataSetReaderBase::TransferDataFile
void TransferDataFile(VTKDataSetReaderBase &reader)
Definition: VTKDataSetReaderBase.h:146
vtkm::cont::DataSet
Contains and manages the geometric data structures that VTK-m operates on.
Definition: DataSet.h:57
vtkm::Int16
int16_t Int16
Base type to use for 16-bit signed integer numbers.
Definition: Types.h:173
vtkm::io::VTKDataSetReaderBase::GetCellsPermutation
vtkm::cont::ArrayHandle< vtkm::Id > GetCellsPermutation() const
Definition: VTKDataSetReaderBase.h:141
vtkm_io_export.h
vtkm::cont::UnknownCellSet
A CellSet of an unknown type.
Definition: UnknownCellSet.h:48
VTKM_IO_EXPORT
#define VTKM_IO_EXPORT
Definition: vtkm_io_export.h:44
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
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:165
vtkm::io::ErrorIO
This class is thrown when VTK-m encounters an error with the file system.
Definition: ErrorIO.h:25
ErrorIO.h
vtkm::io::VTKDataSetReaderBase::ReadArray
void ReadArray(std::vector< T > &buffer)
Definition: VTKDataSetReaderBase.h:195
vtkm::VecTraits::SetComponent
static void SetComponent(T &vector, vtkm::IdComponent, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:131
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
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:169
VTKDataSetTypes.h
VTKM_LOG_S
#define VTKM_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:208
vtkm::io::VTKDataSetReader
Reads a legacy VTK file.
Definition: VTKDataSetReader.h:25
vtkm::io::VTKDataSetReaderBase::ReadArray
void ReadArray(std::vector< vtkm::Vec< vtkm::io::internal::DummyBitType, NumComponents >> &buffer)
Definition: VTKDataSetReaderBase.h:227
vtkm::io::VTKDataSetReaderBase::SkipArray
void SkipArray(std::size_t numElements, vtkm::Vec< vtkm::io::internal::DummyBitType, NumComponents >)
Definition: VTKDataSetReaderBase.h:265
vtkm::Vec< vtkm::Id, 2 >
vtkm::cont::CellSetStructured::SetPointDimensions
void SetPointDimensions(SchedulingRangeType dimensions)
Set the dimensions of the structured array of points.
Definition: CellSetStructured.h:58
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
vtkm::UInt16
uint16_t UInt16
Base type to use for 16-bit unsigned integer numbers.
Definition: Types.h:177
Endian.h
DataSet.h