8#ifndef GRIDFORMAT_VTK_VTS_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTS_WRITER_HPP_
19#include <gridformat/common/field_storage.hpp>
20#include <gridformat/common/lvalue_reference.hpp>
22#include <gridformat/grid/grid.hpp>
24#include <gridformat/grid/type_traits.hpp>
34template<Concepts::StructuredGr
id Gr
id>
37 using CType = CoordinateType<Grid>;
38 static constexpr std::size_t dim = dimension<Grid>;
39 static_assert(dim <= 3);
43 std::array<std::size_t, dim> whole_extent;
46 using Offset = std::array<std::size_t, dim>;
48 explicit VTSWriter(LValueReferenceOf<const Grid> grid,
50 : ParentType(grid.get(),
".vts",
true, std::move(xml_opts))
53 VTSWriter as_piece_for(Domain domain)
const {
54 auto result = this->with(this->_xml_opts);
55 result._domain = std::move(domain);
56 result._offset = _offset;
60 VTSWriter with_offset(Offset offset)
const {
61 auto result = this->with(this->_xml_opts);
62 result._offset = std::move(offset);
63 result._domain = _domain;
68 VTSWriter _with(VTK::XMLOptions xml_opts)
const override {
69 return VTSWriter{this->grid(), std::move(xml_opts)};
72 void _write(std::ostream& s)
const override {
73 auto context = this->_get_write_context(
"StructuredGrid");
74 _set_attributes(context);
76 FieldStorage vtk_point_fields;
77 FieldStorage vtk_cell_fields;
78 std::ranges::for_each(this->_point_field_names(), [&] (
const std::string& name) {
79 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
80 this->_set_data_array(context,
"Piece/PointData", name, vtk_cell_fields.get(name));
82 std::ranges::for_each(this->_cell_field_names(), [&] (
const std::string& name) {
83 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
84 this->_set_data_array(context,
"Piece/CellData", name, vtk_cell_fields.get(name));
87 const FieldPtr coords_field = std::visit([&] <
typename T> (
const Precision<T>&) {
88 return VTK::make_coordinates_field<T>(this->grid(),
true);
89 }, this->_xml_settings.coordinate_precision);
90 this->_set_data_array(context,
"Piece/Points",
"Coordinates", *coords_field);
92 this->_write_xml(std::move(context), s);
95 void _set_attributes(
typename ParentType::WriteContext& context)
const {
96 _set_domain_attributes(context);
97 _set_extent_attributes(context);
100 void _set_domain_attributes(
typename ParentType::WriteContext& context)
const {
101 using VTK::CommonDetail::extents_string;
103 this->_set_attribute(context,
"",
"WholeExtent", extents_string(_domain->whole_extent));
105 this->_set_attribute(context,
"",
"WholeExtent", extents_string(this->grid()));
108 void _set_extent_attributes(
typename ParentType::WriteContext& context)
const {
109 using VTK::CommonDetail::extents_string;
110 if (
int i = 0; _offset) {
111 auto begin = (*_offset);
112 auto end = GridFormat::extents(this->grid());
113 std::ranges::for_each(end, [&] (std::integral
auto& ex) { ex += begin[i++]; });
114 this->_set_attribute(context,
"Piece",
"Extent", extents_string(begin, end));
116 this->_set_attribute(context,
"Piece",
"Extent", extents_string(this->grid()));
120 std::optional<Domain> _domain;
121 std::optional<Offset> _offset;
125VTSWriter(G&&, VTK::XMLOptions = {}) -> VTSWriter<std::remove_cvref_t<G>>;
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Definition: vts_writer.hpp:42
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.