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 =========================================================================*/
25 #ifndef vtkXMLWriter_h
26 #define vtkXMLWriter_h
27 
28 #include "vtkIOXMLModule.h" // For export macro
29 #include "vtkAlgorithm.h"
30 #include <sstream> // For ostringstream ivar
31 
32 class vtkAbstractArray;
33 class vtkArrayIterator;
34 
35 template <class T> class vtkArrayIteratorTemplate;
36 
37 class vtkCellData;
38 class vtkDataArray;
39 class vtkDataCompressor;
40 class vtkDataSet;
42 class vtkOutputStream;
43 class vtkPointData;
44 class vtkPoints;
45 class vtkFieldData;
46 class vtkXMLDataHeader;
47 
48 class vtkStdString;
49 class OffsetsManager; // one per piece/per time
50 class OffsetsManagerGroup; // array of OffsetsManager
51 class OffsetsManagerArray; // array of OffsetsManagerGroup
52 
53 class VTKIOXML_EXPORT vtkXMLWriter : public vtkAlgorithm
54 {
55 public:
56  vtkTypeMacro(vtkXMLWriter, vtkAlgorithm);
57  void PrintSelf(ostream& os, vtkIndent indent);
58 
62  enum { BigEndian, LittleEndian };
63 
70  enum { Ascii, Binary, Appended };
71 
77  enum { Int32=32, Int64=64 };
78 
84  enum { UInt32=32, UInt64=64 };
85 
87 
91  vtkSetMacro(ByteOrder, int);
92  vtkGetMacro(ByteOrder, int);
96 
98 
102  virtual void SetHeaderType(int);
103  vtkGetMacro(HeaderType, int);
107 
109 
113  virtual void SetIdType(int);
114  vtkGetMacro(IdType, int);
118 
120 
123  vtkSetStringMacro(FileName);
124  vtkGetStringMacro(FileName);
126 
128 
131  vtkSetMacro(WriteToOutputString, int);
132  vtkGetMacro(WriteToOutputString, int);
133  vtkBooleanMacro(WriteToOutputString, int);
134  std::string GetOutputString() { return this->OutputString; }
136 
138 
143  vtkGetObjectMacro(Compressor, vtkDataCompressor);
145 
147  {
149  ZLIB
150  };
151 
153 
156  void SetCompressorType(int compressorType);
158  {
159  this->SetCompressorType(NONE);
160  }
162  {
163  this->SetCompressorType(ZLIB);
164  }
166 
168 
174  virtual void SetBlockSize(size_t blockSize);
175  vtkGetMacro(BlockSize, size_t);
177 
179 
184  vtkSetMacro(DataMode, int);
185  vtkGetMacro(DataMode, int);
190 
192 
199  vtkSetMacro(EncodeAppendedData, int);
200  vtkGetMacro(EncodeAppendedData, int);
201  vtkBooleanMacro(EncodeAppendedData, int);
203 
205 
213  vtkDataObject *GetInput() { return this->GetInput(0); }
215 
219  virtual const char* GetDefaultFileExtension()=0;
220 
224  int Write();
225 
226  // See the vtkAlgorithm for a description of what these do
227  virtual int ProcessRequest(vtkInformation* request,
228  vtkInformationVector** inputVector,
229  vtkInformationVector* outputVector);
230 
231 
233 
236  vtkGetMacro(NumberOfTimeSteps, int);
237  vtkSetMacro(NumberOfTimeSteps, int);
239 
241 
244  void Start();
245  void Stop();
246  void WriteNextTime(double time);
248 
249 protected:
252 
253  virtual int RequestInformation(
254  vtkInformation* request,
255  vtkInformationVector** inputVector,
256  vtkInformationVector* outputVector);
257  virtual int RequestData(vtkInformation* request,
258  vtkInformationVector** inputVector,
259  vtkInformationVector* outputVector);
260 
261  // The name of the output file.
262  char* FileName;
263 
264  // The output stream to which the XML is written.
265  ostream* Stream;
266 
267  // Whether this object is writing to a string or a file.
268  // Default is 0: write to file.
270 
271  // The output string.
273 
274  // The output byte order.
276 
277  // The output binary header word type.
279 
280  // The output vtkIdType.
281  int IdType;
282 
283  // The form of binary data to write. Used by subclasses to choose
284  // how to write data.
285  int DataMode;
286 
287  // Whether to base64-encode the appended data section.
289 
290  // The stream position at which appended data starts.
291  vtkTypeInt64 AppendedDataPosition;
292 
293  // appended data offsets for field data
295 
296  // We need a 32 bit signed integer type to which vtkIdType will be
297  // converted if Int32 is specified for the IdType parameter to this
298  // writer.
299 # if VTK_SIZEOF_SHORT == 4
300  typedef short Int32IdType;
301 # elif VTK_SIZEOF_INT == 4
302  typedef int Int32IdType;
303 # elif VTK_SIZEOF_LONG == 4
304  typedef long Int32IdType;
305 # else
306 # error "No native data type can represent a signed 32-bit integer."
307 # endif
308 
309  // Buffer for vtkIdType conversion.
310  Int32IdType* Int32IdTypeBuffer;
311 
312  // The byte swapping buffer.
313  unsigned char* ByteSwapBuffer;
314 
315  // Compression information.
317  size_t BlockSize;
321 
322  // The output stream used to write binary and appended data. May
323  // transparently encode the data.
325 
326  // Allow subclasses to set the data stream.
328  vtkGetObjectMacro(DataStream, vtkOutputStream);
329 
330  // Method to drive most of actual writing.
331  virtual int WriteInternal();
332 
333  // Method defined by subclasses to write data. Return 1 for
334  // success, 0 for failure.
335  virtual int WriteData() { return 1; }
336 
337  // Method defined by subclasses to specify the data set's type name.
338  virtual const char* GetDataSetName()=0;
339 
340  // Methods to define the file's major and minor version numbers.
341  virtual int GetDataSetMajorVersion();
342  virtual int GetDataSetMinorVersion();
343 
344  // Utility methods for subclasses.
346  virtual int StartFile();
347  virtual void WriteFileAttributes();
348  virtual int EndFile();
349  void DeleteAFile();
350  void DeleteAFile(const char* name);
351 
352  virtual int WritePrimaryElement(ostream &os, vtkIndent indent);
353  virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent);
356 
357  // Write enough space to go back and write the given attribute with
358  // at most "length" characters in the value. Returns the stream
359  // position at which attribute should be later written. The default
360  // length of 20 is enough for a 64-bit integer written in decimal or
361  // a double-precision floating point value written to 13 digits of
362  // precision (the other 7 come from a minus sign, decimal place, and
363  // a big exponent like "e+300").
364  vtkTypeInt64 ReserveAttributeSpace(const char* attr, size_t length=20);
365 
366  vtkTypeInt64 GetAppendedDataOffset();
367  void WriteAppendedDataOffset(vtkTypeInt64 streamPos,
368  vtkTypeInt64 &lastoffset,
369  const char* attr=0);
370  void ForwardAppendedDataOffset(vtkTypeInt64 streamPos,
371  vtkTypeInt64 offset,
372  const char* attr=0);
373  void ForwardAppendedDataDouble(vtkTypeInt64 streamPos,
374  double value,
375  const char* attr);
376 
377  int WriteScalarAttribute(const char* name, int data);
378  int WriteScalarAttribute(const char* name, float data);
379  int WriteScalarAttribute(const char* name, double data);
380 #ifdef VTK_USE_64BIT_IDS
381  int WriteScalarAttribute(const char* name, vtkIdType data);
382 #endif
383 
384  int WriteVectorAttribute(const char* name, int length, int* data);
385  int WriteVectorAttribute(const char* name, int length, float* data);
386  int WriteVectorAttribute(const char* name, int length, double* data);
387 #ifdef VTK_USE_64BIT_IDS
388  int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
389 #endif
390 
391  int WriteDataModeAttribute(const char* name);
392  int WriteWordTypeAttribute(const char* name, int dataType);
393  int WriteStringAttribute(const char* name, const char* value);
394 
395  // Returns true if any keys were written.
397 
399  const char* alternateName, int writeNumTuples, int timestep);
400  virtual void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat);
401  virtual void WriteArrayInline(vtkAbstractArray* a, vtkIndent indent,
402  const char* alternateName=0, int writeNumTuples=0);
403  virtual void WriteInlineData(vtkAbstractArray* a, vtkIndent indent);
404 
406  OffsetsManager &offs, const char* alternateName=0, int writeNumTuples=0,
407  int timestep=0);
411  void WriteArrayAppendedData(vtkAbstractArray* a, vtkTypeInt64 pos,
412  vtkTypeInt64 &lastoffset);
413 
414  // Methods for writing points, point data, and cell data.
415  void WriteFieldData(vtkIndent indent);
420  OffsetsManagerGroup *fdManager);
421  void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep,
422  OffsetsManagerGroup *fdManager);
424  OffsetsManagerGroup *pdManager);
425  void WritePointDataAppendedData(vtkPointData* pd, int timestep,
426  OffsetsManagerGroup *pdManager);
428  OffsetsManagerGroup *cdManager);
429  void WriteCellDataAppendedData(vtkCellData* cd, int timestep,
430  OffsetsManagerGroup *cdManager);
431  void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
433  void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager *pdManager);
436  vtkDataArray* zc, vtkIndent indent);
438  vtkDataArray* zc, vtkIndent indent,
439  OffsetsManagerGroup *coordManager);
441  vtkDataArray* zc, int timestep,
442  OffsetsManagerGroup *coordManager);
447  const char* alternateName=0);
449  vtkDataArray* zc, vtkIndent indent);
450 
451  // Internal utility methods.
452  int WriteBinaryDataBlock(unsigned char* in_data, size_t numWords, int wordType);
453  void PerformByteSwap(void* data, size_t numWords, size_t wordSize);
455  int WriteCompressionBlock(unsigned char* data, size_t size);
457  size_t GetWordTypeSize(int dataType);
458  const char* GetWordTypeName(int dataType);
459  size_t GetOutputWordTypeSize(int dataType);
460 
461  char** CreateStringArray(int numStrings);
462  void DestroyStringArray(int numStrings, char** strings);
463 
464  // The current range over which progress is moving. This allows for
465  // incrementally fine-tuned progress updates.
466  virtual void GetProgressRange(float range[2]);
467  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
468  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
469  virtual void SetProgressPartial(float fraction);
470  virtual void UpdateProgressDiscrete(float progress);
471  float ProgressRange[2];
472 
473  ofstream* OutFile;
474  std::ostringstream* OutStringStream;
475 
476  int OpenStream();
477  int OpenFile();
478  int OpenString();
479  void CloseStream();
480  void CloseFile();
481  void CloseString();
482 
483  // The timestep currently being written
486 
487  // Dummy boolean var to start/stop the continue executing:
488  // when using the Start/Stop/WriteNextTime API
489  int UserContinueExecuting; //can only be -1 = invalid, 0 = stop, 1 = start
490 
491  // This variable is used to ease transition to new versions of VTK XML files.
492  // If data that needs to be written satisfies certain conditions,
493  // the writer can use the previous file version version.
494  // For version change 0.1 -> 2.0 (UInt32 header) and 1.0 -> 2.0
495  // (UInt64 header), if data does not have a vtkGhostType array,
496  // the file is written with version: 0.1/1.0.
498 
499  vtkTypeInt64 *NumberOfTimeValues; //one per piece / per timestep
500 
501  friend class vtkXMLWriterHelper;
502 
503 private:
504  vtkXMLWriter(const vtkXMLWriter&) VTK_DELETE_FUNCTION;
505  void operator=(const vtkXMLWriter&) VTK_DELETE_FUNCTION;
506 };
507 
508 #endif
Helper class due to PIMPL excess.
Abstract superclass for all arrays.
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:60
Implementation template for a array iterator.
Abstract superclass to iterate over elements in an vtkAbstractArray.
represent and manipulate cell attribute data
Definition: vtkCellData.h:39
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:55
Abstract interface for data compression classes.
general representation of visualization data
Definition: vtkDataObject.h:65
represent and manipulate attribute data in a dataset
abstract class to specify dataset behavior
Definition: vtkDataSet.h:63
represent and manipulate fields of data
Definition: vtkFieldData.h:57
a simple class to control print indentation
Definition: vtkIndent.h:40
Store zero or more vtkInformation instances.
Store vtkAlgorithm input/output information.
Wraps a binary output stream with a VTK interface.
represent and manipulate point attribute data
Definition: vtkPointData.h:38
represent and manipulate 3D points
Definition: vtkPoints.h:40
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:49
Superclass for VTK's XML file writers.
Definition: vtkXMLWriter.h:54
virtual int GetDataSetMajorVersion()
void EndAppendedData()
bool UsePreviousVersion
Definition: vtkXMLWriter.h:497
int WriteBinaryDataInternal(vtkAbstractArray *a)
int CurrentTimeIndex
Definition: vtkXMLWriter.h:484
int WriteBinaryDataBlock(unsigned char *in_data, size_t numWords, int wordType)
void WriteArrayAppended(vtkAbstractArray *a, vtkIndent indent, OffsetsManager &offs, const char *alternateName=0, int writeNumTuples=0, int timestep=0)
void WriteFieldData(vtkIndent indent)
void SetInputData(vtkDataObject *)
Assign a data object as input.
void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
virtual int GetDataSetMinorVersion()
virtual void SetCompressor(vtkDataCompressor *)
Get/Set the compressor used to compress binary and appended data before writing to the file.
void SetByteOrderToLittleEndian()
void SetCompressorTypeToZLib()
Definition: vtkXMLWriter.h:161
int WriteBinaryData(vtkAbstractArray *a)
unsigned char * ByteSwapBuffer
Definition: vtkXMLWriter.h:313
void WritePCellData(vtkCellData *cd, vtkIndent indent)
vtkDataObject * GetInput(int port)
int UserContinueExecuting
Definition: vtkXMLWriter.h:489
vtkDataObject * GetInput()
Definition: vtkXMLWriter.h:213
void WriteArrayAppendedData(vtkAbstractArray *a, vtkTypeInt64 pos, vtkTypeInt64 &lastoffset)
int WriteCompressionHeader()
void WriteFieldDataAppendedData(vtkFieldData *fd, int timestep, OffsetsManagerGroup *fdManager)
size_t BlockSize
Definition: vtkXMLWriter.h:317
int WriteScalarAttribute(const char *name, int data)
void DestroyStringArray(int numStrings, char **strings)
vtkXMLDataHeader * CompressionHeader
Definition: vtkXMLWriter.h:319
void WriteAppendedDataOffset(vtkTypeInt64 streamPos, vtkTypeInt64 &lastoffset, const char *attr=0)
void ForwardAppendedDataDouble(vtkTypeInt64 streamPos, double value, const char *attr)
void WritePointDataAppended(vtkPointData *pd, vtkIndent indent, OffsetsManagerGroup *pdManager)
int NumberOfTimeSteps
Definition: vtkXMLWriter.h:485
void DeleteAFile()
int Write()
Invoke the writer.
void WritePPointData(vtkPointData *pd, vtkIndent indent)
char * FileName
Definition: vtkXMLWriter.h:262
void WritePPoints(vtkPoints *points, vtkIndent indent)
vtkTypeInt64 AppendedDataPosition
Definition: vtkXMLWriter.h:291
void SetCompressorTypeToNone()
Definition: vtkXMLWriter.h:157
void WriteFieldDataAppended(vtkFieldData *fd, vtkIndent indent, OffsetsManagerGroup *fdManager)
std::string OutputString
Definition: vtkXMLWriter.h:272
virtual int ProcessRequest(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
int WriteStringAttribute(const char *name, const char *value)
bool WriteInformation(vtkInformation *info, vtkIndent indent)
virtual void WriteArrayInline(vtkAbstractArray *a, vtkIndent indent, const char *alternateName=0, int writeNumTuples=0)
virtual int WriteData()
Definition: vtkXMLWriter.h:335
int WriteCompressionBlock(unsigned char *data, size_t size)
void SetDataModeToBinary()
size_t GetOutputWordTypeSize(int dataType)
std::ostringstream * OutStringStream
Definition: vtkXMLWriter.h:474
int WriteVectorAttribute(const char *name, int length, int *data)
void SetInputData(int, vtkDataObject *)
virtual void UpdateProgressDiscrete(float progress)
virtual int EndFile()
void WritePointsAppended(vtkPoints *points, vtkIndent indent, OffsetsManager *manager)
int WriteVectorAttribute(const char *name, int length, float *data)
int WriteDataModeAttribute(const char *name)
void Start()
API to interface an outside the VTK pipeline control.
void WritePointsAppendedData(vtkPoints *points, int timestep, OffsetsManager *pdManager)
void WriteCoordinatesAppendedData(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, int timestep, OffsetsManagerGroup *coordManager)
Int32IdType * Int32IdTypeBuffer
Definition: vtkXMLWriter.h:310
void SetDataModeToAppended()
void SetCompressorType(int compressorType)
Convenience functions to set the compressor to certain known types.
void WriteArrayHeader(vtkAbstractArray *a, vtkIndent indent, const char *alternateName, int writeNumTuples, int timestep)
void WritePointsInline(vtkPoints *points, vtkIndent indent)
virtual void SetProgressRange(const float range[2], int curStep, const float *fractions)
void SetHeaderTypeToUInt32()
vtkTypeInt64 GetAppendedDataOffset()
void WriteCellDataAppendedData(vtkCellData *cd, int timestep, OffsetsManagerGroup *cdManager)
virtual void SetDataStream(vtkOutputStream *)
int WriteVectorAttribute(const char *name, int length, double *data)
ofstream * OutFile
Definition: vtkXMLWriter.h:473
int EncodeAppendedData
Definition: vtkXMLWriter.h:288
void SetHeaderTypeToUInt64()
int WriteScalarAttribute(const char *name, double data)
void WriteCellDataAppended(vtkCellData *cd, vtkIndent indent, OffsetsManagerGroup *cdManager)
virtual void WriteFileAttributes()
void CloseStream()
vtkDataSet * GetInputAsDataSet()
void WritePCoordinates(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, vtkIndent indent)
void WritePArray(vtkAbstractArray *a, vtkIndent indent, const char *alternateName=0)
void WriteCoordinatesInline(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, vtkIndent indent)
void SetByteOrderToBigEndian()
vtkTypeInt64 * NumberOfTimeValues
Definition: vtkXMLWriter.h:499
void WriteCoordinatesAppended(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, vtkIndent indent, OffsetsManagerGroup *coordManager)
size_t CompressionBlockNumber
Definition: vtkXMLWriter.h:318
vtkOutputStream * DataStream
Definition: vtkXMLWriter.h:324
void WriteFieldDataInline(vtkFieldData *fd, vtkIndent indent)
void SetIdTypeToInt32()
void CloseFile()
void WritePointDataInline(vtkPointData *pd, vtkIndent indent)
int OpenStream()
std::string GetOutputString()
Definition: vtkXMLWriter.h:134
ostream * Stream
Definition: vtkXMLWriter.h:265
OffsetsManagerGroup * FieldDataOM
Definition: vtkXMLWriter.h:294
const char * GetWordTypeName(int dataType)
int OpenString()
virtual void GetProgressRange(float range[2])
vtkTypeInt64 ReserveAttributeSpace(const char *attr, size_t length=20)
virtual void SetProgressRange(const float range[2], int curStep, int numSteps)
virtual void WriteInlineData(vtkAbstractArray *a, vtkIndent indent)
int WriteToOutputString
Definition: vtkXMLWriter.h:269
int CreateCompressionHeader(size_t size)
vtkDataCompressor * Compressor
Definition: vtkXMLWriter.h:316
virtual const char * GetDataSetName()=0
virtual int WritePrimaryElement(ostream &os, vtkIndent indent)
virtual const char * GetDefaultFileExtension()=0
Get the default file extension for files written by this writer.
virtual void SetProgressPartial(float fraction)
virtual int StartFile()
int WriteScalarAttribute(const char *name, float data)
void WriteNextTime(double time)
void DeleteAFile(const char *name)
void SetIdTypeToInt64()
void ForwardAppendedDataOffset(vtkTypeInt64 streamPos, vtkTypeInt64 offset, const char *attr=0)
void WritePointDataAppendedData(vtkPointData *pd, int timestep, OffsetsManagerGroup *pdManager)
virtual void SetBlockSize(size_t blockSize)
Get/Set the block size used in compression.
void StartAppendedData()
virtual void SetIdType(int)
Get/Set the size of the vtkIdType values stored in the file.
void CloseString()
vtkTypeInt64 CompressionHeaderPosition
Definition: vtkXMLWriter.h:320
void SetDataModeToAscii()
void WriteAttributeIndices(vtkDataSetAttributes *dsa, char **names)
int WriteAsciiData(vtkAbstractArray *a, vtkIndent indent)
char ** CreateStringArray(int numStrings)
virtual void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat)
size_t GetWordTypeSize(int dataType)
virtual int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent)
virtual void SetHeaderType(int)
Get/Set the binary data header word type.
virtual int WriteInternal()
void WriteCellDataInline(vtkCellData *cd, vtkIndent indent)
int WriteWordTypeAttribute(const char *name, int dataType)
virtual int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
void PerformByteSwap(void *data, size_t numWords, size_t wordSize)
@ points
Definition: vtkX3D.h:446
@ info
Definition: vtkX3D.h:376
@ length
Definition: vtkX3D.h:393
@ value
Definition: vtkX3D.h:220
@ port
Definition: vtkX3D.h:447
@ range
Definition: vtkX3D.h:238
@ time
Definition: vtkX3D.h:497
@ name
Definition: vtkX3D.h:219
@ size
Definition: vtkX3D.h:253
@ offset
Definition: vtkX3D.h:438
@ progress
Definition: vtkX3D.h:452
@ data
Definition: vtkX3D.h:315
@ string
Definition: vtkX3D.h:490
vtkSetMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkBooleanMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkGetStringMacro(ExtensionsString)
Returns a string listing all available extensions.
int vtkIdType
Definition: vtkType.h:287