Main Page | Directories | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

vtkXMLWriter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkXMLWriter.h,v $
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 =========================================================================*/
00035 #ifndef __vtkXMLWriter_h
00036 #define __vtkXMLWriter_h
00037 
00038 #include "vtkProcessObject.h"
00039 
00040 class vtkCellData;
00041 class vtkDataArray;
00042 class vtkDataCompressor;
00043 class vtkDataSet;
00044 class vtkDataSetAttributes;
00045 class vtkOutputStream;
00046 class vtkPointData;
00047 class vtkPoints;
00048 
00049 class VTK_IO_EXPORT vtkXMLWriter : public vtkProcessObject
00050 {
00051 public:
00052   vtkTypeRevisionMacro(vtkXMLWriter,vtkProcessObject);
00053   void PrintSelf(ostream& os, vtkIndent indent);
00054   
00055   //BTX
00057 
00058   enum { BigEndian, LittleEndian };
00059   //ETX
00061   
00062   //BTX
00064 
00067   enum { Ascii, Binary, Appended };
00068   //ETX
00070   
00071   //BTX
00073 
00076   enum { Int32=32, Int64=64 };
00077   //ETX
00079   
00081 
00083   vtkSetMacro(ByteOrder, int);
00084   vtkGetMacro(ByteOrder, int);
00085   void SetByteOrderToBigEndian();
00086   void SetByteOrderToLittleEndian();
00088   
00090 
00092   virtual void SetIdType(int);
00093   vtkGetMacro(IdType, int);
00094   void SetIdTypeToInt32();
00095   void SetIdTypeToInt64();
00097   
00099 
00100   vtkSetStringMacro(FileName);
00101   vtkGetStringMacro(FileName);
00103   
00105 
00107   virtual void SetCompressor(vtkDataCompressor*);
00108   vtkGetObjectMacro(Compressor, vtkDataCompressor);
00110   
00112 
00116   virtual void SetBlockSize(unsigned int blockSize);
00117   vtkGetMacro(BlockSize, unsigned int);
00119   
00121 
00124   vtkSetMacro(DataMode, int);
00125   vtkGetMacro(DataMode, int);
00126   void SetDataModeToAscii();
00127   void SetDataModeToBinary();
00128   void SetDataModeToAppended();
00130   
00132 
00137   vtkSetMacro(EncodeAppendedData, int);
00138   vtkGetMacro(EncodeAppendedData, int);
00139   vtkBooleanMacro(EncodeAppendedData, int);
00141   
00143   virtual const char* GetDefaultFileExtension()=0;
00144   
00146   int Write();
00147   
00148 protected:
00149   vtkXMLWriter();
00150   ~vtkXMLWriter();
00151   
00152   // The name of the output file.
00153   char* FileName;
00154   
00155   // The output stream to which the XML is written.
00156   ostream* Stream;  
00157   
00158   // The output byte order.
00159   int ByteOrder;
00160   
00161   // The output vtkIdType.
00162   int IdType;
00163   
00164   // The form of binary data to write.  Used by subclasses to choose
00165   // how to write data.
00166   int DataMode;
00167   
00168   // Whether to base64-encode the appended data section.
00169   int EncodeAppendedData;
00170   
00171   // The stream position at which appended data starts.
00172   unsigned long AppendedDataPosition;
00173 
00174   //BTX
00175   // We need a 32 bit unsigned integer type for platform-independent
00176   // binary headers.  Note that this is duplicated in
00177   // vtkXMLDataParser.h.
00178 #if VTK_SIZEOF_SHORT == 4
00179   typedef unsigned short HeaderType;
00180 #elif VTK_SIZEOF_INT == 4
00181   typedef unsigned int HeaderType;
00182 #elif VTK_SIZEOF_LONG == 4
00183   typedef unsigned long HeaderType;
00184 #else
00185 # error "No native data type can represent an unsigned 32-bit integer."
00186 #endif
00187   //ETX
00188 
00189   //BTX
00190   // We need a 32 bit signed integer type to which vtkIdType will be
00191   // converted if Int32 is specified for the IdType parameter to this
00192   // writer.
00193 # if VTK_SIZEOF_SHORT == 4
00194   typedef short Int32IdType;
00195 # elif VTK_SIZEOF_INT == 4
00196   typedef int Int32IdType;
00197 # elif VTK_SIZEOF_LONG == 4
00198   typedef long Int32IdType;
00199 # else
00200 #  error "No native data type can represent a signed 32-bit integer."
00201 # endif  
00202   //ETX
00203   
00204   // Buffer for vtkIdType conversion.
00205   Int32IdType* Int32IdTypeBuffer;
00206   
00207   // The byte swapping buffer.
00208   unsigned char* ByteSwapBuffer;
00209   
00210   // Compression information.
00211   vtkDataCompressor* Compressor;
00212   unsigned int   BlockSize;  
00213   unsigned long  CompressionBlockNumber;
00214   HeaderType*    CompressionHeader;
00215   unsigned int   CompressionHeaderLength;
00216   unsigned long  CompressionHeaderPosition;
00217   
00218   // The output stream used to write binary and appended data.  May
00219   // transparently encode the data.
00220   vtkOutputStream* DataStream;
00221   
00222   // Allow subclasses to set the data stream.
00223   virtual void SetDataStream(vtkOutputStream*);
00224   vtkGetObjectMacro(DataStream, vtkOutputStream);  
00225   
00226   // Method to drive most of actual writing.
00227   virtual int WriteInternal();
00228   
00229   // Method defined by subclasses to write data.  Return 1 for
00230   // success, 0 for failure.
00231   virtual int WriteData()=0;
00232   
00233   // Method defined by subclasses to specify the data set's type name.
00234   virtual const char* GetDataSetName()=0;
00235   
00236   // Methods to define the file's major and minor version numbers.
00237   virtual int GetDataSetMajorVersion();
00238   virtual int GetDataSetMinorVersion();
00239   
00240   // Utility methods for subclasses.
00241   vtkDataSet* GetInputAsDataSet();  
00242   int StartFile();
00243   virtual void WriteFileAttributes();
00244   int EndFile();
00245   void DeleteFile();
00246   void DeleteFile(const char* name);
00247 
00248   void StartAppendedData();
00249   void EndAppendedData();
00250   unsigned long ReserveAttributeSpace(const char* attr=0);
00251   unsigned long GetAppendedDataOffset();
00252   unsigned long WriteAppendedDataOffset(unsigned long streamPos,
00253                                         const char* attr=0);
00254   int WriteBinaryData(void* data, int numWords, int wordType);
00255   
00256   int WriteBinaryData(char* data)
00257     { return this->WriteBinaryData(data, static_cast<int>(strlen(data)),
00258                                    VTK_CHAR); }
00259   int WriteBinaryData(char* data, int numWords)
00260     { return this->WriteBinaryData(data, numWords, VTK_CHAR); }
00261   int WriteBinaryData(unsigned char* data, int numWords)
00262     { return this->WriteBinaryData(data, numWords, VTK_UNSIGNED_CHAR); }
00263   int WriteBinaryData(short* data, int numWords)
00264     { return this->WriteBinaryData(data, numWords, VTK_SHORT); }
00265   int WriteBinaryData(unsigned short* data, int numWords)
00266     { return this->WriteBinaryData(data, numWords, VTK_UNSIGNED_SHORT); }
00267   int WriteBinaryData(int* data, int numWords)
00268     { return this->WriteBinaryData(data, numWords, VTK_INT); }
00269   int WriteBinaryData(unsigned int* data, int numWords)
00270     { return this->WriteBinaryData(data, numWords, VTK_UNSIGNED_INT); }
00271   
00272   int WriteAsciiData(void* data, int numWords, int wordType, vtkIndent indent);
00273   
00274   int WriteAsciiData(char* data, vtkIndent indent)
00275     { return this->WriteAsciiData(data, static_cast<int>(strlen(data)),
00276                                   VTK_CHAR, indent); }
00277   int WriteAsciiData(char* data, int numWords, vtkIndent indent)
00278     { return this->WriteAsciiData(data, numWords, VTK_CHAR, indent); }
00279   int WriteAsciiData(unsigned char* data, int numWords, vtkIndent indent)
00280     { return this->WriteAsciiData(data, numWords, VTK_UNSIGNED_CHAR, indent); }
00281   int WriteAsciiData(short* data, int numWords, vtkIndent indent)
00282     { return this->WriteAsciiData(data, numWords, VTK_SHORT, indent); }
00283   int WriteAsciiData(unsigned short* data, int numWords, vtkIndent indent)
00284     { return this->WriteAsciiData(data, numWords, VTK_UNSIGNED_SHORT, indent); }
00285   int WriteAsciiData(int* data, int numWords, vtkIndent indent)
00286     { return this->WriteAsciiData(data, numWords, VTK_INT, indent); }
00287   int WriteAsciiData(unsigned int* data, int numWords, vtkIndent indent)
00288     { return this->WriteAsciiData(data, numWords, VTK_UNSIGNED_INT, indent); }
00289   
00290   int WriteScalarAttribute(const char* name, int data);
00291   int WriteScalarAttribute(const char* name, float data);
00292 #ifdef VTK_ID_TYPE_IS_NOT_BASIC_TYPE
00293   int WriteScalarAttribute(const char* name, vtkIdType data);
00294 #endif
00295   
00296   int WriteVectorAttribute(const char* name, int length, int* data);
00297   int WriteVectorAttribute(const char* name, int length, float* data);
00298   int WriteVectorAttribute(const char* name, int length, double* data);
00299 #ifdef VTK_ID_TYPE_IS_NOT_BASIC_TYPE
00300   int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
00301 #endif
00302   
00303   int WriteDataModeAttribute(const char* name);
00304   int WriteWordTypeAttribute(const char* name, int dataType);
00305   int WriteStringAttribute(const char* name, const char* value);
00306   
00307   unsigned long WriteDataArrayAppended(vtkDataArray* a, vtkIndent indent,
00308                                        const char* alternateName=0);
00309   void WriteDataArrayAppendedData(vtkDataArray* a, unsigned long pos);
00310   void WriteDataArrayInline(vtkDataArray* a, vtkIndent indent,
00311                             const char* alternateName=0);
00312   void WriteInlineData(void* data, int numWords, int wordType,
00313                        vtkIndent indent);
00314   
00315   // Methods for writing points, point data, and cell data.
00316   void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
00317   void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
00318   unsigned long* WritePointDataAppended(vtkPointData* pd, vtkIndent indent);
00319   void WritePointDataAppendedData(vtkPointData* pd, unsigned long* pdPositions);
00320   unsigned long* WriteCellDataAppended(vtkCellData* cd, vtkIndent indent);
00321   void WriteCellDataAppendedData(vtkCellData* cd, unsigned long* cdPositions);
00322   void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
00323   unsigned long WritePointsAppended(vtkPoints* points, vtkIndent indent);
00324   void WritePointsAppendedData(vtkPoints* points, unsigned long pointsPosition);
00325   void WritePointsInline(vtkPoints* points, vtkIndent indent);
00326   void WriteCoordinatesInline(vtkDataArray* xc, vtkDataArray* yc,
00327                               vtkDataArray* zc, vtkIndent indent);
00328   unsigned long* WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc,
00329                                           vtkDataArray* zc, vtkIndent indent);
00330   void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc,
00331                                     vtkDataArray* zc,
00332                                     unsigned long* cPositions);
00333   virtual vtkDataArray* CreateArrayForPoints(vtkDataArray* inArray);
00334   virtual vtkDataArray* CreateArrayForCells(vtkDataArray* inArray);
00335   virtual vtkDataArray* CreateExactCoordinates(vtkDataArray* inArray, int xyz);
00336   void WritePPointData(vtkPointData* pd, vtkIndent indent);
00337   void WritePCellData(vtkCellData* cd, vtkIndent indent);
00338   void WritePPoints(vtkPoints* points, vtkIndent indent);
00339   void WritePDataArray(vtkDataArray* a, vtkIndent indent,
00340                        const char* alternateName=0);
00341   void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc,
00342                          vtkDataArray* zc, vtkIndent indent);
00343   
00344   // Internal utility methods.
00345   int WriteBinaryDataInternal(void* data, int numWords, int wordType);
00346   int WriteBinaryDataBlock(unsigned char* in_data, int numWords, int wordType);
00347   void PerformByteSwap(void* data, int numWords, int wordSize);
00348   int CreateCompressionHeader(unsigned long size);
00349   int WriteCompressionBlock(unsigned char* data, unsigned long size);
00350   int WriteCompressionHeader();
00351   unsigned long GetWordTypeSize(int dataType);
00352   const char* GetWordTypeName(int dataType);
00353   unsigned long GetOutputWordTypeSize(int dataType);
00354   
00355   char** CreateStringArray(int numStrings);
00356   void DestroyStringArray(int numStrings, char** strings);
00357   
00358   // The current range over which progress is moving.  This allows for
00359   // incrementally fine-tuned progress updates.
00360   virtual void GetProgressRange(float* range);
00361   virtual void SetProgressRange(float* range, int curStep, int numSteps);
00362   virtual void SetProgressRange(float* range, int curStep, float* fractions);
00363   virtual void SetProgressPartial(float fraction);
00364   virtual void UpdateProgressDiscrete(float progress);
00365   float ProgressRange[2];
00366   
00367 private:
00368   vtkXMLWriter(const vtkXMLWriter&);  // Not implemented.
00369   void operator=(const vtkXMLWriter&);  // Not implemented.
00370 };
00371 
00372 #endif