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);
93  void SetByteOrderToBigEndian();
94  void SetByteOrderToLittleEndian();
96 
98 
102  virtual void SetHeaderType(int);
103  vtkGetMacro(HeaderType, int);
104  void SetHeaderTypeToUInt32();
105  void SetHeaderTypeToUInt64();
107 
109 
113  virtual void SetIdType(int);
114  vtkGetMacro(IdType, int);
115  void SetIdTypeToInt32();
116  void SetIdTypeToInt64();
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 
142  virtual void SetCompressor(vtkDataCompressor*);
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);
186  void SetDataModeToAscii();
187  void SetDataModeToBinary();
188  void SetDataModeToAppended();
190 
192 
199  vtkSetMacro(EncodeAppendedData, int);
200  vtkGetMacro(EncodeAppendedData, int);
201  vtkBooleanMacro(EncodeAppendedData, int);
203 
205 
210  void SetInputData(vtkDataObject *);
211  void SetInputData(int, vtkDataObject*);
212  vtkDataObject *GetInput(int port);
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:
250  vtkXMLWriter();
251  ~vtkXMLWriter();
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.
327  virtual void SetDataStream(vtkOutputStream*);
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.
345  vtkDataSet* GetInputAsDataSet();
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);
354  void StartAppendedData();
355  void EndAppendedData();
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.
396  bool WriteInformation(vtkInformation *info, vtkIndent indent);
397 
398  void WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent,
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 
405  void WriteArrayAppended(vtkAbstractArray* a, vtkIndent indent,
406  OffsetsManager &offs, const char* alternateName=0, int writeNumTuples=0,
407  int timestep=0);
408  int WriteAsciiData(vtkAbstractArray* a, vtkIndent indent);
409  int WriteBinaryData(vtkAbstractArray* a);
410  int WriteBinaryDataInternal(vtkAbstractArray* a);
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);
416  void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
417  void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
418  void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
419  void WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent,
420  OffsetsManagerGroup *fdManager);
421  void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep,
422  OffsetsManagerGroup *fdManager);
423  void WritePointDataAppended(vtkPointData* pd, vtkIndent indent,
424  OffsetsManagerGroup *pdManager);
425  void WritePointDataAppendedData(vtkPointData* pd, int timestep,
426  OffsetsManagerGroup *pdManager);
427  void WriteCellDataAppended(vtkCellData* cd, vtkIndent indent,
428  OffsetsManagerGroup *cdManager);
429  void WriteCellDataAppendedData(vtkCellData* cd, int timestep,
430  OffsetsManagerGroup *cdManager);
431  void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
432  void WritePointsAppended(vtkPoints* points, vtkIndent indent, OffsetsManager *manager);
433  void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager *pdManager);
434  void WritePointsInline(vtkPoints* points, vtkIndent indent);
435  void WriteCoordinatesInline(vtkDataArray* xc, vtkDataArray* yc,
436  vtkDataArray* zc, vtkIndent indent);
437  void WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc,
438  vtkDataArray* zc, vtkIndent indent,
439  OffsetsManagerGroup *coordManager);
440  void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc,
441  vtkDataArray* zc, int timestep,
442  OffsetsManagerGroup *coordManager);
443  void WritePPointData(vtkPointData* pd, vtkIndent indent);
444  void WritePCellData(vtkCellData* cd, vtkIndent indent);
445  void WritePPoints(vtkPoints* points, vtkIndent indent);
446  void WritePArray(vtkAbstractArray* a, vtkIndent indent,
447  const char* alternateName=0);
448  void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc,
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);
454  int CreateCompressionHeader(size_t size);
455  int WriteCompressionBlock(unsigned char* data, size_t size);
456  int WriteCompressionHeader();
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
vtkTypeInt64 * NumberOfTimeValues
Definition: vtkXMLWriter.h:499
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:47
int NumberOfTimeSteps
Definition: vtkXMLWriter.h:485
Abstract interface for data compression classes.
represent and manipulate point attribute data
Definition: vtkPointData.h:37
vtkDataObject * GetInput()
Assign a data object as input.
Definition: vtkXMLWriter.h:213
Store vtkAlgorithm input/output information.
abstract class to specify dataset behavior
Definition: vtkDataSet.h:62
Abstract superclass for all arrays.
represent and manipulate cell attribute data
Definition: vtkCellData.h:38
virtual int ProcessRequest(vtkInformation *request, vtkInformationVector **inInfo, vtkInformationVector *outInfo)
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
std::string GetOutputString()
Enable writing to an OutputString instead of the default, a file.
Definition: vtkXMLWriter.h:134
char * FileName
Definition: vtkXMLWriter.h:262
Helper class due to PIMPL excess.
int vtkIdType
Definition: vtkType.h:287
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Implementation template for a array iterator.
vtkOutputStream * DataStream
Definition: vtkXMLWriter.h:324
virtual int WriteData()
Definition: vtkXMLWriter.h:335
int WriteToOutputString
Definition: vtkXMLWriter.h:269
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:59
vtkTypeInt64 AppendedDataPosition
Definition: vtkXMLWriter.h:291
Superclass for VTK's XML file writers.
Definition: vtkXMLWriter.h:53
a simple class to control print indentation
Definition: vtkIndent.h:39
int EncodeAppendedData
Definition: vtkXMLWriter.h:288
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
std::ostringstream * OutStringStream
Definition: vtkXMLWriter.h:474
size_t CompressionBlockNumber
Definition: vtkXMLWriter.h:318
vtkGetStringMacro(ExtensionsString)
Returns a string listing all available extensions.
Abstract superclass to iterate over elements in an vtkAbstractArray.
represent and manipulate attribute data in a dataset
vtkSetMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
ofstream * OutFile
Definition: vtkXMLWriter.h:473
void SetCompressorTypeToNone()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:157
vtkTypeInt64 CompressionHeaderPosition
Definition: vtkXMLWriter.h:320
bool UsePreviousVersion
Definition: vtkXMLWriter.h:497
int CurrentTimeIndex
Definition: vtkXMLWriter.h:484
Wraps a binary output stream with a VTK interface.
Store zero or more vtkInformation instances.
ostream * Stream
Definition: vtkXMLWriter.h:265
vtkBooleanMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
std::string OutputString
Definition: vtkXMLWriter.h:272
vtkDataCompressor * Compressor
Definition: vtkXMLWriter.h:316
unsigned char * ByteSwapBuffer
Definition: vtkXMLWriter.h:313
general representation of visualization data
Definition: vtkDataObject.h:64
size_t BlockSize
Definition: vtkXMLWriter.h:317
void SetCompressorTypeToZLib()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:161
represent and manipulate 3D points
Definition: vtkPoints.h:39
vtkXMLDataHeader * CompressionHeader
Definition: vtkXMLWriter.h:319
int UserContinueExecuting
Definition: vtkXMLWriter.h:489
OffsetsManagerGroup * FieldDataOM
Definition: vtkXMLWriter.h:294
represent and manipulate fields of data
Definition: vtkFieldData.h:56
Int32IdType * Int32IdTypeBuffer
Definition: vtkXMLWriter.h:310