VTK  9.0.20200603
vtkXMLReader.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkXMLReader.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
24 #ifndef vtkXMLReader_h
25 #define vtkXMLReader_h
26 
27 #include "vtkAlgorithm.h"
28 #include "vtkIOXMLModule.h" // For export macro
29 
30 #include <string> // for std::string
31 #include <vector>
32 
33 class vtkAbstractArray;
34 class vtkCallbackCommand;
35 class vtkCommand;
37 class vtkDataSet;
39 class vtkXMLDataElement;
40 class vtkXMLDataParser;
42 class vtkInformation;
43 class vtkStringArray;
44 
45 class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
46 {
47 public:
48  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
49  void PrintSelf(ostream& os, vtkIndent indent) override;
50 
51  enum FieldType
52  {
55  OTHER
56  };
57 
59 
62  vtkSetStringMacro(FileName);
63  vtkGetStringMacro(FileName);
65 
67 
70  vtkSetMacro(ReadFromInputString, vtkTypeBool);
71  vtkGetMacro(ReadFromInputString, vtkTypeBool);
72  vtkBooleanMacro(ReadFromInputString, vtkTypeBool);
73  void SetInputString(const std::string& s) { this->InputString = s; }
75 
83  virtual int CanReadFile(const char* name);
84 
86 
89  vtkDataSet* GetOutputAsDataSet();
90  vtkDataSet* GetOutputAsDataSet(int index);
92 
94 
98  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
99  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
100  vtkGetObjectMacro(ColumnArraySelection, vtkDataArraySelection);
102 
104 
107  int GetNumberOfPointArrays();
108  int GetNumberOfCellArrays();
109  int GetNumberOfColumnArrays();
111 
113 
116  int GetNumberOfTimeDataArrays() const;
117  const char* GetTimeDataArray(int idx) const;
118  vtkGetObjectMacro(TimeDataStringArray, vtkStringArray);
120 
122 
128  vtkGetStringMacro(ActiveTimeDataArrayName);
129  vtkSetStringMacro(ActiveTimeDataArrayName);
131 
133 
137  const char* GetPointArrayName(int index);
138  const char* GetCellArrayName(int index);
139  const char* GetColumnArrayName(int index);
141 
143 
147  int GetPointArrayStatus(const char* name);
148  int GetCellArrayStatus(const char* name);
149  void SetPointArrayStatus(const char* name, int status);
150  void SetCellArrayStatus(const char* name, int status);
151  int GetColumnArrayStatus(const char* name);
152  void SetColumnArrayStatus(const char* name, int status);
154 
155  // For the specified port, copy the information this reader sets up in
156  // SetupOutputInformation to outInfo
157  virtual void CopyOutputInformation(vtkInformation* vtkNotUsed(outInfo), int vtkNotUsed(port)) {}
158 
160 
163  vtkSetMacro(TimeStep, int);
164  vtkGetMacro(TimeStep, int);
166 
167  vtkGetMacro(NumberOfTimeSteps, int);
169 
172  vtkGetVector2Macro(TimeStepRange, int);
173  vtkSetVector2Macro(TimeStepRange, int);
175 
180  vtkXMLDataParser* GetXMLParser() { return this->XMLParser; }
181 
183  vtkInformationVector* outputVector) override;
184 
186 
190  void SetReaderErrorObserver(vtkCommand*);
191  vtkGetObjectMacro(ReaderErrorObserver, vtkCommand);
193 
195 
199  void SetParserErrorObserver(vtkCommand*);
200  vtkGetObjectMacro(ParserErrorObserver, vtkCommand);
202 
203 protected:
204  vtkXMLReader();
205  ~vtkXMLReader() override;
206 
207  // Pipeline execution methods to be defined by subclass. Called by
208  // corresponding RequestData methods after appropriate setup has been
209  // done.
210  virtual int ReadXMLInformation();
211  virtual void ReadXMLData();
212 
213  // Get the name of the data set being read.
214  virtual const char* GetDataSetName() = 0;
215 
216  // Test if the reader can read a file with the given version number.
217  virtual int CanReadFileVersion(int major, int minor);
218 
219  // Setup the output with no data available. Used in error cases.
220  virtual void SetupEmptyOutput() = 0;
221 
222  // Setup the output's information.
223  virtual void SetupOutputInformation(vtkInformation* vtkNotUsed(outInfo)) {}
224 
225  // Setup the output's data with allocation.
226  virtual void SetupOutputData();
227 
228  // Read the primary element from the file. This is the element
229  // whose name is the value returned by GetDataSetName().
230  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
231 
232  // Read the top-level element from the file. This is always the
233  // VTKFile element.
234  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
235 
241  int GetLocalDataType(vtkXMLDataElement* da, int datatype);
242 
243  // Create a vtkAbstractArray from its cooresponding XML representation.
244  // Does not allocate.
246 
247  // Create a vtkInformationKey from its corresponding XML representation.
248  // Stores it in the instance of vtkInformationProvided. Does not allocate.
249  int CreateInformationKey(vtkXMLDataElement* eInfoKey, vtkInformation* info);
250 
251  // Populates the info object with the InformationKey children in infoRoot.
252  // Returns false if errors occur.
253  bool ReadInformation(vtkXMLDataElement* infoRoot, vtkInformation* info);
254 
255  // Internal utility methods.
256  virtual int OpenStream();
257  virtual void CloseStream();
258  virtual int OpenVTKFile();
259  virtual void CloseVTKFile();
260  virtual int OpenVTKString();
261  virtual void CloseVTKString();
262  virtual void CreateXMLParser();
263  virtual void DestroyXMLParser();
264  void SetupCompressor(const char* type);
265  int CanReadFileVersionString(const char* version);
266 
272  virtual int CanReadFileWithDataType(const char* dsname);
273 
274  // Returns the major version for the file being read. -1 when invalid.
275  vtkGetMacro(FileMajorVersion, int);
276 
277  // Returns the minor version for the file being read. -1 when invalid.
278  vtkGetMacro(FileMinorVersion, int);
279 
280  // Utility methods for subclasses.
281  int IntersectExtents(int* extent1, int* extent2, int* result);
282  int Min(int a, int b);
283  int Max(int a, int b);
284  void ComputePointDimensions(int* extent, int* dimensions);
285  void ComputePointIncrements(int* extent, vtkIdType* increments);
286  void ComputeCellDimensions(int* extent, int* dimensions);
287  void ComputeCellIncrements(int* extent, vtkIdType* increments);
288  vtkIdType GetStartTuple(int* extent, vtkIdType* increments, int i, int j, int k);
289  void ReadAttributeIndices(vtkXMLDataElement* eDSA, vtkDataSetAttributes* dsa);
290  char** CreateStringArray(int numStrings);
291  void DestroyStringArray(int numStrings, char** strings);
292 
293  // Read an Array values starting at the given index and up to numValues.
294  // This method assumes that the array is of correct size to
295  // accommodate all numValues values. arrayIndex is the value index at which the read
296  // values will be put in the array.
297  virtual int ReadArrayValues(vtkXMLDataElement* da, vtkIdType arrayIndex, vtkAbstractArray* array,
298  vtkIdType startIndex, vtkIdType numValues, FieldType type = OTHER);
299 
300  // Setup the data array selections for the input's set of arrays.
301  void SetDataArraySelections(vtkXMLDataElement* eDSA, vtkDataArraySelection* sel);
302 
303  int SetFieldDataInfo(vtkXMLDataElement* eDSA, int association, vtkIdType numTuples,
304  vtkInformationVector*(&infoVector));
305 
306  // Check whether the given array element is an enabled array.
307  int PointDataArrayIsEnabled(vtkXMLDataElement* ePDA);
308  int CellDataArrayIsEnabled(vtkXMLDataElement* eCDA);
309 
310  // Callback registered with the SelectionObserver.
311  static void SelectionModifiedCallback(
312  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
313 
314  // Give concrete classes an option to squeeze any output arrays
315  // at the end of RequestData.
317 
318  // The vtkXMLDataParser instance used to hide XML reading details.
320 
321  // The FieldData element representation.
323 
324  // The input file's name.
325  char* FileName;
326 
327  // The stream used to read the input.
328  istream* Stream;
329 
330  // Whether this object is reading from a string or a file.
331  // Default is 0: read from file.
333 
334  // The input string.
336 
337  // The array selections.
342 
347 
348  // The observer to modify this object when the array selections are
349  // modified.
351 
352  // Whether there was an error reading the file in RequestInformation.
354 
355  // Whether there was an error reading the file in RequestData.
357 
358  // incrementally fine-tuned progress updates.
359  virtual void GetProgressRange(float* range);
360  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
361  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
362  virtual void UpdateProgressDiscrete(float progress);
363  float ProgressRange[2];
364 
365  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
366  vtkInformationVector* outputVector);
367  virtual int RequestDataObject(vtkInformation* vtkNotUsed(request),
368  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
369  {
370  return 1;
371  }
372  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
373  vtkInformationVector* outputVector);
375 
376  // Whether there was an error reading the XML.
378 
379  // For structured data keep track of dimensions empty of cells. For
380  // unstructured data these are always zero. This is used to support
381  // 1-D and 2-D cell data.
382  int AxesEmpty[3];
383 
384  // The timestep currently being read.
385  int TimeStep;
388  void SetNumberOfTimeSteps(int num);
389  // buffer for reading timestep from the XML file the length is of
390  // NumberOfTimeSteps and therefore is always long enough
391  int* TimeSteps;
392  // Store the range of time steps
393  int TimeStepRange[2];
394 
395  // Now we need to save what was the last time read for each kind of
396  // data to avoid rereading it that is to say we need a var for
397  // e.g. PointData/CellData/Points/Cells...
398  // See SubClass for details with member vars like PointsTimeStep/PointsOffset
399 
400  // Helper function useful to know if a timestep is found in an array of timestep
401  static int IsTimeStepInArray(int timestep, int* timesteps, int length);
402 
403  vtkDataObject* GetCurrentOutput();
404  vtkInformation* GetCurrentOutputInformation();
405 
406  // Flag for whether DataProgressCallback should actually update
407  // progress.
409 
411 
412  void ReadFieldData();
413 
414 private:
415  // The stream used to read the input if it is in a file.
416  istream* FileStream;
417  // The stream used to read the input if it is in a string.
418  std::istringstream* StringStream;
419  int TimeStepWasReadOnce;
420 
421  int FileMajorVersion;
422  int FileMinorVersion;
423 
424  vtkDataObject* CurrentOutput;
425  vtkInformation* CurrentOutputInformation;
426 
427 private:
428  vtkXMLReader(const vtkXMLReader&) = delete;
429  void operator=(const vtkXMLReader&) = delete;
430 
431  vtkCommand* ReaderErrorObserver;
432  vtkCommand* ParserErrorObserver;
433 };
434 
435 #endif
vtkXMLReader::ReadMTime
vtkTimeStamp ReadMTime
Definition: vtkXMLReader.h:374
vtkCommand
superclass for callback/observer methods
Definition: vtkCommand.h:377
vtkXMLReader::POINT_DATA
Definition: vtkXMLReader.h:53
vtkXMLReader::CurrentTimeStep
int CurrentTimeStep
Definition: vtkXMLReader.h:386
vtkXMLReader::FieldDataElement
vtkXMLDataElement * FieldDataElement
Definition: vtkXMLReader.h:322
vtkXMLReader::PointDataArraySelection
vtkDataArraySelection * PointDataArraySelection
Definition: vtkXMLReader.h:338
vtkXMLReader::TimeSteps
int * TimeSteps
Definition: vtkXMLReader.h:391
vtkXMLReader::ReadFromInputString
vtkTypeBool ReadFromInputString
Definition: vtkXMLReader.h:332
vtkXMLReader::SetInputString
void SetInputString(const std::string &s)
Definition: vtkXMLReader.h:73
vtkX3D::type
Definition: vtkX3D.h:522
vtkIdType
int vtkIdType
Definition: vtkType.h:330
vtkXMLReader::XMLParser
vtkXMLDataParser * XMLParser
Definition: vtkXMLReader.h:319
vtkAlgorithm
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:63
vtkDataSetAttributes
represent and manipulate attribute data in a dataset
Definition: vtkDataSetAttributes.h:53
vtkXMLReader::ConvertGhostLevelsToGhostType
virtual void ConvertGhostLevelsToGhostType(FieldType, vtkAbstractArray *, vtkIdType, vtkIdType)
Definition: vtkXMLReader.h:410
vtkXMLReader::InformationError
int InformationError
Definition: vtkXMLReader.h:353
vtkInformationVector
Store zero or more vtkInformation instances.
Definition: vtkInformationVector.h:35
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkXMLReader::FileName
char * FileName
Definition: vtkXMLReader.h:325
vtkX3D::range
Definition: vtkX3D.h:244
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkXMLReader::RequestDataObject
virtual int RequestDataObject(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *vtkNotUsed(outputVector))
Definition: vtkXMLReader.h:367
vtkX3D::length
Definition: vtkX3D.h:399
vtkXMLReader::Stream
istream * Stream
Definition: vtkXMLReader.h:328
vtkXMLReader::DataError
int DataError
Definition: vtkXMLReader.h:356
vtkDataArraySelection
Store on/off settings for data arrays for a vtkSource.
Definition: vtkDataArraySelection.h:34
vtkAlgorithm.h
vtkXMLReader::TimeDataStringArray
vtkStringArray * TimeDataStringArray
Definition: vtkXMLReader.h:341
vtkIossUtilities::CreateArray
vtkSmartPointer< vtkDataArray > CreateArray(const Ioss::Field &field)
Create an array for the given field.
vtkX3D::port
Definition: vtkX3D.h:453
vtkXMLReader::InReadData
int InReadData
Definition: vtkXMLReader.h:408
vtkXMLReader::InputString
std::string InputString
Definition: vtkXMLReader.h:335
vtkXMLReader::SqueezeOutputArrays
virtual void SqueezeOutputArrays(vtkDataObject *)
Definition: vtkXMLReader.h:316
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkXMLReader::ActiveTimeDataArrayName
char * ActiveTimeDataArrayName
Active index of array used for time.
Definition: vtkXMLReader.h:346
vtkXMLReader::CELL_DATA
Definition: vtkXMLReader.h:54
vtkXMLDataElement
Represents an XML element and those nested inside.
Definition: vtkXMLDataElement.h:36
vtkXMLReader::GetXMLParser
vtkXMLDataParser * GetXMLParser()
Returns the internal XML parser.
Definition: vtkXMLReader.h:180
vtkX3D::name
Definition: vtkX3D.h:225
vtkDataSet
abstract class to specify dataset behavior
Definition: vtkDataSet.h:56
vtkAbstractArray
Abstract superclass for all arrays.
Definition: vtkAbstractArray.h:75
vtkInformation
Store vtkAlgorithm input/output information.
Definition: vtkInformation.h:73
vtkX3D::info
Definition: vtkX3D.h:382
vtkX3D::string
Definition: vtkX3D.h:496
vtkXMLReader::CellDataArraySelection
vtkDataArraySelection * CellDataArraySelection
Definition: vtkXMLReader.h:339
vtkXMLReader::TimeStep
int TimeStep
Definition: vtkXMLReader.h:385
vtkXMLReader::FieldType
FieldType
Definition: vtkXMLReader.h:51
vtkAlgorithm::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkCallbackCommand
supports function callbacks
Definition: vtkCallbackCommand.h:44
vtkXMLReader::SetupOutputInformation
virtual void SetupOutputInformation(vtkInformation *vtkNotUsed(outInfo))
Definition: vtkXMLReader.h:223
vtkXMLReader::NumberOfTimeSteps
int NumberOfTimeSteps
Definition: vtkXMLReader.h:387
vtkStringArray
a vtkAbstractArray subclass for strings
Definition: vtkStringArray.h:36
vtkXMLReader::SelectionObserver
vtkCallbackCommand * SelectionObserver
Definition: vtkXMLReader.h:350
vtkX3D::progress
Definition: vtkX3D.h:458
vtkX3D::version
Definition: vtkX3D.h:532
vtkX3D::extent
Definition: vtkX3D.h:351
vtkDataObject
general representation of visualization data
Definition: vtkDataObject.h:59
vtkXMLReader::CopyOutputInformation
virtual void CopyOutputInformation(vtkInformation *vtkNotUsed(outInfo), int vtkNotUsed(port))
Definition: vtkXMLReader.h:157
vtkXMLReader::ReadError
int ReadError
Definition: vtkXMLReader.h:377
vtkX3D::index
Definition: vtkX3D.h:252
vtkAlgorithm::ProcessRequest
virtual vtkTypeBool ProcessRequest(vtkInformation *request, vtkInformationVector **inInfo, vtkInformationVector *outInfo)
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
vtkXMLReader
Superclass for VTK's XML format readers.
Definition: vtkXMLReader.h:45
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkXMLReader::ColumnArraySelection
vtkDataArraySelection * ColumnArraySelection
Definition: vtkXMLReader.h:340
vtkXMLDataParser
Used by vtkXMLReader to parse VTK XML files.
Definition: vtkXMLDataParser.h:42