8#ifndef GRIDFORMAT_VTK_VTU_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTU_WRITER_HPP_
16#include <gridformat/common/field_storage.hpp>
17#include <gridformat/common/lvalue_reference.hpp>
19#include <gridformat/grid/grid.hpp>
29template<Concepts::UnstructuredGr
id Gr
id>
34 explicit VTUWriter(LValueReferenceOf<const Grid> grid,
36 :
ParentType(grid.get(),
".vtu",
false, std::move(xml_opts))
41 return VTUWriter{this->grid(), std::move(xml_opts)};
44 void _write(std::ostream& s)
const override {
45 auto context = this->_get_write_context(
"UnstructuredGrid");
46 this->_set_attribute(context,
"Piece",
"NumberOfPoints", number_of_points(this->grid()));
47 this->_set_attribute(context,
"Piece",
"NumberOfCells", number_of_cells(this->grid()));
49 FieldStorage vtk_point_fields;
50 FieldStorage vtk_cell_fields;
51 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
52 vtk_point_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
53 this->_set_data_array(context,
"Piece/PointData", name, vtk_point_fields.get(name));
55 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
56 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
57 this->_set_data_array(context,
"Piece/CellData", name, vtk_cell_fields.get(name));
60 const auto point_id_map = make_point_id_map(this->grid());
61 const FieldPtr coords_field = std::visit([&] <
typename T> (
const Precision<T>&) {
62 return VTK::make_coordinates_field<T>(this->grid(),
false);
63 }, this->_xml_settings.coordinate_precision);
64 const FieldPtr connectivity_field = std::visit([&] <
typename T> (
const Precision<T>&) {
65 return VTK::make_connectivity_field<T>(this->grid(), point_id_map);
66 }, this->_xml_settings.header_precision);
67 const FieldPtr offsets_field = std::visit([&] <
typename T> (
const Precision<T>&) {
68 return VTK::make_offsets_field<T>(this->grid());
69 }, this->_xml_settings.header_precision);
70 const FieldPtr types_field = VTK::make_cell_types_field(this->grid());
71 this->_set_data_array(context,
"Piece/Points",
"Coordinates", *coords_field);
72 this->_set_data_array(context,
"Piece/Cells",
"connectivity", *connectivity_field);
73 this->_set_data_array(context,
"Piece/Cells",
"offsets", *offsets_field);
74 this->_set_data_array(context,
"Piece/Cells",
"types", *types_field);
75 this->_write_xml(std::move(context), s);
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.