Dune Core Modules (2.4.1)

dataarraywriter.hh
Go to the documentation of this file.
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_DATAARRAYWRITER_HH
5#define DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
6
7#include <cstdint>
8#include <ostream>
9#include <string>
10
12#include <dune/common/indent.hh>
13
14#include <dune/grid/io/file/vtk/streams.hh>
16
25namespace Dune
26{
29
30 namespace VTK {
31
33
52 template<class T>
54 {
55 public:
57 virtual void write (T data) = 0;
59 virtual bool writeIsNoop() const { return false; }
61 virtual ~DataArrayWriter () {}
62 };
63
65 template<class T>
67 {
68 public:
70
78 AsciiDataArrayWriter(std::ostream& theStream, std::string name,
79 int ncomps, const Indent& indent_)
80 : s(theStream), counter(0), numPerLine(12), indent(indent_)
81 {
82 TypeName<T> tn;
83 s << indent << "<DataArray type=\"" << tn() << "\" "
84 << "Name=\"" << name << "\" ";
85 s << "NumberOfComponents=\"" << ncomps << "\" ";
86 s << "format=\"ascii\">\n";
87 ++indent;
88 }
89
91 void write (T data)
92 {
93 typedef typename PrintType<T>::Type PT;
94 if(counter%numPerLine==0) s << indent;
95 else s << " ";
96 s << (PT) data;
97 counter++;
98 if (counter%numPerLine==0) s << "\n";
99 }
100
103 {
104 if (counter%numPerLine!=0) s << "\n";
105 --indent;
106 s << indent << "</DataArray>\n";
107 }
108
109 private:
110 std::ostream& s;
111 int counter;
112 int numPerLine;
113 Indent indent;
114 };
115
117 template<class T>
119 {
120 public:
122
132 BinaryDataArrayWriter(std::ostream& theStream, std::string name,
133 int ncomps, int nitems, const Indent& indent_)
134 : s(theStream), b64(theStream), indent(indent_)
135 {
136 TypeName<T> tn;
137 s << indent << "<DataArray type=\"" << tn() << "\" "
138 << "Name=\"" << name << "\" ";
139 s << "NumberOfComponents=\"" << ncomps << "\" ";
140 s << "format=\"binary\">\n";
141
142 // write indentation for the data chunk
143 s << indent+1;
144 // store size, needs to be exactly 32 bit
145 std::uint32_t size = ncomps*nitems*sizeof(T);
146 b64.write(size);
147 b64.flush();
148 }
149
151 void write (T data)
152 {
153 b64.write(data);
154 }
155
158 {
159 b64.flush();
160 // append newline to written data
161 s << "\n";
162 s << indent << "</DataArray>\n";
163 s.flush();
164 }
165
166 private:
167 std::ostream& s;
168 Base64Stream b64;
169 const Indent& indent;
170 };
171
173 template<class T>
175 {
176 public:
178
190 AppendedRawDataArrayWriter(std::ostream& s, std::string name,
191 int ncomps, unsigned nitems, unsigned& offset,
192 const Indent& indent)
193 {
194 TypeName<T> tn;
195 s << indent << "<DataArray type=\"" << tn() << "\" "
196 << "Name=\"" << name << "\" ";
197 s << "NumberOfComponents=\"" << ncomps << "\" ";
198 s << "format=\"appended\" offset=\""<< offset << "\" />\n";
199 offset += 4; // header
200 offset += ncomps*nitems*sizeof(T);
201 }
202
204 void write (T) { }
205
207 bool writeIsNoop() const { return true; }
208 };
209
211 template<class T>
213 {
214 public:
216
228 AppendedBase64DataArrayWriter(std::ostream& s, std::string name,
229 int ncomps, unsigned nitems,
230 unsigned& offset, const Indent& indent)
231 {
232 TypeName<T> tn;
233 s << indent << "<DataArray type=\"" << tn() << "\" "
234 << "Name=\"" << name << "\" ";
235 s << "NumberOfComponents=\"" << ncomps << "\" ";
236 s << "format=\"appended\" offset=\""<< offset << "\" />\n";
237 offset += 8; // header
238 unsigned bytes = ncomps*nitems*sizeof(T);
239 offset += bytes/3*4;
240 if(bytes%3 != 0)
241 offset += 4;
242 }
243
245 void write (T) { }
246
248 bool writeIsNoop() const { return true; }
249 };
250
252 //
253 // Naked ArrayWriters for the appended section
254 //
255
257 template<class T>
259 {
260 public:
262
268 NakedBase64DataArrayWriter(std::ostream& theStream, int ncomps,
269 int nitems)
270 : b64(theStream)
271 {
272 // store size
273 std::uint32_t size = ncomps*nitems*sizeof(T);
274 b64.write(size);
275 b64.flush();
276 }
277
279 void write (T data)
280 {
281 b64.write(data);
282 }
283
284 private:
285 Base64Stream b64;
286 };
287
289 template<class T>
291 {
292 RawStream s;
293
294 public:
296
302 NakedRawDataArrayWriter(std::ostream& theStream, int ncomps,
303 int nitems)
304 : s(theStream)
305 {
306 s.write((unsigned int)(ncomps*nitems*sizeof(T)));
307 }
308
310 void write (T data)
311 {
312 s.write(data);
313 }
314 };
315
317 //
318 // Factory
319 //
320
322
328 enum Phase { main, appended };
329
330 OutputType type;
331 std::ostream& stream;
332 unsigned offset;
334 Phase phase;
335
336 public:
338
347 inline DataArrayWriterFactory(OutputType type_, std::ostream& stream_)
348 : type(type_), stream(stream_), offset(0), phase(main)
349 { }
350
352
362 inline bool beginAppended() {
363 phase = appended;
364 switch(type) {
365 case ascii : return false;
366 case base64 : return false;
367 case appendedraw : return true;
368 case appendedbase64 : return true;
369 }
370 DUNE_THROW(IOError, "Dune::VTK::DataArrayWriter: unsupported "
371 "OutputType " << type);
372 }
373
375 const std::string& appendedEncoding() const {
376 static const std::string rawString = "raw";
377 static const std::string base64String = "base64";
378
379 switch(type) {
380 case ascii :
381 case base64 :
382 DUNE_THROW(IOError, "DataArrayWriterFactory::appendedEncoding(): No "
383 "appended encoding for OutputType " << type);
384 case appendedraw : return rawString;
385 case appendedbase64 : return base64String;
386 }
387 DUNE_THROW(IOError, "DataArrayWriterFactory::appendedEncoding(): "
388 "unsupported OutputType " << type);
389 }
390
392
405 template<typename T>
406 DataArrayWriter<T>* make(const std::string& name, unsigned ncomps,
407 unsigned nitems, const Indent& indent) {
408 switch(phase) {
409 case main :
410 switch(type) {
411 case ascii :
412 return new AsciiDataArrayWriter<T>(stream, name, ncomps, indent);
413 case base64 :
414 return new BinaryDataArrayWriter<T>(stream, name, ncomps, nitems,
415 indent);
416 case appendedraw :
417 return new AppendedRawDataArrayWriter<T>(stream, name, ncomps,
418 nitems, offset, indent);
419 case appendedbase64 :
420 return new AppendedBase64DataArrayWriter<T>(stream, name, ncomps,
421 nitems, offset,
422 indent);
423 }
424 break;
425 case appended :
426 switch(type) {
427 case ascii :
428 case base64 :
429 break; // invlid in appended mode
430 case appendedraw :
431 return new NakedRawDataArrayWriter<T>(stream, ncomps, nitems);
432 case appendedbase64 :
433 return new NakedBase64DataArrayWriter<T>(stream, ncomps, nitems);
434 }
435 break;
436 }
437 DUNE_THROW(IOError, "Dune::VTK::DataArrayWriter: unsupported "
438 "OutputType " << type << " in phase " << phase);
439 }
440 };
441
442 } // namespace VTK
443
445
446} // namespace Dune
447
448#endif // DUNE_GRID_IO_FILE_VTK_DATAARRAYWRITER_HH
class to base64 encode a stream of data
Definition: streams.hh:14
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:256
Utility class for handling nested indentation in output.
Definition: indent.hh:51
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:213
void write(T)
write one data element to output stream (noop)
Definition: dataarraywriter.hh:245
AppendedBase64DataArrayWriter(std::ostream &s, std::string name, int ncomps, unsigned nitems, unsigned &offset, const Indent &indent)
make a new data array writer
Definition: dataarraywriter.hh:228
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:248
a streaming writer for data array tags, uses appended raw format
Definition: dataarraywriter.hh:175
AppendedRawDataArrayWriter(std::ostream &s, std::string name, int ncomps, unsigned nitems, unsigned &offset, const Indent &indent)
make a new data array writer
Definition: dataarraywriter.hh:190
void write(T)
write one data element to output stream (noop)
Definition: dataarraywriter.hh:204
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:207
a streaming writer for data array tags, uses ASCII inline format
Definition: dataarraywriter.hh:67
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:91
~AsciiDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:102
AsciiDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, const Indent &indent_)
make a new data array writer
Definition: dataarraywriter.hh:78
a streaming writer for data array tags, uses binary inline format
Definition: dataarraywriter.hh:119
~BinaryDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:157
BinaryDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, int nitems, const Indent &indent_)
make a new data array writer
Definition: dataarraywriter.hh:132
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:151
a factory for DataArrayWriters
Definition: dataarraywriter.hh:327
bool beginAppended()
signal start of the appended section
Definition: dataarraywriter.hh:362
DataArrayWriterFactory(OutputType type_, std::ostream &stream_)
create a DataArrayWriterFactory
Definition: dataarraywriter.hh:347
const std::string & appendedEncoding() const
query encoding string for appended data
Definition: dataarraywriter.hh:375
DataArrayWriter< T > * make(const std::string &name, unsigned ncomps, unsigned nitems, const Indent &indent)
create a DataArrayWriter
Definition: dataarraywriter.hh:406
base class for data array writers
Definition: dataarraywriter.hh:54
virtual bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:59
virtual void write(T data)=0
write one data element
virtual ~DataArrayWriter()
virtual destructor
Definition: dataarraywriter.hh:61
a streaming writer for appended data array tags, uses base64 format
Definition: dataarraywriter.hh:259
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:279
NakedBase64DataArrayWriter(std::ostream &theStream, int ncomps, int nitems)
make a new data array writer
Definition: dataarraywriter.hh:268
a streaming writer for appended data arrays, uses raw format
Definition: dataarraywriter.hh:291
NakedRawDataArrayWriter(std::ostream &theStream, int ncomps, int nitems)
make a new data array writer
Definition: dataarraywriter.hh:302
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:310
map type to its VTK name in data array
Definition: common.hh:124
Common stuff for the VTKWriter.
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:40
@ appendedraw
Ouput is to the file is appended raw binary.
Definition: common.hh:46
@ appendedbase64
Ouput is to the file is appended base64 binary.
Definition: common.hh:48
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:44
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignment.hh:10
@ ascii
store data in a human readable form
Definition: grapedataioformattypes.hh:15
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:94
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)