VTK  9.7.1
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
16 #ifndef vtkXMLReader_h
17 #define vtkXMLReader_h
18 
19 #include "vtkAlgorithm.h"
20 #include "vtkIOXMLModule.h" // For export macro
21 #include "vtkSmartPointer.h" // for vtkSmartPointer.
22 
23 #include <istream> // for IStream
24 #include <memory> // for std::unique_ptr
25 #include <streambuf> // for Streambuf
26 #include <string> // for std::string
27 
28 VTK_ABI_NAMESPACE_BEGIN
29 class vtkAbstractArray;
30 class vtkCallbackCommand;
31 class vtkCharArray;
32 class vtkCommand;
33 class vtkDataArray;
35 class vtkDataSet;
37 class vtkInformation;
39 class vtkResourceStream;
40 class vtkStringArray;
41 class vtkXMLDataElement;
42 class vtkXMLDataParser;
43 
44 class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
45 {
46 public:
47  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
48  void PrintSelf(ostream& os, vtkIndent indent) override;
49 
50  enum FieldType
51  {
54  OTHER
55  };
56 
58 
61  vtkSetFilePathMacro(FileName);
62  vtkGetFilePathMacro(FileName);
64 
66 
70  vtkSetMacro(ReadFromInputString, vtkTypeBool);
71  vtkGetMacro(ReadFromInputString, vtkTypeBool);
72  vtkBooleanMacro(ReadFromInputString, vtkTypeBool);
74 
81  void SetInputString(const char* in);
82  void SetInputString(const char* in, int len);
83  void SetBinaryInputString(const char*, int len);
84  void SetInputString(const std::string& input)
85  {
86  this->SetBinaryInputString(input.c_str(), static_cast<int>(input.length()));
87  }
89 
91 
99  virtual void SetInputArray(vtkCharArray*);
101 
103 
108  vtkSetMacro(ReadFromInputStream, bool);
109  vtkGetMacro(ReadFromInputStream, bool);
110  vtkBooleanMacro(ReadFromInputStream, bool);
112 
114 
120  void SetStream(vtkResourceStream* stream);
121  vtkResourceStream* GetStream();
123 
125 
133  virtual int CanReadFile(VTK_FILEPATH const char* name);
134  bool CanReadFile(vtkResourceStream* stream);
136 
138 
141  vtkDataSet* GetOutputAsDataSet();
142  vtkDataSet* GetOutputAsDataSet(int index);
144 
146 
150  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
151  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
152  vtkGetObjectMacro(ColumnArraySelection, vtkDataArraySelection);
154 
156 
159  int GetNumberOfPointArrays();
160  int GetNumberOfCellArrays();
161  int GetNumberOfColumnArrays();
163 
165 
168  int GetNumberOfTimeDataArrays() const;
169  const char* GetTimeDataArray(int idx) const;
170  vtkGetObjectMacro(TimeDataStringArray, vtkStringArray);
172 
174 
180  vtkGetStringMacro(ActiveTimeDataArrayName);
181  vtkSetStringMacro(ActiveTimeDataArrayName);
183 
185 
189  const char* GetPointArrayName(int index);
190  const char* GetCellArrayName(int index);
191  const char* GetColumnArrayName(int index);
193 
195 
199  int GetPointArrayStatus(const char* name);
200  int GetCellArrayStatus(const char* name);
201  void SetPointArrayStatus(const char* name, int status);
202  void SetCellArrayStatus(const char* name, int status);
203  int GetColumnArrayStatus(const char* name);
204  void SetColumnArrayStatus(const char* name, int status);
206 
207  // For the specified port, copy the information this reader sets up in
208  // SetupOutputInformation to outInfo
209  virtual void CopyOutputInformation(vtkInformation* vtkNotUsed(outInfo), int vtkNotUsed(port)) {}
210 
212 
215  vtkSetMacro(TimeStep, int);
216  vtkGetMacro(TimeStep, int);
218 
219  vtkGetMacro(NumberOfTimeSteps, int);
221 
224  vtkGetVector2Macro(TimeStepRange, int);
225  vtkSetVector2Macro(TimeStepRange, int);
227 
232  vtkXMLDataParser* GetXMLParser() { return this->XMLParser; }
233 
235  vtkInformationVector* outputVector) override;
236 
238 
242  void SetReaderErrorObserver(vtkCommand*);
243  vtkGetObjectMacro(ReaderErrorObserver, vtkCommand);
245 
247 
251  void SetParserErrorObserver(vtkCommand*);
252  vtkGetObjectMacro(ParserErrorObserver, vtkCommand);
254 
258  vtkMTimeType GetMTime() override;
259 
260 protected:
261  vtkXMLReader();
262  ~vtkXMLReader() override;
263 
265 
270  virtual int ReadXMLInformation();
271  virtual void ReadXMLData();
273 
277  virtual const char* GetDataSetName() = 0;
278 
282  virtual int CanReadFileVersion(int major, int minor);
283 
287  virtual void SetupEmptyOutput() = 0;
288 
292  virtual void SetupOutputInformation(vtkInformation* vtkNotUsed(outInfo)) {}
293 
297  virtual void SetupOutputData();
298 
303  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
304 
309  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
310 
316  int GetLocalDataType(vtkXMLDataElement* da, int datatype);
317 
323 
328  int CreateInformationKey(vtkXMLDataElement* eInfoKey, vtkInformation* info);
329 
334  bool ReadInformation(vtkXMLDataElement* infoRoot, vtkInformation* info);
335 
337 
340  virtual int OpenStream();
341  virtual void CloseStream();
342  virtual int OpenVTKFile();
343  virtual void CloseVTKFile();
344  virtual int OpenVTKString();
345  virtual void CloseVTKString();
346  virtual void CreateXMLParser();
347  virtual void DestroyXMLParser();
348  void SetupCompressor(const char* type);
349  int CanReadFileVersionString(const char* version);
351 
357  virtual int CanReadFileWithDataType(const char* dsname);
358 
362  vtkGetMacro(FileMajorVersion, int);
363 
367  vtkGetMacro(FileMinorVersion, int);
368 
370 
373  int IntersectExtents(
374  VTK_FUTURE_CONST int extent1[6], VTK_FUTURE_CONST int extent2[6], int result[6]);
375  void ComputePointDimensions(VTK_FUTURE_CONST int extent[6], int* dimensions);
376  void ComputePointIncrements(VTK_FUTURE_CONST int extent[6], vtkIdType* increments);
377  void ComputeCellDimensions(VTK_FUTURE_CONST int extent[6], int* dimensions);
378  void ComputeCellIncrements(VTK_FUTURE_CONST int extent[6], vtkIdType* increments);
379  vtkIdType GetStartTuple(
380  VTK_FUTURE_CONST int extent[6], vtkIdType* increments, int i, int j, int k);
381  void ReadAttributeIndices(vtkXMLDataElement* eDSA, vtkDataSetAttributes* dsa);
382  char** CreateStringArray(int numStrings);
383  void DestroyStringArray(int numStrings, char** strings);
385 
392  virtual int ReadArrayValues(vtkXMLDataElement* da, vtkIdType arrayIndex, vtkAbstractArray* array,
393  vtkIdType startIndex, vtkIdType numValues, FieldType type = OTHER);
394 
403  virtual int ReadArrayTuples(vtkXMLDataElement* da, vtkIdType arrayTupleIndex,
404  vtkAbstractArray* array, vtkIdType startTupleIndex, vtkIdType numTuples,
405  FieldType type = OTHER);
406 
410  void SetDataArraySelections(vtkXMLDataElement* eDSA, vtkDataArraySelection* sel);
411 
412  int SetFieldDataInfo(vtkXMLDataElement* eDSA, int association, vtkIdType numTuples,
413  vtkInformationVector*(&infoVector));
414 
416 
419  int PointDataArrayIsEnabled(vtkXMLDataElement* ePDA);
420  int CellDataArrayIsEnabled(vtkXMLDataElement* eCDA);
422 
426  static void SelectionModifiedCallback(
427  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
428 
434 
444  void MarkIdTypeArrays(vtkXMLDataElement* da);
445 
446  // The vtkXMLDataParser instance used to hide XML reading details.
448 
449  // The FieldData element representation.
451 
452  // The input file's name.
453  char* FileName;
454 
455  // The stream used to read the input.
456  std::istream* Stream;
457 
458  // Whether this object is reading from a string or a file.
459  // Default is 0: read from file.
461 
462  // The input string.
464 
465  // The input array. Keeps a low memory footprint by sourcing StringStream from contents of this
466  // array
468 
469  // The array selections.
474 
480 
486 
487  // The observer to modify this object when the array selections are
488  // modified.
490 
491  // Whether there was an error reading the file in RequestInformation.
493 
494  // Whether there was an error reading the file in RequestData.
496 
497  // incrementally fine-tuned progress updates.
498  virtual void GetProgressRange(float* range);
499  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
500  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
501  virtual void UpdateProgressDiscrete(float progress);
502  float ProgressRange[2];
503 
504  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
505  vtkInformationVector* outputVector);
506  virtual int RequestDataObject(vtkInformation* vtkNotUsed(request),
507  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
508  {
509  return 1;
510  }
511  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
512  vtkInformationVector* outputVector);
514 
515  // Whether there was an error reading the XML.
517 
518  // For structured data keep track of dimensions empty of cells. For
519  // unstructured data these are always zero. This is used to support
520  // 1-D and 2-D cell data.
521  int AxesEmpty[3];
522 
523  // The timestep currently being read.
524  int TimeStep;
527  void SetNumberOfTimeSteps(int num);
528  // buffer for reading timestep from the XML file the length is of
529  // NumberOfTimeSteps and therefore is always long enough
530  int* TimeSteps;
531  // Store the range of time steps
532  int TimeStepRange[2];
533 
534  // Now we need to save what was the last time read for each kind of
535  // data to avoid rereading it that is to say we need a var for
536  // e.g. PointData/CellData/Points/Cells...
537  // See SubClass for details with member vars like PointsTimeStep/PointsOffset
538 
539  // Helper function useful to know if a timestep is found in an array of timestep
540  static int IsTimeStepInArray(int timestep, int* timesteps, int length);
541 
542  vtkDataObject* GetCurrentOutput();
543  vtkInformation* GetCurrentOutputInformation();
544 
545  // Flag for whether DataProgressCallback should actually update
546  // progress.
548 
550 
551  /*
552  * Populate the output's FieldData with the file's FieldData tags content
553  */
554  void ReadFieldData();
555 
556 private:
557  int OpenVTKStream();
558 
559  // The stream used to read the input if it is in a resource stream
560  vtkSmartPointer<vtkResourceStream> ResourceStream;
561 
562  bool ReadFromInputStream = false;
563 
564  // The stream used to read the input if it is in a file.
565  istream* FileStream;
566  // The stream used to read the input if it is in a string.
567  std::istringstream* StringStream;
568 
569  // Used when converting vtkResourceStream into istream
570  std::unique_ptr<std::streambuf> Streambuf;
571  std::unique_ptr<std::istream> StreamBuffer;
572 
573  int TimeStepWasReadOnce;
574 
575  int FileMajorVersion;
576  int FileMinorVersion;
577 
578  vtkDataObject* CurrentOutput;
579  vtkInformation* CurrentOutputInformation;
580 
581  vtkXMLReader(const vtkXMLReader&) = delete;
582  void operator=(const vtkXMLReader&) = delete;
583 
584  vtkCommand* ReaderErrorObserver;
585  vtkCommand* ParserErrorObserver;
586 };
587 
588 VTK_ABI_NAMESPACE_END
589 #endif
virtual void SqueezeOutputArrays(vtkDataObject *)
Give concrete classes an option to squeeze any output arrays at the end of RequestData.
Definition: vtkXMLReader.h:433
abstract base class for most VTK objects
Definition: vtkObject.h:52
Abstract class used for custom streams.
Represents an XML element and those nested inside.
Store vtkAlgorithm input/output information.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:318
abstract class to specify dataset behavior
Definition: vtkDataSet.h:56
Abstract superclass for all arrays.
record modification and/or execution time
Definition: vtkTimeStamp.h:24
vtkXMLDataElement * FieldDataElement
Definition: vtkXMLReader.h:450
vtkCallbackCommand * SelectionObserver
Definition: vtkXMLReader.h:489
a vtkAbstractArray subclass for strings
int vtkIdType
Definition: vtkType.h:363
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkCharArray * InputArray
Definition: vtkXMLReader.h:467
char * ActiveTimeDataArrayName
Name of the field-data array used to determine the time for the dataset being read.
Definition: vtkXMLReader.h:479
char * FileName
Definition: vtkXMLReader.h:453
virtual int RequestDataObject(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
Definition: vtkXMLReader.h:506
int vtkTypeBool
Definition: vtkABI.h:64
virtual vtkTypeBool ProcessRequest(vtkInformation *request, vtkInformationVector **inInfo, vtkInformationVector *outInfo)
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
dynamic, self-adjusting array of char
Definition: vtkCharArray.h:44
vtkSmartPointer< vtkDataArray > TimeDataArray
Populated in ReadXMLInformation from the field data for the array chosen using ActiveTimeDataArrayNam...
Definition: vtkXMLReader.h:485
superclass for callback/observer methods
Definition: vtkCommand.h:384
supports function callbacks
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:53
a simple class to control print indentation
Definition: vtkIndent.h:28
vtkTimeStamp ReadMTime
Definition: vtkXMLReader.h:513
vtkDataArraySelection * PointDataArraySelection
Definition: vtkXMLReader.h:470
vtkDataArraySelection * CellDataArraySelection
Definition: vtkXMLReader.h:471
Store on/off settings for data arrays, etc.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
virtual vtkMTimeType GetMTime()
Return this object's modified time.
std::istream * Stream
Definition: vtkXMLReader.h:456
int NumberOfTimeSteps
Definition: vtkXMLReader.h:526
represent and manipulate attribute data in a dataset
vtkStringArray * TimeDataStringArray
Definition: vtkXMLReader.h:473
vtkTypeBool ReadFromInputString
Definition: vtkXMLReader.h:460
virtual void CopyOutputInformation(vtkInformation *outInfo, int port)
Definition: vtkXMLReader.h:209
virtual void ConvertGhostLevelsToGhostType(FieldType, vtkAbstractArray *, vtkIdType, vtkIdType)
Definition: vtkXMLReader.h:549
std::string InputString
Definition: vtkXMLReader.h:463
#define VTK_FILEPATH
void SetInputString(const std::string &input)
Enable reading from an InputString instead of the default, a file.
Definition: vtkXMLReader.h:84
int InformationError
Definition: vtkXMLReader.h:492
vtkDataArraySelection * ColumnArraySelection
Definition: vtkXMLReader.h:472
Used by vtkXMLReader to parse VTK XML files.
vtkXMLDataParser * GetXMLParser()
Returns the internal XML parser.
Definition: vtkXMLReader.h:232
Store zero or more vtkInformation instances.
virtual void SetupOutputInformation(vtkInformation *outInfo)
Setup the output's information.
Definition: vtkXMLReader.h:292
general representation of visualization data
Definition: vtkDataObject.h:55
vtkSmartPointer< vtkDataArray > CreateArray(const Ioss::Field &field)
Create an array for the given field.
vtkXMLDataParser * XMLParser
Definition: vtkXMLReader.h:447
Superclass for VTK's XML format readers.
Definition: vtkXMLReader.h:44