Dune Core Modules (unstable)

vtuwriter.hh
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 
6 #ifndef DUNE_GRID_IO_FILE_VTK_VTUWRITER_HH
7 #define DUNE_GRID_IO_FILE_VTK_VTUWRITER_HH
8 
9 #include <ostream>
10 #include <string>
11 
13 #include <dune/common/indent.hh>
14 
17 
18 namespace Dune {
19 
22 
23  namespace VTK {
24 
26 
98  class VTUWriter {
99  public:
100  std::ostream& stream;
101  enum Phase { main, appended } phase;
102 
103  private:
104  DataArrayWriterFactory factory;
105  Indent indent;
106 
107  std::string fileType;
108  std::string cellName;
109 
110  bool doAppended;
111 
112  public:
114 
122  inline VTUWriter(std::ostream& stream_, OutputType outputType,
123  FileType fileType_)
124  : stream(stream_), factory(outputType, stream)
125  {
126  switch(fileType_) {
127  case polyData :
128  fileType = "PolyData";
129  cellName = "Lines";
130  break;
131  case unstructuredGrid :
132  fileType = "UnstructuredGrid";
133  cellName = "Cells";
134  break;
135  default :
136  DUNE_THROW(IOError, "VTUWriter: Unknown fileType: " << fileType_);
137  }
138  const std::string& byteOrder = getEndiannessString();
139 
140  stream << indent << "<?xml version=\"1.0\"?>\n";
141  stream << indent << "<VTKFile"
142  << " type=\"" << fileType << "\""
143  << " version=\"0.1\""
144  << " byte_order=\"" << byteOrder << "\">\n";
145  ++indent;
146  }
147 
149  inline ~VTUWriter() {
150  --indent;
151  stream << indent << "</VTKFile>\n"
152  << std::flush;
153  }
154 
156 
167  inline void beginPointData(const std::string& scalars = "",
168  const std::string& vectors = "") {
169  switch(phase) {
170  case main :
171  stream << indent << "<PointData";
172  if(scalars != "") stream << " Scalars=\"" << scalars << "\"";
173  if(vectors != "") stream << " Vectors=\"" << vectors << "\"";
174  stream << ">\n";
175  ++indent;
176  break;
177  case appended :
178  break;
179  }
180  }
182  inline void endPointData() {
183  switch(phase) {
184  case main :
185  --indent;
186  stream << indent << "</PointData>\n";
187  break;
188  case appended :
189  break;
190  }
191  }
192 
194 
205  inline void beginCellData(const std::string& scalars = "",
206  const std::string& vectors = "") {
207  switch(phase) {
208  case main :
209  stream << indent << "<CellData";
210  if(scalars != "") stream << " Scalars=\"" << scalars << "\"";
211  if(vectors != "") stream << " Vectors=\"" << vectors << "\"";
212  stream << ">\n";
213  ++indent;
214  break;
215  case appended :
216  break;
217  }
218  }
220  inline void endCellData() {
221  switch(phase) {
222  case main :
223  --indent;
224  stream << indent << "</CellData>\n";
225  break;
226  case appended :
227  break;
228  }
229  }
230 
232 
238  inline void beginPoints() {
239  switch(phase) {
240  case main :
241  stream << indent << "<Points>\n";
242  ++indent;
243  break;
244  case appended :
245  break;
246  }
247  }
249  inline void endPoints() {
250  switch(phase) {
251  case main :
252  --indent;
253  stream << indent << "</Points>\n";
254  break;
255  case appended :
256  break;
257  }
258  }
259 
261 
274  inline void beginCells() {
275  switch(phase) {
276  case main :
277  stream << indent << "<" << cellName << ">\n";
278  ++indent;
279  break;
280  case appended :
281  break;
282  }
283  }
285  inline void endCells() {
286  switch(phase) {
287  case main :
288  --indent;
289  stream << indent << "</" << cellName << ">\n";
290  break;
291  case appended :
292  break;
293  }
294  }
295 
297 
310  inline void beginMain(unsigned ncells, unsigned npoints) {
311  stream << indent << "<" << fileType << ">\n";
312  ++indent;
313  stream << indent << "<Piece"
314  << " NumberOf" << cellName << "=\"" << ncells << "\""
315  << " NumberOfPoints=\"" << npoints << "\">\n";
316  ++indent;
317  phase = main;
318  }
320  inline void endMain() {
321  --indent;
322  stream << indent << "</Piece>\n";
323  --indent;
324  stream << indent << "</" << fileType << ">\n";
325  }
326 
328 
345  inline bool beginAppended() {
346  doAppended = factory.beginAppended();
347  if(doAppended) {
348  const std::string& encoding = factory.appendedEncoding();
349  stream << indent << "<AppendedData"
350  << " encoding=\"" << encoding << "\">\n";
351  ++indent;
352  // mark begin of data
353  stream << indent << "_";
354  }
355  phase = appended;
356  return doAppended;
357  }
359  inline void endAppended() {
360  if(doAppended) {
361  stream << "\n";
362  --indent;
363  stream << indent << "</AppendedData>\n";
364  }
365  }
366 
368 
380  DataArrayWriter* makeArrayWriter(const std::string& name,
381  unsigned ncomps, unsigned nitems,
382  Precision prec) {
383  return factory.make(name, ncomps, nitems, indent, prec);
384  }
385  };
386 
387  } // namespace VTK
388 
390 
391 } // namespace Dune
392 
393 #endif // DUNE_GRID_IO_FILE_VTK_VTUWRITER_HH
Default exception class for I/O errors.
Definition: exceptions.hh:231
Utility class for handling nested indentation in output.
Definition: indent.hh:53
a factory for DataArrayWriters
Definition: dataarraywriter.hh:462
bool beginAppended()
signal start of the appended section
Definition: dataarraywriter.hh:497
DataArrayWriter * make(const std::string &name, unsigned ncomps, unsigned nitems, const Indent &indent, Precision prec)
create a DataArrayWriter
Definition: dataarraywriter.hh:541
const std::string & appendedEncoding() const
query encoding string for appended data
Definition: dataarraywriter.hh:510
base class for data array writers
Definition: dataarraywriter.hh:56
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:98
bool beginAppended()
start the appended data section
Definition: vtuwriter.hh:345
void endAppended()
finish the appended data section
Definition: vtuwriter.hh:359
VTUWriter(std::ostream &stream_, OutputType outputType, FileType fileType_)
create a VTUWriter object
Definition: vtuwriter.hh:122
void endCellData()
finish CellData section
Definition: vtuwriter.hh:220
void beginMain(unsigned ncells, unsigned npoints)
start the main PolyData/UnstructuredGrid section
Definition: vtuwriter.hh:310
void beginCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:274
void endPointData()
finish PointData section
Definition: vtuwriter.hh:182
void beginCellData(const std::string &scalars="", const std::string &vectors="")
start CellData section
Definition: vtuwriter.hh:205
void beginPointData(const std::string &scalars="", const std::string &vectors="")
start PointData section
Definition: vtuwriter.hh:167
DataArrayWriter * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems, Precision prec)
acquire a DataArrayWriter
Definition: vtuwriter.hh:380
void endPoints()
finish section for the point coordinates
Definition: vtuwriter.hh:249
~VTUWriter()
write footer
Definition: vtuwriter.hh:149
void endCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:285
void beginPoints()
start section for the point coordinates
Definition: vtuwriter.hh:238
void endMain()
finish the main PolyData/UnstructuredGrid section
Definition: vtuwriter.hh:320
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:271
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:43
FileType
which type of VTK file to write
Definition: common.hh:252
@ polyData
for .vtp files (PolyData)
Definition: common.hh:254
@ unstructuredGrid
for .vtu files (UnstructuredGrid)
Definition: common.hh:256
std::string getEndiannessString()
determine endianness of this C++ implementation
Definition: common.hh:232
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 1, 22:29, 2024)