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