VTK
dox/IO/vtkXMLReader.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkXMLReader.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00023 #ifndef __vtkXMLReader_h
00024 #define __vtkXMLReader_h
00025 
00026 #include "vtkAlgorithm.h"
00027 
00028 class vtkAbstractArray;
00029 class vtkCallbackCommand;
00030 class vtkDataArraySelection;
00031 class vtkDataSet;
00032 class vtkDataSetAttributes;
00033 class vtkXMLDataElement;
00034 class vtkXMLDataParser;
00035 class vtkInformationVector;
00036 class vtkInformation;
00037 
00038 class VTK_IO_EXPORT vtkXMLReader : public vtkAlgorithm
00039 {
00040 public:
00041   vtkTypeMacro(vtkXMLReader,vtkAlgorithm);
00042   void PrintSelf(ostream& os, vtkIndent indent);
00043   
00045 
00046   vtkSetStringMacro(FileName);
00047   vtkGetStringMacro(FileName);
00049   
00051   virtual int CanReadFile(const char* name);
00052   
00054 
00055   vtkDataSet* GetOutputAsDataSet();
00056   vtkDataSet* GetOutputAsDataSet(int index);
00058   
00060 
00062   vtkGetObjectMacro(PointDataArraySelection, vtkDataArraySelection);
00063   vtkGetObjectMacro(CellDataArraySelection, vtkDataArraySelection);
00065   
00067 
00068   int GetNumberOfPointArrays();
00069   int GetNumberOfCellArrays();
00071   
00073 
00075   const char* GetPointArrayName(int index);
00076   const char* GetCellArrayName(int index);
00078   
00080 
00082   int GetPointArrayStatus(const char* name);
00083   int GetCellArrayStatus(const char* name);
00084   void SetPointArrayStatus(const char* name, int status);  
00085   void SetCellArrayStatus(const char* name, int status);  
00087 
00088   // For the specified port, copy the information this reader sets up in
00089   // SetupOutputInformation to outInfo
00090   virtual void CopyOutputInformation(vtkInformation *vtkNotUsed(outInfo),
00091                                    int vtkNotUsed(port)) {}
00092 
00094 
00095   vtkSetMacro(TimeStep, int);
00096   vtkGetMacro(TimeStep, int);
00098  
00099   vtkGetMacro(NumberOfTimeSteps, int);
00101 
00102   vtkGetVector2Macro(TimeStepRange, int);
00103   vtkSetVector2Macro(TimeStepRange, int);
00105 
00106   virtual int ProcessRequest(vtkInformation *request,
00107                              vtkInformationVector **inputVector,
00108                              vtkInformationVector *outputVector);
00109   
00110 protected:
00111   vtkXMLReader();
00112   ~vtkXMLReader();
00113    
00114   // Pipeline execution methods to be defined by subclass.  Called by
00115   // corresponding RequestData methods after appropriate setup has been
00116   // done.
00117   virtual int ReadXMLInformation();
00118   virtual void ReadXMLData();
00119   
00120   // Get the name of the data set being read.
00121   virtual const char* GetDataSetName()=0;
00122   
00123   // Test if the reader can read a file with the given version number.
00124   virtual int CanReadFileVersion(int major, int minor);
00125   
00126   // Setup the output with no data available.  Used in error cases.
00127   virtual void SetupEmptyOutput()=0;
00128   
00129   // Setup the output's information.
00130   virtual void SetupOutputInformation(vtkInformation *vtkNotUsed(outInfo)) {}
00131   
00132   // Setup the output's information for the update extent
00133   virtual void SetupUpdateExtentInformation
00134   (vtkInformation *vtkNotUsed(outInfo)) {}
00135 
00136   // Setup the output's data with allocation.
00137   virtual void SetupOutputData();
00138   
00139   // Read the primary element from the file.  This is the element
00140   // whose name is the value returned by GetDataSetName().
00141   virtual int ReadPrimaryElement(vtkXMLDataElement* ePrimary);
00142   
00143   // Read the top-level element from the file.  This is always the
00144   // VTKFile element.
00145   int ReadVTKFile(vtkXMLDataElement* eVTKFile);  
00146   
00147   // Create a vtkAbstractArray from its cooresponding XML representation.
00148   // Does not allocate.
00149   vtkAbstractArray* CreateArray(vtkXMLDataElement* da);
00150 
00151   // Create a vtkInformationKey from its coresponding XML representation.
00152   // Stores it in the instance of vtkInformationProvided. Does not allocate.
00153   int CreateInformationKey(vtkXMLDataElement *eInfoKey, vtkInformation *info);
00154 
00155   // Internal utility methods.
00156   virtual int OpenVTKFile();
00157   virtual void CloseVTKFile();
00158   virtual void CreateXMLParser();
00159   virtual void DestroyXMLParser();
00160   void SetupCompressor(const char* type);
00161   int CanReadFileVersionString(const char* version);
00162 
00163   // Returns the major version for the file being read. -1 when invalid.
00164   vtkGetMacro(FileMajorVersion, int);
00165 
00166   // Returns the minor version for the file being read. -1 when invalid.
00167   vtkGetMacro(FileMinorVersion, int);
00168   
00169   // Utility methods for subclasses.
00170   int IntersectExtents(int* extent1, int* extent2, int* result);
00171   int Min(int a, int b);
00172   int Max(int a, int b);
00173   void ComputePointDimensions(int* extent, int* dimensions);
00174   void ComputePointIncrements(int* extent, vtkIdType* increments);
00175   void ComputeCellDimensions(int* extent, int* dimensions);
00176   void ComputeCellIncrements(int* extent, vtkIdType* increments);
00177   vtkIdType GetStartTuple(int* extent, vtkIdType* increments,
00178                           int i, int j, int k);
00179   void ReadAttributeIndices(vtkXMLDataElement* eDSA,
00180                             vtkDataSetAttributes* dsa);
00181   char** CreateStringArray(int numStrings);
00182   void DestroyStringArray(int numStrings, char** strings);  
00183   
00184   // Setup the data array selections for the input's set of arrays.
00185   void SetDataArraySelections(vtkXMLDataElement* eDSA,
00186                               vtkDataArraySelection* sel);
00187 
00188 //BTX
00189   int SetFieldDataInfo(vtkXMLDataElement *eDSA, int association,  
00190   int numTuples, vtkInformationVector *(&infoVector));
00191 //ETX
00192 
00193   // Check whether the given array element is an enabled array.
00194   int PointDataArrayIsEnabled(vtkXMLDataElement* ePDA);
00195   int CellDataArrayIsEnabled(vtkXMLDataElement* eCDA);
00196   
00197   // Callback registered with the SelectionObserver.
00198   static void SelectionModifiedCallback(vtkObject* caller, unsigned long eid,
00199                                         void* clientdata, void* calldata);
00200   
00201   // The vtkXMLDataParser instance used to hide XML reading details.
00202   vtkXMLDataParser* XMLParser;
00203 
00204   // The FieldData element representation.
00205   vtkXMLDataElement* FieldDataElement;
00206 
00207   // The input file's name.
00208   char* FileName;
00209   
00210   // The stream used to read the input.
00211   istream* Stream;
00212   
00213   // The array selections.
00214   vtkDataArraySelection* PointDataArraySelection;
00215   vtkDataArraySelection* CellDataArraySelection;
00216   
00217   // The observer to modify this object when the array selections are
00218   // modified.
00219   vtkCallbackCommand* SelectionObserver;
00220   
00221   // Whether there was an error reading the file in RequestInformation.
00222   int InformationError;
00223   
00224   // Whether there was an error reading the file in RequestData.
00225   int DataError;
00226   
00227   // incrementally fine-tuned progress updates.
00228   virtual void GetProgressRange(float* range);
00229   virtual void SetProgressRange(float* range, int curStep, int numSteps);
00230   virtual void SetProgressRange(float* range, int curStep, float* fractions);
00231   virtual void UpdateProgressDiscrete(float progress);
00232   float ProgressRange[2];
00233   
00234   virtual int RequestData(vtkInformation *request,
00235                           vtkInformationVector **inputVector,
00236                           vtkInformationVector *outputVector);
00237   virtual int RequestDataObject(vtkInformation *vtkNotUsed(request),
00238                                 vtkInformationVector **vtkNotUsed(inputVector),
00239                                 vtkInformationVector *vtkNotUsed(outputVector))
00240     { return 1; }
00241   virtual int RequestInformation(vtkInformation *request,
00242                                  vtkInformationVector **inputVector,
00243                                  vtkInformationVector *outputVector);
00244   virtual int RequestUpdateExtentInformation
00245   (vtkInformation *request,
00246    vtkInformationVector **inputVector,
00247    vtkInformationVector *outputVector);
00248 
00249   vtkTimeStamp ReadMTime;
00250 
00251   // Whether there was an error reading the XML.
00252   int ReadError;
00253 
00254   // For structured data keep track of dimensions empty of cells.  For
00255   // unstructured data these are always zero.  This is used to support
00256   // 1-D and 2-D cell data.
00257   int AxesEmpty[3];
00258 
00259   // The timestep currently being read.
00260   int TimeStep;
00261   int CurrentTimeStep;
00262   int NumberOfTimeSteps;
00263   void SetNumberOfTimeSteps(int num);
00264   // buffer for reading timestep from the XML file the length is of 
00265   // NumberOfTimeSteps and therefore is always long enough
00266   int *TimeSteps; 
00267   // Store the range of time steps
00268   int TimeStepRange[2];
00269 
00270   // Now we need to save what was the last time read for each kind of 
00271   // data to avoid rereading it that is to say we need a var for 
00272   // e.g. PointData/CellData/Points/Cells...
00273   // See SubClass for details with member vars like PointsTimeStep/PointsOffset
00274 
00275   // Helper function useful to know if a timestep is found in an array of timestep
00276   static int IsTimeStepInArray(int timestep, int* timesteps, int length);
00277 
00278   vtkDataObject* GetCurrentOutput();
00279   vtkInformation* GetCurrentOutputInformation();
00280   
00281 private:
00282   // The stream used to read the input if it is in a file.
00283   ifstream* FileStream;  
00284   int TimeStepWasReadOnce;
00285 
00286   int FileMajorVersion;
00287   int FileMinorVersion;
00288   
00289   vtkDataObject* CurrentOutput;
00290   vtkInformation* CurrentOutputInformation;
00291   
00292 private:
00293   vtkXMLReader(const vtkXMLReader&);  // Not implemented.
00294   void operator=(const vtkXMLReader&);  // Not implemented.
00295 };
00296 
00297 #endif