8#ifndef GRIDFORMAT_VTK_VTP_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTP_WRITER_HPP_
17#include <gridformat/common/ranges.hpp>
18#include <gridformat/common/field_storage.hpp>
19#include <gridformat/common/lvalue_reference.hpp>
21#include <gridformat/grid/grid.hpp>
22#include <gridformat/grid/filtered.hpp>
32 template<
typename Gr
id, std::
size_t size>
33 struct CellTypesPredicate {
34 std::reference_wrapper<const Grid> grid;
35 std::array<CellType, size> cell_types;
37 bool operator()(
const Cell<Grid>& cell)
const {
38 return std::ranges::any_of(cell_types, [&] (
const CellType& _ct) {
39 return _ct == type(grid.get(), cell);
44 template<
typename G, std::
size_t s>
45 CellTypesPredicate(G&&, std::array<CellType, s>&&) -> CellTypesPredicate<std::remove_cvref_t<G>, s>;
46 template<
typename G, std::
size_t s>
47 CellTypesPredicate(G&&,
const std::array<CellType, s>&) -> CellTypesPredicate<std::remove_cvref_t<G>, s>;
56template<Concepts::UnstructuredGr
id Gr
id>
60 static constexpr std::array zero_d_types{CellType::vertex};
61 static constexpr std::array one_d_types{CellType::segment};
62 static constexpr std::array two_d_types{
63 CellType::quadrilateral,
70 explicit VTPWriter(LValueReferenceOf<const Grid> grid,
72 :
ParentType(grid.get(),
".vtp",
false, std::move(xml_opts))
77 return VTPWriter{this->grid(), std::move(xml_opts)};
80 void _write(std::ostream& s)
const override {
81 FilteredGrid verts{this->grid(), Detail::CellTypesPredicate{this->grid(), zero_d_types}};
82 FilteredGrid lines{this->grid(), Detail::CellTypesPredicate{this->grid(), one_d_types}};
83 FilteredGrid polys{this->grid(), Detail::CellTypesPredicate{this->grid(), two_d_types}};
84 FilteredGrid unsupported{
86 [p=Detail::CellTypesPredicate{
87 this->grid(), Ranges::merged(Ranges::merged(zero_d_types, one_d_types), two_d_types)
88 }] (
const Cell<Grid>& cell) {
93 auto verts_range = cells(verts);
94 auto lines_range = cells(lines);
95 auto polys_range = cells(polys);
96 auto unsupported_range = cells(unsupported);
98 if (Ranges::size(unsupported_range) > 0)
99 this->_log_warning(
"Grid contains cell types not supported by .vtp; These will be ignored.");
100 if (!std::ranges::empty(this->_cell_field_names())
101 && !std::ranges::empty(verts_range)
102 + !std::ranges::empty(lines_range)
103 + !std::ranges::empty(polys_range)
106 "You appear to have cell data defined and your grid consists of multiple cell types "
107 "(vertices, lines, polys).\nNote that for correct cell data visualization with vtk, "
108 "your cell iterator must be such that it iterates over the cell types in order "
109 "(vertices -> lines -> polys)."
112 const auto num_verts = Ranges::size(verts_range);
113 const auto num_lines = Ranges::size(lines_range);
114 const auto num_polys = Ranges::size(polys_range);
116 auto context = this->_get_write_context(
"PolyData");
117 this->_set_attribute(context,
"Piece",
"NumberOfPoints", number_of_points(this->grid()));
118 this->_set_attribute(context,
"Piece",
"NumberOfVerts", num_verts);
119 this->_set_attribute(context,
"Piece",
"NumberOfLines", num_lines);
120 this->_set_attribute(context,
"Piece",
"NumberOfStrips",
"0");
121 this->_set_attribute(context,
"Piece",
"NumberOfPolys", num_polys);
123 FieldStorage vtk_point_fields;
124 FieldStorage vtk_cell_fields;
125 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
126 vtk_point_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
127 this->_set_data_array(context,
"Piece/PointData", name, vtk_point_fields.get(name));
129 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
130 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
131 this->_set_data_array(context,
"Piece/CellData", name, vtk_cell_fields.get(name));
134 const FieldPtr coords_field = std::visit([&] <
typename T> (
const Precision<T>&) {
135 return VTK::make_coordinates_field<T>(this->grid(),
false);
136 }, this->_xml_settings.coordinate_precision);
137 this->_set_data_array(context,
"Piece/Points",
"Coordinates", *coords_field);
139 const auto point_id_map = make_point_id_map(this->grid());
140 const auto verts_connectivity_field = _make_connectivity_field(verts, point_id_map);
141 const auto verts_offsets_field = _make_offsets_field(verts);
142 this->_set_data_array(context,
"Piece/Verts",
"connectivity", *verts_connectivity_field);
143 this->_set_data_array(context,
"Piece/Verts",
"offsets", *verts_offsets_field);
145 const auto lines_connectivity_field = _make_connectivity_field(lines, point_id_map);
146 const auto lines_offsets_field = _make_offsets_field(lines);
147 this->_set_data_array(context,
"Piece/Lines",
"connectivity", *lines_connectivity_field);
148 this->_set_data_array(context,
"Piece/Lines",
"offsets", *lines_offsets_field);
150 const auto polys_connectivity_field = _make_connectivity_field(polys, point_id_map);
151 const auto polys_offsets_field = _make_offsets_field(polys);
152 this->_set_data_array(context,
"Piece/Polys",
"connectivity", *polys_connectivity_field);
153 this->_set_data_array(context,
"Piece/Polys",
"offsets", *polys_offsets_field);
155 this->_write_xml(std::move(context), s);
158 template<
typename G,
typename Po
intMap>
159 FieldPtr _make_connectivity_field(G&& grid,
const PointMap& point_id_map)
const {
160 return std::visit([&] <
typename T> (
const Precision<T>&) {
161 return VTK::make_connectivity_field<T>(grid, point_id_map);
162 }, this->_xml_settings.header_precision);
166 FieldPtr _make_offsets_field(G&& grid)
const {
167 return std::visit([&] <
typename T> (
const Precision<T>&) {
168 return VTK::make_offsets_field<T>(grid);
169 }, this->_xml_settings.header_precision);
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.