VTK
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 
30 #include <string> // for std::string
31 
32 class vtkAbstractArray;
33 class vtkCallbackCommand;
35 class vtkDataSet;
37 class vtkXMLDataElement;
38 class vtkXMLDataParser;
40 class vtkInformation;
41 class vtkCommand;
42 
43 class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
44 {
45 public:
46  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
47  void PrintSelf(ostream& os, vtkIndent indent) override;
48 
49  enum FieldType
50  {
53  OTHER
54  };
55 
57 
60  vtkSetStringMacro(FileName);
61  vtkGetStringMacro(FileName);
63 
65 
68  vtkSetMacro(ReadFromInputString, vtkTypeBool);
69  vtkGetMacro(ReadFromInputString, vtkTypeBool);
70  vtkBooleanMacro(ReadFromInputString, vtkTypeBool);
71  void SetInputString(const std::string& s) { this->InputString = s; }
73 
81  virtual int CanReadFile(const char* name);
82 
84 
87  vtkDataSet* GetOutputAsDataSet();
88  vtkDataSet* GetOutputAsDataSet(int index);
90 
92 
96  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
97  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
98  vtkGetObjectMacro(ColumnArraySelection, vtkDataArraySelection);
100 
102 
105  int GetNumberOfPointArrays();
106  int GetNumberOfCellArrays();
107  int GetNumberOfColumnArrays();
109 
111 
115  const char* GetPointArrayName(int index);
116  const char* GetCellArrayName(int index);
117  const char* GetColumnArrayName(int index);
119 
121 
125  int GetPointArrayStatus(const char* name);
126  int GetCellArrayStatus(const char* name);
127  void SetPointArrayStatus(const char* name, int status);
128  void SetCellArrayStatus(const char* name, int status);
129  int GetColumnArrayStatus(const char* name);
130  void SetColumnArrayStatus(const char* name, int status);
132 
133  // For the specified port, copy the information this reader sets up in
134  // SetupOutputInformation to outInfo
135  virtual void CopyOutputInformation(vtkInformation* vtkNotUsed(outInfo), int vtkNotUsed(port)) {}
136 
138 
141  vtkSetMacro(TimeStep, int);
142  vtkGetMacro(TimeStep, int);
144 
145  vtkGetMacro(NumberOfTimeSteps, int);
147 
150  vtkGetVector2Macro(TimeStepRange, int);
151  vtkSetVector2Macro(TimeStepRange, int);
153 
158  vtkXMLDataParser* GetXMLParser() { return this->XMLParser; }
159 
161  vtkInformationVector* outputVector) override;
162 
164 
168  void SetReaderErrorObserver(vtkCommand*);
169  vtkGetObjectMacro(ReaderErrorObserver, vtkCommand);
171 
173 
177  void SetParserErrorObserver(vtkCommand*);
178  vtkGetObjectMacro(ParserErrorObserver, vtkCommand);
180 
181 protected:
182  vtkXMLReader();
183  ~vtkXMLReader() override;
184 
185  // Pipeline execution methods to be defined by subclass. Called by
186  // corresponding RequestData methods after appropriate setup has been
187  // done.
188  virtual int ReadXMLInformation();
189  virtual void ReadXMLData();
190 
191  // Get the name of the data set being read.
192  virtual const char* GetDataSetName() = 0;
193 
194  // Test if the reader can read a file with the given version number.
195  virtual int CanReadFileVersion(int major, int minor);
196 
197  // Setup the output with no data available. Used in error cases.
198  virtual void SetupEmptyOutput() = 0;
199 
200  // Setup the output's information.
201  virtual void SetupOutputInformation(vtkInformation* vtkNotUsed(outInfo)) {}
202 
203  // Setup the output's data with allocation.
204  virtual void SetupOutputData();
205 
206  // Read the primary element from the file. This is the element
207  // whose name is the value returned by GetDataSetName().
208  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
209 
210  // Read the top-level element from the file. This is always the
211  // VTKFile element.
212  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
213 
219  int GetLocalDataType(vtkXMLDataElement* da, int datatype);
220 
221  // Create a vtkAbstractArray from its cooresponding XML representation.
222  // Does not allocate.
223  vtkAbstractArray* CreateArray(vtkXMLDataElement* da);
224 
225  // Create a vtkInformationKey from its corresponding XML representation.
226  // Stores it in the instance of vtkInformationProvided. Does not allocate.
227  int CreateInformationKey(vtkXMLDataElement* eInfoKey, vtkInformation* info);
228 
229  // Populates the info object with the InformationKey children in infoRoot.
230  // Returns false if errors occur.
231  bool ReadInformation(vtkXMLDataElement* infoRoot, vtkInformation* info);
232 
233  // Internal utility methods.
234  virtual int OpenStream();
235  virtual void CloseStream();
236  virtual int OpenVTKFile();
237  virtual void CloseVTKFile();
238  virtual int OpenVTKString();
239  virtual void CloseVTKString();
240  virtual void CreateXMLParser();
241  virtual void DestroyXMLParser();
242  void SetupCompressor(const char* type);
243  int CanReadFileVersionString(const char* version);
244 
250  virtual int CanReadFileWithDataType(const char* dsname);
251 
252  // Returns the major version for the file being read. -1 when invalid.
253  vtkGetMacro(FileMajorVersion, int);
254 
255  // Returns the minor version for the file being read. -1 when invalid.
256  vtkGetMacro(FileMinorVersion, int);
257 
258  // Utility methods for subclasses.
259  int IntersectExtents(int* extent1, int* extent2, int* result);
260  int Min(int a, int b);
261  int Max(int a, int b);
262  void ComputePointDimensions(int* extent, int* dimensions);
263  void ComputePointIncrements(int* extent, vtkIdType* increments);
264  void ComputeCellDimensions(int* extent, int* dimensions);
265  void ComputeCellIncrements(int* extent, vtkIdType* increments);
266  vtkIdType GetStartTuple(int* extent, vtkIdType* increments, int i, int j, int k);
267  void ReadAttributeIndices(vtkXMLDataElement* eDSA, vtkDataSetAttributes* dsa);
268  char** CreateStringArray(int numStrings);
269  void DestroyStringArray(int numStrings, char** strings);
270 
271  // Read an Array values starting at the given index and up to numValues.
272  // This method assumes that the array is of correct size to
273  // accommodate all numValues values. arrayIndex is the value index at which the read
274  // values will be put in the array.
275  virtual int ReadArrayValues(vtkXMLDataElement* da, vtkIdType arrayIndex, vtkAbstractArray* array,
276  vtkIdType startIndex, vtkIdType numValues, FieldType type = OTHER);
277 
278  // Setup the data array selections for the input's set of arrays.
279  void SetDataArraySelections(vtkXMLDataElement* eDSA, vtkDataArraySelection* sel);
280 
281  int SetFieldDataInfo(vtkXMLDataElement* eDSA, int association, vtkIdType numTuples,
282  vtkInformationVector*(&infoVector));
283 
284  // Check whether the given array element is an enabled array.
285  int PointDataArrayIsEnabled(vtkXMLDataElement* ePDA);
286  int CellDataArrayIsEnabled(vtkXMLDataElement* eCDA);
287 
288  // Callback registered with the SelectionObserver.
289  static void SelectionModifiedCallback(
290  vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
291 
292  // Give concrete classes an option to squeeze any output arrays
293  // at the end of RequestData.
295 
296  // The vtkXMLDataParser instance used to hide XML reading details.
298 
299  // The FieldData element representation.
301 
302  // The input file's name.
303  char* FileName;
304 
305  // The stream used to read the input.
306  istream* Stream;
307 
308  // Whether this object is reading from a string or a file.
309  // Default is 0: read from file.
311 
312  // The input string.
314 
315  // The array selections.
319 
320  // The observer to modify this object when the array selections are
321  // modified.
323 
324  // Whether there was an error reading the file in RequestInformation.
326 
327  // Whether there was an error reading the file in RequestData.
329 
330  // incrementally fine-tuned progress updates.
331  virtual void GetProgressRange(float* range);
332  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
333  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
334  virtual void UpdateProgressDiscrete(float progress);
335  float ProgressRange[2];
336 
337  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
338  vtkInformationVector* outputVector);
339  virtual int RequestDataObject(vtkInformation* vtkNotUsed(request),
340  vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
341  {
342  return 1;
343  }
344  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
345  vtkInformationVector* outputVector);
347 
348  // Whether there was an error reading the XML.
350 
351  // For structured data keep track of dimensions empty of cells. For
352  // unstructured data these are always zero. This is used to support
353  // 1-D and 2-D cell data.
354  int AxesEmpty[3];
355 
356  // The timestep currently being read.
357  int TimeStep;
360  void SetNumberOfTimeSteps(int num);
361  // buffer for reading timestep from the XML file the length is of
362  // NumberOfTimeSteps and therefore is always long enough
363  int* TimeSteps;
364  // Store the range of time steps
365  int TimeStepRange[2];
366 
367  // Now we need to save what was the last time read for each kind of
368  // data to avoid rereading it that is to say we need a var for
369  // e.g. PointData/CellData/Points/Cells...
370  // See SubClass for details with member vars like PointsTimeStep/PointsOffset
371 
372  // Helper function useful to know if a timestep is found in an array of timestep
373  static int IsTimeStepInArray(int timestep, int* timesteps, int length);
374 
375  vtkDataObject* GetCurrentOutput();
376  vtkInformation* GetCurrentOutputInformation();
377 
378  // Flag for whether DataProgressCallback should actually update
379  // progress.
381 
383 
384  void ReadFieldData();
385 
386 private:
387  // The stream used to read the input if it is in a file.
388  ifstream* FileStream;
389  // The stream used to read the input if it is in a string.
390  std::istringstream* StringStream;
391  int TimeStepWasReadOnce;
392 
393  int FileMajorVersion;
394  int FileMinorVersion;
395 
396  vtkDataObject* CurrentOutput;
397  vtkInformation* CurrentOutputInformation;
398 
399 private:
400  vtkXMLReader(const vtkXMLReader&) = delete;
401  void operator=(const vtkXMLReader&) = delete;
402 
403  vtkCommand* ReaderErrorObserver;
404  vtkCommand* ParserErrorObserver;
405 };
406 
407 #endif
vtkXMLReader::ReadMTime
vtkTimeStamp ReadMTime
Definition: vtkXMLReader.h:346
vtkCommand
superclass for callback/observer methods
Definition: vtkCommand.h:375
vtkXMLReader::POINT_DATA
Definition: vtkXMLReader.h:51
vtkXMLReader::CurrentTimeStep
int CurrentTimeStep
Definition: vtkXMLReader.h:358
vtkXMLReader::FieldDataElement
vtkXMLDataElement * FieldDataElement
Definition: vtkXMLReader.h:300
vtkXMLReader::PointDataArraySelection
vtkDataArraySelection * PointDataArraySelection
Definition: vtkXMLReader.h:316
vtkXMLReader::TimeSteps
int * TimeSteps
Definition: vtkXMLReader.h:363
vtkXMLReader::ReadFromInputString
vtkTypeBool ReadFromInputString
Definition: vtkXMLReader.h:310
vtkXMLReader::SetInputString
void SetInputString(const std::string &s)
Definition: vtkXMLReader.h:71
vtkX3D::type
Definition: vtkX3D.h:522
vtkIdType
int vtkIdType
Definition: vtkType.h:343
vtkXMLReader::XMLParser
vtkXMLDataParser * XMLParser
Definition: vtkXMLReader.h:297
vtkAlgorithm
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:53
vtkDataSetAttributes
represent and manipulate attribute data in a dataset
Definition: vtkDataSetAttributes.h:53
vtkXMLReader::ConvertGhostLevelsToGhostType
virtual void ConvertGhostLevelsToGhostType(FieldType, vtkAbstractArray *, vtkIdType, vtkIdType)
Definition: vtkXMLReader.h:382
vtkXMLReader::InformationError
int InformationError
Definition: vtkXMLReader.h:325
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:303
vtkX3D::range
Definition: vtkX3D.h:244
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:53
vtkXMLReader::RequestDataObject
virtual int RequestDataObject(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *vtkNotUsed(outputVector))
Definition: vtkXMLReader.h:339
vtkX3D::length
Definition: vtkX3D.h:399
vtkXMLReader::Stream
istream * Stream
Definition: vtkXMLReader.h:306
vtkXMLReader::DataError
int DataError
Definition: vtkXMLReader.h:328
vtkDataArraySelection
Store on/off settings for data arrays for a vtkSource.
Definition: vtkDataArraySelection.h:34
vtkAlgorithm.h
vtkX3D::port
Definition: vtkX3D.h:453
vtkXMLReader::InReadData
int InReadData
Definition: vtkXMLReader.h:380
vtkXMLReader::InputString
std::string InputString
Definition: vtkXMLReader.h:313
vtkXMLReader::SqueezeOutputArrays
virtual void SqueezeOutputArrays(vtkDataObject *)
Definition: vtkXMLReader.h:294
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkXMLReader::CELL_DATA
Definition: vtkXMLReader.h:52
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:158
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:64
vtkX3D::info
Definition: vtkX3D.h:382
vtkX3D::string
Definition: vtkX3D.h:496
vtkXMLReader::CellDataArraySelection
vtkDataArraySelection * CellDataArraySelection
Definition: vtkXMLReader.h:317
vtkXMLReader::TimeStep
int TimeStep
Definition: vtkXMLReader.h:357
vtkXMLReader::FieldType
FieldType
Definition: vtkXMLReader.h:49
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:201
vtkXMLReader::NumberOfTimeSteps
int NumberOfTimeSteps
Definition: vtkXMLReader.h:359
vtkXMLReader::SelectionObserver
vtkCallbackCommand * SelectionObserver
Definition: vtkXMLReader.h:322
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:58
vtkXMLReader::CopyOutputInformation
virtual void CopyOutputInformation(vtkInformation *vtkNotUsed(outInfo), int vtkNotUsed(port))
Definition: vtkXMLReader.h:135
vtkXMLReader::ReadError
int ReadError
Definition: vtkXMLReader.h:349
vtkX3D::index
Definition: vtkX3D.h:252
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:43
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkXMLReader::ColumnArraySelection
vtkDataArraySelection * ColumnArraySelection
Definition: vtkXMLReader.h:318
vtkXMLDataParser
Used by vtkXMLReader to parse VTK XML files.
Definition: vtkXMLDataParser.h:39