-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvtk_stream.cpp
More file actions
130 lines (111 loc) · 2.64 KB
/
vtk_stream.cpp
File metadata and controls
130 lines (111 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "vtk_stream.h"
using namespace mesh3d;
namespace mesh3d {
template<>
void vtk_stream::put<float>(float v) {
union {
float w;
uint32_t i;
char c[4];
} x;
x.w = v;
x.i = __builtin_bswap32(x.i);
o.write(&x.c[0], sizeof(x));
}
template<>
void vtk_stream::put<double>(double v) {
union {
double w;
uint64_t i;
char c[8];
} x;
x.w = v;
x.i = __builtin_bswap64(x.i);
o.write(&x.c[0], sizeof(x));
}
template<>
void vtk_stream::put<uint32_t>(uint32_t v) {
union {
uint32_t i;
char c[4];
} x;
x.i = __builtin_bswap32(v);
o.write(&x.c[0], sizeof(x));
}
template<>
void vtk_stream::put<int32_t>(int32_t v) {
union {
int32_t i;
char c[4];
} x;
x.i = __builtin_bswap32(v);
o.write(&x.c[0], sizeof(x));
}
template<>
void vtk_stream::put<uint64_t>(uint64_t v) {
union {
uint64_t i;
char c[8];
} x;
x.i = __builtin_bswap64(v);
o.write(&x.c[0], sizeof(x));
}
template<>
void vtk_stream::put<int64_t>(int64_t v) {
union {
int64_t i;
char c[8];
} x;
x.i = __builtin_bswap64(v);
o.write(&x.c[0], sizeof(x));
}
template<>
const std::string vtk_stream::name<float>() const { return "float"; }
template<>
const std::string vtk_stream::name<double>() const { return "double"; }
template<>
const std::string vtk_stream::name<int>() const { return "int"; }
template<>
const std::string vtk_stream::name<unsigned int>() const { return "unsigned_int"; }
}
vtk_stream::vtk_stream(const char *fn) : o(fn, std::ios::out | std::ios::binary) {
if (!o)
throw std::invalid_argument("Could not open file `" + std::string(fn) + "'");
header_written = false;
cell_data_written = false;
point_data_written = false;
}
void vtk_stream::write_header(const mesh &m, const std::string &comment) {
if (header_written)
throw std::logic_error("Header is already written");
o << "# vtk DataFile Version 3.0\n"
<< comment << "\n"
<< "BINARY\n"
<< "DATASET UNSTRUCTURED_GRID\n"
<< "POINTS " << m.vertices().size() << " double" << std::endl;
const ptr_vector<vertex> &verts = m.vertices();
for (index i = 0; i < verts.size(); i++) {
const vertex &v = verts[i];
put(v.r().x);
put(v.r().y);
put(v.r().z);
}
const ptr_vector<tetrahedron> &tets = m.tets();
o << "\nCELLS " << tets.size() << " " << 5 * tets.size() << std::endl;
for (index i = 0; i < tets.size(); i++) {
const tetrahedron &t = tets[i];
uint32_t num_vertex = 4;
put(num_vertex);
for (int j = 0; j < 4; j++) {
uint32_t vidx = t.p(j).idx();
put(vidx);
}
}
o << "\nCELL_TYPES " << tets.size() << std::endl;
uint32_t tet_cell_type = 10;
for (index i = 0; i < tets.size(); i++)
put(tet_cell_type);
header_written = true;
nV = verts.size();
nT = tets.size();
}