6#ifndef DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
7#define DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
16#include <dune/common/exceptions.hh>
17#include <dune/common/indent.hh>
74 writeFloat32(data);
break;
76 writeFloat64(data);
break;
78 writeUInt32(data);
break;
80 writeUInt8(data);
break;
82 writeInt32(data);
break;
84 DUNE_THROW(Dune::NotImplemented,
"Unknown precision type");
95 virtual void writeFloat32 (
float data) = 0;
97 virtual void writeFloat64 (
double data) = 0;
99 virtual void writeInt32 (std::int32_t data) = 0;
101 virtual void writeUInt8 (std::uint8_t data) = 0;
103 virtual void writeUInt32 (std::uint32_t data) = 0;
122 int ncomps,
const Indent& indent_,
Precision prec_)
123 :
DataArrayWriter(prec_), s(theStream), counter(0), numPerLine(12), indent(indent_)
125 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
126 <<
"Name=\"" << name <<
"\" ";
127 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
128 s <<
"format=\"ascii\">\n";
135 if (counter%numPerLine!=0) s <<
"\n";
137 s << indent <<
"</DataArray>\n";
142 void writeFloat64 (
double data)
final
143 { write_float(data); }
145 void writeFloat32 (
float data)
final
146 { write_float(data); }
148 void writeInt32 (std::int32_t data)
final
151 void writeUInt32 (std::uint32_t data)
final
154 void writeUInt8 (std::uint8_t data)
final
161 if(counter%numPerLine==0) s << indent;
163 const auto original_precision = std::cout.precision();
164 s << std::setprecision(std::numeric_limits<PT>::digits10) << (PT) data;
165 std::cout.precision(original_precision);
167 if (counter%numPerLine==0) s <<
"\n";
171 void write_float(T data)
174 if(counter%numPerLine==0) s << indent;
176 PT out_data = (PT) data;
177 if (std::fpclassify(out_data) == FP_SUBNORMAL)
182 const auto original_precision = std::cout.precision();
183 s << std::setprecision(std::numeric_limits<PT>::digits10) << out_data;
184 std::cout.precision(original_precision);
186 if (counter%numPerLine==0) s <<
"\n";
211 int ncomps,
int nitems,
const Indent& indent_,
Precision prec_)
212 :
DataArrayWriter(prec_), s(theStream), b64(theStream), indent(indent_)
214 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
215 <<
"Name=\"" << name <<
"\" ";
216 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
217 s <<
"format=\"binary\">\n";
222 std::uint32_t size = ncomps*nitems*
typeSize(prec_);
233 s << indent <<
"</DataArray>\n";
239 void writeFloat64 (
double data)
final
242 void writeFloat32 (
float data)
final
245 void writeInt32 (std::int32_t data)
final
248 void writeUInt32 (std::uint32_t data)
final
251 void writeUInt8 (std::uint8_t data)
final
263 const Indent& indent;
284 int ncomps,
unsigned nitems,
unsigned& offset,
288 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
289 <<
"Name=\"" << name <<
"\" ";
290 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
291 s <<
"format=\"appended\" offset=\""<< offset <<
"\" />\n";
293 offset += ncomps*nitems*
typeSize(prec_);
301 void writeFloat64 (
double)
final {}
302 void writeFloat32 (
float)
final {}
303 void writeInt32 (std::int32_t)
final {}
304 void writeUInt32 (std::uint32_t)
final {}
305 void writeUInt8 (std::uint8_t)
final {}
326 int ncomps,
unsigned nitems,
327 unsigned& offset,
const Indent& indent,
Precision prec_)
330 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
331 <<
"Name=\"" << name <<
"\" ";
332 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
333 s <<
"format=\"appended\" offset=\""<< offset <<
"\" />\n";
335 std::size_t bytes = ncomps*nitems*
typeSize(prec_);
346 void writeFloat64 (
double)
final {}
347 void writeFloat32 (
float)
final {}
348 void writeInt32 (std::int32_t)
final {}
349 void writeUInt32 (std::uint32_t)
final {}
350 void writeUInt8 (std::uint8_t)
final {}
374 std::uint32_t size = ncomps*nitems*
typeSize(prec_);
381 void writeFloat64 (
double data)
final
384 void writeFloat32 (
float data)
final
387 void writeInt32 (std::int32_t data)
final
390 void writeUInt32 (std::uint32_t data)
final
393 void writeUInt8 (std::uint8_t data)
final
428 void writeFloat64 (
double data)
final
431 void writeFloat32 (
float data)
final
434 void writeInt32 (std::int32_t data)
final
437 void writeUInt32 (std::uint32_t data)
final
440 void writeUInt8 (std::uint8_t data)
final
463 enum Phase { main, appended };
466 std::ostream& stream;
483 : type(type_), stream(stream_), offset(0), phase(main)
500 case ascii :
return false;
501 case base64 :
return false;
505 DUNE_THROW(IOError,
"Dune::VTK::DataArrayWriter: unsupported "
506 "OutputType " << type);
511 static const std::string rawString =
"raw";
512 static const std::string base64String =
"base64";
517 DUNE_THROW(IOError,
"DataArrayWriterFactory::appendedEncoding(): No "
518 "appended encoding for OutputType " << type);
522 DUNE_THROW(IOError,
"DataArrayWriterFactory::appendedEncoding(): "
523 "unsupported OutputType " << type);
542 unsigned nitems,
const Indent& indent,
555 nitems, offset, indent, prec);
574 DUNE_THROW(IOError,
"Dune::VTK::DataArrayWriter: unsupported "
575 "OutputType " << type <<
" in phase " << phase);
Common stuff for the VTKWriter.
Include standard header files.
Definition: agrid.hh:60
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
@ ascii
Output to the file is in ascii.
Definition: common.hh:45
@ appendedraw
Output is to the file is appended raw binary.
Definition: common.hh:49
@ appendedbase64
Output is to the file is appended base64 binary.
Definition: common.hh:51
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:47
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:280
std::size_t typeSize(Precision p)
map precision to byte size
Definition: common.hh:300
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:97
base class for data array writers
Definition: dataarraywriter.hh:56
void write(T data)
write one element of data
Definition: dataarraywriter.hh:69
DataArrayWriter(Precision _prec)
construct a data array writer
Definition: dataarraywriter.hh:63
virtual bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:89
virtual ~DataArrayWriter()
virtual destructor
Definition: dataarraywriter.hh:91
a streaming writer for data array tags, uses ASCII inline format
Definition: dataarraywriter.hh:110
~AsciiDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:133
AsciiDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, const Indent &indent_, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:121
a streaming writer for data array tags, uses binary inline format
Definition: dataarraywriter.hh:197
~BinaryDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:228
BinaryDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, int nitems, const Indent &indent_, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:210
a streaming writer for data array tags, uses appended raw format
Definition: dataarraywriter.hh:268
AppendedRawDataArrayWriter(std::ostream &s, std::string name, int ncomps, unsigned nitems, unsigned &offset, const Indent &indent, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:283
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:297
a streaming writer for data array tags, uses appended base64 format
Definition: dataarraywriter.hh:310
AppendedBase64DataArrayWriter(std::ostream &s, std::string name, int ncomps, unsigned nitems, unsigned &offset, const Indent &indent, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:325
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:342
a streaming writer for appended data array tags, uses base64 format
Definition: dataarraywriter.hh:360
NakedBase64DataArrayWriter(std::ostream &theStream, int ncomps, int nitems, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:369
a streaming writer for appended data arrays, uses raw format
Definition: dataarraywriter.hh:408
NakedRawDataArrayWriter(std::ostream &theStream, int ncomps, int nitems, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:419
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
DataArrayWriterFactory(OutputType type_, std::ostream &stream_)
create a DataArrayWriterFactory
Definition: dataarraywriter.hh:482
const std::string & appendedEncoding() const
query encoding string for appended data
Definition: dataarraywriter.hh:510
void write(X &data)
encode a data item
Definition: streams.hh:42
void flush()
flush the current unwritten data to the stream.
Definition: streams.hh:64
write out data in binary
Definition: streams.hh:84
void write(T data)
write data to stream
Definition: streams.hh:93