4#ifndef DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
5#define DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
17#include <dune/grid/io/file/vtk/streams.hh>
71 case Precision::float32:
72 writeFloat32(data);
break;
73 case Precision::float64:
74 writeFloat64(data);
break;
75 case Precision::uint32:
76 writeUInt32(data);
break;
77 case Precision::uint8:
78 writeUInt8(data);
break;
79 case Precision::int32:
80 writeInt32(data);
break;
93 virtual void writeFloat32 (
float data) = 0;
95 virtual void writeFloat64 (
double data) = 0;
97 virtual void writeInt32 (std::int32_t data) = 0;
99 virtual void writeUInt8 (std::uint8_t data) = 0;
101 virtual void writeUInt32 (std::uint32_t data) = 0;
121 :
DataArrayWriter(prec_), s(theStream), counter(0), numPerLine(12), indent(indent_)
123 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
124 <<
"Name=\"" << name <<
"\" ";
125 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
126 s <<
"format=\"ascii\">\n";
133 if (counter%numPerLine!=0) s <<
"\n";
135 s << indent <<
"</DataArray>\n";
140 void writeFloat64 (
double data)
final
141 { write_float(data); }
143 void writeFloat32 (
float data)
final
144 { write_float(data); }
146 void writeInt32 (std::int32_t data)
final
149 void writeUInt32 (std::uint32_t data)
final
152 void writeUInt8 (std::uint8_t data)
final
159 if(counter%numPerLine==0) s << indent;
161 const auto original_precision = std::cout.precision();
162 s << std::setprecision(std::numeric_limits<PT>::digits10) << (PT) data;
163 std::cout.precision(original_precision);
165 if (counter%numPerLine==0) s <<
"\n";
169 void write_float(T data)
172 if(counter%numPerLine==0) s << indent;
174 PT out_data = (PT) data;
175 if (std::fpclassify(out_data) == FP_SUBNORMAL)
180 const auto original_precision = std::cout.precision();
181 s << std::setprecision(std::numeric_limits<PT>::digits10) << out_data;
182 std::cout.precision(original_precision);
184 if (counter%numPerLine==0) s <<
"\n";
210 :
DataArrayWriter(prec_), s(theStream), b64(theStream), indent(indent_)
212 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
213 <<
"Name=\"" << name <<
"\" ";
214 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
215 s <<
"format=\"binary\">\n";
220 std::uint32_t size = ncomps*nitems*
typeSize(prec_);
231 s << indent <<
"</DataArray>\n";
237 void writeFloat64 (
double data)
final
240 void writeFloat32 (
float data)
final
243 void writeInt32 (std::int32_t data)
final
246 void writeUInt32 (std::uint32_t data)
final
249 void writeUInt8 (std::uint8_t data)
final
261 const Indent& indent;
282 int ncomps,
unsigned nitems,
unsigned& offset,
286 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
287 <<
"Name=\"" << name <<
"\" ";
288 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
289 s <<
"format=\"appended\" offset=\""<< offset <<
"\" />\n";
291 offset += ncomps*nitems*
typeSize(prec_);
299 void writeFloat64 (
double)
final {}
300 void writeFloat32 (
float)
final {}
301 void writeInt32 (std::int32_t)
final {}
302 void writeUInt32 (std::uint32_t)
final {}
303 void writeUInt8 (std::uint8_t)
final {}
324 int ncomps,
unsigned nitems,
328 s << indent <<
"<DataArray type=\"" <<
toString(prec_) <<
"\" "
329 <<
"Name=\"" << name <<
"\" ";
330 s <<
"NumberOfComponents=\"" << ncomps <<
"\" ";
331 s <<
"format=\"appended\" offset=\""<< offset <<
"\" />\n";
333 std::size_t bytes = ncomps*nitems*
typeSize(prec_);
344 void writeFloat64 (
double)
final {}
345 void writeFloat32 (
float)
final {}
346 void writeInt32 (std::int32_t)
final {}
347 void writeUInt32 (std::uint32_t)
final {}
348 void writeUInt8 (std::uint8_t)
final {}
372 std::uint32_t size = ncomps*nitems*
typeSize(prec_);
379 void writeFloat64 (
double data)
final
382 void writeFloat32 (
float data)
final
385 void writeInt32 (std::int32_t data)
final
388 void writeUInt32 (std::uint32_t data)
final
391 void writeUInt8 (std::uint8_t data)
final
426 void writeFloat64 (
double data)
final
429 void writeFloat32 (
float data)
final
432 void writeInt32 (std::int32_t data)
final
435 void writeUInt32 (std::uint32_t data)
final
438 void writeUInt8 (std::uint8_t data)
final
461 enum Phase { main, appended };
464 std::ostream& stream;
481 : type(type_), stream(stream_), offset(0), phase(main)
498 case ascii :
return false;
499 case base64 :
return false;
504 "OutputType " << type);
509 static const std::string rawString =
"raw";
510 static const std::string base64String =
"base64";
516 "appended encoding for OutputType " << type);
521 "unsupported OutputType " << type);
540 unsigned nitems,
const Indent& indent,
553 nitems, offset, indent, prec);
573 "OutputType " << type <<
" in phase " << phase);
void write(X &data)
encode a data item
Definition: streams.hh:40
void flush()
flush the current unwritten data to the stream.
Definition: streams.hh:62
Default exception class for I/O errors.
Definition: exceptions.hh:229
Utility class for handling nested indentation in output.
Definition: indent.hh:51
Default exception for dummy implementations.
Definition: exceptions.hh:261
write out data in binary
Definition: streams.hh:82
void write(T data)
write data to stream
Definition: streams.hh:91
a streaming writer for data array tags, uses appended base64 format
Definition: dataarraywriter.hh:308
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:323
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:340
a streaming writer for data array tags, uses appended raw format
Definition: dataarraywriter.hh:266
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:281
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:295
a streaming writer for data array tags, uses ASCII inline format
Definition: dataarraywriter.hh:108
~AsciiDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:131
AsciiDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, const Indent &indent_, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:119
a streaming writer for data array tags, uses binary inline format
Definition: dataarraywriter.hh:195
~BinaryDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:226
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:208
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
DataArrayWriterFactory(OutputType type_, std::ostream &stream_)
create a DataArrayWriterFactory
Definition: dataarraywriter.hh:480
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
void write(T data)
write one element of data
Definition: dataarraywriter.hh:67
DataArrayWriter(Precision _prec)
construct a data array writer
Definition: dataarraywriter.hh:61
virtual bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:87
virtual ~DataArrayWriter()
virtual destructor
Definition: dataarraywriter.hh:89
a streaming writer for appended data array tags, uses base64 format
Definition: dataarraywriter.hh:358
NakedBase64DataArrayWriter(std::ostream &theStream, int ncomps, int nitems, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:367
a streaming writer for appended data arrays, uses raw format
Definition: dataarraywriter.hh:406
NakedRawDataArrayWriter(std::ostream &theStream, int ncomps, int nitems, Precision prec_)
make a new data array writer
Definition: dataarraywriter.hh:417
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
@ ascii
Output to the file is in ascii.
Definition: common.hh:43
@ appendedraw
Output is to the file is appended raw binary.
Definition: common.hh:47
@ appendedbase64
Output is to the file is appended base64 binary.
Definition: common.hh:49
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:45
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:278
std::size_t typeSize(Precision p)
map precision to byte size
Definition: common.hh:298
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignedallocator.hh:11
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:95