Dune Core Modules (2.8.0)

vtuwriter.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4#ifndef DUNE_GRID_IO_FILE_VTK_VTUWRITER_HH
5#define DUNE_GRID_IO_FILE_VTK_VTUWRITER_HH
6
7#include <ostream>
8#include <string>
9
11#include <dune/common/indent.hh>
12
15
16namespace Dune {
17
20
21 namespace VTK {
22
24
96 class VTUWriter {
97 public:
98 std::ostream& stream;
99 enum Phase { main, appended } phase;
100
101 private:
103 Indent indent;
104
105 std::string fileType;
106 std::string cellName;
107
108 bool doAppended;
109
110 public:
112
120 inline VTUWriter(std::ostream& stream_, OutputType outputType,
121 FileType fileType_)
122 : stream(stream_), factory(outputType, stream)
123 {
124 switch(fileType_) {
125 case polyData :
126 fileType = "PolyData";
127 cellName = "Lines";
128 break;
129 case unstructuredGrid :
130 fileType = "UnstructuredGrid";
131 cellName = "Cells";
132 break;
133 default :
134 DUNE_THROW(IOError, "VTUWriter: Unknown fileType: " << fileType_);
135 }
136 const std::string& byteOrder = getEndiannessString();
137
138 stream << indent << "<?xml version=\"1.0\"?>\n";
139 stream << indent << "<VTKFile"
140 << " type=\"" << fileType << "\""
141 << " version=\"0.1\""
142 << " byte_order=\"" << byteOrder << "\">\n";
143 ++indent;
144 }
145
147 inline ~VTUWriter() {
148 --indent;
149 stream << indent << "</VTKFile>\n"
150 << std::flush;
151 }
152
154
165 inline void beginPointData(const std::string& scalars = "",
166 const std::string& vectors = "") {
167 switch(phase) {
168 case main :
169 stream << indent << "<PointData";
170 if(scalars != "") stream << " Scalars=\"" << scalars << "\"";
171 if(vectors != "") stream << " Vectors=\"" << vectors << "\"";
172 stream << ">\n";
173 ++indent;
174 break;
175 case appended :
176 break;
177 }
178 }
180 inline void endPointData() {
181 switch(phase) {
182 case main :
183 --indent;
184 stream << indent << "</PointData>\n";
185 break;
186 case appended :
187 break;
188 }
189 }
190
192
203 inline void beginCellData(const std::string& scalars = "",
204 const std::string& vectors = "") {
205 switch(phase) {
206 case main :
207 stream << indent << "<CellData";
208 if(scalars != "") stream << " Scalars=\"" << scalars << "\"";
209 if(vectors != "") stream << " Vectors=\"" << vectors << "\"";
210 stream << ">\n";
211 ++indent;
212 break;
213 case appended :
214 break;
215 }
216 }
218 inline void endCellData() {
219 switch(phase) {
220 case main :
221 --indent;
222 stream << indent << "</CellData>\n";
223 break;
224 case appended :
225 break;
226 }
227 }
228
230
236 inline void beginPoints() {
237 switch(phase) {
238 case main :
239 stream << indent << "<Points>\n";
240 ++indent;
241 break;
242 case appended :
243 break;
244 }
245 }
247 inline void endPoints() {
248 switch(phase) {
249 case main :
250 --indent;
251 stream << indent << "</Points>\n";
252 break;
253 case appended :
254 break;
255 }
256 }
257
259
272 inline void beginCells() {
273 switch(phase) {
274 case main :
275 stream << indent << "<" << cellName << ">\n";
276 ++indent;
277 break;
278 case appended :
279 break;
280 }
281 }
283 inline void endCells() {
284 switch(phase) {
285 case main :
286 --indent;
287 stream << indent << "</" << cellName << ">\n";
288 break;
289 case appended :
290 break;
291 }
292 }
293
295
308 inline void beginMain(unsigned ncells, unsigned npoints) {
309 stream << indent << "<" << fileType << ">\n";
310 ++indent;
311 stream << indent << "<Piece"
312 << " NumberOf" << cellName << "=\"" << ncells << "\""
313 << " NumberOfPoints=\"" << npoints << "\">\n";
314 ++indent;
315 phase = main;
316 }
318 inline void endMain() {
319 --indent;
320 stream << indent << "</Piece>\n";
321 --indent;
322 stream << indent << "</" << fileType << ">\n";
323 }
324
326
343 inline bool beginAppended() {
344 doAppended = factory.beginAppended();
345 if(doAppended) {
346 const std::string& encoding = factory.appendedEncoding();
347 stream << indent << "<AppendedData"
348 << " encoding=\"" << encoding << "\">\n";
349 ++indent;
350 // mark begin of data
351 stream << indent << "_";
352 }
353 phase = appended;
354 return doAppended;
355 }
357 inline void endAppended() {
358 if(doAppended) {
359 stream << "\n";
360 --indent;
361 stream << indent << "</AppendedData>\n";
362 }
363 }
364
366
378 DataArrayWriter* makeArrayWriter(const std::string& name,
379 unsigned ncomps, unsigned nitems,
380 Precision prec) {
381 return factory.make(name, ncomps, nitems, indent, prec);
382 }
383 };
384
385 } // namespace VTK
386
388
389} // namespace Dune
390
391#endif // DUNE_GRID_IO_FILE_VTK_VTUWRITER_HH
Default exception class for I/O errors.
Definition: exceptions.hh:229
Utility class for handling nested indentation in output.
Definition: indent.hh:51
a factory for DataArrayWriters
Definition: dataarraywriter.hh:460
bool beginAppended()
signal start of the appended section
Definition: dataarraywriter.hh:495
DataArrayWriter * make(const std::string &name, unsigned ncomps, unsigned nitems, const Indent &indent, Precision prec)
create a DataArrayWriter
Definition: dataarraywriter.hh:539
const std::string & appendedEncoding() const
query encoding string for appended data
Definition: dataarraywriter.hh:508
base class for data array writers
Definition: dataarraywriter.hh:54
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:96
DataArrayWriter * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems, Precision prec)
acquire a DataArrayWriter
Definition: vtuwriter.hh:378
bool beginAppended()
start the appended data section
Definition: vtuwriter.hh:343
void endAppended()
finish the appended data section
Definition: vtuwriter.hh:357
VTUWriter(std::ostream &stream_, OutputType outputType, FileType fileType_)
create a VTUWriter object
Definition: vtuwriter.hh:120
void endCellData()
finish CellData section
Definition: vtuwriter.hh:218
void beginMain(unsigned ncells, unsigned npoints)
start the main PolyData/UnstructuredGrid section
Definition: vtuwriter.hh:308
void beginCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:272
void endPointData()
finish PointData section
Definition: vtuwriter.hh:180
void beginCellData(const std::string &scalars="", const std::string &vectors="")
start CellData section
Definition: vtuwriter.hh:203
void beginPointData(const std::string &scalars="", const std::string &vectors="")
start PointData section
Definition: vtuwriter.hh:165
void endPoints()
finish section for the point coordinates
Definition: vtuwriter.hh:247
~VTUWriter()
write footer
Definition: vtuwriter.hh:147
void endCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:283
void beginPoints()
start section for the point coordinates
Definition: vtuwriter.hh:236
void endMain()
finish the main PolyData/UnstructuredGrid section
Definition: vtuwriter.hh:318
Data array writers for the VTKWriter.
A few common exception classes.
Common stuff for the VTKWriter.
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:269
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:41
FileType
which type of VTK file to write
Definition: common.hh:250
@ polyData
for .vtp files (PolyData)
Definition: common.hh:252
@ unstructuredGrid
for .vtu files (UnstructuredGrid)
Definition: common.hh:254
std::string getEndiannessString()
determine endianness of this C++ implementation
Definition: common.hh:230
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignedallocator.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)