Dune Core Modules (2.3.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 <ostream>
8#include <string>
9
11#include <dune/common/indent.hh>
12
13#include <dune/grid/io/file/vtk/streams.hh>
15
24namespace Dune
25{
28
29 namespace VTK {
30
32
51 template<class T>
53 {
54 public:
56 virtual void write (T data) = 0;
58 virtual bool writeIsNoop() const { return false; }
60 virtual ~DataArrayWriter () {}
61 };
62
64 template<class T>
66 {
67 public:
69
77 AsciiDataArrayWriter(std::ostream& theStream, std::string name,
78 int ncomps, const Indent& indent_)
79 : s(theStream), counter(0), numPerLine(12), indent(indent_)
80 {
81 TypeName<T> tn;
82 s << indent << "<DataArray type=\"" << tn() << "\" "
83 << "Name=\"" << name << "\" ";
84 s << "NumberOfComponents=\"" << ncomps << "\" ";
85 s << "format=\"ascii\">\n";
86 ++indent;
87 }
88
90 void write (T data)
91 {
92 typedef typename PrintType<T>::Type PT;
93 if(counter%numPerLine==0) s << indent;
94 else s << " ";
95 s << (PT) data;
96 counter++;
97 if (counter%numPerLine==0) s << "\n";
98 }
99
102 {
103 if (counter%numPerLine!=0) s << "\n";
104 --indent;
105 s << indent << "</DataArray>\n";
106 }
107
108 private:
109 std::ostream& s;
110 int counter;
111 int numPerLine;
112 Indent indent;
113 };
114
116 template<class T>
118 {
119 public:
121
131 BinaryDataArrayWriter(std::ostream& theStream, std::string name,
132 int ncomps, int nitems, const Indent& indent_)
133 : s(theStream), b64(theStream), indent(indent_)
134 {
135 TypeName<T> tn;
136 s << indent << "<DataArray type=\"" << tn() << "\" "
137 << "Name=\"" << name << "\" ";
138 s << "NumberOfComponents=\"" << ncomps << "\" ";
139 s << "format=\"binary\">\n";
140
141 // write indentation for the data chunk
142 s << indent+1;
143 // store size
144 unsigned long int size = ncomps*nitems*sizeof(T);
145 b64.write(size);
146 b64.flush();
147 }
148
150 void write (T data)
151 {
152 b64.write(data);
153 }
154
157 {
158 b64.flush();
159 // append newline to written data
160 s << "\n";
161 s << indent << "</DataArray>\n";
162 s.flush();
163 }
164
165 private:
166 std::ostream& s;
167 Base64Stream b64;
168 const Indent& indent;
169 };
170
172 template<class T>
174 {
175 public:
177
189 AppendedRawDataArrayWriter(std::ostream& s, std::string name,
190 int ncomps, unsigned nitems, unsigned& offset,
191 const Indent& indent)
192 {
193 TypeName<T> tn;
194 s << indent << "<DataArray type=\"" << tn() << "\" "
195 << "Name=\"" << name << "\" ";
196 s << "NumberOfComponents=\"" << ncomps << "\" ";
197 s << "format=\"appended\" offset=\""<< offset << "\" />\n";
198 offset += 4; // header
199 offset += ncomps*nitems*sizeof(T);
200 }
201
203 void write (T data) { }
204
206 bool writeIsNoop() const { return true; }
207 };
208
210 template<class T>
212 {
213 public:
215
227 AppendedBase64DataArrayWriter(std::ostream& s, std::string name,
228 int ncomps, unsigned nitems,
229 unsigned& offset, const Indent& indent)
230 {
231 TypeName<T> tn;
232 s << indent << "<DataArray type=\"" << tn() << "\" "
233 << "Name=\"" << name << "\" ";
234 s << "NumberOfComponents=\"" << ncomps << "\" ";
235 s << "format=\"appended\" offset=\""<< offset << "\" />\n";
236 offset += 8; // header
237 unsigned bytes = ncomps*nitems*sizeof(T);
238 offset += bytes/3*4;
239 if(bytes%3 != 0)
240 offset += 4;
241 }
242
244 void write (T data) { }
245
247 bool writeIsNoop() const { return true; }
248 };
249
251 //
252 // Naked ArrayWriters for the appended section
253 //
254
256 template<class T>
258 {
259 public:
261
267 NakedBase64DataArrayWriter(std::ostream& theStream, int ncomps,
268 int nitems)
269 : b64(theStream)
270 {
271 // store size
272 unsigned long int size = ncomps*nitems*sizeof(T);
273 b64.write(size);
274 b64.flush();
275 }
276
278 void write (T data)
279 {
280 b64.write(data);
281 }
282
283 private:
284 Base64Stream b64;
285 };
286
288 template<class T>
290 {
291 RawStream s;
292
293 public:
295
301 NakedRawDataArrayWriter(std::ostream& theStream, int ncomps,
302 int nitems)
303 : s(theStream)
304 {
305 s.write((unsigned int)(ncomps*nitems*sizeof(T)));
306 }
307
309 void write (T data)
310 {
311 s.write(data);
312 }
313 };
314
316 //
317 // Factory
318 //
319
321
327 enum Phase { main, appended };
328
329 OutputType type;
330 std::ostream& stream;
331 unsigned offset;
333 Phase phase;
334
335 public:
337
346 inline DataArrayWriterFactory(OutputType type_, std::ostream& stream_)
347 : type(type_), stream(stream_), offset(0), phase(main)
348 { }
349
351
361 inline bool beginAppended() {
362 phase = appended;
363 switch(type) {
364 case ascii : return false;
365 case base64 : return false;
366 case appendedraw : return true;
367 case appendedbase64 : return true;
368 }
369 DUNE_THROW(IOError, "Dune::VTK::DataArrayWriter: unsupported "
370 "OutputType " << type);
371 }
372
374 const std::string& appendedEncoding() const {
375 static const std::string rawString = "raw";
376 static const std::string base64String = "base64";
377
378 switch(type) {
379 case ascii :
380 case base64 :
381 DUNE_THROW(IOError, "DataArrayWriterFactory::appendedEncoding(): No "
382 "appended encoding for OutputType " << type);
383 case appendedraw : return rawString;
384 case appendedbase64 : return base64String;
385 }
386 DUNE_THROW(IOError, "DataArrayWriterFactory::appendedEncoding(): "
387 "unsupported OutputType " << type);
388 }
389
391
404 template<typename T>
405 DataArrayWriter<T>* make(const std::string& name, unsigned ncomps,
406 unsigned nitems, const Indent& indent) {
407 switch(phase) {
408 case main :
409 switch(type) {
410 case ascii :
411 return new AsciiDataArrayWriter<T>(stream, name, ncomps, indent);
412 case base64 :
413 return new BinaryDataArrayWriter<T>(stream, name, ncomps, nitems,
414 indent);
415 case appendedraw :
416 return new AppendedRawDataArrayWriter<T>(stream, name, ncomps,
417 nitems, offset, indent);
418 case appendedbase64 :
419 return new AppendedBase64DataArrayWriter<T>(stream, name, ncomps,
420 nitems, offset,
421 indent);
422 }
423 break;
424 case appended :
425 switch(type) {
426 case ascii :
427 case base64 :
428 break; // invlid in appended mode
429 case appendedraw :
430 return new NakedRawDataArrayWriter<T>(stream, ncomps, nitems);
431 case appendedbase64 :
432 return new NakedBase64DataArrayWriter<T>(stream, ncomps, nitems);
433 }
434 break;
435 }
436 DUNE_THROW(IOError, "Dune::VTK::DataArrayWriter: unsupported "
437 "OutputType " << type << " in phase " << phase);
438 }
439 };
440
441 } // namespace VTK
442
444
445} // namespace Dune
446
447#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:257
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:212
void write(T data)
write one data element to output stream (noop)
Definition: dataarraywriter.hh:244
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:227
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:247
a streaming writer for data array tags, uses appended raw format
Definition: dataarraywriter.hh:174
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:189
bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:206
void write(T data)
write one data element to output stream (noop)
Definition: dataarraywriter.hh:203
a streaming writer for data array tags, uses ASCII inline format
Definition: dataarraywriter.hh:66
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:90
~AsciiDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:101
AsciiDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, const Indent &indent_)
make a new data array writer
Definition: dataarraywriter.hh:77
a streaming writer for data array tags, uses binary inline format
Definition: dataarraywriter.hh:118
~BinaryDataArrayWriter()
finish output; writes end tag
Definition: dataarraywriter.hh:156
BinaryDataArrayWriter(std::ostream &theStream, std::string name, int ncomps, int nitems, const Indent &indent_)
make a new data array writer
Definition: dataarraywriter.hh:131
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:150
a factory for DataArrayWriters
Definition: dataarraywriter.hh:326
bool beginAppended()
signal start of the appeneded section
Definition: dataarraywriter.hh:361
DataArrayWriterFactory(OutputType type_, std::ostream &stream_)
create a DataArrayWriterFactory
Definition: dataarraywriter.hh:346
const std::string & appendedEncoding() const
query encoding string for appended data
Definition: dataarraywriter.hh:374
DataArrayWriter< T > * make(const std::string &name, unsigned ncomps, unsigned nitems, const Indent &indent)
create a DataArrayWriter
Definition: dataarraywriter.hh:405
base class for data array writers
Definition: dataarraywriter.hh:53
virtual bool writeIsNoop() const
whether calls to write may be skipped
Definition: dataarraywriter.hh:58
virtual void write(T data)=0
write one data element
virtual ~DataArrayWriter()
virtual destructor
Definition: dataarraywriter.hh:60
a streaming writer for appended data array tags, uses base64 format
Definition: dataarraywriter.hh:258
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:278
NakedBase64DataArrayWriter(std::ostream &theStream, int ncomps, int nitems)
make a new data array writer
Definition: dataarraywriter.hh:267
a streaming writer for appended data arrays, uses raw format
Definition: dataarraywriter.hh:290
NakedRawDataArrayWriter(std::ostream &theStream, int ncomps, int nitems)
make a new data array writer
Definition: dataarraywriter.hh:301
void write(T data)
write one data element to output stream
Definition: dataarraywriter.hh:309
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:244
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignment.hh:14
@ 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)