GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
vts_writer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
8#ifndef GRIDFORMAT_VTK_VTS_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTS_WRITER_HPP_
10
11#include <array>
12#include <ranges>
13#include <ostream>
14#include <utility>
15#include <string>
16#include <optional>
17
19#include <gridformat/common/field_storage.hpp>
20#include <gridformat/common/lvalue_reference.hpp>
21
22#include <gridformat/grid/grid.hpp>
24#include <gridformat/grid/type_traits.hpp>
27
28namespace GridFormat {
29
34template<Concepts::StructuredGrid Grid>
35class VTSWriter : public VTK::XMLWriterBase<Grid, VTSWriter<Grid>> {
37 using CType = CoordinateType<Grid>;
38 static constexpr std::size_t dim = dimension<Grid>;
39 static_assert(dim <= 3);
40
41 public:
42 struct Domain {
43 std::array<std::size_t, dim> whole_extent;
44 };
45
46 using Offset = std::array<std::size_t, dim>;
47
48 explicit VTSWriter(LValueReferenceOf<const Grid> grid,
49 VTK::XMLOptions xml_opts = {})
50 : ParentType(grid.get(), ".vts", true, std::move(xml_opts))
51 {}
52
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;
57 return result;
58 }
59
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;
64 return result;
65 }
66
67 private:
68 VTSWriter _with(VTK::XMLOptions xml_opts) const override {
69 return VTSWriter{this->grid(), std::move(xml_opts)};
70 }
71
72 void _write(std::ostream& s) const override {
73 auto context = this->_get_write_context("StructuredGrid");
74 _set_attributes(context);
75
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));
81 });
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));
85 });
86
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);
91
92 this->_write_xml(std::move(context), s);
93 }
94
95 void _set_attributes(typename ParentType::WriteContext& context) const {
96 _set_domain_attributes(context);
97 _set_extent_attributes(context);
98 }
99
100 void _set_domain_attributes(typename ParentType::WriteContext& context) const {
101 using VTK::CommonDetail::extents_string;
102 if (_domain)
103 this->_set_attribute(context, "", "WholeExtent", extents_string(_domain->whole_extent));
104 else
105 this->_set_attribute(context, "", "WholeExtent", extents_string(this->grid()));
106 }
107
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));
115 } else {
116 this->_set_attribute(context, "Piece", "Extent", extents_string(this->grid()));
117 }
118 }
119
120 std::optional<Domain> _domain;
121 std::optional<Offset> _offset;
122};
123
124template<typename G>
125VTSWriter(G&&, VTK::XMLOptions = {}) -> VTSWriter<std::remove_cvref_t<G>>;
126
127} // namespace GridFormat
128
129#endif // GRIDFORMAT_VTK_VTS_WRITER_HPP_
Base class for VTK-XML Writer implementations.
Definition: xml.hpp:186
Writer for .vts file format.
Definition: vts_writer.hpp:35
Grid concepts.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Options for VTK-XML files for setting the desired encoding, data format and compression.
Definition: xml.hpp:99
Definition: vts_writer.hpp:42
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.