-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvtk_stream.h
More file actions
141 lines (123 loc) · 3.37 KB
/
vtk_stream.h
File metadata and controls
141 lines (123 loc) · 3.37 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
131
132
133
134
135
136
137
138
139
140
141
#ifndef __MESH3D__VTK_STREAM_H__
#define __MESH3D__VTK_STREAM_H__
#include <fstream>
#include "common.h"
#include "mesh.h"
#include <stdexcept>
#include <stdint.h>
namespace mesh3d {
/** A class for exporting mesh and accompanying data to vtk file */
class vtk_stream {
std::ofstream o;
bool header_written;
bool cell_data_written;
bool point_data_written;
index nV, nT;
template <class T>
void put(T v);
template <class T>
const std::string name() const;
public:
/** Construct vtk stream for specified file */
vtk_stream(const char *fn);
/** Write vtk header */
void write_header(const mesh &m, const std::string &comment = "comment");
/** Append scalar cell data to vtk file */
template <class T>
void append_cell_data(const T *v, const std::string &id);
/** Append scalar point data to vtk file */
template <class T>
void append_point_data(const T *v, const std::string &id);
/** Append vector cell data to vtk file */
template <class T>
void append_cell_data(const vec<T> *v, const std::string &id);
/** Append vector point data to vtk file */
template <class T>
void append_point_data(const vec<T> *v, const std::string &id);
/** Finalize vtk file */
void close() {
if (!cell_data_written) {
cell_data_written = true;
o << "\nCELL_DATA " << nT;
}
if (!point_data_written) {
point_data_written = true;
o << "\nPOINT_DATA " << nV;
}
o.close();
}
/** Finalize and destroy vtk file */
~vtk_stream() {
close();
}
};
template <class T>
void vtk_stream::append_cell_data(const T *v, const std::string &id) {
if (!header_written)
throw std::logic_error("Write header first");
if (point_data_written)
throw std::logic_error("All cell data should be written prior to point data");
if (!cell_data_written) {
cell_data_written = true;
o << "\nCELL_DATA " << nT;
}
o << "\nSCALARS " << id << " " << name<T>()
<< "\nLOOKUP_TABLE default" << std::endl;
for (index i = 0; i < nT; i++)
put(v[i]);
}
template <class T>
void vtk_stream::append_cell_data(const vec<T> *v, const std::string &id) {
if (!header_written)
throw std::logic_error("Write header first");
if (point_data_written)
throw std::logic_error("All cell data should be written prior to point data");
if (!cell_data_written) {
cell_data_written = true;
o << "\nCELL_DATA " << nT;
}
o << "\nVECTORS " << id << " " << name<T>() << std::endl;
for (index i = 0; i < nT; i++) {
put(v[i].x);
put(v[i].y);
put(v[i].z);
}
}
template <class T>
void vtk_stream::append_point_data(const T *v, const std::string &id) {
if (!header_written)
throw std::logic_error("Write header first");
if (!cell_data_written) {
cell_data_written = true;
o << "\nCELL_DATA " << nT;
}
if (!point_data_written) {
point_data_written = true;
o << "\nPOINT_DATA " << nV;
}
o << "\nSCALARS " << id << " " << name<T>()
<< "\nLOOKUP_TABLE default" << std::endl;
for (index i = 0; i < nV; i++)
put(v[i]);
}
template <class T>
void vtk_stream::append_point_data(const vec<T> *v, const std::string &id) {
if (!header_written)
throw std::logic_error("Write header first");
if (!cell_data_written) {
cell_data_written = true;
o << "\nCELL_DATA " << nT;
}
if (!point_data_written) {
point_data_written = true;
o << "\nPOINT_DATA " << nV;
}
o << "\nVECTORS " << id << " " << name<T>() << std::endl;
for (index i = 0; i < nV; i++) {
put(v[i].x);
put(v[i].y);
put(v[i].z);
}
}
}
#endif