VTK
dox/IO/vtkXMLWriter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkXMLWriter.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 =========================================================================*/
00024 #ifndef __vtkXMLWriter_h
00025 #define __vtkXMLWriter_h
00026 
00027 #include "vtkAlgorithm.h"
00028 
00029 class vtkAbstractArray;
00030 class vtkArrayIterator;
00031 //BTX
00032 template <class T> class vtkArrayIteratorTemplate;
00033 //ETX
00034 class vtkCellData;
00035 class vtkDataArray;
00036 class vtkDataCompressor;
00037 class vtkDataSet;
00038 class vtkDataSetAttributes;
00039 class vtkOutputStream;
00040 class vtkPointData;
00041 class vtkPoints;
00042 class vtkFieldData;
00043 //BTX
00044 class vtkStdString;
00045 class OffsetsManager;      // one per piece/per time
00046 class OffsetsManagerGroup; // array of OffsetsManager
00047 class OffsetsManagerArray; // array of OffsetsManagerGroup
00048 //ETX
00049 
00050 class VTK_IO_EXPORT vtkXMLWriter : public vtkAlgorithm
00051 {
00052 public:
00053   vtkTypeMacro(vtkXMLWriter,vtkAlgorithm);
00054   void PrintSelf(ostream& os, vtkIndent indent);
00055  
00056   //BTX
00058 
00059   enum { BigEndian, LittleEndian };
00060   //ETX
00062 
00063   //BTX
00065 
00069   typedef vtkIdType OffsetType;
00070   //ETX
00072 
00073   //BTX
00075 
00078   enum { Ascii, Binary, Appended };
00079   //ETX
00081   
00082   //BTX
00084 
00087   enum { Int32=32, Int64=64 };
00088   //ETX
00090   
00092 
00094   vtkSetMacro(ByteOrder, int);
00095   vtkGetMacro(ByteOrder, int);
00096   void SetByteOrderToBigEndian();
00097   void SetByteOrderToLittleEndian();
00099   
00101 
00103   virtual void SetIdType(int);
00104   vtkGetMacro(IdType, int);
00105   void SetIdTypeToInt32();
00106   void SetIdTypeToInt64();
00108   
00110 
00111   vtkSetStringMacro(FileName);
00112   vtkGetStringMacro(FileName);
00114   
00116 
00118   virtual void SetCompressor(vtkDataCompressor*);
00119   vtkGetObjectMacro(Compressor, vtkDataCompressor);
00121 
00122 //BTX
00123   enum CompressorType
00124     {
00125     NONE,
00126     ZLIB
00127     };
00128 //ETX
00129 
00131 
00132   void SetCompressorType(int compressorType);
00133   void SetCompressorTypeToNone()
00134     {
00135     this->SetCompressorType(NONE);
00136     }
00137   void SetCompressorTypeToZLib()
00138     {
00139     this->SetCompressorType(ZLIB);
00140     }
00142 
00144 
00148   virtual void SetBlockSize(unsigned int blockSize);
00149   vtkGetMacro(BlockSize, unsigned int);
00151   
00153 
00156   vtkSetMacro(DataMode, int);
00157   vtkGetMacro(DataMode, int);
00158   void SetDataModeToAscii();
00159   void SetDataModeToBinary();
00160   void SetDataModeToAppended();
00162   
00164 
00169   vtkSetMacro(EncodeAppendedData, int);
00170   vtkGetMacro(EncodeAppendedData, int);
00171   vtkBooleanMacro(EncodeAppendedData, int);
00173   
00175 
00177   void SetInput(vtkDataObject *);
00178   void SetInput(int, vtkDataObject*);
00179   vtkDataObject *GetInput(int port);
00180   vtkDataObject *GetInput() { return this->GetInput(0); };
00182 
00184   virtual const char* GetDefaultFileExtension()=0;
00185   
00187   int Write();
00188 
00189   // See the vtkAlgorithm for a description of what these do
00190   virtual int ProcessRequest(vtkInformation* request,
00191                              vtkInformationVector** inputVector,
00192                              vtkInformationVector* outputVector);
00193 
00195 
00196   vtkSetMacro(TimeStep, int);
00197   vtkGetMacro(TimeStep, int);
00199 
00201 
00202   vtkGetVector2Macro(TimeStepRange, int);
00203   vtkSetVector2Macro(TimeStepRange, int);
00205 
00207 
00208   vtkGetMacro(NumberOfTimeSteps,int);
00209   vtkSetMacro(NumberOfTimeSteps,int);
00211 
00213 
00214   void Start();
00215   void Stop();
00216   void WriteNextTime(double time);
00218 
00219 protected:
00220   vtkXMLWriter();
00221   ~vtkXMLWriter();
00222 
00223   virtual int RequestInformation(
00224                           vtkInformation* request,
00225                           vtkInformationVector** inputVector,
00226                           vtkInformationVector* outputVector);
00227   virtual int RequestData(vtkInformation* request,
00228                           vtkInformationVector** inputVector,
00229                           vtkInformationVector* outputVector);
00230 
00231   // The name of the output file.
00232   char* FileName;
00233   
00234   // The output stream to which the XML is written.
00235   ostream* Stream;  
00236   
00237   // The output byte order.
00238   int ByteOrder;
00239   
00240   // The output vtkIdType.
00241   int IdType;
00242   
00243   // The form of binary data to write.  Used by subclasses to choose
00244   // how to write data.
00245   int DataMode;
00246   
00247   // Whether to base64-encode the appended data section.
00248   int EncodeAppendedData;
00249   
00250   // The stream position at which appended data starts.
00251   OffsetType AppendedDataPosition;
00252 
00253   // appended data offsets for field data
00254   OffsetsManagerGroup *FieldDataOM;  //one per array
00255 
00256   //BTX
00257   // We need a 32 bit unsigned integer type for platform-independent
00258   // binary headers.  Note that this is duplicated in
00259   // vtkXMLDataParser.h.
00260 #if VTK_SIZEOF_SHORT == 4
00261   typedef unsigned short HeaderType;
00262 #elif VTK_SIZEOF_INT == 4
00263   typedef unsigned int HeaderType;
00264 #elif VTK_SIZEOF_LONG == 4
00265   typedef unsigned long HeaderType;
00266 #else
00267 # error "No native data type can represent an unsigned 32-bit integer."
00268 #endif
00269   //ETX
00270 
00271   //BTX
00272   // We need a 32 bit signed integer type to which vtkIdType will be
00273   // converted if Int32 is specified for the IdType parameter to this
00274   // writer.
00275 # if VTK_SIZEOF_SHORT == 4
00276   typedef short Int32IdType;
00277 # elif VTK_SIZEOF_INT == 4
00278   typedef int Int32IdType;
00279 # elif VTK_SIZEOF_LONG == 4
00280   typedef long Int32IdType;
00281 # else
00282 #  error "No native data type can represent a signed 32-bit integer."
00283 # endif  
00284   //ETX
00285   
00286   // Buffer for vtkIdType conversion.
00287   Int32IdType* Int32IdTypeBuffer;
00288   
00289   // The byte swapping buffer.
00290   unsigned char* ByteSwapBuffer;
00291   
00292   // Compression information.
00293   vtkDataCompressor* Compressor;
00294   unsigned int   BlockSize;  
00295   OffsetType  CompressionBlockNumber;
00296   HeaderType*    CompressionHeader;
00297   unsigned int   CompressionHeaderLength;
00298   OffsetType  CompressionHeaderPosition;
00299   
00300   // The output stream used to write binary and appended data.  May
00301   // transparently encode the data.
00302   vtkOutputStream* DataStream;
00303   
00304   // Allow subclasses to set the data stream.
00305   virtual void SetDataStream(vtkOutputStream*);
00306   vtkGetObjectMacro(DataStream, vtkOutputStream);  
00307   
00308   // Method to drive most of actual writing.
00309   virtual int WriteInternal();
00310   
00311   // Method defined by subclasses to write data.  Return 1 for
00312   // success, 0 for failure.
00313   virtual int WriteData() {return 1;};
00314   
00315   // Method defined by subclasses to specify the data set's type name.
00316   virtual const char* GetDataSetName()=0;
00317   
00318   // Methods to define the file's major and minor version numbers.
00319   virtual int GetDataSetMajorVersion();
00320   virtual int GetDataSetMinorVersion();
00321   
00322   // Utility methods for subclasses.
00323   vtkDataSet* GetInputAsDataSet();  
00324   int StartFile();
00325   virtual void WriteFileAttributes();
00326   int EndFile();
00327   void DeleteAFile();
00328   void DeleteAFile(const char* name);
00329 
00330   virtual int WritePrimaryElement(ostream &os, vtkIndent indent);
00331   virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent);
00332   void StartAppendedData();
00333   void EndAppendedData();
00334 
00335   // Write enough space to go back and write the given attribute with
00336   // at most "length" characters in the value.  Returns the stream
00337   // position at which attribute should be later written.  The default
00338   // length of 20 is enough for a 64-bit integer written in decimal or
00339   // a double-precision floating point value written to 13 digits of
00340   // precision (the other 7 come from a minus sign, decimal place, and
00341   // a big exponent like "e+300").
00342   OffsetType ReserveAttributeSpace(const char* attr, int length=20);
00343 
00344   OffsetType GetAppendedDataOffset();
00345   OffsetType WriteAppendedDataOffset(OffsetType streamPos,
00346                                         OffsetType &lastoffset,
00347                                         const char* attr=0);
00348   OffsetType ForwardAppendedDataOffset(OffsetType streamPos,
00349                                          OffsetType offset,
00350                                          const char* attr=0);
00351   OffsetType ForwardAppendedDataDouble(OffsetType streamPos,
00352                                           double value,
00353                                           const char* attr);
00354 
00355   int WriteScalarAttribute(const char* name, int data);
00356   int WriteScalarAttribute(const char* name, float data);
00357   int WriteScalarAttribute(const char* name, double data);
00358 #ifdef VTK_USE_64BIT_IDS
00359   int WriteScalarAttribute(const char* name, vtkIdType data);
00360 #endif
00361   
00362   int WriteVectorAttribute(const char* name, int length, int* data);
00363   int WriteVectorAttribute(const char* name, int length, float* data);
00364   int WriteVectorAttribute(const char* name, int length, double* data);
00365 #ifdef VTK_USE_64BIT_IDS
00366   int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
00367 #endif
00368   
00369   int WriteDataModeAttribute(const char* name);
00370   int WriteWordTypeAttribute(const char* name, int dataType);
00371   int WriteStringAttribute(const char* name, const char* value);
00372   
00373   void WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent,
00374     const char* alternateName, int writeNumTuples, int timestep);
00375   void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat);
00376   void WriteArrayInline(vtkAbstractArray* a, vtkIndent indent,
00377     const char* alternateName=0, int writeNumTuples=0);
00378   void WriteInlineData(vtkAbstractArray* a, vtkIndent indent);
00379   
00380   void WriteArrayAppended(vtkAbstractArray* a, vtkIndent indent,
00381     OffsetsManager &offs, const char* alternateName=0,  int writeNumTuples=0, 
00382     int timestep=0);
00383   int WriteAsciiData(vtkAbstractArray* a, vtkIndent indent);
00384   int WriteBinaryData(vtkAbstractArray* a);
00385   int WriteBinaryDataInternal(vtkAbstractArray* a, OffsetType data_size);
00386   void WriteArrayAppendedData(vtkAbstractArray* a, OffsetType pos, 
00387     OffsetType &lastoffset);
00388   
00389   // Methods for writing points, point data, and cell data.
00390   void WriteFieldData(vtkIndent indent);
00391   void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
00392   void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
00393   void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
00394   void WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent,
00395                               OffsetsManagerGroup *fdManager);
00396   void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep,
00397                                   OffsetsManagerGroup *fdManager);
00398   void  WritePointDataAppended(vtkPointData* pd, vtkIndent indent, 
00399                                OffsetsManagerGroup *pdManager);
00400   void WritePointDataAppendedData(vtkPointData* pd, int timestep,
00401                                   OffsetsManagerGroup *pdManager);
00402   void WriteCellDataAppended(vtkCellData* cd, vtkIndent indent, 
00403                              OffsetsManagerGroup *cdManager);
00404   void WriteCellDataAppendedData(vtkCellData* cd, int timestep,
00405                                  OffsetsManagerGroup *cdManager);
00406   void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
00407   void WritePointsAppended(vtkPoints* points, vtkIndent indent, OffsetsManager *manager);
00408   void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager *pdManager);
00409   void WritePointsInline(vtkPoints* points, vtkIndent indent);
00410   void WriteCoordinatesInline(vtkDataArray* xc, vtkDataArray* yc,
00411                               vtkDataArray* zc, vtkIndent indent);
00412   void WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc,
00413                                 vtkDataArray* zc, vtkIndent indent, 
00414                                 OffsetsManagerGroup *coordManager);
00415   void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc,
00416                                     vtkDataArray* zc, int timestep,
00417                                     OffsetsManagerGroup *coordManager);
00418   virtual vtkAbstractArray* CreateArrayForPoints(vtkAbstractArray* inArray);
00419   virtual vtkAbstractArray* CreateArrayForCells(vtkAbstractArray* inArray);
00420   virtual vtkDataArray* CreateExactCoordinates(vtkDataArray* inArray, int xyz);
00421   void WritePPointData(vtkPointData* pd, vtkIndent indent);
00422   void WritePCellData(vtkCellData* cd, vtkIndent indent);
00423   void WritePPoints(vtkPoints* points, vtkIndent indent);
00424   void WritePArray(vtkAbstractArray* a, vtkIndent indent, 
00425     const char* alternateName=0);
00426   void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc,
00427                          vtkDataArray* zc, vtkIndent indent);
00428   
00429   // Internal utility methods.
00430   int WriteBinaryDataInternal(void* data, OffsetType numWords, int wordType);
00431   int WriteBinaryDataBlock(unsigned char* in_data, OffsetType numWords, int wordType);
00432   void PerformByteSwap(void* data, OffsetType numWords, int wordSize);
00433   int CreateCompressionHeader(OffsetType size);
00434   int WriteCompressionBlock(unsigned char* data, OffsetType size);
00435   int WriteCompressionHeader();
00436   OffsetType GetWordTypeSize(int dataType);
00437   const char* GetWordTypeName(int dataType);
00438   OffsetType GetOutputWordTypeSize(int dataType);
00439   
00440   char** CreateStringArray(int numStrings);
00441   void DestroyStringArray(int numStrings, char** strings);
00442   
00443   // The current range over which progress is moving.  This allows for
00444   // incrementally fine-tuned progress updates.
00445   virtual void GetProgressRange(float* range);
00446   virtual void SetProgressRange(float* range, int curStep, int numSteps);
00447   virtual void SetProgressRange(float* range, int curStep, float* fractions);
00448   virtual void SetProgressPartial(float fraction);
00449   virtual void UpdateProgressDiscrete(float progress);
00450   float ProgressRange[2];
00451 
00452   ofstream* OutFile;
00453 
00454   int OpenFile();
00455   void CloseFile();
00456 
00457   // The timestep currently being written
00458   int TimeStep;
00459   int CurrentTimeIndex;
00460   int NumberOfTimeSteps;
00461   // Store the range of time steps
00462   int TimeStepRange[2];
00463 
00464   // Dummy boolean var to start/stop the continue executing:
00465   // when using the Start/Stop/WriteNextTime API
00466   int UserContinueExecuting; //can only be -1 = invalid, 0 = stop, 1 = start
00467 
00468   unsigned long *NumberOfTimeValues; //one per piece / per timestep
00469   //BTX
00470   friend class vtkXMLWriterHelper;
00471   //ETX
00472 
00473 private:
00474   vtkXMLWriter(const vtkXMLWriter&);  // Not implemented.
00475   void operator=(const vtkXMLWriter&);  // Not implemented.
00476 };
00477 
00478 #endif