8#ifndef GRIDFORMAT_VTK_VTI_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTI_WRITER_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/type_traits.hpp>
32template<Concepts::ImageGr
id Gr
id>
35 using CType = CoordinateType<Grid>;
36 static constexpr std::size_t dim = dimension<Grid>;
40 std::array<CType, dim> origin;
41 std::array<std::size_t, dim> whole_extent;
44 using Offset = std::array<std::size_t, dim>;
46 explicit VTIWriter(LValueReferenceOf<const Grid> grid,
48 : ParentType(grid.get(),
".vti",
true, std::move(xml_opts))
51 VTIWriter as_piece_for(Domain domain)
const {
52 auto result = this->with(this->_xml_opts);
53 result._domain = std::move(domain);
54 result._offset = _offset;
58 VTIWriter with_offset(Offset offset)
const {
59 auto result = this->with(this->_xml_opts);
60 result._offset = std::move(offset);
61 result._domain = _domain;
66 VTIWriter _with(VTK::XMLOptions xml_opts)
const override {
67 return VTIWriter{this->grid(), std::move(xml_opts)};
70 void _write(std::ostream& s)
const override {
71 auto context = this->_get_write_context(
"ImageData");
72 _set_attributes(context);
74 FieldStorage vtk_point_fields;
75 FieldStorage vtk_cell_fields;
76 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
77 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
78 this->_set_data_array(context,
"Piece/PointData", name, vtk_cell_fields.get(name));
80 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
81 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
82 this->_set_data_array(context,
"Piece/CellData", name, vtk_cell_fields.get(name));
85 this->_write_xml(std::move(context), s);
88 void _set_attributes(
typename ParentType::WriteContext& context)
const {
89 _set_domain_attributes(context);
90 _set_extent_attributes(context);
91 this->_set_attribute(context,
"",
"Spacing", VTK::CommonDetail::number_string_3d(spacing(this->grid())));
92 this->_set_attribute(context,
"",
"Direction", VTK::CommonDetail::direction_string(basis(this->grid())));
95 void _set_domain_attributes(
typename ParentType::WriteContext& context)
const {
96 using VTK::CommonDetail::extents_string;
97 using VTK::CommonDetail::number_string_3d;
99 this->_set_attribute(context,
"",
"WholeExtent", extents_string(_domain->whole_extent));
100 this->_set_attribute(context,
"",
"Origin", number_string_3d(_domain->origin));
102 this->_set_attribute(context,
"",
"WholeExtent", extents_string(this->grid()));
103 this->_set_attribute(context,
"",
"Origin", number_string_3d(origin(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::optional<Domain> _domain;
120 std::optional<Offset> _offset;
124VTIWriter(G&&, VTK::XMLOptions opts = {}) -> VTIWriter<std::remove_cvref_t<G>>;
128template<
typename... Args>
Definition: vti_writer.hpp:39
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.