1#ifndef TATOOINE_DETAIL_LINE_VTP_WRITER_H
2#define TATOOINE_DETAIL_LINE_VTP_WRITER_H
15template <
typename Line, unsigned_integral HeaderType, integral ConnectivityInt,
27 auto write(filesystem::path
const& path)
const {
28 auto file = std::ofstream{path};
29 if (!file.is_open()) {
30 throw std::runtime_error{
"Could open file " + path.string() +
33 auto offset = std::size_t{};
34 write_vtk_file(file, offset);
40 <<
" type=\"PolyData\""
42 <<
" byte_order=\"LittleEndian\""
43 <<
" header_type=\"" << vtk::xml::to_data_type<HeaderType>()
45 write_polydata(file, offset);
46 write_appended_data(file);
51 file <<
" <PolyData>\n";
52 write_piece(file, offset);
53 file <<
" </PolyData>\n";
56 auto write_piece(std::ofstream& file, std::size_t& offset)
const {
61 write_points(file, offset);
62 write_lines(file, offset);
63 write_point_data(file, offset);
64 file <<
" </Piece>\n";
67 auto write_points(std::ofstream& file, std::size_t& offset)
const {
68 auto const num_bytes_points = HeaderType(
sizeof(
typename Line::real_type) *
72 <<
" format=\"appended\""
73 <<
" offset=\"" << offset <<
"\""
75 << vtk::xml::to_data_type<typename Line::real_type>()
76 <<
"\" NumberOfComponents=\"3\"/>\n"
78 offset += num_bytes_points +
sizeof(HeaderType);
81 auto write_lines(std::ofstream& file, std::size_t& offset)
const {
83 write_lines_connectivity(file, offset);
84 write_lines_offsets(file, offset);
85 file <<
" </Lines>\n";
89 std::size_t& offset)
const {
90 auto const num_bytes =
92 file <<
" <DataArray format=\"appended\" offset=\"" << offset
94 << vtk::xml::to_data_type<ConnectivityInt>()
95 <<
"\" Name=\"connectivity\"/>\n";
96 offset += num_bytes +
sizeof(HeaderType);
101 file <<
" <DataArray format=\"appended\" offset=\"" << offset
103 << vtk::xml::to_data_type<OffsetInt>()
104 <<
"\" Name=\"offsets\"/>\n";
105 offset += num_bytes +
sizeof(HeaderType);
109 file <<
" <PointData>\n";
114 name, *prop, file, offset);
116 file <<
" </PointData>\n";
120 file <<
" <AppendedData encoding=\"raw\">\n_";
121 write_vertex_positions_to_appended_data(file);
122 write_line_connectivity_to_appended_data(file);
123 write_line_offsets_to_appended_data(file);
125 write_vertex_property_appended_data<
129 file <<
"\n </AppendedData>\n";
135 file.write(
reinterpret_cast<char const*
>(&arr_size),
sizeof(HeaderType));
137 for (
auto const v : m_line.
vertices()) {
138 if constexpr (num_dimensions() == 2) {
139 file.write(
reinterpret_cast<char const*
>(m_line[v].data()),
141 file.write(
reinterpret_cast<char const*
>(&zero),
143 }
else if constexpr (num_dimensions() == 3) {
144 file.write(
reinterpret_cast<char const*
>(m_line[v].data()),
152 auto connectivity_data = std::vector<ConnectivityInt>{};
154 for (ConnectivityInt i = 0;
155 i < static_cast<ConnectivityInt>(m_line.
num_vertices()) - 1; ++i) {
156 connectivity_data.push_back(i);
157 connectivity_data.push_back(i + 1);
160 connectivity_data.push_back(
161 static_cast<ConnectivityInt
>(m_line.
num_vertices() - 1));
162 connectivity_data.push_back(0);
164 auto const arr_size =
static_cast<HeaderType
>(connectivity_data.size() *
165 sizeof(ConnectivityInt));
166 file.write(
reinterpret_cast<char const*
>(&arr_size),
sizeof(HeaderType));
167 file.write(
reinterpret_cast<char const*
>(connectivity_data.data()),
186 for (std::size_t i = 1; i <
size(offsets); ++i) {
187 offsets[i] += offsets[i - 1];
189 auto const arr_size =
191 file.write(
reinterpret_cast<char const*
>(&arr_size),
sizeof(HeaderType));
192 file.write(
reinterpret_cast<char const*
>(offsets.data()), arr_size);
208 template <
typename... Ts>
212 std::size_t& offset)
const {
214 if (prop.type() ==
typeid(Ts)) {
215 if constexpr (tensor_rank<Ts> <= 1) {
216 file <<
" <DataArray"
217 <<
" Name=\"" << name <<
"\""
218 <<
" format=\"appended\""
219 <<
" offset=\"" << offset <<
"\""
221 << tatooine::vtk::xml::to_data_type<tatooine::value_type<Ts>>()
222 <<
"\" NumberOfComponents=\""
223 << tensor_num_components<Ts> <<
"\"/>\n";
224 offset += m_line.num_vertices() * sizeof(Ts) + sizeof(HeaderType);
225 }
else if constexpr (tensor_rank<Ts> == 2) {
226 for (std::size_t i = 0; i < Ts::dimension(1); ++i) {
227 file <<
" <DataArray"
228 <<
" Name=\"" << name <<
"_col_" << i <<
"\""
229 <<
" format=\"appended\""
230 <<
" offset=\"" << offset <<
"\""
232 << vtk::xml::to_data_type<tatooine::value_type<Ts>>()
233 <<
"\" NumberOfComponents=\"" << Ts::dimension(0) <<
"\"/>\n";
235 tensor_dimension<Ts, 0> +
243 template <
typename... Ts>
245 std::ofstream& file)
const {
247 if (prop.type() ==
typeid(Ts)) {
248 write_vertex_property_appended_data(prop.template cast_to_typed<Ts>(),
254 template <
typename T>
257 if constexpr (tensor_rank<T> <= 1) {
258 auto data = std::vector<T>{};
259 for (
auto const v : m_line.vertices()) {
260 data.push_back(prop[v]);
262 auto const num_bytes =
264 m_line.num_vertices());
265 file.write(
reinterpret_cast<char const*
>(&num_bytes),
sizeof(HeaderType));
266 file.write(
reinterpret_cast<char const*
>(data.data()), num_bytes);
267 }
else if constexpr (tensor_rank<T> == 2) {
268 auto const num_bytes =
270 m_line.num_vertices() / tensor_dimension<T, 0>);
271 for (std::size_t i = 0; i < tensor_dimension<T, 1>; ++i) {
272 file.write(
reinterpret_cast<char const*
>(&num_bytes),
274 for (
auto const v : m_line.vertices()) {
275 auto data_begin = &prop[v](0, i);
276 file.write(
reinterpret_cast<char const*
>(data_begin),
VecF< 3 > vec3f
Definition: vec_typedefs.h:40
typename value_type_impl< T >::type value_type
Definition: type_traits.h:280
mat44f mat4f
Definition: mat_typedefs.h:299
VecD< 3 > vec3d
Definition: vec_typedefs.h:51
mat22d mat2d
Definition: mat_typedefs.h:370
mat33d mat3d
Definition: mat_typedefs.h:371
constexpr auto invoke(invocable auto &&...funcs)
Definition: functional.h:8
mat33f mat3f
Definition: mat_typedefs.h:298
VecD< 2 > vec2d
Definition: vec_typedefs.h:50
VecF< 4 > vec4f
Definition: vec_typedefs.h:41
line< real_number, NumDimensions > Line
Definition: line.h:857
VecD< 4 > vec4d
Definition: vec_typedefs.h:52
auto size(vec< ValueType, N > const &v)
Definition: vec.h:148
mat22f mat2f
Definition: mat_typedefs.h:297
VecF< 2 > vec2f
Definition: vec_typedefs.h:39
Definition: vtp_writer.h:18
auto write_vertex_positions_to_appended_data(std::ofstream &file) const
Definition: vtp_writer.h:132
auto write(filesystem::path const &path) const
Definition: vtp_writer.h:27
auto write_points(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:67
auto write_line_connectivity_to_appended_data(std::ofstream &file) const
Definition: vtp_writer.h:151
static auto constexpr num_dimensions()
Definition: vtp_writer.h:19
auto write_lines_offsets(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:99
auto write_line_offsets_to_appended_data(std::ofstream &file) const
Definition: vtp_writer.h:184
typename Line::template typed_vertex_property_type< T > typed_vertex_property_type
Definition: vtp_writer.h:23
auto write_polydata(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:50
auto write_vertex_property_appended_data(typed_vertex_property_type< T > const &prop, std::ofstream &file) const
Definition: vtp_writer.h:255
auto write_point_data(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:108
auto write_piece(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:56
auto write_lines(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:81
auto write_vertex_property_appended_data(vertex_property_type const &prop, std::ofstream &file) const
Definition: vtp_writer.h:244
Line const & m_line
Definition: vtp_writer.h:25
auto write_appended_data(std::ofstream &file) const
Definition: vtp_writer.h:119
auto write_vtk_file(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:38
auto write_vertex_property_data_array(auto const &name, vertex_property_type const &prop, std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:209
typename Line::vertex_property_type vertex_property_type
Definition: vtp_writer.h:20
auto write_lines_connectivity(std::ofstream &file, std::size_t &offset) const
Definition: vtp_writer.h:88
auto vertices() const
Definition: line.h:250
auto num_vertices() const
Definition: line.h:251
Real real_type
Definition: line.h:38
auto num_line_segments() const
Definition: line.h:252
static constexpr auto num_dimensions() -> std::size_t
Definition: line.h:72
deque_property< vertex_handle > vertex_property_type
Definition: line.h:55
auto is_closed() const
Definition: line.h:305
auto vertex_properties() const -> auto const &
Definition: line.h:450