8#ifndef GRIDFORMAT_VTK_VTR_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTR_WRITER_HPP_
20#include <gridformat/common/field_storage.hpp>
21#include <gridformat/common/lvalue_reference.hpp>
23#include <gridformat/grid/grid.hpp>
24#include <gridformat/grid/type_traits.hpp>
34template<Concepts::RectilinearGr
id Gr
id>
37 using CType = CoordinateType<Grid>;
38 static constexpr std::size_t dim = dimension<Grid>;
39 static constexpr std::size_t space_dim = 3;
40 static_assert(dim <= 3);
44 std::array<std::size_t, dim> whole_extent;
47 using Offset = std::array<std::size_t, dim>;
49 explicit VTRWriter(LValueReferenceOf<const Grid> grid,
51 : ParentType(grid.get(),
".vtr",
true, std::move(xml_opts))
54 VTRWriter as_piece_for(Domain domain)
const {
55 auto result = this->with(this->_xml_opts);
56 result._domain = std::move(domain);
57 result._offset = _offset;
61 VTRWriter with_offset(Offset offset)
const {
62 auto result = this->with(this->_xml_opts);
63 result._offset = std::move(offset);
64 result._domain = _domain;
69 VTRWriter _with(VTK::XMLOptions xml_opts)
const override {
70 return VTRWriter{this->grid(), std::move(xml_opts)};
73 void _write(std::ostream& s)
const override {
74 auto context = this->_get_write_context(
"RectilinearGrid");
75 _set_attributes(context);
77 FieldStorage vtk_point_fields;
78 FieldStorage vtk_cell_fields;
79 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
80 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
81 this->_set_data_array(context,
"Piece/PointData", name, vtk_cell_fields.get(name));
83 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
84 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
85 this->_set_data_array(context,
"Piece/CellData", name, vtk_cell_fields.get(name));
88 const auto coord_fields = _make_ordinate_fields();
89 for (
unsigned dir = 0; dir < space_dim; ++dir)
90 this->_set_data_array(context,
"Piece/Coordinates",
"X_" + std::to_string(dir), *coord_fields[dir]);
91 this->_write_xml(std::move(context), s);
94 void _set_attributes(
typename ParentType::WriteContext& context)
const {
95 _set_domain_attributes(context);
96 _set_extent_attributes(context);
99 void _set_domain_attributes(
typename ParentType::WriteContext& context)
const {
100 using VTK::CommonDetail::extents_string;
102 this->_set_attribute(context,
"",
"WholeExtent", extents_string(_domain->whole_extent));
104 this->_set_attribute(context,
"",
"WholeExtent", extents_string(this->grid()));
107 void _set_extent_attributes(
typename ParentType::WriteContext& context)
const {
108 using VTK::CommonDetail::extents_string;
109 if (
int i = 0; _offset) {
110 auto begin = (*_offset);
111 auto end = GridFormat::extents(this->grid());
112 std::ranges::for_each(end, [&] (std::integral
auto& ex) { ex += begin[i++]; });
113 this->_set_attribute(context,
"Piece",
"Extent", extents_string(begin, end));
115 this->_set_attribute(context,
"Piece",
"Extent", extents_string(this->grid()));
119 std::array<FieldPtr, space_dim> _make_ordinate_fields()
const {
120 std::array<FieldPtr, space_dim> result;
121 std::visit([&] <
typename T> (
const Precision<T>& prec) {
122 for (
unsigned dir = 0; dir < space_dim; ++dir) {
124 result[dir] =
make_field_ptr(RangeField{ordinates(this->grid(), dir), prec});
126 result[dir] =
make_field_ptr(RangeField{std::vector<double>{}, prec});
128 }, this->_xml_settings.coordinate_precision);
132 std::optional<Domain> _domain;
133 std::optional<Offset> _offset;
137VTRWriter(G&&, VTK::XMLOptions = {}) -> VTRWriter<std::remove_cvref_t<G>>;
141template<
typename... Args>
FieldPtr make_field_ptr(F &&f)
Factory function for field pointers.
Definition: field.hpp:192
Definition: vtr_writer.hpp:43
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.