VTK  9.0.20210227
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 #include "vtkSmartPointer.h" // for vtkSmartPointer.
30 
31 #include <string> // for std::string
32 #include <vector>
33 
34 class vtkAbstractArray;
35 class vtkCallbackCommand;
36 class vtkCommand;
37 class vtkDataArray;
39 class vtkDataSet;
41 class vtkXMLDataElement;
42 class vtkXMLDataParser;
44 class vtkInformation;
45 class vtkStringArray;
46 
47 class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
48 {
49 public:
50  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
51  void PrintSelf(ostream& os, vtkIndent indent) override;
52 
53  enum FieldType
54  {
57  OTHER
58  };
59 
61 
64  vtkSetStringMacro(FileName);
65  vtkGetStringMacro(FileName);
67 
69 
72  vtkSetMacro(ReadFromInputString, vtkTypeBool);
73  vtkGetMacro(ReadFromInputString, vtkTypeBool);
74  vtkBooleanMacro(ReadFromInputString, vtkTypeBool);
75  void SetInputString(const std::string& s) { this->InputString = s; }
77 
85  virtual int CanReadFile(const char* name);
86 
88 
91  vtkDataSet* GetOutputAsDataSet();
92  vtkDataSet* GetOutputAsDataSet(int index);
94 
96 
100  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
101  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
102  vtkGetObjectMacro(ColumnArraySelection, vtkDataArraySelection);
104 
106 
109  int GetNumberOfPointArrays();
110  int GetNumberOfCellArrays();
111  int GetNumberOfColumnArrays();
113 
115 
118  int GetNumberOfTimeDataArrays() const;
119  const char* GetTimeDataArray(int idx) const;
120  vtkGetObjectMacro(TimeDataStringArray, vtkStringArray);
122 
124 
130  vtkGetStringMacro(ActiveTimeDataArrayName);
131  vtkSetStringMacro(ActiveTimeDataArrayName);
133 
135 
139  const char* GetPointArrayName(int index);
140  const char* GetCellArrayName(int index);
141  const char* GetColumnArrayName(int index);
143 
145 
149  int GetPointArrayStatus(const char* name);
150  int GetCellArrayStatus(const char* name);
151  void SetPointArrayStatus(const char* name, int status);
152  void SetCellArrayStatus(const char* name, int status);
153  int GetColumnArrayStatus(const char* name);
154  void SetColumnArrayStatus(const char* name, int status);
156 
157  // For the specified port, copy the information this reader sets up in
158  // SetupOutputInformation to outInfo
159  virtual void CopyOutputInformation(vtkInformation* vtkNotUsed(outInfo), int vtkNotUsed(port)) {}
160 
162 
165  vtkSetMacro(TimeStep, int);
166  vtkGetMacro(TimeStep, int);
168 
169  vtkGetMacro(NumberOfTimeSteps, int);
171 
174  vtkGetVector2Macro(TimeStepRange, int);
175  vtkSetVector2Macro(TimeStepRange, int);
177 
182  vtkXMLDataParser* GetXMLParser() { return this->XMLParser; }
183 
185  vtkInformationVector* outputVector) override;
186 
188 
192  void SetReaderErrorObserver(vtkCommand*);
193  vtkGetObjectMacro(ReaderErrorObserver, vtkCommand);
195 
197 
201  void SetParserErrorObserver(vtkCommand*);
202  vtkGetObjectMacro(ParserErrorObserver, vtkCommand);
204 
205 protected:
206  vtkXMLReader();
207  ~vtkXMLReader() override;
208 
209  // Pipeline execution methods to be defined by subclass. Called by
210  // corresponding RequestData methods after appropriate setup has been
211  // done.
212  virtual int ReadXMLInformation();
213  virtual void ReadXMLData();
214 
215  // Get the name of the data set being read.
216  virtual const char* GetDataSetName() = 0;
217 
218  // Test if the reader can read a file with the given version number.
219  virtual int CanReadFileVersion(int major, int minor);
220 
221  // Setup the output with no data available. Used in error cases.
222  virtual void SetupEmptyOutput() = 0;
223 
224  // Setup the output's information.
225  virtual void SetupOutputInformation(vtkInformation* vtkNotUsed(outInfo)) {}
226 
227  // Setup the output's data with allocation.
228  virtual void SetupOutputData();
229 
230  // Read the primary element from the file. This is the element
231  // whose name is the value returned by GetDataSetName().
232  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
233 
234  // Read the top-level element from the file. This is always the
235  // VTKFile element.
236  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
237 
243  int GetLocalDataType(vtkXMLDataElement* da, int datatype);
244 
245  // Create a vtkAbstractArray from its cooresponding XML representation.
246  // Does not allocate.
248 
249  // Create a vtkInformationKey from its corresponding XML representation.
250  // Stores it in the instance of vtkInformationProvided. Does not allocate.
251  int CreateInformationKey(vtkXMLDataElement* eInfoKey, vtkInformation* info);
252 
253  // Populates the info object with the InformationKey children in infoRoot.
254  // Returns false if errors occur.
255  bool ReadInformation(vtkXMLDataElement* infoRoot, vtkInformation* info);
256 
257  // Internal utility methods.
258  virtual int OpenStream();
259  virtual void CloseStream();
260  virtual int OpenVTKFile();
261  virtual void CloseVTKFile();
262  virtual int OpenVTKString();
263  virtual void CloseVTKString();
264  virtual void CreateXMLParser();
265  virtual void DestroyXMLParser();
266  void SetupCompressor(const char* type);
267  int CanReadFileVersionString(const char* version);
268 
274  virtual int CanReadFileWithDataType(const char* dsname);
275 
276  // Returns the major version for the file being read. -1 when invalid.
277  vtkGetMacro(FileMajorVersion, int);
278 
279  // Returns the minor version for the file being read. -1 when invalid.
280  vtkGetMacro(FileMinorVersion, int);
281 
282  // Utility methods for subclasses.
283  int IntersectExtents(int* extent1, int* extent2, int* result);
284  int Min(int a, int b);
285  int Max(int a, int b);
286  void ComputePointDimensions(int* extent, int* dimensions);
287  void ComputePointIncrements(int* extent, vtkIdType* increments);
288  void ComputeCellDimensions(int* extent, int* dimensions);
289  void ComputeCellIncrements(int* extent, vtkIdType* increments);
290  vtkIdType GetStartTuple(int* extent, vtkIdType* increments, int i, int j, int k);
291  void ReadAttributeIndices(vtkXMLDataElement* eDSA, vtkDataSetAttributes* dsa);
292  char** CreateStringArray(int numStrings);
293  void DestroyStringArray(int numStrings, char** strings);
294 
295  // Read an Array values starting at the given index and up to numValues.
296  // This method assumes that the array is of correct size to
297  // accommodate all numValues values. arrayIndex is the value index at which the read
298  // values will be put in the array.
299  virtual int ReadArrayValues(vtkXMLDataElement* da, vtkIdType arrayIndex, vtkAbstractArray* array,
300  vtkIdType startIndex, vtkIdType numValues, FieldType type = OTHER);
301 
302  // Setup the data array selections for the input's set of arrays.
303  void SetDataArraySelections(vtkXMLDataElement* eDSA, vtkDataArraySelection* sel);
304 
305  int SetFieldDataInfo(vtkXMLDataElement* eDSA, int association, vtkIdType numTuples,
306  vtkInformationVector*(&infoVector));
307 
308  // Check whether the given array element is an enabled array.
309  int PointDataArrayIsEnabled(vtkXMLDataElement* ePDA);
310  int CellDataArrayIsEnabled(vtkXMLDataElement* eCDA);
311 
312  // Callback registered with the SelectionObserver.
313  static void SelectionModifiedCallback(
314  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
315 
316  // Give concrete classes an option to squeeze any output arrays
317  // at the end of RequestData.
319 
329  void MarkIdTypeArrays(vtkXMLDataElement* da);
330 
331  // The vtkXMLDataParser instance used to hide XML reading details.
333 
334  // The FieldData element representation.
336 
337  // The input file's name.
338  char* FileName;
339 
340  // The stream used to read the input.
341  istream* Stream;
342 
343  // Whether this object is reading from a string or a file.
344  // Default is 0: read from file.
346 
347  // The input string.
349 
350  // The array selections.
355 
361 
367 
368  // The observer to modify this object when the array selections are
369  // modified.
371 
372  // Whether there was an error reading the file in RequestInformation.
374 
375  // Whether there was an error reading the file in RequestData.
377 
378  // incrementally fine-tuned progress updates.
379  virtual void GetProgressRange(float* range);
380  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
381  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
382  virtual void UpdateProgressDiscrete(float progress);
383  float ProgressRange[2];
384 
385  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
386  vtkInformationVector* outputVector);
387  virtual int RequestDataObject(vtkInformation* vtkNotUsed(request),
388  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
389  {
390  return 1;
391  }
392  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
393  vtkInformationVector* outputVector);
395 
396  // Whether there was an error reading the XML.
398 
399  // For structured data keep track of dimensions empty of cells. For
400  // unstructured data these are always zero. This is used to support
401  // 1-D and 2-D cell data.
402  int AxesEmpty[3];
403 
404  // The timestep currently being read.
405  int TimeStep;
408  void SetNumberOfTimeSteps(int num);
409  // buffer for reading timestep from the XML file the length is of
410  // NumberOfTimeSteps and therefore is always long enough
411  int* TimeSteps;
412  // Store the range of time steps
413  int TimeStepRange[2];
414 
415  // Now we need to save what was the last time read for each kind of
416  // data to avoid rereading it that is to say we need a var for
417  // e.g. PointData/CellData/Points/Cells...
418  // See SubClass for details with member vars like PointsTimeStep/PointsOffset
419 
420  // Helper function useful to know if a timestep is found in an array of timestep
421  static int IsTimeStepInArray(int timestep, int* timesteps, int length);
422 
423  vtkDataObject* GetCurrentOutput();
424  vtkInformation* GetCurrentOutputInformation();
425 
426  // Flag for whether DataProgressCallback should actually update
427  // progress.
429 
431 
432  void ReadFieldData();
433 
434 private:
435  // The stream used to read the input if it is in a file.
436  istream* FileStream;
437  // The stream used to read the input if it is in a string.
438  std::istringstream* StringStream;
439  int TimeStepWasReadOnce;
440 
441  int FileMajorVersion;
442  int FileMinorVersion;
443 
444  vtkDataObject* CurrentOutput;
445  vtkInformation* CurrentOutputInformation;
446 
447 private:
448  vtkXMLReader(const vtkXMLReader&) = delete;
449  void operator=(const vtkXMLReader&) = delete;
450 
451  vtkCommand* ReaderErrorObserver;
452  vtkCommand* ParserErrorObserver;
453 };
454 
455 #endif
vtkXMLReader::ReadMTime
vtkTimeStamp ReadMTime
Definition: vtkXMLReader.h:394
vtkCommand
superclass for callback/observer methods
Definition: vtkCommand.h:393
vtkXMLReader::POINT_DATA
Definition: vtkXMLReader.h:55
vtkXMLReader::CurrentTimeStep
int CurrentTimeStep
Definition: vtkXMLReader.h:406
vtkXMLReader::FieldDataElement
vtkXMLDataElement * FieldDataElement
Definition: vtkXMLReader.h:335
vtkXMLReader::PointDataArraySelection
vtkDataArraySelection * PointDataArraySelection
Definition: vtkXMLReader.h:351
vtkXMLReader::TimeSteps
int * TimeSteps
Definition: vtkXMLReader.h:411
vtkXMLReader::ReadFromInputString
vtkTypeBool ReadFromInputString
Definition: vtkXMLReader.h:345
vtkXMLReader::SetInputString
void SetInputString(const std::string &s)
Definition: vtkXMLReader.h:75
vtkX3D::type
Definition: vtkX3D.h:522
vtkIdType
int vtkIdType
Definition: vtkType.h:338
vtkXMLReader::XMLParser
vtkXMLDataParser * XMLParser
Definition: vtkXMLReader.h:332
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:60
vtkXMLReader::ConvertGhostLevelsToGhostType
virtual void ConvertGhostLevelsToGhostType(FieldType, vtkAbstractArray *, vtkIdType, vtkIdType)
Definition: vtkXMLReader.h:430
vtkXMLReader::InformationError
int InformationError
Definition: vtkXMLReader.h:373
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:338
vtkX3D::range
Definition: vtkX3D.h:244
vtkSmartPointer< vtkDataArray >
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:387
vtkX3D::length
Definition: vtkX3D.h:399
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
vtkXMLReader::Stream
istream * Stream
Definition: vtkXMLReader.h:341
vtkXMLReader::DataError
int DataError
Definition: vtkXMLReader.h:376
vtkDataArraySelection
Store on/off settings for data arrays for a vtkSource.
Definition: vtkDataArraySelection.h:34
vtkAlgorithm.h
vtkXMLReader::TimeDataStringArray
vtkStringArray * TimeDataStringArray
Definition: vtkXMLReader.h:354
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:428
vtkXMLReader::InputString
std::string InputString
Definition: vtkXMLReader.h:348
vtkXMLReader::SqueezeOutputArrays
virtual void SqueezeOutputArrays(vtkDataObject *)
Definition: vtkXMLReader.h:318
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkXMLReader::ActiveTimeDataArrayName
char * ActiveTimeDataArrayName
Name of the field-data array used to determine the time for the dataset being read.
Definition: vtkXMLReader.h:360
vtkSmartPointer.h
vtkXMLReader::CELL_DATA
Definition: vtkXMLReader.h:56
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:182
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:352
vtkXMLReader::TimeStep
int TimeStep
Definition: vtkXMLReader.h:405
vtkXMLReader::FieldType
FieldType
Definition: vtkXMLReader.h:53
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:225
vtkXMLReader::NumberOfTimeSteps
int NumberOfTimeSteps
Definition: vtkXMLReader.h:407
vtkStringArray
a vtkAbstractArray subclass for strings
Definition: vtkStringArray.h:36
vtkXMLReader::SelectionObserver
vtkCallbackCommand * SelectionObserver
Definition: vtkXMLReader.h:370
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:159
vtkXMLReader::ReadError
int ReadError
Definition: vtkXMLReader.h:397
vtkX3D::index
Definition: vtkX3D.h:252
vtkXMLReader::TimeDataArray
vtkSmartPointer< vtkDataArray > TimeDataArray
Populated in ReadXMLInformation from the field data for the array chosen using ActiveTimeDataArrayNam...
Definition: vtkXMLReader.h:366
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:47
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkXMLReader::ColumnArraySelection
vtkDataArraySelection * ColumnArraySelection
Definition: vtkXMLReader.h:353
vtkXMLDataParser
Used by vtkXMLReader to parse VTK XML files.
Definition: vtkXMLDataParser.h:42