VTK
vtkXMLWriter.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkXMLWriter.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
24 #ifndef __vtkXMLWriter_h
25 #define __vtkXMLWriter_h
26 
27 #include "vtkAlgorithm.h"
28 
29 class vtkAbstractArray;
30 class vtkArrayIterator;
31 //BTX
32 template <class T> class vtkArrayIteratorTemplate;
33 //ETX
34 class vtkCellData;
35 class vtkDataArray;
36 class vtkDataCompressor;
37 class vtkDataSet;
39 class vtkOutputStream;
40 class vtkPointData;
41 class vtkPoints;
42 class vtkFieldData;
43 //BTX
44 class vtkStdString;
45 class OffsetsManager; // one per piece/per time
46 class OffsetsManagerGroup; // array of OffsetsManager
47 class OffsetsManagerArray; // array of OffsetsManagerGroup
48 //ETX
49 
51 {
52 public:
53  vtkTypeMacro(vtkXMLWriter,vtkAlgorithm);
54  void PrintSelf(ostream& os, vtkIndent indent);
55 
56  //BTX
58 
59  enum { BigEndian, LittleEndian };
60  //ETX
62 
63  //BTX
65 
70  //ETX
72 
73  //BTX
75 
78  enum { Ascii, Binary, Appended };
79  //ETX
81 
82  //BTX
84 
87  enum { Int32=32, Int64=64 };
88  //ETX
90 
92 
94  vtkSetMacro(ByteOrder, int);
95  vtkGetMacro(ByteOrder, int);
96  void SetByteOrderToBigEndian();
97  void SetByteOrderToLittleEndian();
99 
101 
103  virtual void SetIdType(int);
104  vtkGetMacro(IdType, int);
105  void SetIdTypeToInt32();
106  void SetIdTypeToInt64();
108 
110 
111  vtkSetStringMacro(FileName);
112  vtkGetStringMacro(FileName);
114 
116 
118  virtual void SetCompressor(vtkDataCompressor*);
119  vtkGetObjectMacro(Compressor, vtkDataCompressor);
121 
122 //BTX
124  {
126  ZLIB
127  };
128 //ETX
129 
131 
132  void SetCompressorType(int compressorType);
134  {
135  this->SetCompressorType(NONE);
136  }
138  {
139  this->SetCompressorType(ZLIB);
140  }
142 
144 
148  virtual void SetBlockSize(unsigned int blockSize);
149  vtkGetMacro(BlockSize, unsigned int);
151 
153 
156  vtkSetMacro(DataMode, int);
157  vtkGetMacro(DataMode, int);
158  void SetDataModeToAscii();
159  void SetDataModeToBinary();
160  void SetDataModeToAppended();
162 
164 
169  vtkSetMacro(EncodeAppendedData, int);
170  vtkGetMacro(EncodeAppendedData, int);
171  vtkBooleanMacro(EncodeAppendedData, int);
173 
175 
177  void SetInput(vtkDataObject *);
178  void SetInput(int, vtkDataObject*);
179  vtkDataObject *GetInput(int port);
180  vtkDataObject *GetInput() { return this->GetInput(0); };
182 
184  virtual const char* GetDefaultFileExtension()=0;
185 
187  int Write();
188 
189  // See the vtkAlgorithm for a description of what these do
190  virtual int ProcessRequest(vtkInformation* request,
191  vtkInformationVector** inputVector,
192  vtkInformationVector* outputVector);
193 
195 
196  vtkSetMacro(TimeStep, int);
197  vtkGetMacro(TimeStep, int);
199 
201 
202  vtkGetVector2Macro(TimeStepRange, int);
203  vtkSetVector2Macro(TimeStepRange, int);
205 
207 
208  vtkGetMacro(NumberOfTimeSteps,int);
209  vtkSetMacro(NumberOfTimeSteps,int);
211 
213 
214  void Start();
215  void Stop();
216  void WriteNextTime(double time);
218 
219 protected:
220  vtkXMLWriter();
221  ~vtkXMLWriter();
222 
223  virtual int RequestInformation(
224  vtkInformation* request,
225  vtkInformationVector** inputVector,
226  vtkInformationVector* outputVector);
227  virtual int RequestData(vtkInformation* request,
228  vtkInformationVector** inputVector,
229  vtkInformationVector* outputVector);
230 
231  // The name of the output file.
232  char* FileName;
233 
234  // The output stream to which the XML is written.
235  ostream* Stream;
236 
237  // The output byte order.
239 
240  // The output vtkIdType.
241  int IdType;
242 
243  // The form of binary data to write. Used by subclasses to choose
244  // how to write data.
245  int DataMode;
246 
247  // Whether to base64-encode the appended data section.
249 
250  // The stream position at which appended data starts.
252 
253  // appended data offsets for field data
255 
256  //BTX
257  // We need a 32 bit unsigned integer type for platform-independent
258  // binary headers. Note that this is duplicated in
259  // vtkXMLDataParser.h.
260 #if VTK_SIZEOF_SHORT == 4
261  typedef unsigned short HeaderType;
262 #elif VTK_SIZEOF_INT == 4
263  typedef unsigned int HeaderType;
264 #elif VTK_SIZEOF_LONG == 4
265  typedef unsigned long HeaderType;
266 #else
267 # error "No native data type can represent an unsigned 32-bit integer."
268 #endif
269  //ETX
270 
271  //BTX
272  // We need a 32 bit signed integer type to which vtkIdType will be
273  // converted if Int32 is specified for the IdType parameter to this
274  // writer.
275 # if VTK_SIZEOF_SHORT == 4
276  typedef short Int32IdType;
277 # elif VTK_SIZEOF_INT == 4
278  typedef int Int32IdType;
279 # elif VTK_SIZEOF_LONG == 4
280  typedef long Int32IdType;
281 # else
282 # error "No native data type can represent a signed 32-bit integer."
283 # endif
284  //ETX
285 
286  // Buffer for vtkIdType conversion.
287  Int32IdType* Int32IdTypeBuffer;
288 
289  // The byte swapping buffer.
290  unsigned char* ByteSwapBuffer;
291 
292  // Compression information.
294  unsigned int BlockSize;
296  HeaderType* CompressionHeader;
299 
300  // The output stream used to write binary and appended data. May
301  // transparently encode the data.
303 
304  // Allow subclasses to set the data stream.
305  virtual void SetDataStream(vtkOutputStream*);
306  vtkGetObjectMacro(DataStream, vtkOutputStream);
307 
308  // Method to drive most of actual writing.
309  virtual int WriteInternal();
310 
311  // Method defined by subclasses to write data. Return 1 for
312  // success, 0 for failure.
313  virtual int WriteData() {return 1;};
314 
315  // Method defined by subclasses to specify the data set's type name.
316  virtual const char* GetDataSetName()=0;
317 
318  // Methods to define the file's major and minor version numbers.
319  virtual int GetDataSetMajorVersion();
320  virtual int GetDataSetMinorVersion();
321 
322  // Utility methods for subclasses.
323  vtkDataSet* GetInputAsDataSet();
324  int StartFile();
325  virtual void WriteFileAttributes();
326  int EndFile();
327  void DeleteAFile();
328  void DeleteAFile(const char* name);
329 
330  virtual int WritePrimaryElement(ostream &os, vtkIndent indent);
331  virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent);
332  void StartAppendedData();
333  void EndAppendedData();
334 
335  // Write enough space to go back and write the given attribute with
336  // at most "length" characters in the value. Returns the stream
337  // position at which attribute should be later written. The default
338  // length of 20 is enough for a 64-bit integer written in decimal or
339  // a double-precision floating point value written to 13 digits of
340  // precision (the other 7 come from a minus sign, decimal place, and
341  // a big exponent like "e+300").
342  OffsetType ReserveAttributeSpace(const char* attr, int length=20);
343 
344  OffsetType GetAppendedDataOffset();
345  OffsetType WriteAppendedDataOffset(OffsetType streamPos,
346  OffsetType &lastoffset,
347  const char* attr=0);
348  OffsetType ForwardAppendedDataOffset(OffsetType streamPos,
349  OffsetType offset,
350  const char* attr=0);
351  OffsetType ForwardAppendedDataDouble(OffsetType streamPos,
352  double value,
353  const char* attr);
354 
355  int WriteScalarAttribute(const char* name, int data);
356  int WriteScalarAttribute(const char* name, float data);
357  int WriteScalarAttribute(const char* name, double data);
358 #ifdef VTK_USE_64BIT_IDS
359  int WriteScalarAttribute(const char* name, vtkIdType data);
360 #endif
361 
362  int WriteVectorAttribute(const char* name, int length, int* data);
363  int WriteVectorAttribute(const char* name, int length, float* data);
364  int WriteVectorAttribute(const char* name, int length, double* data);
365 #ifdef VTK_USE_64BIT_IDS
366  int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
367 #endif
368 
369  int WriteDataModeAttribute(const char* name);
370  int WriteWordTypeAttribute(const char* name, int dataType);
371  int WriteStringAttribute(const char* name, const char* value);
372 
373  void WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent,
374  const char* alternateName, int writeNumTuples, int timestep);
375  void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat);
376  void WriteArrayInline(vtkAbstractArray* a, vtkIndent indent,
377  const char* alternateName=0, int writeNumTuples=0);
378  void WriteInlineData(vtkAbstractArray* a, vtkIndent indent);
379 
380  void WriteArrayAppended(vtkAbstractArray* a, vtkIndent indent,
381  OffsetsManager &offs, const char* alternateName=0, int writeNumTuples=0,
382  int timestep=0);
383  int WriteAsciiData(vtkAbstractArray* a, vtkIndent indent);
384  int WriteBinaryData(vtkAbstractArray* a);
385  int WriteBinaryDataInternal(vtkAbstractArray* a, OffsetType data_size);
386  void WriteArrayAppendedData(vtkAbstractArray* a, OffsetType pos,
387  OffsetType &lastoffset);
388 
389  // Methods for writing points, point data, and cell data.
390  void WriteFieldData(vtkIndent indent);
391  void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
392  void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
393  void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
394  void WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent,
395  OffsetsManagerGroup *fdManager);
396  void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep,
397  OffsetsManagerGroup *fdManager);
398  void WritePointDataAppended(vtkPointData* pd, vtkIndent indent,
399  OffsetsManagerGroup *pdManager);
400  void WritePointDataAppendedData(vtkPointData* pd, int timestep,
401  OffsetsManagerGroup *pdManager);
402  void WriteCellDataAppended(vtkCellData* cd, vtkIndent indent,
403  OffsetsManagerGroup *cdManager);
404  void WriteCellDataAppendedData(vtkCellData* cd, int timestep,
405  OffsetsManagerGroup *cdManager);
406  void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
407  void WritePointsAppended(vtkPoints* points, vtkIndent indent, OffsetsManager *manager);
408  void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager *pdManager);
409  void WritePointsInline(vtkPoints* points, vtkIndent indent);
410  void WriteCoordinatesInline(vtkDataArray* xc, vtkDataArray* yc,
411  vtkDataArray* zc, vtkIndent indent);
412  void WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc,
413  vtkDataArray* zc, vtkIndent indent,
414  OffsetsManagerGroup *coordManager);
415  void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc,
416  vtkDataArray* zc, int timestep,
417  OffsetsManagerGroup *coordManager);
418  virtual vtkAbstractArray* CreateArrayForPoints(vtkAbstractArray* inArray);
419  virtual vtkAbstractArray* CreateArrayForCells(vtkAbstractArray* inArray);
420  virtual vtkDataArray* CreateExactCoordinates(vtkDataArray* inArray, int xyz);
421  void WritePPointData(vtkPointData* pd, vtkIndent indent);
422  void WritePCellData(vtkCellData* cd, vtkIndent indent);
423  void WritePPoints(vtkPoints* points, vtkIndent indent);
424  void WritePArray(vtkAbstractArray* a, vtkIndent indent,
425  const char* alternateName=0);
426  void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc,
427  vtkDataArray* zc, vtkIndent indent);
428 
429  // Internal utility methods.
430  int WriteBinaryDataInternal(void* data, OffsetType numWords, int wordType);
431  int WriteBinaryDataBlock(unsigned char* in_data, OffsetType numWords, int wordType);
432  void PerformByteSwap(void* data, OffsetType numWords, int wordSize);
433  int CreateCompressionHeader(OffsetType size);
434  int WriteCompressionBlock(unsigned char* data, OffsetType size);
435  int WriteCompressionHeader();
436  OffsetType GetWordTypeSize(int dataType);
437  const char* GetWordTypeName(int dataType);
438  OffsetType GetOutputWordTypeSize(int dataType);
439 
440  char** CreateStringArray(int numStrings);
441  void DestroyStringArray(int numStrings, char** strings);
442 
443  // The current range over which progress is moving. This allows for
444  // incrementally fine-tuned progress updates.
445  virtual void GetProgressRange(float* range);
446  virtual void SetProgressRange(float* range, int curStep, int numSteps);
447  virtual void SetProgressRange(float* range, int curStep, float* fractions);
448  virtual void SetProgressPartial(float fraction);
449  virtual void UpdateProgressDiscrete(float progress);
450  float ProgressRange[2];
451 
452  ofstream* OutFile;
453 
454  int OpenFile();
455  void CloseFile();
456 
457  // The timestep currently being written
458  int TimeStep;
461  // Store the range of time steps
462  int TimeStepRange[2];
463 
464  // Dummy boolean var to start/stop the continue executing:
465  // when using the Start/Stop/WriteNextTime API
466  int UserContinueExecuting; //can only be -1 = invalid, 0 = stop, 1 = start
467 
468  unsigned long *NumberOfTimeValues; //one per piece / per timestep
469  //BTX
470  friend class vtkXMLWriterHelper;
471  //ETX
472 
473 private:
474  vtkXMLWriter(const vtkXMLWriter&); // Not implemented.
475  void operator=(const vtkXMLWriter&); // Not implemented.
476 };
477 
478 #endif