Home Docs Forums Bugzilla LXR Doxygen CVS Bonsai
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

vtkDataReader.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkDataReader.h,v $
00005   Language:  C++
00006   Date:      $Date: 2001/11/30 12:49:50 $
00007   Version:   $Revision: 1.65 $
00008 
00009 
00010 Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen 
00011 All rights reserved.
00012 
00013 Redistribution and use in source and binary forms, with or without
00014 modification, are permitted provided that the following conditions are met:
00015 
00016  * Redistributions of source code must retain the above copyright notice,
00017    this list of conditions and the following disclaimer.
00018 
00019  * Redistributions in binary form must reproduce the above copyright notice,
00020    this list of conditions and the following disclaimer in the documentation
00021    and/or other materials provided with the distribution.
00022 
00023  * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
00024    of any contributors may be used to endorse or promote products derived
00025    from this software without specific prior written permission.
00026 
00027  * Modified source versions must be plainly marked as such, and must not be
00028    misrepresented as being the original software.
00029 
00030 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00031 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
00034 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00036 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00037 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00038 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00039 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00040 
00041 =========================================================================*/
00042 // .NAME vtkDataReader - helper superclass for objects that read vtk data files
00043 // .SECTION Description
00044 // vtkDataReader is a helper superclass that reads the vtk data file header,
00045 // dataset type, and attribute data (point and cell attributes such as
00046 // scalars, vectors, normals, etc.) from a vtk data file.  See text for
00047 // the format of the various vtk file types.
00048 //
00049 // .SECTION See Also
00050 // vtkPolyDataReader vtkStructuredPointsReader vtkStructuredGridReader
00051 // vtkUnsutructuredGridReader vtkRectilinearGridReader
00052 
00053 #ifndef __vtkDataReader_h
00054 #define __vtkDataReader_h
00055 
00056 #include "vtkSource.h"
00057 #include "vtkDataSetAttributes.h"
00058 
00059 #define VTK_ASCII 1
00060 #define VTK_BINARY 2
00061 
00062 class vtkDataSet;
00063 class vtkPointSet;
00064 class vtkRectilinearGrid;
00065 
00066 class VTK_IO_EXPORT vtkDataReader : public vtkSource
00067 {
00068 public:
00069   static vtkDataReader *New();
00070   vtkTypeMacro(vtkDataReader,vtkObject);
00071   void PrintSelf(ostream& os, vtkIndent indent);
00072 
00073   // Description:
00074   // Specify file name of vtk data file to read.
00075   vtkSetStringMacro(FileName);
00076   vtkGetStringMacro(FileName);
00077 
00078   // Description:
00079   // Is the file a valid vtk file of the passed dataset type ?
00080   // The dataset type is passed as a lower case string.
00081   int IsFileValid(const char *dstype);
00082   int IsFileStructuredPoints() {
00083     return this->IsFileValid("structured_points");};  
00084   int IsFilePolyData() {
00085     return this->IsFileValid("polydata");};  
00086   int IsFileStructuredGrid() {
00087     return this->IsFileValid("structured_grid");};  
00088   int IsFileUnstructuredGrid() {
00089     return this->IsFileValid("unstructured_grid");};  
00090   int IsFileRectilinearGrid() {
00091     return this->IsFileValid("rectilinear_grid");};
00092   
00093   // Description:
00094   // Specify the InputString for use when reading from a character array.
00095   // Optionally include the length for binary strings.
00096   void SetInputString(const char *in);
00097   vtkGetStringMacro(InputString);
00098   void SetInputString(const char *in, int len);
00099   vtkGetMacro(InputStringLength, int);
00100   void SetBinaryInputString(const char *, int len);
00101     
00102   // Description:
00103   // Get the header from the vtk data file.
00104   vtkGetStringMacro(Header);
00105 
00106   // Description:
00107   // Enable reading from an InputString instead of the default, a file.
00108   vtkSetMacro(ReadFromInputString,int);
00109   vtkGetMacro(ReadFromInputString,int);
00110   vtkBooleanMacro(ReadFromInputString,int);
00111 
00112   // Description:
00113   // Get the type of file (ASCII or BINARY). Returned value only valid
00114   // after file has been read.
00115   vtkGetMacro(FileType,int);
00116 
00117   // Description:
00118   // How many attributes of various types are in this file? This 
00119   // requires reading the file, so the filename must be set prior 
00120   // to invoking this operation. (Note: file characteristics are
00121   // cached, so only a single read is necessary to return file
00122   // characteristics.)
00123   int GetNumberOfScalarsInFile()
00124     {this->CharacterizeFile(); return this->NumberOfScalarsInFile;}
00125   int GetNumberOfVectorsInFile()
00126     {this->CharacterizeFile(); return this->NumberOfVectorsInFile;}
00127   int GetNumberOfTensorsInFile()
00128     {this->CharacterizeFile(); return this->NumberOfTensorsInFile;}
00129   int GetNumberOfNormalsInFile()
00130     {this->CharacterizeFile(); return this->NumberOfNormalsInFile;}
00131   int GetNumberOfTCoordsInFile()
00132     {this->CharacterizeFile(); return this->NumberOfTCoordsInFile;}
00133   int GetNumberOfFieldDataInFile()
00134     {this->CharacterizeFile(); return this->NumberOfFieldDataInFile;}
00135   
00136   // Description:
00137   // What is the name of the ith attribute of a certain type
00138   // in this file? This requires reading the file, so the filename 
00139   // must be set prior to invoking this operation.
00140   const char *GetScalarsNameInFile(int i);
00141   const char *GetVectorsNameInFile(int i);
00142   const char *GetTensorsNameInFile(int i);
00143   const char *GetNormalsNameInFile(int i);
00144   const char *GetTCoordsNameInFile(int i);
00145   const char *GetFieldDataNameInFile(int i);
00146   
00147   // Description:
00148   // Set the name of the scalar data to extract. If not specified, first 
00149   // scalar data encountered is extracted.
00150   vtkSetStringMacro(ScalarsName);
00151   vtkGetStringMacro(ScalarsName);
00152 
00153   // Description:
00154   // Set the name of the vector data to extract. If not specified, first 
00155   // vector data encountered is extracted.
00156   vtkSetStringMacro(VectorsName);
00157   vtkGetStringMacro(VectorsName);
00158 
00159   // Description:
00160   // Set the name of the tensor data to extract. If not specified, first 
00161   // tensor data encountered is extracted.
00162   vtkSetStringMacro(TensorsName);
00163   vtkGetStringMacro(TensorsName);
00164 
00165   // Description:
00166   // Set the name of the normal data to extract. If not specified, first 
00167   // normal data encountered is extracted.
00168   vtkSetStringMacro(NormalsName);
00169   vtkGetStringMacro(NormalsName);
00170 
00171   // Description:
00172   // Set the name of the texture coordinate data to extract. If not specified,
00173   // first texture coordinate data encountered is extracted.
00174   vtkSetStringMacro(TCoordsName);
00175   vtkGetStringMacro(TCoordsName);
00176 
00177   // Description:
00178   // Set the name of the lookup table data to extract. If not specified, uses 
00179   // lookup table named by scalar. Otherwise, this specification supersedes.
00180   vtkSetStringMacro(LookupTableName);
00181   vtkGetStringMacro(LookupTableName);
00182 
00183   // Description:
00184   // Set the name of the field data to extract. If not specified, uses 
00185   // first field data encountered in file.
00186   vtkSetStringMacro(FieldDataName);
00187   vtkGetStringMacro(FieldDataName);
00188 
00189   // Description:
00190   // Open a vtk data file. Returns zero if error.
00191   int OpenVTKFile();
00192 
00193   // Description:
00194   // Read the header of a vtk data file. Returns 0 if error.
00195   int ReadHeader();
00196 
00197   // Description:
00198   // Read the cell data of a vtk data file. The number of cells (from the 
00199   // dataset) must match the number of cells defined in cell attributes (unless
00200   // no geometry was defined).
00201   int ReadCellData(vtkDataSet *ds, int numCells);
00202 
00203   // Description:
00204   // Read the point data of a vtk data file. The number of points (from the
00205   // dataset) must match the number of points defined in point attributes
00206   // (unless no geometry was defined).
00207   int ReadPointData(vtkDataSet *ds, int numPts);
00208 
00209   // Description:
00210   // Read point coordinates. Return 0 if error.
00211   int ReadPoints(vtkPointSet *ps, int numPts);
00212 
00213   // Description:
00214   // Read a bunch of "cells". Return 0 if error.
00215   int ReadCells(int size, int *data);
00216 
00217   // Description:
00218   // Read a piece of the cells (for streaming compliance)
00219   int ReadCells(int size, int *data, int skip1, int read2, int skip3);
00220 
00221   // Description:
00222   // Read the coordinates for a rectilinear grid. The axes parameter specifies
00223   // which coordinate axes (0,1,2) is being read.
00224   int ReadCoordinates(vtkRectilinearGrid *rg, int axes, int numCoords);
00225 
00226   // Description:
00227   // Helper functions for reading data.
00228   vtkDataArray *ReadArray(const char *dataType, int numTuples, int numComp);
00229   vtkFieldData *ReadFieldData();
00230 
00231   // Description:
00232   // Internal function to read in a value.  Returns zero if there was an
00233   // error.
00234   int Read(char *);
00235   int Read(unsigned char *);
00236   int Read(short *);
00237   int Read(unsigned short *);
00238   int Read(int *);
00239   int Read(unsigned int *);
00240   int Read(long *);
00241   int Read(unsigned long *);
00242   int Read(float *);
00243   int Read(double *);
00244 
00245   // Description:
00246   // Close the vtk file.
00247   void CloseVTKFile();
00248 
00249 //BTX
00250   // Description:
00251   // Internal function to read in a line up to 256 characters.
00252   // Returns zero if there was an error.
00253   int ReadLine(char result[256]);
00254 
00255   // Description:
00256   // Internal function to read in a string up to 256 characters.
00257   // Returns zero if there was an error.
00258   int ReadString(char result[256]);
00259 
00260   // Description:
00261   // Helper method for reading in data.
00262   char *LowerCase(char *str, const size_t len=256);
00263   
00264   // Description:
00265   // Return the istream being used to read in the data.
00266   istream *GetIStream() {return this->IS;};
00267 //ETX
00268 
00269 protected:
00270   vtkDataReader();
00271   ~vtkDataReader();
00272 
00273   char *FileName;
00274   int FileType;
00275   istream *IS;
00276 
00277   char *ScalarsName;
00278   char *VectorsName;
00279   char *TensorsName;
00280   char *TCoordsName;
00281   char *NormalsName;
00282   char *LookupTableName;
00283   char *FieldDataName;
00284   char *ScalarLut;
00285 
00286   int ReadFromInputString;
00287   char *InputString;
00288   int InputStringLength;
00289   int InputStringPos;
00290 
00291   vtkSetStringMacro(ScalarLut);
00292   vtkGetStringMacro(ScalarLut);
00293 
00294   char *Header;
00295 
00296   int ReadScalarData(vtkDataSetAttributes *a, int num);
00297   int ReadVectorData(vtkDataSetAttributes *a, int num);
00298   int ReadNormalData(vtkDataSetAttributes *a, int num);
00299   int ReadTensorData(vtkDataSetAttributes *a, int num);
00300   int ReadCoScalarData(vtkDataSetAttributes *a, int num);
00301   int ReadLutData(vtkDataSetAttributes *a);
00302   int ReadTCoordsData(vtkDataSetAttributes *a, int num);
00303 
00304   int ReadDataSetData(vtkDataSet *ds);
00305 
00306   // This supports getting additional information from vtk files
00307   int  NumberOfScalarsInFile;
00308   char **ScalarsNameInFile;
00309   int ScalarsNameAllocSize;
00310   int  NumberOfVectorsInFile;
00311   char **VectorsNameInFile;
00312   int VectorsNameAllocSize;
00313   int  NumberOfTensorsInFile;
00314   char **TensorsNameInFile;
00315   int TensorsNameAllocSize;
00316   int  NumberOfTCoordsInFile;
00317   char **TCoordsNameInFile;
00318   int TCoordsNameAllocSize;
00319   int  NumberOfNormalsInFile;
00320   char **NormalsNameInFile;
00321   int NormalsNameAllocSize;
00322   int  NumberOfFieldDataInFile;
00323   char **FieldDataNameInFile;
00324   int FieldDataNameAllocSize;
00325   vtkTimeStamp CharacteristicsTime;
00326 
00327   void InitializeCharacteristics();
00328   int CharacterizeFile(); //read entire file, storing important characteristics
00329   void CheckFor(const char* name, char *line, int &num, char** &array, 
00330                 int& allocSize);
00331 
00332 private:
00333   vtkDataReader(const vtkDataReader&);  // Not implemented.
00334   void operator=(const vtkDataReader&);  // Not implemented.
00335 };
00336 
00337 #endif
00338 
00339