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 =========================================================================*/
33 #ifndef vtkXMLWriter_h
34 #define vtkXMLWriter_h
35 
36 #include "vtkIOXMLModule.h" // For export macro
37 #include "vtkAlgorithm.h"
38 #include <sstream> // For ostringstream ivar
39 
40 class vtkAbstractArray;
41 class vtkArrayIterator;
42 
43 template <class T> class vtkArrayIteratorTemplate;
44 
45 class vtkCellData;
46 class vtkDataArray;
47 class vtkDataCompressor;
48 class vtkDataSet;
50 class vtkFieldData;
51 class vtkOutputStream;
52 class vtkPointData;
53 class vtkPoints;
54 class vtkFieldData;
55 class vtkXMLDataHeader;
56 
57 class vtkStdString;
58 class OffsetsManager; // one per piece/per time
59 class OffsetsManagerGroup; // array of OffsetsManager
60 class OffsetsManagerArray; // array of OffsetsManagerGroup
61 
62 class VTKIOXML_EXPORT vtkXMLWriter : public vtkAlgorithm
63 {
64 public:
65  vtkTypeMacro(vtkXMLWriter, vtkAlgorithm);
66  void PrintSelf(ostream& os, vtkIndent indent) override;
67 
71  enum { BigEndian, LittleEndian };
72 
79  enum { Ascii, Binary, Appended };
80 
86  enum { Int32=32, Int64=64 };
87 
93  enum { UInt32=32, UInt64=64 };
94 
96 
100  vtkSetMacro(ByteOrder, int);
101  vtkGetMacro(ByteOrder, int);
102  void SetByteOrderToBigEndian();
103  void SetByteOrderToLittleEndian();
105 
107 
111  virtual void SetHeaderType(int);
112  vtkGetMacro(HeaderType, int);
113  void SetHeaderTypeToUInt32();
114  void SetHeaderTypeToUInt64();
116 
118 
122  virtual void SetIdType(int);
123  vtkGetMacro(IdType, int);
124  void SetIdTypeToInt32();
125  void SetIdTypeToInt64();
127 
129 
132  vtkSetStringMacro(FileName);
133  vtkGetStringMacro(FileName);
135 
137 
140  vtkSetMacro(WriteToOutputString, vtkTypeBool);
141  vtkGetMacro(WriteToOutputString, vtkTypeBool);
142  vtkBooleanMacro(WriteToOutputString, vtkTypeBool);
143  std::string GetOutputString() { return this->OutputString; }
145 
147 
151  virtual void SetCompressor(vtkDataCompressor*);
152  vtkGetObjectMacro(Compressor, vtkDataCompressor);
154 
156  {
160  LZMA
161  };
162 
164 
167  void SetCompressorType(int compressorType);
169  {
170  this->SetCompressorType(NONE);
171  }
173  {
174  this->SetCompressorType(LZ4);
175  }
177  {
178  this->SetCompressorType(ZLIB);
179  }
181  {
182  this->SetCompressorType(LZMA);
183  }
184 
185  void SetCompressionLevel(int compressorLevel);
186  vtkGetMacro(CompressionLevel, int);
188 
190 
196  virtual void SetBlockSize(size_t blockSize);
197  vtkGetMacro(BlockSize, size_t);
199 
201 
206  vtkSetMacro(DataMode, int);
207  vtkGetMacro(DataMode, int);
208  void SetDataModeToAscii();
209  void SetDataModeToBinary();
210  void SetDataModeToAppended();
212 
214 
221  vtkSetMacro(EncodeAppendedData, vtkTypeBool);
222  vtkGetMacro(EncodeAppendedData, vtkTypeBool);
223  vtkBooleanMacro(EncodeAppendedData, vtkTypeBool);
225 
227 
232  void SetInputData(vtkDataObject *);
233  void SetInputData(int, vtkDataObject*);
234  vtkDataObject *GetInput(int port);
235  vtkDataObject *GetInput() { return this->GetInput(0); }
237 
241  virtual const char* GetDefaultFileExtension()=0;
242 
246  int Write();
247 
248  // See the vtkAlgorithm for a description of what these do
249  int ProcessRequest(vtkInformation* request,
250  vtkInformationVector** inputVector,
251  vtkInformationVector* outputVector) override;
252 
253 
255 
258  vtkGetMacro(NumberOfTimeSteps, int);
259  vtkSetMacro(NumberOfTimeSteps, int);
261 
263 
266  void Start();
267  void Stop();
268  void WriteNextTime(double time);
270 
271 protected:
272  vtkXMLWriter();
273  ~vtkXMLWriter() override;
274 
275  virtual int RequestInformation(
276  vtkInformation* request,
277  vtkInformationVector** inputVector,
278  vtkInformationVector* outputVector);
279  virtual int RequestData(vtkInformation* request,
280  vtkInformationVector** inputVector,
281  vtkInformationVector* outputVector);
282 
283  // The name of the output file.
284  char* FileName;
285 
286  // The output stream to which the XML is written.
287  ostream* Stream;
288 
289  // Whether this object is writing to a string or a file.
290  // Default is 0: write to file.
292 
293  // The output string.
295 
296  // The output byte order.
298 
299  // The output binary header word type.
301 
302  // The output vtkIdType.
303  int IdType;
304 
305  // The form of binary data to write. Used by subclasses to choose
306  // how to write data.
307  int DataMode;
308 
309  // Whether to base64-encode the appended data section.
311 
312  // The stream position at which appended data starts.
313  vtkTypeInt64 AppendedDataPosition;
314 
315  // appended data offsets for field data
317 
318  // We need a 32 bit signed integer type to which vtkIdType will be
319  // converted if Int32 is specified for the IdType parameter to this
320  // writer.
321 # if VTK_SIZEOF_SHORT == 4
322  typedef short Int32IdType;
323 # elif VTK_SIZEOF_INT == 4
324  typedef int Int32IdType;
325 # elif VTK_SIZEOF_LONG == 4
326  typedef long Int32IdType;
327 # else
328 # error "No native data type can represent a signed 32-bit integer."
329 # endif
330 
331  // Buffer for vtkIdType conversion.
332  Int32IdType* Int32IdTypeBuffer;
333 
334  // The byte swapping buffer.
335  unsigned char* ByteSwapBuffer;
336 
337  // Compression information.
339  size_t BlockSize;
343  // Compression Level for vtkDataCompressor objects
344  // 1 (worst compression, fastest) ... 9 (best compression, slowest)
345  int CompressionLevel = 5;
346 
347  // The output stream used to write binary and appended data. May
348  // transparently encode the data.
350 
351  // Allow subclasses to set the data stream.
352  virtual void SetDataStream(vtkOutputStream*);
353  vtkGetObjectMacro(DataStream, vtkOutputStream);
354 
355  // Method to drive most of actual writing.
356  virtual int WriteInternal();
357 
358  // Method defined by subclasses to write data. Return 1 for
359  // success, 0 for failure.
360  virtual int WriteData() { return 1; }
361 
362  // Method defined by subclasses to specify the data set's type name.
363  virtual const char* GetDataSetName()=0;
364 
365  // Methods to define the file's major and minor version numbers.
366  virtual int GetDataSetMajorVersion();
367  virtual int GetDataSetMinorVersion();
368 
369  // Utility methods for subclasses.
370  vtkDataSet* GetInputAsDataSet();
371  virtual int StartFile();
372  virtual void WriteFileAttributes();
373  virtual int EndFile();
374  void DeleteAFile();
375  void DeleteAFile(const char* name);
376 
377  virtual int WritePrimaryElement(ostream &os, vtkIndent indent);
378  virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent);
379  void StartAppendedData();
380  void EndAppendedData();
381 
382  // Write enough space to go back and write the given attribute with
383  // at most "length" characters in the value. Returns the stream
384  // position at which attribute should be later written. The default
385  // length of 20 is enough for a 64-bit integer written in decimal or
386  // a double-precision floating point value written to 13 digits of
387  // precision (the other 7 come from a minus sign, decimal place, and
388  // a big exponent like "e+300").
389  vtkTypeInt64 ReserveAttributeSpace(const char* attr, size_t length=20);
390 
391  vtkTypeInt64 GetAppendedDataOffset();
392  void WriteAppendedDataOffset(vtkTypeInt64 streamPos,
393  vtkTypeInt64 &lastoffset,
394  const char* attr=nullptr);
395  void ForwardAppendedDataOffset(vtkTypeInt64 streamPos,
396  vtkTypeInt64 offset,
397  const char* attr=nullptr);
398  void ForwardAppendedDataDouble(vtkTypeInt64 streamPos,
399  double value,
400  const char* attr);
401 
402  int WriteScalarAttribute(const char* name, int data);
403  int WriteScalarAttribute(const char* name, float data);
404  int WriteScalarAttribute(const char* name, double data);
405 #ifdef VTK_USE_64BIT_IDS
406  int WriteScalarAttribute(const char* name, vtkIdType data);
407 #endif
408 
409  int WriteVectorAttribute(const char* name, int length, int* data);
410  int WriteVectorAttribute(const char* name, int length, float* data);
411  int WriteVectorAttribute(const char* name, int length, double* data);
412 #ifdef VTK_USE_64BIT_IDS
413  int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
414 #endif
415 
416  int WriteDataModeAttribute(const char* name);
417  int WriteWordTypeAttribute(const char* name, int dataType);
418  int WriteStringAttribute(const char* name, const char* value);
419 
420  // Returns true if any keys were written.
421  bool WriteInformation(vtkInformation *info, vtkIndent indent);
422 
423  void WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent,
424  const char* alternateName, int writeNumTuples, int timestep);
425  virtual void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat);
426  virtual void WriteArrayInline(vtkAbstractArray* a, vtkIndent indent,
427  const char* alternateName=nullptr, int writeNumTuples=0);
428  virtual void WriteInlineData(vtkAbstractArray* a, vtkIndent indent);
429 
430  void WriteArrayAppended(vtkAbstractArray* a, vtkIndent indent,
431  OffsetsManager &offs, const char* alternateName=nullptr, int writeNumTuples=0,
432  int timestep=0);
433  int WriteAsciiData(vtkAbstractArray* a, vtkIndent indent);
434  int WriteBinaryData(vtkAbstractArray* a);
435  int WriteBinaryDataInternal(vtkAbstractArray* a);
436  void WriteArrayAppendedData(vtkAbstractArray* a, vtkTypeInt64 pos,
437  vtkTypeInt64 &lastoffset);
438 
439  // Methods for writing points, point data, and cell data.
440  void WriteFieldData(vtkIndent indent);
441  void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
442  void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
443  void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
444  void WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent,
445  OffsetsManagerGroup *fdManager);
446  void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep,
447  OffsetsManagerGroup *fdManager);
448  void WritePointDataAppended(vtkPointData* pd, vtkIndent indent,
449  OffsetsManagerGroup *pdManager);
450  void WritePointDataAppendedData(vtkPointData* pd, int timestep,
451  OffsetsManagerGroup *pdManager);
452  void WriteCellDataAppended(vtkCellData* cd, vtkIndent indent,
453  OffsetsManagerGroup *cdManager);
454  void WriteCellDataAppendedData(vtkCellData* cd, int timestep,
455  OffsetsManagerGroup *cdManager);
456  void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
457  void WritePointsAppended(vtkPoints* points, vtkIndent indent, OffsetsManager *manager);
458  void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager *pdManager);
459  void WritePointsInline(vtkPoints* points, vtkIndent indent);
460  void WriteCoordinatesInline(vtkDataArray* xc, vtkDataArray* yc,
461  vtkDataArray* zc, vtkIndent indent);
462  void WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc,
463  vtkDataArray* zc, vtkIndent indent,
464  OffsetsManagerGroup *coordManager);
465  void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc,
466  vtkDataArray* zc, int timestep,
467  OffsetsManagerGroup *coordManager);
468  void WritePPointData(vtkPointData* pd, vtkIndent indent);
469  void WritePCellData(vtkCellData* cd, vtkIndent indent);
470  void WritePPoints(vtkPoints* points, vtkIndent indent);
471  void WritePArray(vtkAbstractArray* a, vtkIndent indent,
472  const char* alternateName=nullptr);
473  void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc,
474  vtkDataArray* zc, vtkIndent indent);
475 
476  // Internal utility methods.
477  int WriteBinaryDataBlock(unsigned char* in_data, size_t numWords, int wordType);
478  void PerformByteSwap(void* data, size_t numWords, size_t wordSize);
479  int CreateCompressionHeader(size_t size);
480  int WriteCompressionBlock(unsigned char* data, size_t size);
481  int WriteCompressionHeader();
482  size_t GetWordTypeSize(int dataType);
483  const char* GetWordTypeName(int dataType);
484  size_t GetOutputWordTypeSize(int dataType);
485 
486  char** CreateStringArray(int numStrings);
487  void DestroyStringArray(int numStrings, char** strings);
488 
489  // The current range over which progress is moving. This allows for
490  // incrementally fine-tuned progress updates.
491  virtual void GetProgressRange(float range[2]);
492  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
493  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
494  virtual void SetProgressPartial(float fraction);
495  virtual void UpdateProgressDiscrete(float progress);
496  float ProgressRange[2];
497 
498  // This shallows copy input field data to the passed field data and
499  // then adds any additional field arrays. For example, TimeValue.
500  void UpdateFieldData(vtkFieldData*);
501 
502  ofstream* OutFile;
503  std::ostringstream* OutStringStream;
504 
505  int OpenStream();
506  int OpenFile();
507  int OpenString();
508  void CloseStream();
509  void CloseFile();
510  void CloseString();
511 
512  // The timestep currently being written
515 
516  // Dummy boolean var to start/stop the continue executing:
517  // when using the Start/Stop/WriteNextTime API
518  int UserContinueExecuting; //can only be -1 = invalid, 0 = stop, 1 = start
519 
520  // This variable is used to ease transition to new versions of VTK XML files.
521  // If data that needs to be written satisfies certain conditions,
522  // the writer can use the previous file version version.
523  // For version change 0.1 -> 2.0 (UInt32 header) and 1.0 -> 2.0
524  // (UInt64 header), if data does not have a vtkGhostType array,
525  // the file is written with version: 0.1/1.0.
527 
528  vtkTypeInt64 *NumberOfTimeValues; //one per piece / per timestep
529 
530  friend class vtkXMLWriterHelper;
531 
532 private:
533  vtkXMLWriter(const vtkXMLWriter&) = delete;
534  void operator=(const vtkXMLWriter&) = delete;
535 };
536 
537 #endif
vtkTypeInt64 * NumberOfTimeValues
Definition: vtkXMLWriter.h:528
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:40
int NumberOfTimeSteps
Definition: vtkXMLWriter.h:514
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:235
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:143
char * FileName
Definition: vtkXMLWriter.h:284
Helper class due to PIMPL excess.
vtkTypeBool EncodeAppendedData
Definition: vtkXMLWriter.h:310
int vtkIdType
Definition: vtkType.h:347
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:349
virtual int WriteData()
Definition: vtkXMLWriter.h:360
int vtkTypeBool
Definition: vtkABI.h:69
void SetCompressorTypeToLZ4()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:172
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:59
vtkTypeInt64 AppendedDataPosition
Definition: vtkXMLWriter.h:313
Superclass for VTK&#39;s XML file writers.
Definition: vtkXMLWriter.h:62
a simple class to control print indentation
Definition: vtkIndent.h:39
void SetCompressorTypeToLZMA()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:180
vtkTypeBool WriteToOutputString
Definition: vtkXMLWriter.h:291
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
std::ostringstream * OutStringStream
Definition: vtkXMLWriter.h:503
size_t CompressionBlockNumber
Definition: vtkXMLWriter.h:340
Abstract superclass to iterate over elements in an vtkAbstractArray.
represent and manipulate attribute data in a dataset
ofstream * OutFile
Definition: vtkXMLWriter.h:502
void SetCompressorTypeToNone()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:168
vtkTypeInt64 CompressionHeaderPosition
Definition: vtkXMLWriter.h:342
bool UsePreviousVersion
Definition: vtkXMLWriter.h:526
int CurrentTimeIndex
Definition: vtkXMLWriter.h:513
Wraps a binary output stream with a VTK interface.
Store zero or more vtkInformation instances.
ostream * Stream
Definition: vtkXMLWriter.h:287
std::string OutputString
Definition: vtkXMLWriter.h:294
vtkDataCompressor * Compressor
Definition: vtkXMLWriter.h:338
unsigned char * ByteSwapBuffer
Definition: vtkXMLWriter.h:335
general representation of visualization data
Definition: vtkDataObject.h:64
size_t BlockSize
Definition: vtkXMLWriter.h:339
void SetCompressorTypeToZLib()
Convenience functions to set the compressor to certain known types.
Definition: vtkXMLWriter.h:176
represent and manipulate 3D points
Definition: vtkPoints.h:39
vtkXMLDataHeader * CompressionHeader
Definition: vtkXMLWriter.h:341
int UserContinueExecuting
Definition: vtkXMLWriter.h:518
OffsetsManagerGroup * FieldDataOM
Definition: vtkXMLWriter.h:316
represent and manipulate fields of data
Definition: vtkFieldData.h:56
Int32IdType * Int32IdTypeBuffer
Definition: vtkXMLWriter.h:332