4 #ifndef DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
5 #define DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
14 #include <dune/common/exceptions.hh>
15 #include <dune/common/indent.hh>
72 writeFloat32(data);
break;
74 writeFloat64(data);
break;
76 writeUInt32(data);
break;
78 writeUInt8(data);
break;
80 writeInt32(data);
break;
82 DUNE_THROW(Dune::NotImplemented,
"Unknown precision type");
93 virtual void writeFloat32 (
float data) = 0;
95 virtual void writeFloat64 (
double data) = 0;
97 virtual void writeInt32 (std::int32_t data) = 0;
101 virtual void writeUInt32 (std::uint32_t data) = 0;
120 int ncomps,
const Indent& indent_,
Precision prec)
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
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";
209 int ncomps,
int nitems,
const Indent& indent_,
Precision prec)
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
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 data)
final {}
300 void writeFloat32 (
float data)
final {}
301 void writeInt32 (std::int32_t data)
final {}
302 void writeUInt32 (std::uint32_t data)
final {}
324 int ncomps,
unsigned nitems,
325 unsigned& offset,
const Indent& indent,
Precision prec)
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 data)
final {}
345 void writeFloat32 (
float data)
final {}
346 void writeInt32 (std::int32_t data)
final {}
347 void writeUInt32 (std::uint32_t data)
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
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
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;
503 DUNE_THROW(IOError,
"Dune::VTK::DataArrayWriter: unsupported "
504 "OutputType " << type);
509 static const std::string rawString =
"raw";
510 static const std::string base64String =
"base64";
515 DUNE_THROW(IOError,
"DataArrayWriterFactory::appendedEncoding(): No "
516 "appended encoding for OutputType " << type);
520 DUNE_THROW(IOError,
"DataArrayWriterFactory::appendedEncoding(): "
521 "unsupported OutputType " << type);
540 unsigned nitems,
const Indent& indent,
553 nitems, offset, indent, prec);
572 DUNE_THROW(IOError,
"Dune::VTK::DataArrayWriter: unsupported "
573 "OutputType " << type <<
" in phase " << phase);
Common stuff for the VTKWriter.
unsigned char uint8_t
Definition: yaspgrid.hh:16
Include standard header files.
Definition: agrid.hh:59
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:319
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:42
@ ascii
Output to the file is in ascii.
Definition: common.hh:44
@ appendedraw
Output is to the file is appended raw binary.
Definition: common.hh:48
@ appendedbase64
Output is to the file is appended base64 binary.
Definition: common.hh:50
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:46
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:328
std::size_t typeSize(Precision p)
map precision to byte size
Definition: common.hh:348
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:96
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 data array tags, uses ASCII inline format
Definition: dataarraywriter.hh:108
AsciiDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, const Indent &indent_, Precision prec)
make a new data array writer
Definition: dataarraywriter.hh:119
~AsciiDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:131
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 streaming writer for data array tags, uses appended raw format
Definition: dataarraywriter.hh:266
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:295
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
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 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 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
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
write out data in binary
Definition: streams.hh:82
void write(T data)
write data to stream
Definition: streams.hh:91