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