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) VTK_OVERRIDE;
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  {
150  LZ4
151  };
152 
154 
157  void SetCompressorType(int compressorType);
159  {
160  this->SetCompressorType(NONE);
161  }
163  {
164  this->SetCompressorType(LZ4);
165  }
167  {
168  this->SetCompressorType(ZLIB);
169  }
171 
173 
179  virtual void SetBlockSize(size_t blockSize);
180  vtkGetMacro(BlockSize, size_t);
182 
184 
189  vtkSetMacro(DataMode, int);
190  vtkGetMacro(DataMode, int);
191  void SetDataModeToAscii();
192  void SetDataModeToBinary();
193  void SetDataModeToAppended();
195 
197 
204  vtkSetMacro(EncodeAppendedData, int);
205  vtkGetMacro(EncodeAppendedData, int);
206  vtkBooleanMacro(EncodeAppendedData, int);
208 
210 
215  void SetInputData(vtkDataObject *);
216  void SetInputData(int, vtkDataObject*);
217  vtkDataObject *GetInput(int port);
218  vtkDataObject *GetInput() { return this->GetInput(0); }
220 
224  virtual const char* GetDefaultFileExtension()=0;
225 
229  int Write();
230 
231  // See the vtkAlgorithm for a description of what these do
232  int ProcessRequest(vtkInformation* request,
233  vtkInformationVector** inputVector,
234  vtkInformationVector* outputVector) VTK_OVERRIDE;
235 
236 
238 
241  vtkGetMacro(NumberOfTimeSteps, int);
242  vtkSetMacro(NumberOfTimeSteps, int);
244 
246 
249  void Start();
250  void Stop();
251  void WriteNextTime(double time);
253 
254 protected:
255  vtkXMLWriter();
256  ~vtkXMLWriter() VTK_OVERRIDE;
257 
258  virtual int RequestInformation(
259  vtkInformation* request,
260  vtkInformationVector** inputVector,
261  vtkInformationVector* outputVector);
262  virtual int RequestData(vtkInformation* request,
263  vtkInformationVector** inputVector,
264  vtkInformationVector* outputVector);
265 
266  // The name of the output file.
267  char* FileName;
268 
269  // The output stream to which the XML is written.
270  ostream* Stream;
271 
272  // Whether this object is writing to a string or a file.
273  // Default is 0: write to file.
275 
276  // The output string.
278 
279  // The output byte order.
281 
282  // The output binary header word type.
284 
285  // The output vtkIdType.
286  int IdType;
287 
288  // The form of binary data to write. Used by subclasses to choose
289  // how to write data.
290  int DataMode;
291 
292  // Whether to base64-encode the appended data section.
294 
295  // The stream position at which appended data starts.
296  vtkTypeInt64 AppendedDataPosition;
297 
298  // appended data offsets for field data
300 
301  // We need a 32 bit signed integer type to which vtkIdType will be
302  // converted if Int32 is specified for the IdType parameter to this
303  // writer.
304 # if VTK_SIZEOF_SHORT == 4
305  typedef short Int32IdType;
306 # elif VTK_SIZEOF_INT == 4
307  typedef int Int32IdType;
308 # elif VTK_SIZEOF_LONG == 4
309  typedef long Int32IdType;
310 # else
311 # error "No native data type can represent a signed 32-bit integer."
312 # endif
313 
314  // Buffer for vtkIdType conversion.
315  Int32IdType* Int32IdTypeBuffer;
316 
317  // The byte swapping buffer.
318  unsigned char* ByteSwapBuffer;
319 
320  // Compression information.
322  size_t BlockSize;
326 
327  // The output stream used to write binary and appended data. May
328  // transparently encode the data.
330 
331  // Allow subclasses to set the data stream.
332  virtual void SetDataStream(vtkOutputStream*);
333  vtkGetObjectMacro(DataStream, vtkOutputStream);
334 
335  // Method to drive most of actual writing.
336  virtual int WriteInternal();
337 
338  // Method defined by subclasses to write data. Return 1 for
339  // success, 0 for failure.
340  virtual int WriteData() { return 1; }
341 
342  // Method defined by subclasses to specify the data set's type name.
343  virtual const char* GetDataSetName()=0;
344 
345  // Methods to define the file's major and minor version numbers.
346  virtual int GetDataSetMajorVersion();
347  virtual int GetDataSetMinorVersion();
348 
349  // Utility methods for subclasses.
350  vtkDataSet* GetInputAsDataSet();
351  virtual int StartFile();
352  virtual void WriteFileAttributes();
353  virtual int EndFile();
354  void DeleteAFile();
355  void DeleteAFile(const char* name);
356 
357  virtual int WritePrimaryElement(ostream &os, vtkIndent indent);
358  virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent);
359  void StartAppendedData();
360  void EndAppendedData();
361 
362  // Write enough space to go back and write the given attribute with
363  // at most "length" characters in the value. Returns the stream
364  // position at which attribute should be later written. The default
365  // length of 20 is enough for a 64-bit integer written in decimal or
366  // a double-precision floating point value written to 13 digits of
367  // precision (the other 7 come from a minus sign, decimal place, and
368  // a big exponent like "e+300").
369  vtkTypeInt64 ReserveAttributeSpace(const char* attr, size_t length=20);
370 
371  vtkTypeInt64 GetAppendedDataOffset();
372  void WriteAppendedDataOffset(vtkTypeInt64 streamPos,
373  vtkTypeInt64 &lastoffset,
374  const char* attr=0);
375  void ForwardAppendedDataOffset(vtkTypeInt64 streamPos,
376  vtkTypeInt64 offset,
377  const char* attr=0);
378  void ForwardAppendedDataDouble(vtkTypeInt64 streamPos,
379  double value,
380  const char* attr);
381 
382  int WriteScalarAttribute(const char* name, int data);
383  int WriteScalarAttribute(const char* name, float data);
384  int WriteScalarAttribute(const char* name, double data);
385 #ifdef VTK_USE_64BIT_IDS
386  int WriteScalarAttribute(const char* name, vtkIdType data);
387 #endif
388 
389  int WriteVectorAttribute(const char* name, int length, int* data);
390  int WriteVectorAttribute(const char* name, int length, float* data);
391  int WriteVectorAttribute(const char* name, int length, double* data);
392 #ifdef VTK_USE_64BIT_IDS
393  int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
394 #endif
395 
396  int WriteDataModeAttribute(const char* name);
397  int WriteWordTypeAttribute(const char* name, int dataType);
398  int WriteStringAttribute(const char* name, const char* value);
399 
400  // Returns true if any keys were written.
401  bool WriteInformation(vtkInformation *info, vtkIndent indent);
402 
403  void WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent,
404  const char* alternateName, int writeNumTuples, int timestep);
405  virtual void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat);
406  virtual void WriteArrayInline(vtkAbstractArray* a, vtkIndent indent,
407  const char* alternateName=0, int writeNumTuples=0);
408  virtual void WriteInlineData(vtkAbstractArray* a, vtkIndent indent);
409 
410  void WriteArrayAppended(vtkAbstractArray* a, vtkIndent indent,
411  OffsetsManager &offs, const char* alternateName=0, int writeNumTuples=0,
412  int timestep=0);
413  int WriteAsciiData(vtkAbstractArray* a, vtkIndent indent);
414  int WriteBinaryData(vtkAbstractArray* a);
415  int WriteBinaryDataInternal(vtkAbstractArray* a);
416  void WriteArrayAppendedData(vtkAbstractArray* a, vtkTypeInt64 pos,
417  vtkTypeInt64 &lastoffset);
418 
419  // Methods for writing points, point data, and cell data.
420  void WriteFieldData(vtkIndent indent);
421  void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
422  void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
423  void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
424  void WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent,
425  OffsetsManagerGroup *fdManager);
426  void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep,
427  OffsetsManagerGroup *fdManager);
428  void WritePointDataAppended(vtkPointData* pd, vtkIndent indent,
429  OffsetsManagerGroup *pdManager);
430  void WritePointDataAppendedData(vtkPointData* pd, int timestep,
431  OffsetsManagerGroup *pdManager);
432  void WriteCellDataAppended(vtkCellData* cd, vtkIndent indent,
433  OffsetsManagerGroup *cdManager);
434  void WriteCellDataAppendedData(vtkCellData* cd, int timestep,
435  OffsetsManagerGroup *cdManager);
436  void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
437  void WritePointsAppended(vtkPoints* points, vtkIndent indent, OffsetsManager *manager);
438  void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager *pdManager);
439  void WritePointsInline(vtkPoints* points, vtkIndent indent);
440  void WriteCoordinatesInline(vtkDataArray* xc, vtkDataArray* yc,
441  vtkDataArray* zc, vtkIndent indent);
442  void WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc,
443  vtkDataArray* zc, vtkIndent indent,
444  OffsetsManagerGroup *coordManager);
445  void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc,
446  vtkDataArray* zc, int timestep,
447  OffsetsManagerGroup *coordManager);
448  void WritePPointData(vtkPointData* pd, vtkIndent indent);
449  void WritePCellData(vtkCellData* cd, vtkIndent indent);
450  void WritePPoints(vtkPoints* points, vtkIndent indent);
451  void WritePArray(vtkAbstractArray* a, vtkIndent indent,
452  const char* alternateName=0);
453  void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc,
454  vtkDataArray* zc, vtkIndent indent);
455 
456  // Internal utility methods.
457  int WriteBinaryDataBlock(unsigned char* in_data, size_t numWords, int wordType);
458  void PerformByteSwap(void* data, size_t numWords, size_t wordSize);
459  int CreateCompressionHeader(size_t size);
460  int WriteCompressionBlock(unsigned char* data, size_t size);
461  int WriteCompressionHeader();
462  size_t GetWordTypeSize(int dataType);
463  const char* GetWordTypeName(int dataType);
464  size_t GetOutputWordTypeSize(int dataType);
465 
466  char** CreateStringArray(int numStrings);
467  void DestroyStringArray(int numStrings, char** strings);
468 
469  // The current range over which progress is moving. This allows for
470  // incrementally fine-tuned progress updates.
471  virtual void GetProgressRange(float range[2]);
472  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
473  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
474  virtual void SetProgressPartial(float fraction);
475  virtual void UpdateProgressDiscrete(float progress);
476  float ProgressRange[2];
477 
478  ofstream* OutFile;
479  std::ostringstream* OutStringStream;
480 
481  int OpenStream();
482  int OpenFile();
483  int OpenString();
484  void CloseStream();
485  void CloseFile();
486  void CloseString();
487 
488  // The timestep currently being written
491 
492  // Dummy boolean var to start/stop the continue executing:
493  // when using the Start/Stop/WriteNextTime API
494  int UserContinueExecuting; //can only be -1 = invalid, 0 = stop, 1 = start
495 
496  // This variable is used to ease transition to new versions of VTK XML files.
497  // If data that needs to be written satisfies certain conditions,
498  // the writer can use the previous file version version.
499  // For version change 0.1 -> 2.0 (UInt32 header) and 1.0 -> 2.0
500  // (UInt64 header), if data does not have a vtkGhostType array,
501  // the file is written with version: 0.1/1.0.
503 
504  vtkTypeInt64 *NumberOfTimeValues; //one per piece / per timestep
505 
506  friend class vtkXMLWriterHelper;
507 
508 private:
509  vtkXMLWriter(const vtkXMLWriter&) VTK_DELETE_FUNCTION;
510  void operator=(const vtkXMLWriter&) VTK_DELETE_FUNCTION;
511 };
512 
513 #endif
vtkTypeInt64 * NumberOfTimeValues
Definition: vtkXMLWriter.h:504
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:40
int NumberOfTimeSteps
Definition: vtkXMLWriter.h:490
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:218
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:267
Helper class due to PIMPL excess.
int vtkIdType
Definition: vtkType.h:345
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:329
virtual int WriteData()
Definition: vtkXMLWriter.h:340
void SetCompressorTypeToLZ4()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:162
int WriteToOutputString
Definition: vtkXMLWriter.h:274
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:59
vtkTypeInt64 AppendedDataPosition
Definition: vtkXMLWriter.h:296
Superclass for VTK&#39;s XML file writers.
Definition: vtkXMLWriter.h:53
a simple class to control print indentation
Definition: vtkIndent.h:39
int EncodeAppendedData
Definition: vtkXMLWriter.h:293
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
std::ostringstream * OutStringStream
Definition: vtkXMLWriter.h:479
size_t CompressionBlockNumber
Definition: vtkXMLWriter.h:323
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:478
void SetCompressorTypeToNone()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:158
vtkTypeInt64 CompressionHeaderPosition
Definition: vtkXMLWriter.h:325
bool UsePreviousVersion
Definition: vtkXMLWriter.h:502
int CurrentTimeIndex
Definition: vtkXMLWriter.h:489
Wraps a binary output stream with a VTK interface.
Store zero or more vtkInformation instances.
ostream * Stream
Definition: vtkXMLWriter.h:270
vtkBooleanMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
std::string OutputString
Definition: vtkXMLWriter.h:277
vtkDataCompressor * Compressor
Definition: vtkXMLWriter.h:321
unsigned char * ByteSwapBuffer
Definition: vtkXMLWriter.h:318
general representation of visualization data
Definition: vtkDataObject.h:64
size_t BlockSize
Definition: vtkXMLWriter.h:322
void SetCompressorTypeToZLib()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:166
represent and manipulate 3D points
Definition: vtkPoints.h:39
vtkXMLDataHeader * CompressionHeader
Definition: vtkXMLWriter.h:324
int UserContinueExecuting
Definition: vtkXMLWriter.h:494
OffsetsManagerGroup * FieldDataOM
Definition: vtkXMLWriter.h:299
represent and manipulate fields of data
Definition: vtkFieldData.h:56
Int32IdType * Int32IdTypeBuffer
Definition: vtkXMLWriter.h:315