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