VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
WbWriterVtkBinary.cpp
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
37#include <cstring>
38
39using namespace std;
40
41std::string WbWriterVtkBinary::writeLines(const std::string &filename, std::vector<UbTupleFloat3> &nodes,
42 std::vector<UbTupleInt2> &lines)
43{
44 return WbWriterVtkASCII::getInstance()->writeLines(filename, nodes, lines);
45}
46/*===============================================================================*/
47std::string WbWriterVtkBinary::writeTriangles(const std::string &filename, std::vector<UbTupleFloat3> &nodes,
48 std::vector<UbTupleInt3> &cells)
49{
50 return WbWriterVtkASCII::getInstance()->writeTriangles(filename, nodes, cells);
51}
52/*===============================================================================*/
53std::string WbWriterVtkBinary::writeTrianglesWithNodeData(const std::string &filename,
54 std::vector<UbTupleFloat3> &nodes,
55 std::vector<UbTupleInt3> &cells,
56 std::vector<std::string> &datanames,
57 std::vector<std::vector<double>> &nodedata)
58{
59 return WbWriterVtkASCII::getInstance()->writeTrianglesWithNodeData(filename, nodes, cells, datanames, nodedata);
60}
61/*===============================================================================*/
62std::string WbWriterVtkBinary::writeQuads(const string &filename, vector<UbTupleFloat3> &nodes,
64{
65 string vtkfilename = filename + getFileExtension();
66 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuads to " << vtkfilename << " - start");
67
68 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
69 if (!out) {
70 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
72 if (path.size() > 0) {
74 out.open(vtkfilename.c_str(), ios::out | ios::binary);
75 }
76 if (!out)
77 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
78 }
79
80 // HEADER-SECTION
81 // WRITE BIGENDIAN VtkBinary FILE
83 int nofNodes = (int)nodes.size();
84 int nofCells = (int)cells.size();
85
86 out << "# vtk DataFile Version 4.0"
87 << "\n";
88 out << "D3Q19MasterNodeGrid"
89 << "\n";
90 out << ""
91 << "\n";
92
93 // POINTS SECTION
94 out << "DATASET UNSTRUCTURED_GRID"
95 << "\n";
96 out << "POINTS " << nofNodes << " float"
97 << "\n";
98 for (int n = 0; n < nofNodes; n++) {
99 float x1 = (float)val<1>(nodes[n]);
100 float x2 = (float)val<2>(nodes[n]);
101 float x3 = (float)val<3>(nodes[n]);
102
103 if (swapByte) {
104 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
105 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
106 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
107 }
108
109 out.write((char *)&x1, sizeof(float));
110 out.write((char *)&x2, sizeof(float));
111 out.write((char *)&x3, sizeof(float));
112 }
113 out << "\n";
114
115 // CELLS SECTION
116 out << "CELLS " << nofCells << " " << nofCells * 5 << "\n";
117
118 int nodesPerCellDummy = 4; // nofNodesPerCell
119 if (swapByte)
120 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
121 for (int c = 0; c < (int)cells.size(); c++) {
122 int SW = val<1>(cells[c]);
123 int SE = val<2>(cells[c]);
124 int NE = val<3>(cells[c]);
125 int NW = val<4>(cells[c]);
126 if (swapByte) {
127 ub_system::swapByteOrder((unsigned char *)&SW, sizeof(int));
128 ub_system::swapByteOrder((unsigned char *)&SE, sizeof(int));
129 ub_system::swapByteOrder((unsigned char *)&NW, sizeof(int));
130 ub_system::swapByteOrder((unsigned char *)&NE, sizeof(int));
131 }
132
133 out.write((char *)&nodesPerCellDummy, sizeof(int));
134 out.write((char *)&SW, sizeof(int));
135 out.write((char *)&SE, sizeof(int));
136 out.write((char *)&NW, sizeof(int));
137 out.write((char *)&NE, sizeof(int));
138 }
139 out << "\n";
140
141 out << "CELL_TYPES " << (int)cells.size() << "\n";
142 int celltype = 8;
143 if (swapByte)
144 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
145 for (int c = 0; c < nofCells; c++)
146 out.write((char *)&celltype, sizeof(int));
147
148 out << endl;
149 out.close();
150
151 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuads to " << vtkfilename << " - end");
152 return vtkfilename;
153}
154/*===============================================================================*/
155std::string WbWriterVtkBinary::writeQuadsWithNodeData(const string &filename, vector<UbTupleFloat3> &nodes,
156 vector<UbTupleInt4> &cells, vector<string> &datanames,
158{
159 string vtkfilename = filename + getFileExtension();
160 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuadsWithNodeData to " << vtkfilename << " - start");
161
162 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
163 if (!out) {
164 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
166 if (path.size() > 0) {
168 out.open(vtkfilename.c_str(), ios::out | ios::binary);
169 }
170 if (!out)
171 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
172 }
173
174 // WRITE BIGENDIAN VtkBinary FILE
176 int nofNodes = (int)nodes.size();
177 int nofCells = (int)cells.size();
178
179 out << "# vtk DataFile Version 4.0"
180 << "\n";
181 out << "D3Q19MasterNodeGrid"
182 << "\n";
183 out << ""
184 << "\n";
185
186 // POINTS SECTION
187 out << "DATASET UNSTRUCTURED_GRID"
188 << "\n";
189 out << "POINTS " << nofNodes << " float"
190 << "\n";
191 for (int n = 0; n < nofNodes; n++) {
192 float x1 = (float)val<1>(nodes[n]);
193 float x2 = (float)val<2>(nodes[n]);
194 float x3 = (float)val<3>(nodes[n]);
195
196 if (swapByte) {
197 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
198 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
199 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
200 }
201
202 out.write((char *)&x1, sizeof(float));
203 out.write((char *)&x2, sizeof(float));
204 out.write((char *)&x3, sizeof(float));
205 }
206 out << "\n";
207
208 // CELLS SECTION
209 out << "CELLS " << nofCells << " " << nofCells * 5 << "\n";
210
211 int nodesPerCellDummy = 4; // nofNodesPerCell
212 if (swapByte)
213 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
214 for (int c = 0; c < (int)cells.size(); c++) {
215 int SW = val<1>(cells[c]);
216 int SE = val<2>(cells[c]);
217 int NE = val<3>(cells[c]);
218 int NW = val<4>(cells[c]);
219 if (swapByte) {
220 ub_system::swapByteOrder((unsigned char *)&SW, sizeof(int));
221 ub_system::swapByteOrder((unsigned char *)&SE, sizeof(int));
222 ub_system::swapByteOrder((unsigned char *)&NW, sizeof(int));
223 ub_system::swapByteOrder((unsigned char *)&NE, sizeof(int));
224 }
225
226 out.write((char *)&nodesPerCellDummy, sizeof(int));
227 out.write((char *)&SW, sizeof(int));
228 out.write((char *)&SE, sizeof(int));
229 out.write((char *)&NW, sizeof(int));
230 out.write((char *)&NE, sizeof(int));
231 }
232 out << "\n";
233
234 out << "CELL_TYPES " << (int)cells.size() << "\n";
235 int celltype = 8;
236 if (swapByte)
237 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
238 for (int c = 0; c < nofCells; c++)
239 out.write((char *)&celltype, sizeof(int));
240
241 out << endl;
242
243 // DATA SECTION
244 // write data section
245 out << "POINT_DATA " << nofNodes << "\n";
246 for (int s = 0; s < (int)datanames.size(); ++s) {
247 if ((int)nodedata[s].size() != nofNodes)
248 throw UbException(UB_EXARGS, "datasetsize must be equal to nofNodes");
249 out << "SCALARS " << datanames[s] << " float 1 \n LOOKUP_TABLE default \n";
250 for (int d = 0; d < (int)nodedata[s].size(); d++) {
251 float dummy = (float)nodedata[s][d];
252 if (swapByte)
253 ub_system::swapByteOrder((unsigned char *)&dummy, sizeof(float));
254 out.write((const char *)&dummy, sizeof(float));
255 }
256 out << endl;
257 }
258 out.close();
259
260 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuadsWithNodeData to " << vtkfilename << " - end");
261 return vtkfilename;
262}
263/*===============================================================================*/
264std::string WbWriterVtkBinary::writeQuadsWithCellData(const string &filename, vector<UbTupleFloat3> &nodes,
265 vector<UbTupleInt4> &cells, vector<string> &datanames,
267{
268 // HEADER-SECTION
269 string vtkfilename = filename + getFileExtension();
270 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuadsWithCellData to " << vtkfilename << " - start");
271
272 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
273 if (!out) {
274 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
276 if (path.size() > 0) {
278 out.open(vtkfilename.c_str(), ios::out | ios::binary);
279 }
280 if (!out)
281 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
282 }
283
284 // WRITE BIGENDIAN VtkBinary FILE
286 int nofNodes = (int)nodes.size();
287 int nofCells = (int)cells.size();
288
289 out << "# vtk DataFile Version 4.0"
290 << "\n";
291 out << "D3Q19MasterNodeGrid"
292 << "\n";
293 out << ""
294 << "\n";
295
296 // POINTS SECTION
297 out << "DATASET UNSTRUCTURED_GRID"
298 << "\n";
299 out << "POINTS " << nofNodes << " float"
300 << "\n";
301 for (int n = 0; n < nofNodes; n++) {
302 float x1 = (float)val<1>(nodes[n]);
303 float x2 = (float)val<2>(nodes[n]);
304 float x3 = (float)val<3>(nodes[n]);
305
306 if (swapByte) {
307 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
308 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
309 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
310 }
311
312 out.write((char *)&x1, sizeof(float));
313 out.write((char *)&x2, sizeof(float));
314 out.write((char *)&x3, sizeof(float));
315 }
316 out << "\n";
317
318 // CELLS SECTION
319 out << "CELLS " << nofCells << " " << nofCells * 5 << "\n";
320
321 int nodesPerCellDummy = 4; // nofNodesPerCell
322 if (swapByte)
323 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
324 for (int c = 0; c < (int)cells.size(); c++) {
325 int SW = val<1>(cells[c]);
326 int SE = val<2>(cells[c]);
327 int NE = val<3>(cells[c]);
328 int NW = val<4>(cells[c]);
329 if (swapByte) {
330 ub_system::swapByteOrder((unsigned char *)&SW, sizeof(int));
331 ub_system::swapByteOrder((unsigned char *)&SE, sizeof(int));
332 ub_system::swapByteOrder((unsigned char *)&NW, sizeof(int));
333 ub_system::swapByteOrder((unsigned char *)&NE, sizeof(int));
334 }
335
336 out.write((char *)&nodesPerCellDummy, sizeof(int));
337 out.write((char *)&SW, sizeof(int));
338 out.write((char *)&SE, sizeof(int));
339 out.write((char *)&NW, sizeof(int));
340 out.write((char *)&NE, sizeof(int));
341 }
342 out << "\n";
343
344 out << "CELL_TYPES " << (int)cells.size() << "\n";
345 int celltype = 8;
346 if (swapByte)
347 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
348 for (int c = 0; c < nofCells; c++)
349 out.write((char *)&celltype, sizeof(int));
350
351 out << endl;
352
353 // DATA SECTION
354 // write data section
355 out << "CELL_DATA " << nofCells << "\n";
356 for (int s = 0; s < (int)datanames.size(); ++s) {
357 if ((int)celldata[s].size() != nofCells)
358 throw UbException(UB_EXARGS, "datasetsize must be equal to nofNodes");
359 out << "SCALARS " << datanames[s] << " float 1 \n LOOKUP_TABLE default \n";
360 for (int d = 0; d < (int)celldata[s].size(); d++) {
361 float dummy = (float)celldata[s][d];
362 if (swapByte)
363 ub_system::swapByteOrder((unsigned char *)&dummy, sizeof(float));
364 out.write((const char *)&dummy, sizeof(float));
365 }
366 out << endl;
367 }
368 out.close();
369
370 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuadsWithCellData to " << vtkfilename << " - end");
371 return vtkfilename;
372}
373/*===============================================================================*/
379{
380 string vtkfilename = filename + getFileExtension();
381 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuadsWithNodeAndCellData to " << vtkfilename << " - start");
382
383 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
384 if (!out) {
385 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
387 if (path.size() > 0) {
389 out.open(vtkfilename.c_str(), ios::out | ios::binary);
390 }
391 if (!out)
392 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
393 }
394
395 // HEADER-SECTION
396 // WRITE BIGENDIAN VtkBinary FILE
398 int nofNodes = (int)nodes.size();
399 int nofCells = (int)cells.size();
400
401 out << "# vtk DataFile Version 4.0"
402 << "\n";
403 out << "D3Q19MasterNodeGrid"
404 << "\n";
405 out << ""
406 << "\n";
407
408 // POINTS SECTION
409 out << "DATASET UNSTRUCTURED_GRID"
410 << "\n";
411 out << "POINTS " << nofNodes << " float"
412 << "\n";
413 for (int n = 0; n < nofNodes; n++) {
414 float x1 = (float)val<1>(nodes[n]);
415 float x2 = (float)val<2>(nodes[n]);
416 float x3 = (float)val<3>(nodes[n]);
417
418 if (swapByte) {
419 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
420 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
421 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
422 }
423
424 out.write((char *)&x1, sizeof(float));
425 out.write((char *)&x2, sizeof(float));
426 out.write((char *)&x3, sizeof(float));
427 }
428 out << "\n";
429
430 // CELLS SECTION
431 out << "CELLS " << nofCells << " " << nofCells * 5 << "\n";
432
433 int nodesPerCellDummy = 4; // nofNodesPerCell
434 if (swapByte)
435 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
436 for (int c = 0; c < (int)cells.size(); c++) {
437 int SW = val<1>(cells[c]);
438 int SE = val<2>(cells[c]);
439 int NE = val<3>(cells[c]);
440 int NW = val<4>(cells[c]);
441 if (swapByte) {
442 ub_system::swapByteOrder((unsigned char *)&SW, sizeof(int));
443 ub_system::swapByteOrder((unsigned char *)&SE, sizeof(int));
444 ub_system::swapByteOrder((unsigned char *)&NW, sizeof(int));
445 ub_system::swapByteOrder((unsigned char *)&NE, sizeof(int));
446 }
447
448 out.write((char *)&nodesPerCellDummy, sizeof(int));
449 out.write((char *)&SW, sizeof(int));
450 out.write((char *)&SE, sizeof(int));
451 out.write((char *)&NW, sizeof(int));
452 out.write((char *)&NE, sizeof(int));
453 }
454 out << "\n";
455
456 out << "CELL_TYPES " << (int)cells.size() << "\n";
457 int celltype = 8;
458 if (swapByte)
459 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
460 for (int c = 0; c < nofCells; c++)
461 out.write((char *)&celltype, sizeof(int));
462
463 out << endl;
464
465 // NODE DATA SECTION
466 // write data section
467 out << "POINT_DATA " << nofNodes << "\n";
468 for (int s = 0; s < (int)nodedatanames.size(); ++s) {
469 if ((int)nodedata[s].size() != nofNodes)
470 throw UbException(UB_EXARGS, "datasetsize must be equal to nofNodes");
471 out << "SCALARS " << nodedatanames[s] << " float 1 \n LOOKUP_TABLE default \n";
472 for (int d = 0; d < (int)nodedata[s].size(); d++) {
473 float dummy = (float)nodedata[s][d];
474 if (swapByte)
475 ub_system::swapByteOrder((unsigned char *)&dummy, sizeof(float));
476 out.write((const char *)&dummy, sizeof(float));
477 }
478 out << endl;
479 }
480
481 // CELL DATA SECTION
482 // write data section
483 out << "CELL_DATA " << nofCells << "\n";
484 for (int s = 0; s < (int)celldatanames.size(); ++s) {
485 if ((int)celldata[s].size() != nofCells)
486 throw UbException(UB_EXARGS, "datasetsize must be equal to nofNodes");
487 out << "SCALARS " << celldatanames[s] << " float 1 \n LOOKUP_TABLE default \n";
488 for (int d = 0; d < (int)celldata[s].size(); d++) {
489 float dummy = (float)celldata[s][d];
490 if (swapByte)
491 ub_system::swapByteOrder((unsigned char *)&dummy, sizeof(float));
492 out.write((const char *)&dummy, sizeof(float));
493 }
494 out << endl;
495 }
496
497 out.close();
498
499 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeQuadsWithNodeAndCellData to " << vtkfilename << " - end");
500 return vtkfilename;
501}
502/*===============================================================================*/
503std::string WbWriterVtkBinary::writeOctsWithCellData(const string &filename, vector<UbTupleFloat3> &nodes,
504 vector<UbTupleInt8> &cells, vector<string> &datanames,
506{
507 string vtkfilename = filename + getFileExtension();
508 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeOctsWithCellData to " << vtkfilename << " - start");
509
510 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
511 if (!out) {
512 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
514 if (path.size() > 0) {
516 out.open(vtkfilename.c_str(), ios::out | ios::binary);
517 }
518 if (!out)
519 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
520 }
521
522 // HEADER-SECTION
523 // WRITE BIGENDIAN VtkBinary FILE
525 int nofNodes = (int)nodes.size();
526 int nofCells = (int)cells.size();
527
528 out << "# vtk DataFile Version 4.0"
529 << "\n";
530 out << "D3Q19MasterNodeGrid"
531 << "\n";
532 out << ""
533 << "\n";
534
535 // POINTS SECTION
536 out << "DATASET UNSTRUCTURED_GRID"
537 << "\n";
538 out << "POINTS " << nofNodes << " float"
539 << "\n";
540 for (int n = 0; n < nofNodes; n++) {
541 float x1 = (float)val<1>(nodes[n]);
542 float x2 = (float)val<2>(nodes[n]);
543 float x3 = (float)val<3>(nodes[n]);
544
545 if (swapByte) {
546 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
547 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
548 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
549 }
550
551 out.write((char *)&x1, sizeof(float));
552 out.write((char *)&x2, sizeof(float));
553 out.write((char *)&x3, sizeof(float));
554 }
555 out << "\n";
556
557 // CELLS SECTION
558 out << "CELLS " << nofCells << " " << nofCells * 9 << "\n";
559
560 int nodesPerCellDummy = 8; // nofNodesPerCell
561 if (swapByte)
562 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
563 for (int c = 0; c < (int)cells.size(); c++) {
564 int BSW = val<1>(cells[c]);
565 int TSW = val<5>(cells[c]);
566 int BSE = val<2>(cells[c]);
567 int TSE = val<6>(cells[c]);
568 int BNW = val<3>(cells[c]);
569 int TNW = val<7>(cells[c]);
570 int BNE = val<4>(cells[c]);
571 int TNE = val<8>(cells[c]);
572 if (swapByte) {
573 ub_system::swapByteOrder((unsigned char *)&BSW, sizeof(int));
574 ub_system::swapByteOrder((unsigned char *)&BSE, sizeof(int));
575 ub_system::swapByteOrder((unsigned char *)&BNW, sizeof(int));
576 ub_system::swapByteOrder((unsigned char *)&BNE, sizeof(int));
577 ub_system::swapByteOrder((unsigned char *)&TSW, sizeof(int));
578 ub_system::swapByteOrder((unsigned char *)&TSE, sizeof(int));
579 ub_system::swapByteOrder((unsigned char *)&TNW, sizeof(int));
580 ub_system::swapByteOrder((unsigned char *)&TNE, sizeof(int));
581 }
582
583 out.write((char *)&nodesPerCellDummy, sizeof(int));
584 out.write((char *)&BSW, sizeof(int));
585 out.write((char *)&BSE, sizeof(int));
586 out.write((char *)&BNE, sizeof(int));
587 out.write((char *)&BNW, sizeof(int));
588 out.write((char *)&TSW, sizeof(int));
589 out.write((char *)&TSE, sizeof(int));
590 out.write((char *)&TNE, sizeof(int));
591 out.write((char *)&TNW, sizeof(int));
592 }
593 out << "\n";
594
595 out << "CELL_TYPES " << (int)cells.size() << "\n";
596 int celltype = 11;
597 if (swapByte)
598 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
599 for (int c = 0; c < nofCells; c++)
600 out.write((char *)&celltype, sizeof(int));
601
602 out << endl;
603
604 // CELL DATA SECTION
605 // write data section
606 out << "CELL_DATA " << nofCells << "\n";
607 for (int s = 0; s < (int)datanames.size(); ++s) {
608 if ((int)celldata[s].size() != nofCells)
609 throw UbException(UB_EXARGS, "datasetsize must be equal to nofNodes");
610 out << "SCALARS " << datanames[s] << " float 1 \n LOOKUP_TABLE default \n";
611 for (int d = 0; d < (int)celldata[s].size(); d++) {
612 float dummy = (float)celldata[s][d];
613 if (swapByte)
614 ub_system::swapByteOrder((unsigned char *)&dummy, sizeof(float));
615 out.write((const char *)&dummy, sizeof(float));
616 }
617 out << endl;
618 }
619 out.close();
620
621 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeOctsWithCellData to " << vtkfilename << " - end");
622 return vtkfilename;
623}
624/*===============================================================================*/
625std::string WbWriterVtkBinary::writeOctsWithNodeData(const string &filename, vector<UbTupleFloat3> &nodes,
626 vector<UbTupleUInt8> &cells, vector<string> &datanames,
628{
629 // HEADER-SECTION
630 string vtkfilename = filename + getFileExtension();
631 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeOctsWithNodeData to " << vtkfilename << " - start");
632
633 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
634 if (!out) {
635 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
637 if (path.size() > 0) {
639 out.open(vtkfilename.c_str(), ios::out | ios::binary);
640 }
641 if (!out)
642 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
643 }
644
645 // WRITE BIGENDIAN VtkBinary FILE
647 int nofNodes = (int)nodes.size();
648 int nofCells = (int)cells.size();
649
650 out << "# vtk DataFile Version 4.0"
651 << "\n";
652 out << "D3Q19MasterNodeGrid"
653 << "\n";
654 out << ""
655 << "\n";
656
657 // POINTS SECTION
658 out << "DATASET UNSTRUCTURED_GRID"
659 << "\n";
660 out << "POINTS " << nofNodes << " float"
661 << "\n";
662 for (int n = 0; n < nofNodes; n++) {
663 float x1 = val<1>(nodes[n]);
664 float x2 = val<2>(nodes[n]);
665 float x3 = val<3>(nodes[n]);
666
667 if (swapByte) {
668 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
669 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
670 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
671 }
672
673 out.write((char *)&x1, sizeof(float));
674 out.write((char *)&x2, sizeof(float));
675 out.write((char *)&x3, sizeof(float));
676 }
677 out << "\n";
678
679 // CELLS SECTION
680 out << "CELLS " << nofCells << " " << nofCells * 9 << "\n";
681
682 int nodesPerCellDummy = 8; // nofNodesPerCell
683 if (swapByte)
684 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
685 for (int c = 0; c < (int)cells.size(); c++) {
686 int BSW = val<1>(cells[c]);
687 int TSW = val<5>(cells[c]);
688 int BSE = val<2>(cells[c]);
689 int TSE = val<6>(cells[c]);
690 int BNW = val<3>(cells[c]);
691 int TNW = val<7>(cells[c]);
692 int BNE = val<4>(cells[c]);
693 int TNE = val<8>(cells[c]);
694 if (swapByte) {
695 ub_system::swapByteOrder((unsigned char *)&BSW, sizeof(int));
696 ub_system::swapByteOrder((unsigned char *)&BSE, sizeof(int));
697 ub_system::swapByteOrder((unsigned char *)&BNW, sizeof(int));
698 ub_system::swapByteOrder((unsigned char *)&BNE, sizeof(int));
699 ub_system::swapByteOrder((unsigned char *)&TSW, sizeof(int));
700 ub_system::swapByteOrder((unsigned char *)&TSE, sizeof(int));
701 ub_system::swapByteOrder((unsigned char *)&TNW, sizeof(int));
702 ub_system::swapByteOrder((unsigned char *)&TNE, sizeof(int));
703 }
704
705 out.write((char *)&nodesPerCellDummy, sizeof(int));
706 out.write((char *)&BSW, sizeof(int));
707 out.write((char *)&BSE, sizeof(int));
708 out.write((char *)&BNE, sizeof(int));
709 out.write((char *)&BNW, sizeof(int));
710 out.write((char *)&TSW, sizeof(int));
711 out.write((char *)&TSE, sizeof(int));
712 out.write((char *)&TNE, sizeof(int));
713 out.write((char *)&TNW, sizeof(int));
714 }
715 out << "\n";
716
717 out << "CELL_TYPES " << (int)cells.size() << "\n";
718 int celltype = 11;
719 if (swapByte)
720 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
721 for (int c = 0; c < nofCells; c++)
722 out.write((char *)&celltype, sizeof(int));
723
724 out << endl;
725
726 // NODE DATA SECTION
727 // write data section
728 out << "POINT_DATA " << nofNodes << "\n";
729 for (int s = 0; s < (int)datanames.size(); ++s) {
730 if ((int)nodedata[s].size() != nofNodes)
731 throw UbException(UB_EXARGS, "datasetsize must be equal to nofNodes");
732 out << "SCALARS " << datanames[s] << " float 1 \n LOOKUP_TABLE default \n";
733 for (int d = 0; d < (int)nodedata[s].size(); d++) {
734 float dummy = (float)nodedata[s][d];
735 if (swapByte)
736 ub_system::swapByteOrder((unsigned char *)&dummy, sizeof(float));
737 out.write((const char *)&dummy, sizeof(float));
738 }
739 out << endl;
740 }
741
742 out.close();
743
744 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeOctsWithNodeData to " << vtkfilename << " - end");
745 return vtkfilename;
746}
747/*===============================================================================*/
748std::string WbWriterVtkBinary::writeOcts(const string &filename, vector<UbTupleFloat3> &nodes,
749 vector<UbTupleInt8> &cells)
750{
751 string vtkfilename = filename + getFileExtension();
752 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeOcts to " << vtkfilename << " - start");
753
754 ofstream out(vtkfilename.c_str(), ofstream::out | ofstream::binary);
755 if (!out) {
756 out.clear(); // flags ruecksetzen (ansonsten liefert utern if(!out) weiterhin true!!!
758 if (path.size() > 0) {
760 out.open(vtkfilename.c_str(), ios::out | ios::binary);
761 }
762 if (!out)
763 throw UbException(UB_EXARGS, "couldn't open file " + vtkfilename);
764 }
765
766 // HEADER-SECTION
767 // WRITE BIGENDIAN VtkBinary FILE
769 int nofNodes = (int)nodes.size();
770 int nofCells = (int)cells.size();
771
772 out << "# vtk DataFile Version 4.0"
773 << "\n";
774 out << "D3Q19MasterNodeGrid"
775 << "\n";
776 out << ""
777 << "\n";
778
779 // POINTS SECTION
780 out << "DATASET UNSTRUCTURED_GRID"
781 << "\n";
782 out << "POINTS " << nofNodes << " float"
783 << "\n";
784 for (int n = 0; n < nofNodes; n++) {
785 float x1 = val<1>(nodes[n]);
786 float x2 = val<2>(nodes[n]);
787 float x3 = val<3>(nodes[n]);
788
789 if (swapByte) {
790 ub_system::swapByteOrder((unsigned char *)&x1, sizeof(float));
791 ub_system::swapByteOrder((unsigned char *)&x2, sizeof(float));
792 ub_system::swapByteOrder((unsigned char *)&x3, sizeof(float));
793 }
794
795 out.write((char *)&x1, sizeof(float));
796 out.write((char *)&x2, sizeof(float));
797 out.write((char *)&x3, sizeof(float));
798 }
799 out << "\n";
800
801 // CELLS SECTION
802 out << "CELLS " << nofCells << " " << nofCells * 9 << "\n";
803
804 int nodesPerCellDummy = 8; // nofNodesPerCell
805 if (swapByte)
806 ub_system::swapByteOrder((unsigned char *)&nodesPerCellDummy, sizeof(int));
807 for (int c = 0; c < (int)cells.size(); c++) {
808 int BSW = val<1>(cells[c]);
809 int TSW = val<5>(cells[c]);
810 int BSE = val<2>(cells[c]);
811 int TSE = val<6>(cells[c]);
812 int BNW = val<3>(cells[c]);
813 int TNW = val<7>(cells[c]);
814 int BNE = val<4>(cells[c]);
815 int TNE = val<8>(cells[c]);
816 if (swapByte) {
817 ub_system::swapByteOrder((unsigned char *)&BSW, sizeof(int));
818 ub_system::swapByteOrder((unsigned char *)&BSE, sizeof(int));
819 ub_system::swapByteOrder((unsigned char *)&BNW, sizeof(int));
820 ub_system::swapByteOrder((unsigned char *)&BNE, sizeof(int));
821 ub_system::swapByteOrder((unsigned char *)&TSW, sizeof(int));
822 ub_system::swapByteOrder((unsigned char *)&TSE, sizeof(int));
823 ub_system::swapByteOrder((unsigned char *)&TNW, sizeof(int));
824 ub_system::swapByteOrder((unsigned char *)&TNE, sizeof(int));
825 }
826
827 out.write((char *)&nodesPerCellDummy, sizeof(int));
828 out.write((char *)&BSW, sizeof(int));
829 out.write((char *)&BSE, sizeof(int));
830 out.write((char *)&BNE, sizeof(int));
831 out.write((char *)&BNW, sizeof(int));
832 out.write((char *)&TSW, sizeof(int));
833 out.write((char *)&TSE, sizeof(int));
834 out.write((char *)&TNE, sizeof(int));
835 out.write((char *)&TNW, sizeof(int));
836 }
837 out << "\n";
838
839 out << "CELL_TYPES " << (int)cells.size() << "\n";
840 int celltype = 11;
841 if (swapByte)
842 ub_system::swapByteOrder((unsigned char *)&celltype, sizeof(int));
843 for (int c = 0; c < nofCells; c++)
844 out.write((char *)&celltype, sizeof(int));
845
846 out << endl;
847 out.close();
848
849 UBLOG(logDEBUG1, "WbWriterVtkBinary::writeOcts to " << vtkfilename << " - end");
850 return vtkfilename;
851}
852
static WbWriterVtkASCII * getInstance()
std::string getFileExtension() override
std::shared_ptr< T > SPtr
#define UBLOG(level, logtext)
Definition UbLogger.h:327
#define UB_EXARGS
Definition UbException.h:73
@ logDEBUG1
Definition UbLogger.h:54
std::string writeLines(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt2 > &lines) override
std::string writeTriangles(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt3 > &cells) override
std::string writeQuadsWithNodeAndCellData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt4 > &cells, std::vector< std::string > &nodedatanames, std::vector< std::vector< double > > &nodedata, std::vector< std::string > &celldatanames, std::vector< std::vector< double > > &celldata) override
std::string writeOctsWithCellData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt8 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &celldata) override
std::string writeTrianglesWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt3 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
std::string writeQuadsWithCellData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt4 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &celldata) override
std::string writeLines(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt2 > &lines) override
std::string writeTrianglesWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt3 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
std::string writeTriangles(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt3 > &cells) override
std::string writeQuads(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt4 > &cells) override
std::string writeQuadsWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt4 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
std::string writeOcts(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt8 > &cells) override
std::string writeOctsWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleUInt8 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
bool makeDirectory(const std::string &dir)
Definition UbSystem.h:280
std::string getPathFromString(const std::string &fileStringWithPath)
Definition UbSystem.h:345
void swapByteOrder(unsigned char *toSwap, int length)
Definition UbSystem.h:439
bool isLittleEndian()
Definition UbSystem.h:418