Tatooine
vtk_writer.h
Go to the documentation of this file.
1#ifndef TATOOINE_DETAIL_LINE_VTL_WRITER_H
2#define TATOOINE_DETAIL_LINE_VTL_WRITER_H
3//==============================================================================
4#include <tatooine/concepts.h>
7#include <boost/range/algorithm_ext/iota.hpp>
8
9#include <array>
10#include <vector>
11//==============================================================================
12namespace tatooine::detail::line {
13//==============================================================================
14template <typename Line, unsigned_integral HeaderType = std::uint64_t,
15 integral ConnectivityInt = std::int64_t,
16 integral OffsetInt = std::int64_t>
17requires(Line::num_dimensions() == 2 ||
19 static auto constexpr num_dimensions() { return Line::num_dimensions(); }
21 template <typename T>
23 typename Line::template typed_vertex_property_type<T>;
24 //----------------------------------------------------------------------------
25 Line const& m_line;
26 //----------------------------------------------------------------------------
27 auto write(filesystem::path const& path, std::string const& title) 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 }
34 if (writer.is_open()) {
35 writer.set_title(title);
36 writer.write_header();
37
38 // write points
39 auto ps = std::vector<std::array<typename Line::real_type, 3>>{};
40 ps.reserve(m_line.vertices().size());
41 for (auto const& v : m_line.vertices()) {
42 auto const& p = m_line[v];
43 if constexpr (num_dimensions() == 3) {
44 ps.push_back({p(0), p(1), p(2)});
45 } else {
46 ps.push_back({p(0), p(1), 0});
47 }
48 }
49 writer.write_points(ps);
50
51 // write lines
52 auto line_seq = std::vector<std::vector<std::size_t>>(
53 1, std::vector<std::size_t>(m_line.vertices().size()));
54 boost::iota(line_seq.front(), 0);
55 if (m_line.is_closed()) {
56 line_seq.front().push_back(0);
57 }
58 writer.write_lines(line_seq);
59
60 writer.write_point_data(m_line.vertices().size());
61
62 // write properties
63 for (auto& [name, prop] : m_line.vertex_properties()) {
64 auto const& type = prop->type();
65 if (type == typeid(float)) {
66 write_prop<float>(writer, name, prop);
67 } else if (type == typeid(vec<float, 2>)) {
68 write_prop<vec<float, 2>>(writer, name, prop);
69 } else if (type == typeid(vec<float, 3>)) {
70 write_prop<vec<float, 3>>(writer, name, prop);
71 } else if (type == typeid(vec<float, 4>)) {
72 write_prop<vec<float, 4>>(writer, name, prop);
73
74 } else if (type == typeid(double)) {
75 write_prop<double>(writer, name, prop);
76 } else if (type == typeid(vec<double, 2>)) {
77 write_prop<vec<double, 2>>(writer, name, prop);
78 } else if (type == typeid(vec<double, 3>)) {
79 write_prop<vec<double, 3>>(writer, name, prop);
80 } else if (type == typeid(vec<double, 4>)) {
81 write_prop<vec<double, 4>>(writer, name, prop);
82 }
83 }
84 writer.close();
85 }
86 }
87 //----------------------------------------------------------------------------
88 template <typename T>
89 static auto write_prop(
90 vtk::legacy_file_writer& writer, std::string const& name,
91 std::unique_ptr<vertex_property_type> const& prop) -> void {
92 auto const& deque = dynamic_cast<typed_vertex_property_type<T>*>(prop.get())
93 ->internal_container();
94
95 writer.write_scalars(name, std::vector<T>(begin(deque), end(deque)));
96 }
97};
98//==============================================================================
99} // namespace tatooine::detail::line
100//==============================================================================
101#endif
Definition: vtk_legacy.h:448
Definition: merge.h:8
auto end(vertex_container< Real, NumDimensions, Handle > const &it)
Definition: vertex_container.h:46
auto begin(vertex_container< Real, NumDimensions, Handle > const &it)
Definition: vertex_container.h:41
line< real_number, NumDimensions > Line
Definition: line.h:857
Definition: vtk_writer.h:18
auto write(filesystem::path const &path, std::string const &title) const
Definition: vtk_writer.h:27
typename Line::vertex_property_type vertex_property_type
Definition: vtk_writer.h:20
Line const & m_line
Definition: vtk_writer.h:25
typename Line::template typed_vertex_property_type< T > typed_vertex_property_type
Definition: vtk_writer.h:23
static auto write_prop(vtk::legacy_file_writer &writer, std::string const &name, std::unique_ptr< vertex_property_type > const &prop) -> void
Definition: vtk_writer.h:89
static auto constexpr num_dimensions()
Definition: vtk_writer.h:19
Definition: line.h:35
auto vertices() const
Definition: line.h:250
auto push_back(arithmetic auto const ... components)
Definition: line.h:176
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
Definition: vec.h:12