VTK  9.3.20240328
vtkXMLReader.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
34 #ifndef vtkXMLReader_h
35 #define vtkXMLReader_h
36 
37 #include "vtkAlgorithm.h"
38 #include "vtkIOXMLModule.h" // For export macro
39 #include "vtkSmartPointer.h" // for vtkSmartPointer.
40 
41 #include <string> // for std::string
42 
43 VTK_ABI_NAMESPACE_BEGIN
44 class vtkAbstractArray;
45 class vtkCallbackCommand;
46 class vtkCharArray;
47 class vtkCommand;
48 class vtkDataArray;
50 class vtkDataSet;
52 class vtkXMLDataElement;
53 class vtkXMLDataParser;
55 class vtkInformation;
56 class vtkStringArray;
57 
58 class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
59 {
60 public:
61  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
62  void PrintSelf(ostream& os, vtkIndent indent) override;
63 
64  enum FieldType
65  {
68  OTHER
69  };
70 
72 
78 
80 
83  vtkSetMacro(ReadFromInputString, vtkTypeBool);
84  vtkGetMacro(ReadFromInputString, vtkTypeBool);
85  vtkBooleanMacro(ReadFromInputString, vtkTypeBool);
87 
93  void SetInputString(const char* in);
94  void SetInputString(const char* in, int len);
95  void SetBinaryInputString(const char*, int len);
96  void SetInputString(const std::string& input)
97  {
98  this->SetBinaryInputString(input.c_str(), static_cast<int>(input.length()));
99  }
101 
103 
111  virtual void SetInputArray(vtkCharArray*);
113 
121  virtual int CanReadFile(VTK_FILEPATH const char* name);
122 
124 
130 
132 
136  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
137  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
138  vtkGetObjectMacro(ColumnArraySelection, vtkDataArraySelection);
140 
142 
149 
151 
155  const char* GetTimeDataArray(int idx) const;
156  vtkGetObjectMacro(TimeDataStringArray, vtkStringArray);
158 
160 
166  vtkGetStringMacro(ActiveTimeDataArrayName);
167  vtkSetStringMacro(ActiveTimeDataArrayName);
169 
171 
175  const char* GetPointArrayName(int index);
176  const char* GetCellArrayName(int index);
177  const char* GetColumnArrayName(int index);
179 
181 
185  int GetPointArrayStatus(const char* name);
186  int GetCellArrayStatus(const char* name);
187  void SetPointArrayStatus(const char* name, int status);
188  void SetCellArrayStatus(const char* name, int status);
189  int GetColumnArrayStatus(const char* name);
190  void SetColumnArrayStatus(const char* name, int status);
192 
193  // For the specified port, copy the information this reader sets up in
194  // SetupOutputInformation to outInfo
195  virtual void CopyOutputInformation(vtkInformation* vtkNotUsed(outInfo), int vtkNotUsed(port)) {}
196 
198 
201  vtkSetMacro(TimeStep, int);
202  vtkGetMacro(TimeStep, int);
204 
205  vtkGetMacro(NumberOfTimeSteps, int);
207 
210  vtkGetVector2Macro(TimeStepRange, int);
211  vtkSetVector2Macro(TimeStepRange, int);
213 
218  vtkXMLDataParser* GetXMLParser() { return this->XMLParser; }
219 
221  vtkInformationVector* outputVector) override;
222 
224 
229  vtkGetObjectMacro(ReaderErrorObserver, vtkCommand);
231 
233 
238  vtkGetObjectMacro(ParserErrorObserver, vtkCommand);
240 
241 protected:
243  ~vtkXMLReader() override;
244 
246 
251  virtual int ReadXMLInformation();
252  virtual void ReadXMLData();
254 
258  virtual const char* GetDataSetName() = 0;
259 
263  virtual int CanReadFileVersion(int major, int minor);
264 
268  virtual void SetupEmptyOutput() = 0;
269 
273  virtual void SetupOutputInformation(vtkInformation* vtkNotUsed(outInfo)) {}
274 
278  virtual void SetupOutputData();
279 
284  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
285 
290  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
291 
297  int GetLocalDataType(vtkXMLDataElement* da, int datatype);
298 
304 
310 
316 
318 
321  virtual int OpenStream();
322  virtual void CloseStream();
323  virtual int OpenVTKFile();
324  virtual void CloseVTKFile();
325  virtual int OpenVTKString();
326  virtual void CloseVTKString();
327  virtual void CreateXMLParser();
328  virtual void DestroyXMLParser();
329  void SetupCompressor(const char* type);
332 
338  virtual int CanReadFileWithDataType(const char* dsname);
339 
343  vtkGetMacro(FileMajorVersion, int);
344 
348  vtkGetMacro(FileMinorVersion, int);
349 
351 
354  int IntersectExtents(int* extent1, int* extent2, int* result);
355  int Min(int a, int b);
356  int Max(int a, int b);
357  void ComputePointDimensions(int* extent, int* dimensions);
358  void ComputePointIncrements(int* extent, vtkIdType* increments);
359  void ComputeCellDimensions(int* extent, int* dimensions);
360  void ComputeCellIncrements(int* extent, vtkIdType* increments);
361  vtkIdType GetStartTuple(int* extent, vtkIdType* increments, int i, int j, int k);
363  char** CreateStringArray(int numStrings);
364  void DestroyStringArray(int numStrings, char** strings);
366 
373  virtual int ReadArrayValues(vtkXMLDataElement* da, vtkIdType arrayIndex, vtkAbstractArray* array,
374  vtkIdType startIndex, vtkIdType numValues, FieldType type = OTHER);
375 
384  virtual int ReadArrayTuples(vtkXMLDataElement* da, vtkIdType arrayTupleIndex,
385  vtkAbstractArray* array, vtkIdType startTupleIndex, vtkIdType numTuples,
386  FieldType type = OTHER);
387 
392 
393  int SetFieldDataInfo(vtkXMLDataElement* eDSA, int association, vtkIdType numTuples,
394  vtkInformationVector*(&infoVector));
395 
397 
403 
408  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
409 
415 
426 
427  // The vtkXMLDataParser instance used to hide XML reading details.
429 
430  // The FieldData element representation.
432 
433  // The input file's name.
434  char* FileName;
435 
436  // The stream used to read the input.
437  istream* Stream;
438 
439  // Whether this object is reading from a string or a file.
440  // Default is 0: read from file.
442 
443  // The input string.
445 
446  // The input array. Keeps a low memory footprint by sourcing StringStream from contents of this
447  // array
449 
450  // The array selections.
455 
461 
467 
468  // The observer to modify this object when the array selections are
469  // modified.
471 
472  // Whether there was an error reading the file in RequestInformation.
474 
475  // Whether there was an error reading the file in RequestData.
477 
478  // incrementally fine-tuned progress updates.
479  virtual void GetProgressRange(float* range);
480  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
481  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
482  virtual void UpdateProgressDiscrete(float progress);
483  float ProgressRange[2];
484 
485  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
486  vtkInformationVector* outputVector);
487  virtual int RequestDataObject(vtkInformation* vtkNotUsed(request),
488  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
489  {
490  return 1;
491  }
492  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
493  vtkInformationVector* outputVector);
495 
496  // Whether there was an error reading the XML.
498 
499  // For structured data keep track of dimensions empty of cells. For
500  // unstructured data these are always zero. This is used to support
501  // 1-D and 2-D cell data.
502  int AxesEmpty[3];
503 
504  // The timestep currently being read.
505  int TimeStep;
508  void SetNumberOfTimeSteps(int num);
509  // buffer for reading timestep from the XML file the length is of
510  // NumberOfTimeSteps and therefore is always long enough
511  int* TimeSteps;
512  // Store the range of time steps
513  int TimeStepRange[2];
514 
515  // Now we need to save what was the last time read for each kind of
516  // data to avoid rereading it that is to say we need a var for
517  // e.g. PointData/CellData/Points/Cells...
518  // See SubClass for details with member vars like PointsTimeStep/PointsOffset
519 
520  // Helper function useful to know if a timestep is found in an array of timestep
521  static int IsTimeStepInArray(int timestep, int* timesteps, int length);
522 
525 
526  // Flag for whether DataProgressCallback should actually update
527  // progress.
529 
531 
532  /*
533  * Populate the output's FieldData with the file's FieldData tags content
534  */
536 
537 private:
538  // The stream used to read the input if it is in a file.
539  istream* FileStream;
540  // The stream used to read the input if it is in a string.
541  std::istringstream* StringStream;
542  int TimeStepWasReadOnce;
543 
544  int FileMajorVersion;
545  int FileMinorVersion;
546 
547  vtkDataObject* CurrentOutput;
548  vtkInformation* CurrentOutputInformation;
549 
550  vtkXMLReader(const vtkXMLReader&) = delete;
551  void operator=(const vtkXMLReader&) = delete;
552 
553  vtkCommand* ReaderErrorObserver;
554  vtkCommand* ParserErrorObserver;
555 };
556 
557 VTK_ABI_NAMESPACE_END
558 #endif
Abstract superclass for all arrays.
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:161
supports function callbacks
dynamic, self-adjusting array of char
Definition: vtkCharArray.h:60
superclass for callback/observer methods
Definition: vtkCommand.h:384
Store on/off settings for data arrays, etc.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:154
general representation of visualization data
represent and manipulate attribute data in a dataset
abstract class to specify dataset behavior
Definition: vtkDataSet.h:165
a simple class to control print indentation
Definition: vtkIndent.h:108
Store zero or more vtkInformation instances.
Store vtkAlgorithm input/output information.
abstract base class for most VTK objects
Definition: vtkObject.h:161
a vtkAbstractArray subclass for strings
record modification and/or execution time
Definition: vtkTimeStamp.h:44
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:59
vtkCharArray * InputArray
Definition: vtkXMLReader.h:448
virtual int ReadArrayTuples(vtkXMLDataElement *da, vtkIdType arrayTupleIndex, vtkAbstractArray *array, vtkIdType startTupleIndex, vtkIdType numTuples, FieldType type=OTHER)
Read an Array values starting at the given tuple index and up to numTuples taking into account the nu...
vtkXMLDataElement * FieldDataElement
Definition: vtkXMLReader.h:431
vtkCallbackCommand * SelectionObserver
Definition: vtkXMLReader.h:470
virtual void SetProgressRange(const float range[2], int curStep, int numSteps)
virtual void ConvertGhostLevelsToGhostType(FieldType, vtkAbstractArray *, vtkIdType, vtkIdType)
Definition: vtkXMLReader.h:530
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)
Test if the reader can read a file with the given version number.
void SetInputString(const std::string &input)
Enable reading from an InputString instead of the default, a file.
Definition: vtkXMLReader.h:96
virtual void GetProgressRange(float *range)
virtual void DestroyXMLParser()
Internal utility methods.
int NumberOfTimeSteps
Definition: vtkXMLReader.h:507
int CellDataArrayIsEnabled(vtkXMLDataElement *eCDA)
Check whether the given array element is an enabled array.
istream * Stream
Definition: vtkXMLReader.h:437
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)
Read an Array values starting at the given index and up to numValues.
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:218
vtkDataArraySelection * CellDataArraySelection
Definition: vtkXMLReader.h:452
virtual void SetupOutputData()
Setup the output's data with allocation.
void ComputeCellIncrements(int *extent, vtkIdType *increments)
Utility methods for subclasses.
int CreateInformationKey(vtkXMLDataElement *eInfoKey, vtkInformation *info)
Create a vtkInformationKey from its corresponding XML representation.
void SetDataArraySelections(vtkXMLDataElement *eDSA, vtkDataArraySelection *sel)
Setup the data array selections for the input's set of arrays.
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
Setup the output with no data available.
int GetNumberOfTimeDataArrays() const
Getters for time data array candidates.
virtual void SqueezeOutputArrays(vtkDataObject *)
Give concrete classes an option to squeeze any output arrays at the end of RequestData.
Definition: vtkXMLReader.h:414
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)
Utility methods for subclasses.
virtual void CreateXMLParser()
Internal utility methods.
vtkSmartPointer< vtkDataArray > TimeDataArray
Populated in ReadXMLInformation from the field data for the array chosen using ActiveTimeDataArrayNam...
Definition: vtkXMLReader.h:466
vtkAbstractArray * CreateArray(vtkXMLDataElement *da)
Create a vtkAbstractArray from its corresponding XML representation.
virtual void CloseStream()
Internal utility methods.
int CanReadFileVersionString(const char *version)
Internal utility methods.
void ReadFieldData()
virtual void SetupOutputInformation(vtkInformation *vtkNotUsed(outInfo))
Setup the output's information.
Definition: vtkXMLReader.h:273
vtkIdType GetStartTuple(int *extent, vtkIdType *increments, int i, int j, int k)
Utility methods for subclasses.
virtual const char * GetDataSetName()=0
Get the name of the data set being read.
vtkTypeBool ReadFromInputString
Definition: vtkXMLReader.h:441
void SetupCompressor(const char *type)
Internal utility methods.
vtkDataArraySelection * ColumnArraySelection
Definition: vtkXMLReader.h:453
int InformationError
Definition: vtkXMLReader.h:473
vtkXMLDataParser * XMLParser
Definition: vtkXMLReader.h:428
virtual void SetInputArray(vtkCharArray *)
Specify the vtkCharArray to be used when reading from a string.
void ComputePointIncrements(int *extent, vtkIdType *increments)
Utility methods for subclasses.
virtual int ReadXMLInformation()
Pipeline execution methods to be defined by subclass.
int GetNumberOfColumnArrays()
Get the number of point, cell or column arrays available in the input.
void SetInputString(const char *in, int len)
Enable reading from an InputString instead of the default, a file.
void ReadAttributeIndices(vtkXMLDataElement *eDSA, vtkDataSetAttributes *dsa)
Utility methods for subclasses.
int PointDataArrayIsEnabled(vtkXMLDataElement *ePDA)
Check whether the given array element is an enabled array.
virtual void CloseVTKFile()
Internal utility methods.
virtual int OpenVTKFile()
Internal utility methods.
void SetInputString(const char *in)
Specify the InputString for use when reading from a character array.
vtkTimeStamp ReadMTime
Definition: vtkXMLReader.h:494
void ComputeCellDimensions(int *extent, int *dimensions)
Utility methods for subclasses.
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)
Callback registered with the SelectionObserver.
static int IsTimeStepInArray(int timestep, int *timesteps, int length)
char ** CreateStringArray(int numStrings)
Utility methods for subclasses.
virtual int RequestDataObject(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *vtkNotUsed(outputVector))
Definition: vtkXMLReader.h:487
void ComputePointDimensions(int *extent, int *dimensions)
Utility methods for subclasses.
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)
Utility methods for subclasses.
char * ActiveTimeDataArrayName
Name of the field-data array used to determine the time for the dataset being read.
Definition: vtkXMLReader.h:460
virtual int ReadVTKFile(vtkXMLDataElement *eVTKFile)
Read the top-level element from the file.
virtual int OpenVTKString()
Internal utility methods.
virtual int ReadPrimaryElement(vtkXMLDataElement *ePrimary)
Read the primary element from the file.
void SetBinaryInputString(const char *, int len)
Enable reading from an InputString instead of the default, a file.
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)
Populates the info object with the InformationKey children in infoRoot.
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:434
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()
Pipeline execution methods to be defined by subclass.
virtual int OpenStream()
Internal utility methods.
vtkStringArray * TimeDataStringArray
Definition: vtkXMLReader.h:454
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)
Utility methods for subclasses.
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()
Internal utility methods.
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:444
vtkDataArraySelection * PointDataArraySelection
Definition: vtkXMLReader.h:451
virtual void CopyOutputInformation(vtkInformation *vtkNotUsed(outInfo), int vtkNotUsed(port))
Definition: vtkXMLReader.h:195
int SetFieldDataInfo(vtkXMLDataElement *eDSA, int association, vtkIdType numTuples, vtkInformationVector *(&infoVector))
int Min(int a, int b)
Utility methods for subclasses.
@ info
Definition: vtkX3D.h:376
@ length
Definition: vtkX3D.h:393
@ port
Definition: vtkX3D.h:447
@ range
Definition: vtkX3D.h:238
@ extent
Definition: vtkX3D.h:345
@ type
Definition: vtkX3D.h:516
@ version
Definition: vtkX3D.h:526
@ name
Definition: vtkX3D.h:219
@ index
Definition: vtkX3D.h:246
@ progress
Definition: vtkX3D.h:452
@ string
Definition: vtkX3D.h:490
int vtkTypeBool
Definition: vtkABI.h:64
int vtkIdType
Definition: vtkType.h:315
#define VTK_FILEPATH