VTK  9.2.20230320
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 =========================================================================*/
46 #ifndef vtkXMLReader_h
47 #define vtkXMLReader_h
48 
49 #include "vtkAlgorithm.h"
50 #include "vtkIOXMLModule.h" // For export macro
51 #include "vtkSmartPointer.h" // for vtkSmartPointer.
52 
53 #include <string> // for std::string
54 
55 VTK_ABI_NAMESPACE_BEGIN
56 class vtkAbstractArray;
57 class vtkCallbackCommand;
58 class vtkCommand;
59 class vtkDataArray;
61 class vtkDataSet;
63 class vtkXMLDataElement;
64 class vtkXMLDataParser;
66 class vtkInformation;
67 class vtkStringArray;
68 
69 class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
70 {
71 public:
72  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
73  void PrintSelf(ostream& os, vtkIndent indent) override;
74 
75  enum FieldType
76  {
79  OTHER
80  };
81 
83 
89 
91 
94  vtkSetMacro(ReadFromInputString, vtkTypeBool);
95  vtkGetMacro(ReadFromInputString, vtkTypeBool);
96  vtkBooleanMacro(ReadFromInputString, vtkTypeBool);
97  void SetInputString(const std::string& s) { this->InputString = s; }
99 
107  virtual int CanReadFile(VTK_FILEPATH const char* name);
108 
110 
116 
118 
122  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
123  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
124  vtkGetObjectMacro(ColumnArraySelection, vtkDataArraySelection);
126 
128 
135 
137 
141  const char* GetTimeDataArray(int idx) const;
142  vtkGetObjectMacro(TimeDataStringArray, vtkStringArray);
144 
146 
152  vtkGetStringMacro(ActiveTimeDataArrayName);
153  vtkSetStringMacro(ActiveTimeDataArrayName);
155 
157 
161  const char* GetPointArrayName(int index);
162  const char* GetCellArrayName(int index);
163  const char* GetColumnArrayName(int index);
165 
167 
171  int GetPointArrayStatus(const char* name);
172  int GetCellArrayStatus(const char* name);
173  void SetPointArrayStatus(const char* name, int status);
174  void SetCellArrayStatus(const char* name, int status);
175  int GetColumnArrayStatus(const char* name);
176  void SetColumnArrayStatus(const char* name, int status);
178 
179  // For the specified port, copy the information this reader sets up in
180  // SetupOutputInformation to outInfo
181  virtual void CopyOutputInformation(vtkInformation* vtkNotUsed(outInfo), int vtkNotUsed(port)) {}
182 
184 
187  vtkSetMacro(TimeStep, int);
188  vtkGetMacro(TimeStep, int);
190 
191  vtkGetMacro(NumberOfTimeSteps, int);
193 
196  vtkGetVector2Macro(TimeStepRange, int);
197  vtkSetVector2Macro(TimeStepRange, int);
199 
204  vtkXMLDataParser* GetXMLParser() { return this->XMLParser; }
205 
207  vtkInformationVector* outputVector) override;
208 
210 
215  vtkGetObjectMacro(ReaderErrorObserver, vtkCommand);
217 
219 
224  vtkGetObjectMacro(ParserErrorObserver, vtkCommand);
226 
227 protected:
229  ~vtkXMLReader() override;
230 
231  // Pipeline execution methods to be defined by subclass. Called by
232  // corresponding RequestData methods after appropriate setup has been
233  // done.
234  virtual int ReadXMLInformation();
235  virtual void ReadXMLData();
236 
237  // Get the name of the data set being read.
238  virtual const char* GetDataSetName() = 0;
239 
240  // Test if the reader can read a file with the given version number.
241  virtual int CanReadFileVersion(int major, int minor);
242 
243  // Setup the output with no data available. Used in error cases.
244  virtual void SetupEmptyOutput() = 0;
245 
246  // Setup the output's information.
247  virtual void SetupOutputInformation(vtkInformation* vtkNotUsed(outInfo)) {}
248 
249  // Setup the output's data with allocation.
250  virtual void SetupOutputData();
251 
252  // Read the primary element from the file. This is the element
253  // whose name is the value returned by GetDataSetName().
254  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
255 
256  // Read the top-level element from the file. This is always the
257  // VTKFile element.
258  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
259 
265  int GetLocalDataType(vtkXMLDataElement* da, int datatype);
266 
267  // Create a vtkAbstractArray from its corresponding XML representation.
268  // Does not allocate.
270 
271  // Create a vtkInformationKey from its corresponding XML representation.
272  // Stores it in the instance of vtkInformationProvided. Does not allocate.
274 
275  // Populates the info object with the InformationKey children in infoRoot.
276  // Returns false if errors occur.
278 
279  // Internal utility methods.
280  virtual int OpenStream();
281  virtual void CloseStream();
282  virtual int OpenVTKFile();
283  virtual void CloseVTKFile();
284  virtual int OpenVTKString();
285  virtual void CloseVTKString();
286  virtual void CreateXMLParser();
287  virtual void DestroyXMLParser();
288  void SetupCompressor(const char* type);
290 
296  virtual int CanReadFileWithDataType(const char* dsname);
297 
298  // Returns the major version for the file being read. -1 when invalid.
299  vtkGetMacro(FileMajorVersion, int);
300 
301  // Returns the minor version for the file being read. -1 when invalid.
302  vtkGetMacro(FileMinorVersion, int);
303 
304  // Utility methods for subclasses.
305  int IntersectExtents(int* extent1, int* extent2, int* result);
306  int Min(int a, int b);
307  int Max(int a, int b);
308  void ComputePointDimensions(int* extent, int* dimensions);
309  void ComputePointIncrements(int* extent, vtkIdType* increments);
310  void ComputeCellDimensions(int* extent, int* dimensions);
311  void ComputeCellIncrements(int* extent, vtkIdType* increments);
312  vtkIdType GetStartTuple(int* extent, vtkIdType* increments, int i, int j, int k);
314  char** CreateStringArray(int numStrings);
315  void DestroyStringArray(int numStrings, char** strings);
316 
317  // Read an Array values starting at the given index and up to numValues.
318  // This method assumes that the array is of correct size to
319  // accommodate all numValues values. arrayIndex is the value index at which the read
320  // values will be put in the array.
321  virtual int ReadArrayValues(vtkXMLDataElement* da, vtkIdType arrayIndex, vtkAbstractArray* array,
322  vtkIdType startIndex, vtkIdType numValues, FieldType type = OTHER);
323 
324  // Setup the data array selections for the input's set of arrays.
326 
327  int SetFieldDataInfo(vtkXMLDataElement* eDSA, int association, vtkIdType numTuples,
328  vtkInformationVector*(&infoVector));
329 
330  // Check whether the given array element is an enabled array.
333 
334  // Callback registered with the SelectionObserver.
336  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
337 
338  // Give concrete classes an option to squeeze any output arrays
339  // at the end of RequestData.
341 
352 
353  // The vtkXMLDataParser instance used to hide XML reading details.
355 
356  // The FieldData element representation.
358 
359  // The input file's name.
360  char* FileName;
361 
362  // The stream used to read the input.
363  istream* Stream;
364 
365  // Whether this object is reading from a string or a file.
366  // Default is 0: read from file.
368 
369  // The input string.
371 
372  // The array selections.
377 
383 
389 
390  // The observer to modify this object when the array selections are
391  // modified.
393 
394  // Whether there was an error reading the file in RequestInformation.
396 
397  // Whether there was an error reading the file in RequestData.
399 
400  // incrementally fine-tuned progress updates.
401  virtual void GetProgressRange(float* range);
402  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
403  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
404  virtual void UpdateProgressDiscrete(float progress);
405  float ProgressRange[2];
406 
407  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
408  vtkInformationVector* outputVector);
409  virtual int RequestDataObject(vtkInformation* vtkNotUsed(request),
410  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
411  {
412  return 1;
413  }
414  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
415  vtkInformationVector* outputVector);
417 
418  // Whether there was an error reading the XML.
420 
421  // For structured data keep track of dimensions empty of cells. For
422  // unstructured data these are always zero. This is used to support
423  // 1-D and 2-D cell data.
424  int AxesEmpty[3];
425 
426  // The timestep currently being read.
427  int TimeStep;
430  void SetNumberOfTimeSteps(int num);
431  // buffer for reading timestep from the XML file the length is of
432  // NumberOfTimeSteps and therefore is always long enough
433  int* TimeSteps;
434  // Store the range of time steps
435  int TimeStepRange[2];
436 
437  // Now we need to save what was the last time read for each kind of
438  // data to avoid rereading it that is to say we need a var for
439  // e.g. PointData/CellData/Points/Cells...
440  // See SubClass for details with member vars like PointsTimeStep/PointsOffset
441 
442  // Helper function useful to know if a timestep is found in an array of timestep
443  static int IsTimeStepInArray(int timestep, int* timesteps, int length);
444 
447 
448  // Flag for whether DataProgressCallback should actually update
449  // progress.
451 
453 
455 
456 private:
457  // The stream used to read the input if it is in a file.
458  istream* FileStream;
459  // The stream used to read the input if it is in a string.
460  std::istringstream* StringStream;
461  int TimeStepWasReadOnce;
462 
463  int FileMajorVersion;
464  int FileMinorVersion;
465 
466  vtkDataObject* CurrentOutput;
467  vtkInformation* CurrentOutputInformation;
468 
469 private:
470  vtkXMLReader(const vtkXMLReader&) = delete;
471  void operator=(const vtkXMLReader&) = delete;
472 
473  vtkCommand* ReaderErrorObserver;
474  vtkCommand* ParserErrorObserver;
475 };
476 
477 VTK_ABI_NAMESPACE_END
478 #endif
Abstract superclass for all arrays.
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:123
supports function callbacks
superclass for callback/observer methods
Definition: vtkCommand.h:396
Store on/off settings for data arrays, etc.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:166
general representation of visualization data
represent and manipulate attribute data in a dataset
abstract class to specify dataset behavior
Definition: vtkDataSet.h:174
a simple class to control print indentation
Definition: vtkIndent.h:120
Store zero or more vtkInformation instances.
Store vtkAlgorithm input/output information.
abstract base class for most VTK objects
Definition: vtkObject.h:83
a vtkAbstractArray subclass for strings
record modification and/or execution time
Definition: vtkTimeStamp.h:56
Represents an XML element and those nested inside.
Used by vtkXMLReader to parse VTK XML files.
Superclass for VTK's XML format readers.
Definition: vtkXMLReader.h:70
vtkXMLDataElement * FieldDataElement
Definition: vtkXMLReader.h:357
vtkCallbackCommand * SelectionObserver
Definition: vtkXMLReader.h:392
virtual void SetProgressRange(const float range[2], int curStep, int numSteps)
virtual void ConvertGhostLevelsToGhostType(FieldType, vtkAbstractArray *, vtkIdType, vtkIdType)
Definition: vtkXMLReader.h:452
int GetColumnArrayStatus(const char *name)
Get/Set whether the point, cell, column or time array with the given name is to be read.
virtual int CanReadFileVersion(int major, int minor)
virtual void GetProgressRange(float *range)
virtual void DestroyXMLParser()
int NumberOfTimeSteps
Definition: vtkXMLReader.h:429
int CellDataArrayIsEnabled(vtkXMLDataElement *eCDA)
istream * Stream
Definition: vtkXMLReader.h:363
void SetColumnArrayStatus(const char *name, int status)
Get/Set whether the point, cell, column or time array with the given name is to be read.
virtual int ReadArrayValues(vtkXMLDataElement *da, vtkIdType arrayIndex, vtkAbstractArray *array, vtkIdType startIndex, vtkIdType numValues, FieldType type=OTHER)
vtkDataSet * GetOutputAsDataSet(int index)
Get the output as a vtkDataSet pointer.
void SetReaderErrorObserver(vtkCommand *)
Set/get the ErrorObserver for the internal reader This is useful for applications that want to catch ...
vtkXMLDataParser * GetXMLParser()
Returns the internal XML parser.
Definition: vtkXMLReader.h:204
vtkDataArraySelection * CellDataArraySelection
Definition: vtkXMLReader.h:374
virtual void SetupOutputData()
void ComputeCellIncrements(int *extent, vtkIdType *increments)
int CreateInformationKey(vtkXMLDataElement *eInfoKey, vtkInformation *info)
void SetDataArraySelections(vtkXMLDataElement *eDSA, vtkDataArraySelection *sel)
void SetParserErrorObserver(vtkCommand *)
Set/get the ErrorObserver for the internal xml parser This is useful for applications that want to ca...
virtual void SetupEmptyOutput()=0
int GetNumberOfTimeDataArrays() const
Getters for time data array candidates.
virtual void SqueezeOutputArrays(vtkDataObject *)
Definition: vtkXMLReader.h:340
vtkDataSet * GetOutputAsDataSet()
Get the output as a vtkDataSet pointer.
int GetNumberOfPointArrays()
Get the number of point, cell or column arrays available in the input.
int Max(int a, int b)
virtual void CreateXMLParser()
vtkSmartPointer< vtkDataArray > TimeDataArray
Populated in ReadXMLInformation from the field data for the array chosen using ActiveTimeDataArrayNam...
Definition: vtkXMLReader.h:388
vtkAbstractArray * CreateArray(vtkXMLDataElement *da)
virtual void CloseStream()
int CanReadFileVersionString(const char *version)
void ReadFieldData()
virtual void SetupOutputInformation(vtkInformation *vtkNotUsed(outInfo))
Definition: vtkXMLReader.h:247
vtkIdType GetStartTuple(int *extent, vtkIdType *increments, int i, int j, int k)
virtual const char * GetDataSetName()=0
vtkTypeBool ReadFromInputString
Definition: vtkXMLReader.h:367
void SetupCompressor(const char *type)
vtkDataArraySelection * ColumnArraySelection
Definition: vtkXMLReader.h:375
int InformationError
Definition: vtkXMLReader.h:395
vtkXMLDataParser * XMLParser
Definition: vtkXMLReader.h:354
void SetInputString(const std::string &s)
Enable reading from an InputString instead of the default, a file.
Definition: vtkXMLReader.h:97
void ComputePointIncrements(int *extent, vtkIdType *increments)
virtual int ReadXMLInformation()
int GetNumberOfColumnArrays()
Get the number of point, cell or column arrays available in the input.
void ReadAttributeIndices(vtkXMLDataElement *eDSA, vtkDataSetAttributes *dsa)
int PointDataArrayIsEnabled(vtkXMLDataElement *ePDA)
virtual void CloseVTKFile()
virtual int OpenVTKFile()
vtkTimeStamp ReadMTime
Definition: vtkXMLReader.h:416
void ComputeCellDimensions(int *extent, int *dimensions)
virtual int CanReadFile(VTK_FILEPATH const char *name)
Test whether the file (type) with the given name can be read by this reader.
void SetNumberOfTimeSteps(int num)
virtual int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
static void SelectionModifiedCallback(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata)
static int IsTimeStepInArray(int timestep, int *timesteps, int length)
char ** CreateStringArray(int numStrings)
virtual int RequestDataObject(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *vtkNotUsed(outputVector))
Definition: vtkXMLReader.h:409
void ComputePointDimensions(int *extent, int *dimensions)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int GetNumberOfCellArrays()
Get the number of point, cell or column arrays available in the input.
void MarkIdTypeArrays(vtkXMLDataElement *da)
XML files have not consistently saved out adequate meta-data in past to correctly create vtkIdTypeArr...
~vtkXMLReader() override
virtual int CanReadFileWithDataType(const char *dsname)
This method is used by CanReadFile() to check if the reader can read an XML with the primary element ...
const char * GetCellArrayName(int index)
Get the name of the point, cell, column or time array with the given index in the input.
int IntersectExtents(int *extent1, int *extent2, int *result)
char * ActiveTimeDataArrayName
Name of the field-data array used to determine the time for the dataset being read.
Definition: vtkXMLReader.h:382
virtual int ReadVTKFile(vtkXMLDataElement *eVTKFile)
virtual int OpenVTKString()
virtual int ReadPrimaryElement(vtkXMLDataElement *ePrimary)
vtkGetFilePathMacro(FileName)
Get/Set the name of the input file.
const char * GetPointArrayName(int index)
Get the name of the point, cell, column or time array with the given index in the input.
const char * GetTimeDataArray(int idx) const
Getters for time data array candidates.
int GetLocalDataType(vtkXMLDataElement *da, int datatype)
If the IdType argument is present in the provided XMLDataElement and the provided dataType has the sa...
bool ReadInformation(vtkXMLDataElement *infoRoot, vtkInformation *info)
vtkInformation * GetCurrentOutputInformation()
vtkTypeBool ProcessRequest(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
int GetCellArrayStatus(const char *name)
Get/Set whether the point, cell, column or time array with the given name is to be read.
vtkSetFilePathMacro(FileName)
Get/Set the name of the input file.
int GetPointArrayStatus(const char *name)
Get/Set whether the point, cell, column or time array with the given name is to be read.
char * FileName
Definition: vtkXMLReader.h:360
const char * GetColumnArrayName(int index)
Get the name of the point, cell, column or time array with the given index in the input.
virtual void UpdateProgressDiscrete(float progress)
vtkDataObject * GetCurrentOutput()
virtual void ReadXMLData()
virtual int OpenStream()
vtkStringArray * TimeDataStringArray
Definition: vtkXMLReader.h:376
virtual int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
virtual void SetProgressRange(const float range[2], int curStep, const float *fractions)
void DestroyStringArray(int numStrings, char **strings)
void SetCellArrayStatus(const char *name, int status)
Get/Set whether the point, cell, column or time array with the given name is to be read.
virtual void CloseVTKString()
void SetPointArrayStatus(const char *name, int status)
Get/Set whether the point, cell, column or time array with the given name is to be read.
std::string InputString
Definition: vtkXMLReader.h:370
vtkDataArraySelection * PointDataArraySelection
Definition: vtkXMLReader.h:373
virtual void CopyOutputInformation(vtkInformation *vtkNotUsed(outInfo), int vtkNotUsed(port))
Definition: vtkXMLReader.h:181
int SetFieldDataInfo(vtkXMLDataElement *eDSA, int association, vtkIdType numTuples, vtkInformationVector *(&infoVector))
int Min(int a, int b)
@ info
Definition: vtkX3D.h:388
@ length
Definition: vtkX3D.h:405
@ port
Definition: vtkX3D.h:459
@ range
Definition: vtkX3D.h:250
@ extent
Definition: vtkX3D.h:357
@ type
Definition: vtkX3D.h:528
@ version
Definition: vtkX3D.h:538
@ name
Definition: vtkX3D.h:231
@ index
Definition: vtkX3D.h:258
@ progress
Definition: vtkX3D.h:464
@ string
Definition: vtkX3D.h:502
int vtkTypeBool
Definition: vtkABI.h:71
int vtkIdType
Definition: vtkType.h:327
#define VTK_FILEPATH