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