VTK  9.7.1
vtkXMLWriter.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
21 #ifndef vtkXMLWriter_h
22 #define vtkXMLWriter_h
23 
24 #include "vtkIOXMLModule.h" // For export macro
25 #include "vtkXMLWriterBase.h"
26 
27 #include <sstream> // For ostringstream ivar
28 
29 VTK_ABI_NAMESPACE_BEGIN
30 class vtkAbstractArray;
31 class vtkArrayIterator;
32 
33 template <class T>
35 
36 class vtkCellData;
37 class vtkDataArray;
38 class vtkDataCompressor;
39 class vtkDataSet;
41 class vtkFieldData;
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 vtkXMLWriterBase
54 {
55 public:
57  void PrintSelf(ostream& os, vtkIndent indent) override;
58 
60 
65  void SetInputData(vtkDataObject*);
66  void SetInputData(int, vtkDataObject*);
67  vtkDataObject* GetInput(int port);
68  vtkDataObject* GetInput() { return this->GetInput(0); }
70 
71  // See the vtkAlgorithm for a description of what these do
73  vtkInformationVector* outputVector) override;
74 
76 
79  vtkGetMacro(NumberOfTimeSteps, int);
80  vtkSetMacro(NumberOfTimeSteps, int);
82 
84 
87  void Start();
88  void Stop();
89  void WriteNextTime(double time);
91 
93 
102  vtkSetClampMacro(Precision, int, 0, 17);
103  vtkGetMacro(Precision, int);
105 
106 protected:
107  vtkXMLWriter();
108  ~vtkXMLWriter() override;
109 
110  virtual int RequestInformation(vtkInformation* request, vtkInformationVector** inputVector,
111  vtkInformationVector* outputVector);
112  virtual int RequestData(vtkInformation* request, vtkInformationVector** inputVector,
113  vtkInformationVector* outputVector);
114 
115  // The output stream to which the XML is written.
116  ostream* Stream;
117 
118  // The stream position at which appended data starts.
119  vtkTypeInt64 AppendedDataPosition;
120 
121  // appended data offsets for field data
122  OffsetsManagerGroup* FieldDataOM; // one per array
123 
124  // We need a 32 bit signed integer type to which vtkIdType will be
125  // converted if Int32 is specified for the IdType parameter to this
126  // writer.
127 #if VTK_SIZEOF_SHORT == 4
128  typedef short Int32IdType;
129 #elif VTK_SIZEOF_INT == 4
130  typedef int Int32IdType;
131 #elif VTK_SIZEOF_LONG == 4
132  typedef long Int32IdType;
133 #else
134 #error "No native data type can represent a signed 32-bit integer."
135 #endif
136 
137  // Buffer for vtkIdType conversion.
138  Int32IdType* Int32IdTypeBuffer;
139 
140  // The byte swapping buffer.
141  unsigned char* ByteSwapBuffer;
142 
143  // Compression information.
147 
148  // The output stream used to write binary and appended data. May
149  // transparently encode the data.
151 
152  // Allow subclasses to set the data stream.
153  virtual void SetDataStream(vtkOutputStream*);
154  vtkGetObjectMacro(DataStream, vtkOutputStream);
155 
156  // Method to drive most of actual writing.
157  virtual int WriteInternal();
158 
159  // Method defined by subclasses to write data. Return 1 for
160  // success, 0 for failure.
161  virtual int WriteData() { return 1; }
162 
163  // Method defined by subclasses to specify the data set's type name.
164  virtual const char* GetDataSetName() = 0;
165 
166  // Utility methods for subclasses.
167  vtkDataSet* GetDataSetInput();
168  virtual int StartFile();
169  virtual void WriteFileAttributes();
170  virtual int EndFile();
171  void DeleteAFile();
172  void DeleteAFile(const char* name);
173 
174  virtual int WritePrimaryElement(ostream& os, vtkIndent indent);
175  virtual void WritePrimaryElementAttributes(ostream& os, vtkIndent indent);
176  void StartAppendedData();
177  void EndAppendedData();
178 
179  // Write enough space to go back and write the given attribute with
180  // at most "length" characters in the value. Returns the stream
181  // position at which attribute should be later written. The default
182  // length of 20 is enough for a 64-bit integer written in decimal or
183  // a double-precision floating point value written to 13 digits of
184  // precision (the other 7 come from a minus sign, decimal place, and
185  // a big exponent like "e+300").
186  vtkTypeInt64 ReserveAttributeSpace(const char* attr, size_t length = 20);
187 
188  vtkTypeInt64 GetAppendedDataOffset();
189  void WriteAppendedDataOffset(
190  vtkTypeInt64 streamPos, vtkTypeInt64& lastoffset, const char* attr = nullptr);
191  void ForwardAppendedDataOffset(
192  vtkTypeInt64 streamPos, vtkTypeInt64 offset, const char* attr = nullptr);
193  void ForwardAppendedDataDouble(vtkTypeInt64 streamPos, double value, const char* attr);
194 
195  int WriteScalarAttribute(const char* name, int data);
196  int WriteScalarAttribute(const char* name, float data);
197  int WriteScalarAttribute(const char* name, double data);
198 #ifdef VTK_USE_64BIT_IDS
199  int WriteScalarAttribute(const char* name, vtkIdType data);
200 #endif
201 
202  int WriteVectorAttribute(const char* name, int length, VTK_FUTURE_CONST int* data);
203  int WriteVectorAttribute(const char* name, int length, VTK_FUTURE_CONST float* data);
204  int WriteVectorAttribute(const char* name, int length, VTK_FUTURE_CONST double* data);
205 #ifdef VTK_USE_64BIT_IDS
206  int WriteVectorAttribute(const char* name, int length, vtkIdType* data);
207 #endif
208 
209  int WriteDataModeAttribute(const char* name);
210  int WriteWordTypeAttribute(const char* name, int dataType);
211  int WriteStringAttribute(const char* name, const char* value);
212 
213  // Returns true if any keys were written.
214  bool WriteInformation(vtkInformation* info, vtkIndent indent);
215 
216  void WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent, const char* alternateName,
217  int writeNumTuples, int timestep);
218  virtual void WriteArrayFooter(
219  ostream& os, vtkIndent indent, vtkAbstractArray* a, int shortFormat);
220  virtual void WriteArrayInline(vtkAbstractArray* a, vtkIndent indent,
221  const char* alternateName = nullptr, int writeNumTuples = 0);
222  virtual void WriteInlineData(vtkAbstractArray* a, vtkIndent indent);
223 
224  void WriteArrayAppended(vtkAbstractArray* a, vtkIndent indent, OffsetsManager& offs,
225  const char* alternateName = nullptr, int writeNumTuples = 0, int timestep = 0);
226  int WriteAsciiData(vtkAbstractArray* a, vtkIndent indent);
227  int WriteBinaryData(vtkAbstractArray* a);
228  int WriteBinaryDataInternal(vtkAbstractArray* a);
229  void WriteArrayAppendedData(vtkAbstractArray* a, vtkTypeInt64 pos, vtkTypeInt64& lastoffset);
230 
231  // Methods for writing points, point data, and cell data.
232  void WriteFieldData(vtkIndent indent);
233  void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
234  void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
235  void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
236  void WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent, OffsetsManagerGroup* fdManager);
237  void WriteFieldDataAppendedData(vtkFieldData* fd, int timestep, OffsetsManagerGroup* fdManager);
238  void WritePointDataAppended(vtkPointData* pd, vtkIndent indent, OffsetsManagerGroup* pdManager);
239  void WritePointDataAppendedData(vtkPointData* pd, int timestep, OffsetsManagerGroup* pdManager);
240  void WriteCellDataAppended(vtkCellData* cd, vtkIndent indent, OffsetsManagerGroup* cdManager);
241  void WriteCellDataAppendedData(vtkCellData* cd, int timestep, OffsetsManagerGroup* cdManager);
242  void WriteAttributeIndices(vtkDataSetAttributes* dsa, char** names);
243  void WritePointsAppended(vtkPoints* points, vtkIndent indent, OffsetsManager* manager);
244  void WritePointsAppendedData(vtkPoints* points, int timestep, OffsetsManager* pdManager);
245  void WritePointsInline(vtkPoints* points, vtkIndent indent);
246  void WriteCoordinatesInline(
247  vtkDataArray* xc, vtkDataArray* yc, vtkDataArray* zc, vtkIndent indent);
248  void WriteCoordinatesAppended(vtkDataArray* xc, vtkDataArray* yc, vtkDataArray* zc,
249  vtkIndent indent, OffsetsManagerGroup* coordManager);
250  void WriteCoordinatesAppendedData(vtkDataArray* xc, vtkDataArray* yc, vtkDataArray* zc,
251  int timestep, OffsetsManagerGroup* coordManager);
252  void WritePPointData(vtkPointData* pd, vtkIndent indent);
253  void WritePCellData(vtkCellData* cd, vtkIndent indent);
254  void WritePPoints(vtkPoints* points, vtkIndent indent);
255  void WritePArray(vtkAbstractArray* a, vtkIndent indent, const char* alternateName = nullptr);
256  void WritePCoordinates(vtkDataArray* xc, vtkDataArray* yc, vtkDataArray* zc, vtkIndent indent);
257 
258  // Internal utility methods.
259  int WriteBinaryDataBlock(unsigned char* in_data, size_t numWords, int wordType);
260  void PerformByteSwap(void* data, size_t numWords, size_t wordSize);
261  int CreateCompressionHeader(size_t size);
262  int WriteCompressionBlock(unsigned char* data, size_t size);
263  int WriteCompressionHeader();
264  size_t GetWordTypeSize(int dataType);
265  const char* GetWordTypeName(int dataType);
266  size_t GetOutputWordTypeSize(int dataType);
267 
268  char** CreateStringArray(int numStrings);
269  void DestroyStringArray(int numStrings, char** strings);
270 
271  // The current range over which progress is moving. This allows for
272  // incrementally fine-tuned progress updates.
273  virtual void GetProgressRange(float range[2]);
274  virtual void SetProgressRange(const float range[2], int curStep, int numSteps);
275  virtual void SetProgressRange(const float range[2], int curStep, const float* fractions);
276  virtual void SetProgressPartial(float fraction);
277  virtual void UpdateProgressDiscrete(float progress);
278  float ProgressRange[2];
279 
280  // This shallows copy input field data to the passed field data and
281  // then adds any additional field arrays. For example, TimeValue.
282  void UpdateFieldData(vtkFieldData*);
283 
284  ostream* OutFile;
285  std::ostringstream* OutStringStream;
286 
287  int OpenStream();
288  int OpenFile();
289  int OpenString();
290  void CloseStream();
291  void CloseFile();
292  void CloseString();
293 
294  // The timestep currently being written
297 
298  // Dummy boolean var to start/stop the continue executing:
299  // when using the Start/Stop/WriteNextTime API
300  int UserContinueExecuting; // can only be -1 = invalid, 0 = stop, 1 = start
301 
302  vtkTypeInt64* NumberOfTimeValues; // one per piece / per timestep
304 
305  friend class vtkXMLWriterHelper;
306 
307 private:
308  vtkXMLWriter(const vtkXMLWriter&) = delete;
309  void operator=(const vtkXMLWriter&) = delete;
310 };
311 
312 VTK_ABI_NAMESPACE_END
313 #endif
vtkTypeInt64 * NumberOfTimeValues
Definition: vtkXMLWriter.h:302
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:27
int NumberOfTimeSteps
Definition: vtkXMLWriter.h:296
Abstract interface for data compression classes.
represent and manipulate point attribute data
Definition: vtkPointData.h:30
vtkDataObject * GetInput()
Assign a data object as input.
Definition: vtkXMLWriter.h:68
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:31
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
ostream * OutFile
Definition: vtkXMLWriter.h:284
Helper class due to PIMPL excess.
int vtkIdType
Definition: vtkType.h:363
Implementation template for a array iterator.
vtkOutputStream * DataStream
Definition: vtkXMLWriter.h:150
virtual int WriteData()
Definition: vtkXMLWriter.h:161
int vtkTypeBool
Definition: vtkABI.h:64
virtual vtkTypeBool ProcessRequest(vtkInformation *request, vtkInformationVector **inInfo, vtkInformationVector *outInfo)
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
vtkTypeInt64 AppendedDataPosition
Definition: vtkXMLWriter.h:119
Superclass for VTK's XML file writers.
Definition: vtkXMLWriter.h:53
a simple class to control print indentation
Definition: vtkIndent.h:28
Abstract base class for VTK-XML writers.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
std::ostringstream * OutStringStream
Definition: vtkXMLWriter.h:285
size_t CompressionBlockNumber
Definition: vtkXMLWriter.h:144
Abstract superclass to iterate over elements in an vtkAbstractArray.
represent and manipulate attribute data in a dataset
vtkTypeInt64 CompressionHeaderPosition
Definition: vtkXMLWriter.h:146
int CurrentTimeIndex
Definition: vtkXMLWriter.h:295
Wraps a binary output stream with a VTK interface.
Store zero or more vtkInformation instances.
ostream * Stream
Definition: vtkXMLWriter.h:116
unsigned char * ByteSwapBuffer
Definition: vtkXMLWriter.h:141
general representation of visualization data
Definition: vtkDataObject.h:55
represent and manipulate 3D points
Definition: vtkPoints.h:30
vtkXMLDataHeader * CompressionHeader
Definition: vtkXMLWriter.h:145
int UserContinueExecuting
Definition: vtkXMLWriter.h:300
OffsetsManagerGroup * FieldDataOM
Definition: vtkXMLWriter.h:122
Represents and manipulates a collection of data arrays.
Definition: vtkFieldData.h:51
Int32IdType * Int32IdTypeBuffer
Definition: vtkXMLWriter.h:138