Tatooine
edge_vtp_writer.h
Go to the documentation of this file.
1#ifndef TATOOINE_DETAIL_UNSTRUCTURED_SIMPLICIAL_GRID_EDGE_VTP_WRITER_H
2#define TATOOINE_DETAIL_UNSTRUCTURED_SIMPLICIAL_GRID_EDGE_VTP_WRITER_H
3//==============================================================================
4#include <tatooine/concepts.h>
6#include <tatooine/linspace.h>
7#include <tatooine/vtk/xml.h>
8
9#include <array>
10#include <vector>
11//==============================================================================
13//==============================================================================
14template <typename Grid, unsigned_integral HeaderType = std::uint64_t,
15 integral ConnectivityInt = std::int64_t,
16 integral OffsetInt = std::int64_t>
17requires(Grid::num_dimensions() == 2 || Grid::num_dimensions() == 3)
19 static auto constexpr num_dimensions() { return Grid::num_dimensions(); }
20 using vertex_property_type = typename Grid::vertex_property_type;
21 template <typename T>
23 typename Grid::template typed_vertex_property_type<T>;
24 //----------------------------------------------------------------------------
25 Grid const& m_grid;
26 //----------------------------------------------------------------------------
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() +
31 " for writing."};
32 }
33 auto offset = std::size_t{};
34 write_vtk_file(file, offset);
35 }
36 //----------------------------------------------------------------------------
37 private:
38 auto write_vtk_file(std::ofstream& file, auto& offset) const {
39 auto const num_bytes_points = HeaderType(sizeof(typename Grid::real_type) *
40 3 * m_grid.vertices().size());
41 auto const num_bytes_connectivity = m_grid.simplices().size() *
42 m_grid.num_vertices_per_simplex() *
43 sizeof(ConnectivityInt);
44 auto const num_bytes_offsets =
45 sizeof(OffsetInt) * m_grid.simplices().size();
46 file << "<VTKFile"
47 << " type=\"PolyData\""
48 << " version=\"1.0\""
49 << " byte_order=\"LittleEndian\""
50 << " header_type=\""
52 vtk::xml::to_data_type<HeaderType>())
53 << "\">\n"
54 << "<PolyData>\n"
55 << "<Piece"
56 << " NumberOfPoints=\"" << m_grid.vertices().size() << "\""
57 << " NumberOfPolys=\"0\""
58 << " NumberOfVerts=\"0\""
59 << " NumberOfLines=\"" << m_grid.simplices().size() << "\""
60 << " NumberOfStrips=\"0\""
61 << ">\n"
62 // Points
63 << "<Points>"
64 << "<DataArray"
65 << " format=\"appended\""
66 << " offset=\"" << offset << "\""
67 << " type=\""
69 vtk::xml::to_data_type<typename Grid::real_type>())
70 << "\" NumberOfComponents=\"3\"/>"
71 << "</Points>\n";
72 offset += num_bytes_points + sizeof(HeaderType);
73 // Lines
74 file << "<Lines>\n"
75 // Lines - connectivity
76 << "<DataArray format=\"appended\" offset=\"" << offset << "\" type=\""
78 vtk::xml::to_data_type<ConnectivityInt>())
79 << "\" Name=\"connectivity\"/>\n";
80 offset += num_bytes_connectivity + sizeof(HeaderType);
81 // Lines - offsets
82 file << "<DataArray format=\"appended\" offset=\"" << offset << "\" type=\""
84 vtk::xml::to_data_type<OffsetInt>())
85 << "\" Name=\"offsets\"/>\n";
86 offset += num_bytes_offsets + sizeof(HeaderType);
87 file << "</Lines>\n"
88 << "</Piece>\n"
89 << "</PolyData>\n"
90 << "<AppendedData encoding=\"raw\">\n_";
91 // Writing vertex data to appended data section
92
93 using namespace std::ranges;
94 {
95 file.write(reinterpret_cast<char const*>(&num_bytes_points),
96 sizeof(HeaderType));
97 if constexpr (Grid::num_dimensions() == 2) {
98 auto point_data = std::vector<vec<typename Grid::real_type, 3>>(
99 m_grid.vertices().size());
100 auto position = [this](auto const v) -> auto& { return m_grid.at(v); };
101 constexpr auto to_3d = [](auto const& p) {
102 return vec{p.x(), p.y(), typename Grid::real_type(0)};
103 };
104 copy(m_grid.vertices() | views::transform(position) |
105 views::transform(to_3d),
106 begin(point_data));
107 file.write(reinterpret_cast<char const*>(point_data.data()),
108 num_bytes_points);
109 } else if constexpr (Grid::num_dimensions() == 3) {
110 file.write(reinterpret_cast<char const*>(m_grid.vertices().data()),
111 num_bytes_points);
112 }
113 }
114
115 // Writing lines connectivity data to appended data section
116 {
117 auto connectivity_data = std::vector<ConnectivityInt>(
118 m_grid.simplices().size() * m_grid.num_vertices_per_simplex());
119 auto index = [](auto const x) -> ConnectivityInt { return x.index(); };
120 copy(m_grid.simplices().data_container() | views::transform(index),
121 begin(connectivity_data));
122 file.write(reinterpret_cast<char const*>(&num_bytes_connectivity),
123 sizeof(HeaderType));
124 file.write(reinterpret_cast<char const*>(connectivity_data.data()),
125 num_bytes_connectivity);
126 }
127
128 // Writing lines offsets to appended data section
129 {
130 auto offsets = std::vector<OffsetInt>(m_grid.simplices().size(),
131 m_grid.num_vertices_per_simplex());
132 for (std::size_t i = 1; i < size(offsets); ++i) {
133 offsets[i] += offsets[i - 1];
134 };
135 file.write(reinterpret_cast<char const*>(&num_bytes_offsets),
136 sizeof(HeaderType));
137 file.write(reinterpret_cast<char const*>(offsets.data()),
138 num_bytes_offsets);
139 }
140 file << "\n</AppendedData>\n"
141 << "</VTKFile>";
142 }
143};
144//==============================================================================
145} // namespace tatooine::detail::unstructured_simplicial_grid
146//==============================================================================
147#endif
Definition: concepts.h:21
Definition: concepts.h:27
Definition: edge_vtp_writer.h:12
auto size(simplex_container< Real, NumDimensions, SimplexDim > simplices)
Definition: simplex_container.h:89
auto begin(simplex_container< Real, NumDimensions, SimplexDim > simplices)
Definition: simplex_container.h:79
auto to_string(data_type const t) -> std::string_view
auto constexpr index(handle< Child, Int > const h)
Definition: handle.h:119
auto write_vtk_file(std::ofstream &file, auto &offset) const
Definition: edge_vtp_writer.h:38
auto write(filesystem::path const &path) const
Definition: edge_vtp_writer.h:27
static auto constexpr num_dimensions()
Definition: edge_vtp_writer.h:19
typename Grid::template typed_vertex_property_type< T > typed_vertex_property_type
Definition: edge_vtp_writer.h:23
typename Grid::vertex_property_type vertex_property_type
Definition: edge_vtp_writer.h:20
Grid const & m_grid
Definition: edge_vtp_writer.h:25
Definition: vec.h:12