GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
vtp_writer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
8#ifndef GRIDFORMAT_VTK_VTP_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTP_WRITER_HPP_
10
11#include <ranges>
12#include <ostream>
13#include <iostream>
14#include <algorithm>
15#include <functional>
16
17#include <gridformat/common/ranges.hpp>
18#include <gridformat/common/filtered_range.hpp>
19#include <gridformat/common/field_storage.hpp>
20#include <gridformat/common/lvalue_reference.hpp>
21
22#include <gridformat/grid/grid.hpp>
25
26namespace GridFormat {
27
28#ifndef DOXYGEN
29namespace Detail {
30
31 template<typename Grid, std::size_t size>
32 struct CellTypesPredicate {
33 std::reference_wrapper<const Grid> grid;
34 std::array<CellType, size> cell_types;
35
36 bool operator()(const Cell<Grid>& cell) const {
37 return std::ranges::any_of(cell_types, [&] (const CellType& _ct) {
38 return _ct == type(grid.get(), cell);
39 });
40 }
41 };
42
43 template<typename G, std::size_t s>
44 CellTypesPredicate(G&&, std::array<CellType, s>&&) -> CellTypesPredicate<std::remove_cvref_t<G>, s>;
45 template<typename G, std::size_t s>
46 CellTypesPredicate(G&&, const std::array<CellType, s>&) -> CellTypesPredicate<std::remove_cvref_t<G>, s>;
47
48} // namespace Detail
49#endif // DOXYGEN
50
55template<Concepts::UnstructuredGrid Grid>
56class VTPWriter : public VTK::XMLWriterBase<Grid, VTPWriter<Grid>> {
58
59 static constexpr std::array zero_d_types{CellType::vertex};
60 static constexpr std::array one_d_types{CellType::segment};
61 static constexpr std::array two_d_types{
62 CellType::quadrilateral,
63 CellType::pixel,
64 CellType::polygon,
65 CellType::triangle
66 };
67
68 public:
69 explicit VTPWriter(LValueReferenceOf<const Grid> grid,
70 VTK::XMLOptions xml_opts = {})
71 : ParentType(grid.get(), ".vtp", false, std::move(xml_opts))
72 {}
73
74 private:
75 VTPWriter _with(VTK::XMLOptions xml_opts) const override {
76 return VTPWriter{this->grid(), std::move(xml_opts)};
77 }
78
79 void _write(std::ostream& s) const override {
80 auto verts_range = _get_cell_range(Detail::CellTypesPredicate{this->grid(), zero_d_types});
81 auto lines_range = _get_cell_range(Detail::CellTypesPredicate{this->grid(), one_d_types});
82 auto polys_range = _get_cell_range(Detail::CellTypesPredicate{this->grid(), two_d_types});
83 auto unsupported_range = _get_cell_range(
84 [p=Detail::CellTypesPredicate{
85 this->grid(), Ranges::merged(Ranges::merged(zero_d_types, one_d_types), two_d_types)
86 }] (const Cell<Grid>& cell) {
87 return !p(cell);
88 }
89 );
90
91 if (Ranges::size(unsupported_range) > 0)
92 this->_log_warning("Grid contains cell types not supported by .vtp; These will be ignored.");
93 if (!std::ranges::empty(this->_cell_field_names())
94 && !std::ranges::empty(verts_range)
95 + !std::ranges::empty(lines_range)
96 + !std::ranges::empty(polys_range)
97 > 1)
98 this->_log_warning(
99 "You appear to have cell data defined and your grid consists of multiple cell types "
100 "(vertices, lines, polys).\nNote that for correct cell data visualization with vtk, "
101 "your cell iterator must be such that it iterates over the cell types in order "
102 "(vertices -> lines -> polys)."
103 );
104
105 const auto num_verts = Ranges::size(verts_range);
106 const auto num_lines = Ranges::size(lines_range);
107 const auto num_polys = Ranges::size(polys_range);
108
109 auto context = this->_get_write_context("PolyData");
110 this->_set_attribute(context, "Piece", "NumberOfPoints", number_of_points(this->grid()));
111 this->_set_attribute(context, "Piece", "NumberOfVerts", num_verts);
112 this->_set_attribute(context, "Piece", "NumberOfLines", num_lines);
113 this->_set_attribute(context, "Piece", "NumberOfStrips", "0");
114 this->_set_attribute(context, "Piece", "NumberOfPolys", num_polys);
115
116 FieldStorage vtk_point_fields;
117 FieldStorage vtk_cell_fields;
118 std::ranges::for_each(this->_point_field_names(), [&] (const std::string& name) {
119 vtk_point_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
120 this->_set_data_array(context, "Piece/PointData", name, vtk_point_fields.get(name));
121 });
122 std::ranges::for_each(this->_cell_field_names(), [&] (const std::string& name) {
123 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
124 this->_set_data_array(context, "Piece/CellData", name, vtk_cell_fields.get(name));
125 });
126
127 const FieldPtr coords_field = std::visit([&] <typename T> (const Precision<T>&) {
128 return VTK::make_coordinates_field<T>(this->grid(), false);
129 }, this->_xml_settings.coordinate_precision);
130 this->_set_data_array(context, "Piece/Points", "Coordinates", *coords_field);
131
132 const auto point_id_map = make_point_id_map(this->grid());
133 const auto verts_connectivity_field = _make_connectivity_field(verts_range, point_id_map);
134 const auto verts_offsets_field = _make_offsets_field(verts_range);
135 this->_set_data_array(context, "Piece/Verts", "connectivity", *verts_connectivity_field);
136 this->_set_data_array(context, "Piece/Verts", "offsets", *verts_offsets_field);
137
138 const auto lines_connectivity_field = _make_connectivity_field(lines_range, point_id_map);
139 const auto lines_offsets_field = _make_offsets_field(lines_range);
140 this->_set_data_array(context, "Piece/Lines", "connectivity", *lines_connectivity_field);
141 this->_set_data_array(context, "Piece/Lines", "offsets", *lines_offsets_field);
142
143 const auto polys_connectivity_field = _make_connectivity_field(polys_range, point_id_map);
144 const auto polys_offsets_field = _make_offsets_field(polys_range);
145 this->_set_data_array(context, "Piece/Polys", "connectivity", *polys_connectivity_field);
146 this->_set_data_array(context, "Piece/Polys", "offsets", *polys_offsets_field);
147
148 this->_write_xml(std::move(context), s);
149 }
150
151 template<typename Predicate>
152 std::ranges::forward_range auto _get_cell_range(Predicate&& pred) const {
153 return Ranges::filter_by(std::forward<Predicate>(pred), cells(this->grid()));
154 }
155
156 template<typename CellsRange, typename PointMap>
157 FieldPtr _make_connectivity_field(CellsRange&& cells, const PointMap& point_id_map) const {
158 return std::visit([&] <typename T> (const Precision<T>&) {
159 return VTK::make_connectivity_field<T>(this->grid(), cells, point_id_map);
160 }, this->_xml_settings.header_precision);
161 }
162
163 template<typename CellsRange>
164 FieldPtr _make_offsets_field(CellsRange&& cells) const {
165 return std::visit([&] <typename T> (const Precision<T>&) {
166 return VTK::make_offsets_field<T>(this->grid(), cells);
167 }, this->_xml_settings.header_precision);
168 }
169};
170
171template<typename G>
172VTPWriter(G&&, VTK::XMLOptions = {}) -> VTPWriter<std::remove_cvref_t<G>>;
173
174} // namespace GridFormat
175
176#endif // GRIDFORMAT_VTK_VTP_WRITER_HPP_
Base class for VTK-XML Writer implementations.
Definition: xml.hpp:186
Writer for .vtu file format.
Definition: vtp_writer.hpp:56
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Options for VTK-XML files for setting the desired encoding, data format and compression.
Definition: xml.hpp:99
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.