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