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