GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
vtu_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_VTU_WRITER_HPP_
9#define GRIDFORMAT_VTK_VTU_WRITER_HPP_
10
11#include <ranges>
12#include <ostream>
13#include <string>
14
16#include <gridformat/common/field_storage.hpp>
17#include <gridformat/common/lvalue_reference.hpp>
18
19#include <gridformat/grid/grid.hpp>
22
23namespace GridFormat {
24
29template<Concepts::UnstructuredGrid Grid>
30class VTUWriter : public VTK::XMLWriterBase<Grid, VTUWriter<Grid>> {
32
33 public:
34 explicit VTUWriter(LValueReferenceOf<const Grid> grid,
35 VTK::XMLOptions xml_opts = {})
36 : ParentType(grid.get(), ".vtu", false, std::move(xml_opts))
37 {}
38
39 private:
40 VTUWriter _with(VTK::XMLOptions xml_opts) const override {
41 return VTUWriter{this->grid(), std::move(xml_opts)};
42 }
43
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()));
48
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));
54 });
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));
58 });
59
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);
76 }
77};
78
79template<typename G>
80VTUWriter(G&&, VTK::XMLOptions = {}) -> VTUWriter<std::remove_cvref_t<G>>;
81
82} // namespace GridFormat
83
84#endif // GRIDFORMAT_VTK_VTU_WRITER_HPP_
Base class for VTK-XML Writer implementations.
Definition: xml.hpp:190
Writer for .vtu file format.
Definition: vtu_writer.hpp:30
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
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.