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