VTK  9.7.20260805
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
20
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
29VTK_ABI_NAMESPACE_BEGIN
32
33template <class T>
35
36class vtkCellData;
37class vtkDataArray;
39class vtkDataSet;
41class vtkFieldData;
42class vtkOutputStream;
43class vtkPointData;
44class vtkPoints;
45class vtkFieldData;
47
48class vtkStdString;
49class OffsetsManager; // one per piece/per time
50class OffsetsManagerGroup; // array of OffsetsManager
51class OffsetsManagerArray; // array of OffsetsManagerGroup
52
53class VTKIOXML_EXPORT vtkXMLWriter : public vtkXMLWriterBase
54{
55public:
57 void PrintSelf(ostream& os, vtkIndent indent) override;
58
60
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
106protected:
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.
120
121 // appended data offsets for field data
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.
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.
168 virtual int StartFile();
169 virtual void WriteFileAttributes();
170 virtual int EndFile();
172 void DeleteAFile(const char* name);
173
174 virtual int WritePrimaryElement(ostream& os, vtkIndent indent);
175 virtual void WritePrimaryElementAttributes(ostream& os, vtkIndent indent);
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();
190 vtkTypeInt64 streamPos, vtkTypeInt64& lastoffset, const char* attr = nullptr);
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.
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);
221 const char* alternateName = nullptr, int writeNumTuples = 0);
223
225 const char* alternateName = nullptr, int writeNumTuples = 0, int timestep = 0);
229 void WriteArrayAppendedData(vtkAbstractArray* a, vtkTypeInt64 pos, vtkTypeInt64& lastoffset);
230
231 // Methods for writing points, point data, and cell data.
241 void WriteCellDataAppendedData(vtkCellData* cd, int timestep, OffsetsManagerGroup* cdManager);
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);
247 vtkDataArray* xc, vtkDataArray* yc, vtkDataArray* zc, vtkIndent indent);
249 vtkIndent indent, OffsetsManagerGroup* coordManager);
251 int timestep, OffsetsManagerGroup* coordManager);
254 void WritePPoints(vtkPoints* points, vtkIndent indent);
255 void WritePArray(vtkAbstractArray* a, vtkIndent indent, const char* alternateName = nullptr);
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);
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);
279
280 // This shallows copy input field data to the passed field data and
281 // then adds any additional field arrays. For example, TimeValue.
283
284 ostream* OutFile;
285 std::ostringstream* OutStringStream;
286
288 int OpenFile();
291 void CloseFile();
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
307private:
308 vtkXMLWriter(const vtkXMLWriter&) = delete;
309 void operator=(const vtkXMLWriter&) = delete;
310};
311
312VTK_ABI_NAMESPACE_END
313#endif
Helper class due to PIMPL excess.
Abstract superclass for all arrays.
Implementation template for a array iterator.
Abstract superclass to iterate over elements in an vtkAbstractArray.
represent and manipulate cell attribute data
Definition vtkCellData.h:32
Abstract interface for data compression classes.
general representation of visualization data
represent and manipulate attribute data in a dataset
abstract class to specify dataset behavior
Definition vtkDataSet.h:56
Represents and manipulates a collection of data arrays.
a simple class to control print indentation
Definition vtkIndent.h:29
Store zero or more vtkInformation instances.
Store vtkAlgorithm input/output information.
Wraps a binary output stream with a VTK interface.
represent and manipulate point attribute data
represent and manipulate 3D points
Definition vtkPoints.h:31
Wrapper around std::string to keep symbols short.
void EndAppendedData()
int WriteBinaryDataInternal(vtkAbstractArray *a)
int WriteBinaryDataBlock(unsigned char *in_data, size_t numWords, int wordType)
void WriteFieldData(vtkIndent indent)
void SetInputData(vtkDataObject *)
Assign a data object as input.
virtual const char * GetDataSetName()=0
friend class vtkXMLWriterHelper
void Stop()
API to interface an outside the VTK pipeline control.
ostream * OutFile
int WriteBinaryData(vtkAbstractArray *a)
unsigned char * ByteSwapBuffer
void WritePCellData(vtkCellData *cd, vtkIndent indent)
int UserContinueExecuting
void WriteArrayAppendedData(vtkAbstractArray *a, vtkTypeInt64 pos, vtkTypeInt64 &lastoffset)
int WriteCompressionHeader()
void WriteFieldDataAppendedData(vtkFieldData *fd, int timestep, OffsetsManagerGroup *fdManager)
int WriteVectorAttribute(const char *name, int length, VTK_FUTURE_CONST float *data)
int WriteScalarAttribute(const char *name, int data)
void DestroyStringArray(int numStrings, char **strings)
vtkXMLDataHeader * CompressionHeader
void ForwardAppendedDataDouble(vtkTypeInt64 streamPos, double value, const char *attr)
void WritePointDataAppended(vtkPointData *pd, vtkIndent indent, OffsetsManagerGroup *pdManager)
void DeleteAFile()
void WritePPointData(vtkPointData *pd, vtkIndent indent)
void WritePPoints(vtkPoints *points, vtkIndent indent)
vtkDataObject * GetInput()
Assign a data object as input.
vtkTypeInt64 AppendedDataPosition
void WriteFieldDataAppended(vtkFieldData *fd, vtkIndent indent, OffsetsManagerGroup *fdManager)
int WriteStringAttribute(const char *name, const char *value)
bool WriteInformation(vtkInformation *info, vtkIndent indent)
~vtkXMLWriter() override
char ** CreateStringArray(int numStrings)
virtual int WriteData()
int WriteCompressionBlock(unsigned char *data, size_t size)
size_t GetOutputWordTypeSize(int dataType)
void UpdateFieldData(vtkFieldData *)
std::ostringstream * OutStringStream
void SetInputData(int, vtkDataObject *)
Assign a data object as input.
virtual void UpdateProgressDiscrete(float progress)
virtual int EndFile()
void ForwardAppendedDataOffset(vtkTypeInt64 streamPos, vtkTypeInt64 offset, const char *attr=nullptr)
void WritePointsAppended(vtkPoints *points, vtkIndent indent, OffsetsManager *manager)
int WriteDataModeAttribute(const char *name)
void WritePArray(vtkAbstractArray *a, vtkIndent indent, const char *alternateName=nullptr)
void Start()
API to interface an outside the VTK pipeline control.
void WritePointsAppendedData(vtkPoints *points, int timestep, OffsetsManager *pdManager)
void WriteCoordinatesAppendedData(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, int timestep, OffsetsManagerGroup *coordManager)
Int32IdType * Int32IdTypeBuffer
void WriteArrayHeader(vtkAbstractArray *a, vtkIndent indent, const char *alternateName, int writeNumTuples, int timestep)
virtual void WriteArrayInline(vtkAbstractArray *a, vtkIndent indent, const char *alternateName=nullptr, int writeNumTuples=0)
void WritePointsInline(vtkPoints *points, vtkIndent indent)
virtual void SetProgressRange(const float range[2], int curStep, const float *fractions)
vtkTypeInt64 GetAppendedDataOffset()
void WriteCellDataAppendedData(vtkCellData *cd, int timestep, OffsetsManagerGroup *cdManager)
virtual void SetDataStream(vtkOutputStream *)
vtkTypeBool ProcessRequest(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
Upstream/Downstream requests form the generalized interface through which executives invoke a algorit...
int WriteScalarAttribute(const char *name, double data)
void WriteCellDataAppended(vtkCellData *cd, vtkIndent indent, OffsetsManagerGroup *cdManager)
virtual void WriteFileAttributes()
void CloseStream()
int WriteVectorAttribute(const char *name, int length, VTK_FUTURE_CONST double *data)
void WritePCoordinates(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, vtkIndent indent)
void WriteCoordinatesInline(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, vtkIndent indent)
vtkTypeInt64 * NumberOfTimeValues
void WriteCoordinatesAppended(vtkDataArray *xc, vtkDataArray *yc, vtkDataArray *zc, vtkIndent indent, OffsetsManagerGroup *coordManager)
size_t CompressionBlockNumber
vtkOutputStream * DataStream
void WriteFieldDataInline(vtkFieldData *fd, vtkIndent indent)
void CloseFile()
void WritePointDataInline(vtkPointData *pd, vtkIndent indent)
int OpenStream()
vtkDataSet * GetDataSetInput()
void WriteArrayAppended(vtkAbstractArray *a, vtkIndent indent, OffsetsManager &offs, const char *alternateName=nullptr, int writeNumTuples=0, int timestep=0)
ostream * Stream
OffsetsManagerGroup * FieldDataOM
int OpenString()
virtual void GetProgressRange(float range[2])
vtkTypeInt64 ReserveAttributeSpace(const char *attr, size_t length=20)
virtual void SetProgressRange(const float range[2], int curStep, int numSteps)
virtual void WriteInlineData(vtkAbstractArray *a, vtkIndent indent)
int CreateCompressionHeader(size_t size)
int WriteVectorAttribute(const char *name, int length, VTK_FUTURE_CONST int *data)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual int WritePrimaryElement(ostream &os, vtkIndent indent)
virtual void SetProgressPartial(float fraction)
virtual int StartFile()
float ProgressRange[2]
int WriteScalarAttribute(const char *name, float data)
void WriteNextTime(double time)
API to interface an outside the VTK pipeline control.
void DeleteAFile(const char *name)
const char * GetWordTypeName(int dataType)
void WritePointDataAppendedData(vtkPointData *pd, int timestep, OffsetsManagerGroup *pdManager)
void StartAppendedData()
void CloseString()
vtkTypeInt64 CompressionHeaderPosition
void WriteAppendedDataOffset(vtkTypeInt64 streamPos, vtkTypeInt64 &lastoffset, const char *attr=nullptr)
vtkDataObject * GetInput(int port)
Assign a data object as input.
void WriteAttributeIndices(vtkDataSetAttributes *dsa, char **names)
int WriteAsciiData(vtkAbstractArray *a, vtkIndent indent)
virtual void WriteArrayFooter(ostream &os, vtkIndent indent, vtkAbstractArray *a, int shortFormat)
size_t GetWordTypeSize(int dataType)
virtual int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
virtual void WritePrimaryElementAttributes(ostream &os, vtkIndent indent)
virtual int WriteInternal()
void WriteCellDataInline(vtkCellData *cd, vtkIndent indent)
int WriteWordTypeAttribute(const char *name, int dataType)
virtual int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
void PerformByteSwap(void *data, size_t numWords, size_t wordSize)
int vtkTypeBool
Definition vtkABI.h:64
#define vtkDataArray
int vtkIdType
Definition vtkType.h:363