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