GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/vts_writer.hpp
Date: 2024-11-20 14:41:59
Exec Total Coverage
Lines: 49 49 100.0%
Functions: 37 40 92.5%
Branches: 53 100 53.0%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup VTK
6 * \copydoc GridFormat::VTSWriter
7 */
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
18 #include <gridformat/common/field.hpp>
19 #include <gridformat/common/field_storage.hpp>
20 #include <gridformat/common/lvalue_reference.hpp>
21
22 #include <gridformat/grid/grid.hpp>
23 #include <gridformat/grid/concepts.hpp>
24 #include <gridformat/grid/type_traits.hpp>
25 #include <gridformat/vtk/common.hpp>
26 #include <gridformat/vtk/xml.hpp>
27
28 namespace GridFormat {
29
30 /*!
31 * \ingroup VTK
32 * \brief Writer for .vts file format
33 */
34 template<Concepts::StructuredGrid Grid>
35 class VTSWriter : public VTK::XMLWriterBase<Grid, VTSWriter<Grid>> {
36 using ParentType = 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 14181 explicit VTSWriter(LValueReferenceOf<const Grid> grid,
49 VTK::XMLOptions xml_opts = {})
50
2/4
✓ Branch 2 taken 7141 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 7141 times.
✗ Branch 7 not taken.
42543 : ParentType(grid.get(), ".vts", true, std::move(xml_opts))
51 14181 {}
52
53 4112 VTSWriter as_piece_for(Domain domain) const {
54 4112 auto result = this->with(this->_xml_opts);
55 4112 result._domain = std::move(domain);
56 4112 result._offset = _offset;
57 4112 return result;
58 }
59
60 4112 VTSWriter with_offset(Offset offset) const {
61 4112 auto result = this->with(this->_xml_opts);
62 4112 result._offset = std::move(offset);
63 4112 result._domain = _domain;
64 4112 return result;
65 }
66
67 private:
68 9484 VTSWriter _with(VTK::XMLOptions xml_opts) const override {
69
1/2
✓ Branch 4 taken 4774 times.
✗ Branch 5 not taken.
9484 return VTSWriter{this->grid(), std::move(xml_opts)};
70 }
71
72 4744 void _write(std::ostream& s) const override {
73
2/4
✓ Branch 1 taken 2396 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2396 times.
✗ Branch 5 not taken.
9488 auto context = this->_get_write_context("StructuredGrid");
74
1/2
✓ Branch 1 taken 2396 times.
✗ Branch 2 not taken.
4744 _set_attributes(context);
75
76 4744 FieldStorage vtk_point_fields;
77 4744 FieldStorage vtk_cell_fields;
78
2/4
✓ Branch 1 taken 2396 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2396 times.
✗ Branch 5 not taken.
29588 std::ranges::for_each(this->_point_field_names(), [&] (const std::string& name) {
79
6/12
✓ Branch 1 taken 2929 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2929 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2929 times.
✗ Branch 8 not taken.
✓ Branch 15 taken 9721 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 9721 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 9721 times.
✗ Branch 22 not taken.
12650 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
80
4/8
✓ Branch 2 taken 2929 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2929 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 9721 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 9721 times.
✗ Branch 17 not taken.
12650 this->_set_data_array(context, "Piece/PointData", name, vtk_cell_fields.get(name));
81 });
82
2/4
✓ Branch 1 taken 2396 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2396 times.
✗ Branch 5 not taken.
29588 std::ranges::for_each(this->_cell_field_names(), [&] (const std::string& name) {
83
6/12
✓ Branch 1 taken 2929 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2929 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2929 times.
✗ Branch 8 not taken.
✓ Branch 15 taken 9721 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 9721 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 9721 times.
✗ Branch 22 not taken.
12650 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
84
4/8
✓ Branch 2 taken 2929 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2929 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 9721 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 9721 times.
✗ Branch 17 not taken.
12650 this->_set_data_array(context, "Piece/CellData", name, vtk_cell_fields.get(name));
85 });
86
87 2444 const FieldPtr coords_field = std::visit([&] <typename T> (const Precision<T>&) {
88 2396 return VTK::make_coordinates_field<T>(this->grid(), true);
89
1/2
✓ Branch 1 taken 2396 times.
✗ Branch 2 not taken.
4744 }, this->_xml_settings.coordinate_precision);
90
3/4
✓ Branch 2 taken 2396 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2120 times.
✓ Branch 7 taken 276 times.
10040 this->_set_data_array(context, "Piece/Points", "Coordinates", *coords_field);
91
92
1/2
✓ Branch 2 taken 2120 times.
✗ Branch 3 not taken.
4192 this->_write_xml(std::move(context), s);
93 6400 }
94
95 4744 void _set_attributes(typename ParentType::WriteContext& context) const {
96 4744 _set_domain_attributes(context);
97 4744 _set_extent_attributes(context);
98 4744 }
99
100 4744 void _set_domain_attributes(typename ParentType::WriteContext& context) const {
101 using VTK::CommonDetail::extents_string;
102
2/2
✓ Branch 1 taken 2072 times.
✓ Branch 2 taken 324 times.
4744 if (_domain)
103
3/6
✓ Branch 2 taken 2072 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2072 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2072 times.
✗ Branch 10 not taken.
12336 this->_set_attribute(context, "", "WholeExtent", extents_string(_domain->whole_extent));
104 else
105
3/6
✓ Branch 2 taken 324 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 324 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 324 times.
✗ Branch 10 not taken.
1896 this->_set_attribute(context, "", "WholeExtent", extents_string(this->grid()));
106 4744 }
107
108 4744 void _set_extent_attributes(typename ParentType::WriteContext& context) const {
109 using VTK::CommonDetail::extents_string;
110
2/2
✓ Branch 1 taken 2072 times.
✓ Branch 2 taken 324 times.
4744 if (int i = 0; _offset) {
111 4112 auto begin = (*_offset);
112
1/2
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
4112 auto end = GridFormat::extents(this->grid());
113
1/2
✓ Branch 1 taken 2072 times.
✗ Branch 2 not taken.
9888 std::ranges::for_each(end, [&] (std::integral auto& ex) { ex += begin[i++]; });
114
3/6
✓ Branch 1 taken 2072 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2072 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 2072 times.
✗ Branch 9 not taken.
12336 this->_set_attribute(context, "Piece", "Extent", extents_string(begin, end));
115 } else {
116
3/6
✓ Branch 2 taken 324 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 324 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 324 times.
✗ Branch 10 not taken.
1896 this->_set_attribute(context, "Piece", "Extent", extents_string(this->grid()));
117 }
118 4744 }
119
120 std::optional<Domain> _domain;
121 std::optional<Offset> _offset;
122 };
123
124 template<typename G>
125 VTSWriter(G&&, VTK::XMLOptions = {}) -> VTSWriter<std::remove_cvref_t<G>>;
126
127 } // namespace GridFormat
128
129 #endif // GRIDFORMAT_VTK_VTS_WRITER_HPP_
130