vtkXMLReader.h 12 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkXMLReader.h

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8 9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16 17 18 19 20
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
// .NAME vtkXMLReader - Superclass for VTK's XML format readers.
// .SECTION Description
// vtkXMLReader uses vtkXMLDataParser to parse a VTK XML input file.
// Concrete subclasses then traverse the parsed file structure and
// extract data.

21 22
#ifndef vtkXMLReader_h
#define vtkXMLReader_h
23

24
#include "vtkIOXMLModule.h" // For export macro
25
#include "vtkAlgorithm.h"
26

27
#include <string> // for std::string
28

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
29
class vtkAbstractArray;
30 31 32 33 34 35
class vtkCallbackCommand;
class vtkDataArraySelection;
class vtkDataSet;
class vtkDataSetAttributes;
class vtkXMLDataElement;
class vtkXMLDataParser;
36
class vtkInformationVector;
37
class vtkInformation;
38
class vtkCommand;
39

40
class VTKIOXML_EXPORT vtkXMLReader : public vtkAlgorithm
41 42
{
public:
43
  vtkTypeMacro(vtkXMLReader, vtkAlgorithm);
44
  void PrintSelf(ostream& os, vtkIndent indent);
45

46 47 48 49
  // Description:
  // Get/Set the name of the input file.
  vtkSetStringMacro(FileName);
  vtkGetStringMacro(FileName);
50

51 52 53 54 55 56 57
  // Description:
  // Enable writing to an InputString instead of the default, a file.
  vtkSetMacro(ReadFromInputString,int);
  vtkGetMacro(ReadFromInputString,int);
  vtkBooleanMacro(ReadFromInputString,int);
  void SetInputString(std::string s) { this->InputString = s; }

58
  // Description:
59 60 61 62 63
  // Test whether the file (type) with the given name can be read by this
  // reader. If the file has a newer version than the reader, we still say
  // we can read the file type and we fail later, when we try to read the file.
  // This enables clients (ParaView) to distinguish between failures when we
  // need to look for another reader and failures when we don't.
64
  virtual int CanReadFile(const char* name);
65

66 67 68
  // Description:
  // Get the output as a vtkDataSet pointer.
  vtkDataSet* GetOutputAsDataSet();
69
  vtkDataSet* GetOutputAsDataSet(int index);
70

71 72 73 74 75
  // Description:
  // Get the data array selection tables used to configure which data
  // arrays are loaded by the reader.
  vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
  vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
76 77

  // Description:
78 79 80
  // Get the number of point or cell arrays available in the input.
  int GetNumberOfPointArrays();
  int GetNumberOfCellArrays();
81

82 83 84 85 86
  // Description:
  // Get the name of the point or cell array with the given index in
  // the input.
  const char* GetPointArrayName(int index);
  const char* GetCellArrayName(int index);
87

88 89 90 91 92
  // Description:
  // Get/Set whether the point or cell array with the given name is to
  // be read.
  int GetPointArrayStatus(const char* name);
  int GetCellArrayStatus(const char* name);
93 94
  void SetPointArrayStatus(const char* name, int status);
  void SetCellArrayStatus(const char* name, int status);
95 96 97 98 99 100

  // For the specified port, copy the information this reader sets up in
  // SetupOutputInformation to outInfo
  virtual void CopyOutputInformation(vtkInformation *vtkNotUsed(outInfo),
                                   int vtkNotUsed(port)) {}

101
  // Description:
102
  // Which TimeStep to read.
103 104
  vtkSetMacro(TimeStep, int);
  vtkGetMacro(TimeStep, int);
105

106 107 108 109 110 111
  vtkGetMacro(NumberOfTimeSteps, int);
  // Description:
  // Which TimeStepRange to read
  vtkGetVector2Macro(TimeStepRange, int);
  vtkSetVector2Macro(TimeStepRange, int);

112 113 114 115 116 117 118 119
  // Description:
  // Returns the internal XML parser. This can be used to access
  // the XML DOM after RequestInformation() was called.
  vtkXMLDataParser* GetXMLParser()
  {
    return this->XMLParser;
  }

120 121 122
  virtual int ProcessRequest(vtkInformation *request,
                             vtkInformationVector **inputVector,
                             vtkInformationVector *outputVector);
123

124 125 126 127 128 129 130 131 132 133 134 135
  // Description:
  // Set/get the ErrorObserver for the internal reader
  // This is useful for applications that want to catch error messages.
  void SetReaderErrorObserver(vtkCommand *);
  vtkGetObjectMacro(ReaderErrorObserver,vtkCommand);

  // Description:
  // Set/get the ErrorObserver for the internal xml parser
  // This is useful for applications that want to catch error messages.
  void SetParserErrorObserver(vtkCommand *);
  vtkGetObjectMacro(ParserErrorObserver,vtkCommand);

136 137 138
protected:
  vtkXMLReader();
  ~vtkXMLReader();
139

140
  // Pipeline execution methods to be defined by subclass.  Called by
141
  // corresponding RequestData methods after appropriate setup has been
142
  // done.
143
  virtual int ReadXMLInformation();
144
  virtual void ReadXMLData();
145

146 147
  // Get the name of the data set being read.
  virtual const char* GetDataSetName()=0;
148

149 150
  // Test if the reader can read a file with the given version number.
  virtual int CanReadFileVersion(int major, int minor);
151

152 153
  // Setup the output with no data available.  Used in error cases.
  virtual void SetupEmptyOutput()=0;
154

155
  // Setup the output's information.
156
  virtual void SetupOutputInformation(vtkInformation *vtkNotUsed(outInfo)) {}
157

158
  // Setup the output's data with allocation.
159
  virtual void SetupOutputData();
160

161 162 163
  // Read the primary element from the file.  This is the element
  // whose name is the value returned by GetDataSetName().
  virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
164

165 166
  // Read the top-level element from the file.  This is always the
  // VTKFile element.
167
  virtual int ReadVTKFile(vtkXMLDataElement* eVTKFile);
168

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
169
  // Create a vtkAbstractArray from its cooresponding XML representation.
170
  // Does not allocate.
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
171
  vtkAbstractArray* CreateArray(vtkXMLDataElement* da);
172 173 174 175 176

  // Create a vtkInformationKey from its coresponding XML representation.
  // Stores it in the instance of vtkInformationProvided. Does not allocate.
  int CreateInformationKey(vtkXMLDataElement *eInfoKey, vtkInformation *info);

177
  // Internal utility methods.
178 179
  virtual int OpenStream();
  virtual void CloseStream();
180 181
  virtual int OpenVTKFile();
  virtual void CloseVTKFile();
182 183
  virtual int OpenVTKString();
  virtual void CloseVTKString();
184 185
  virtual void CreateXMLParser();
  virtual void DestroyXMLParser();
186 187
  void SetupCompressor(const char* type);
  int CanReadFileVersionString(const char* version);
188

189 190 191 192 193 194
  // Description:
  // This method is used by CanReadFile() to check if the reader can read an XML
  // with the primary element with the given name. Default implementation
  // compares the name with the text returned by this->GetDataSetName().
  virtual int CanReadFileWithDataType(const char* dsname);

195 196 197 198 199
  // Returns the major version for the file being read. -1 when invalid.
  vtkGetMacro(FileMajorVersion, int);

  // Returns the minor version for the file being read. -1 when invalid.
  vtkGetMacro(FileMinorVersion, int);
200

201 202 203 204
  // Utility methods for subclasses.
  int IntersectExtents(int* extent1, int* extent2, int* result);
  int Min(int a, int b);
  int Max(int a, int b);
205 206 207 208
  void ComputePointDimensions(int* extent, int* dimensions);
  void ComputePointIncrements(int* extent, vtkIdType* increments);
  void ComputeCellDimensions(int* extent, int* dimensions);
  void ComputeCellIncrements(int* extent, vtkIdType* increments);
209 210
  vtkIdType GetStartTuple(int* extent, vtkIdType* increments,
                          int i, int j, int k);
211 212 213
  void ReadAttributeIndices(vtkXMLDataElement* eDSA,
                            vtkDataSetAttributes* dsa);
  char** CreateStringArray(int numStrings);
214 215
  void DestroyStringArray(int numStrings, char** strings);

216 217 218
  // Setup the data array selections for the input's set of arrays.
  void SetDataArraySelections(vtkXMLDataElement* eDSA,
                              vtkDataArraySelection* sel);
219 220

//BTX
221
  int SetFieldDataInfo(vtkXMLDataElement *eDSA, int association,
222
  int numTuples, vtkInformationVector *(&infoVector));
223 224
//ETX

225 226 227
  // Check whether the given array element is an enabled array.
  int PointDataArrayIsEnabled(vtkXMLDataElement* ePDA);
  int CellDataArrayIsEnabled(vtkXMLDataElement* eCDA);
228

229 230 231
  // Callback registered with the SelectionObserver.
  static void SelectionModifiedCallback(vtkObject* caller, unsigned long eid,
                                        void* clientdata, void* calldata);
232

233 234 235 236
  // Give concrete classes an option to squeeze any output arrays
  // at the end of RequestData.
  virtual void SqueezeOutputArrays(vtkDataObject*) {}

237 238
  // The vtkXMLDataParser instance used to hide XML reading details.
  vtkXMLDataParser* XMLParser;
239 240 241 242

  // The FieldData element representation.
  vtkXMLDataElement* FieldDataElement;

243 244
  // The input file's name.
  char* FileName;
245

246 247
  // The stream used to read the input.
  istream* Stream;
248

249 250 251 252 253 254 255
  // Whether this object is reading from a string or a file.
  // Default is 0: read from file.
  int ReadFromInputString;

  // The input string.
  std::string InputString;

256 257 258
  // The array selections.
  vtkDataArraySelection* PointDataArraySelection;
  vtkDataArraySelection* CellDataArraySelection;
259

260 261 262
  // The observer to modify this object when the array selections are
  // modified.
  vtkCallbackCommand* SelectionObserver;
263

264
  // Whether there was an error reading the file in RequestInformation.
265
  int InformationError;
266

267
  // Whether there was an error reading the file in RequestData.
268
  int DataError;
269

270 271
  // incrementally fine-tuned progress updates.
  virtual void GetProgressRange(float* range);
272 273
  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
274 275
  virtual void UpdateProgressDiscrete(float progress);
  float ProgressRange[2];
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  virtual int RequestData(vtkInformation *request,
                          vtkInformationVector **inputVector,
                          vtkInformationVector *outputVector);
  virtual int RequestDataObject(vtkInformation *vtkNotUsed(request),
                                vtkInformationVector **vtkNotUsed(inputVector),
                                vtkInformationVector *vtkNotUsed(outputVector))
    { return 1; }
  virtual int RequestInformation(vtkInformation *request,
                                 vtkInformationVector **inputVector,
                                 vtkInformationVector *outputVector);
  vtkTimeStamp ReadMTime;

  // Whether there was an error reading the XML.
  int ReadError;

292 293 294 295 296
  // For structured data keep track of dimensions empty of cells.  For
  // unstructured data these are always zero.  This is used to support
  // 1-D and 2-D cell data.
  int AxesEmpty[3];

297 298 299 300 301
  // The timestep currently being read.
  int TimeStep;
  int CurrentTimeStep;
  int NumberOfTimeSteps;
  void SetNumberOfTimeSteps(int num);
302
  // buffer for reading timestep from the XML file the length is of
303
  // NumberOfTimeSteps and therefore is always long enough
304
  int *TimeSteps;
305 306 307
  // Store the range of time steps
  int TimeStepRange[2];

308 309
  // Now we need to save what was the last time read for each kind of
  // data to avoid rereading it that is to say we need a var for
310 311 312
  // e.g. PointData/CellData/Points/Cells...
  // See SubClass for details with member vars like PointsTimeStep/PointsOffset

313
  // Helper function useful to know if a timestep is found in an array of timestep
314 315
  static int IsTimeStepInArray(int timestep, int* timesteps, int length);

316 317
  vtkDataObject* GetCurrentOutput();
  vtkInformation* GetCurrentOutputInformation();
318

319 320
private:
  // The stream used to read the input if it is in a file.
321
  ifstream* FileStream;
322 323
  // The stream used to read the input if it is in a string.
  std::istringstream* StringStream;
324
  int TimeStepWasReadOnce;
325 326 327

  int FileMajorVersion;
  int FileMinorVersion;
328

329 330
  vtkDataObject* CurrentOutput;
  vtkInformation* CurrentOutputInformation;
331

332 333 334
private:
  vtkXMLReader(const vtkXMLReader&);  // Not implemented.
  void operator=(const vtkXMLReader&);  // Not implemented.
335 336 337

  vtkCommand *ReaderErrorObserver;
  vtkCommand *ParserErrorObserver;
338 339 340
};

#endif