GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/vti_writer.hpp
Date: 2024-11-20 14:41:59
Exec Total Coverage
Lines: 49 49 100.0%
Functions: 79 85 92.9%
Branches: 93 176 52.8%

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::VTIWriter
7 */
8 #ifndef GRIDFORMAT_VTK_VTI_WRITER_HPP_
9 #define GRIDFORMAT_VTK_VTI_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_storage.hpp>
19 #include <gridformat/common/lvalue_reference.hpp>
20
21 #include <gridformat/grid/grid.hpp>
22 #include <gridformat/grid/type_traits.hpp>
23 #include <gridformat/vtk/common.hpp>
24 #include <gridformat/vtk/xml.hpp>
25
26 namespace GridFormat {
27
28 /*!
29 * \ingroup VTK
30 * \brief Writer for .vti file format
31 */
32 template<Concepts::ImageGrid Grid>
33 class VTIWriter : public VTK::XMLWriterBase<Grid, VTIWriter<Grid>> {
34 using ParentType = VTK::XMLWriterBase<Grid, VTIWriter<Grid>>;
35 using CType = CoordinateType<Grid>;
36 static constexpr std::size_t dim = dimension<Grid>;
37
38 public:
39 struct Domain {
40 std::array<CType, dim> origin;
41 std::array<std::size_t, dim> whole_extent;
42 };
43
44 using Offset = std::array<std::size_t, dim>;
45
46 14586 explicit VTIWriter(LValueReferenceOf<const Grid> grid,
47 VTK::XMLOptions xml_opts = {})
48
2/4
✓ Branch 2 taken 7392 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 7392 times.
✗ Branch 7 not taken.
43758 : ParentType(grid.get(), ".vti", true, std::move(xml_opts))
49 14586 {}
50
51 4174 VTIWriter as_piece_for(Domain domain) const {
52 4174 auto result = this->with(this->_xml_opts);
53 4174 result._domain = std::move(domain);
54 4174 result._offset = _offset;
55 4174 return result;
56 }
57
58 4174 VTIWriter with_offset(Offset offset) const {
59 4174 auto result = this->with(this->_xml_opts);
60 4174 result._offset = std::move(offset);
61 4174 result._domain = _domain;
62 4174 return result;
63 }
64
65 private:
66 9748 VTIWriter _with(VTK::XMLOptions xml_opts) const override {
67
1/2
✓ Branch 4 taken 4936 times.
✗ Branch 5 not taken.
9748 return VTIWriter{this->grid(), std::move(xml_opts)};
68 }
69
70 4895 void _write(std::ostream& s) const override {
71
2/4
✓ Branch 1 taken 2493 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2493 times.
✗ Branch 5 not taken.
9790 auto context = this->_get_write_context("ImageData");
72
1/2
✓ Branch 1 taken 2493 times.
✗ Branch 2 not taken.
4895 _set_attributes(context);
73
74 4895 FieldStorage vtk_point_fields;
75 4895 FieldStorage vtk_cell_fields;
76
2/4
✓ Branch 1 taken 2493 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2493 times.
✗ Branch 5 not taken.
30139 std::ranges::for_each(this->_point_field_names(), [&] (const std::string& name) {
77
12/24
✓ Branch 1 taken 3036 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3036 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3036 times.
✗ Branch 8 not taken.
✓ Branch 15 taken 9730 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 9730 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 9730 times.
✗ Branch 22 not taken.
✓ Branch 29 taken 91 times.
✗ Branch 30 not taken.
✓ Branch 32 taken 91 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 91 times.
✗ Branch 36 not taken.
✓ Branch 43 taken 91 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 91 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 91 times.
✗ Branch 50 not taken.
12948 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_point_field_ptr(name)));
78
8/16
✓ Branch 2 taken 3036 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 3036 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 9730 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 9730 times.
✗ Branch 17 not taken.
✓ Branch 22 taken 91 times.
✗ Branch 23 not taken.
✓ Branch 26 taken 91 times.
✗ Branch 27 not taken.
✓ Branch 32 taken 91 times.
✗ Branch 33 not taken.
✓ Branch 36 taken 91 times.
✗ Branch 37 not taken.
12948 this->_set_data_array(context, "Piece/PointData", name, vtk_cell_fields.get(name));
79 });
80
2/4
✓ Branch 1 taken 2493 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2493 times.
✗ Branch 5 not taken.
30139 std::ranges::for_each(this->_cell_field_names(), [&] (const std::string& name) {
81
12/24
✓ Branch 1 taken 3027 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3027 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3027 times.
✗ Branch 8 not taken.
✓ Branch 15 taken 9730 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 9730 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 9730 times.
✗ Branch 22 not taken.
✓ Branch 29 taken 91 times.
✗ Branch 30 not taken.
✓ Branch 32 taken 91 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 91 times.
✗ Branch 36 not taken.
✓ Branch 43 taken 91 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 91 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 91 times.
✗ Branch 50 not taken.
12939 vtk_cell_fields.set(name, VTK::make_vtk_field(this->_get_cell_field_ptr(name)));
82
8/16
✓ Branch 2 taken 3027 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 3027 times.
✗ Branch 7 not taken.
✓ Branch 12 taken 9730 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 9730 times.
✗ Branch 17 not taken.
✓ Branch 22 taken 91 times.
✗ Branch 23 not taken.
✓ Branch 26 taken 91 times.
✗ Branch 27 not taken.
✓ Branch 32 taken 91 times.
✗ Branch 33 not taken.
✓ Branch 36 taken 91 times.
✗ Branch 37 not taken.
12939 this->_set_data_array(context, "Piece/CellData", name, vtk_cell_fields.get(name));
83 });
84
85
1/2
✓ Branch 2 taken 2493 times.
✗ Branch 3 not taken.
4895 this->_write_xml(std::move(context), s);
86 4895 }
87
88 4895 void _set_attributes(typename ParentType::WriteContext& context) const {
89 4895 _set_domain_attributes(context);
90 4895 _set_extent_attributes(context);
91
7/12
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 2472 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 21 times.
✓ Branch 6 taken 2472 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 21 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2472 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 21 times.
✗ Branch 13 not taken.
14685 this->_set_attribute(context, "", "Spacing", VTK::CommonDetail::number_string_3d(spacing(this->grid())));
92
7/12
✓ Branch 2 taken 2459 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2459 times.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2459 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 34 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2459 times.
✗ Branch 13 not taken.
14685 this->_set_attribute(context, "", "Direction", VTK::CommonDetail::direction_string(basis(this->grid())));
93 4895 }
94
95 4895 void _set_domain_attributes(typename ParentType::WriteContext& context) const {
96 using VTK::CommonDetail::extents_string;
97 using VTK::CommonDetail::number_string_3d;
98
2/2
✓ Branch 1 taken 2118 times.
✓ Branch 2 taken 375 times.
4895 if (_domain) {
99
3/6
✓ Branch 2 taken 2118 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2118 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2118 times.
✗ Branch 10 not taken.
12522 this->_set_attribute(context, "", "WholeExtent", extents_string(_domain->whole_extent));
100
3/6
✓ Branch 2 taken 2118 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2118 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2118 times.
✗ Branch 10 not taken.
12522 this->_set_attribute(context, "", "Origin", number_string_3d(_domain->origin));
101 } else {
102
3/6
✓ Branch 2 taken 375 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 375 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 375 times.
✗ Branch 10 not taken.
2163 this->_set_attribute(context, "", "WholeExtent", extents_string(this->grid()));
103
7/12
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 370 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 370 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 370 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
✗ Branch 13 not taken.
2163 this->_set_attribute(context, "", "Origin", number_string_3d(origin(this->grid())));
104 }
105 4895 }
106
107 4895 void _set_extent_attributes(typename ParentType::WriteContext& context) const {
108 using VTK::CommonDetail::extents_string;
109
2/2
✓ Branch 1 taken 2118 times.
✓ Branch 2 taken 375 times.
4895 if (int i = 0; _offset) {
110 4174 auto begin = (*_offset);
111
1/2
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
4174 auto end = GridFormat::extents(this->grid());
112
1/2
✓ Branch 1 taken 2118 times.
✗ Branch 2 not taken.
10050 std::ranges::for_each(end, [&] (std::integral auto& ex) { ex += begin[i++]; });
113
3/6
✓ Branch 1 taken 2118 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2118 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 2118 times.
✗ Branch 9 not taken.
12522 this->_set_attribute(context, "Piece", "Extent", extents_string(begin, end));
114 } else {
115
3/6
✓ Branch 2 taken 375 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 375 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 375 times.
✗ Branch 10 not taken.
2163 this->_set_attribute(context, "Piece", "Extent", extents_string(this->grid()));
116 }
117 4895 }
118
119 std::optional<Domain> _domain;
120 std::optional<Offset> _offset;
121 };
122
123 template<typename G>
124 VTIWriter(G&&, VTK::XMLOptions opts = {}) -> VTIWriter<std::remove_cvref_t<G>>;
125
126 namespace Traits {
127
128 template<typename... Args>
129 struct WritesConnectivity<VTIWriter<Args...>> : public std::false_type {};
130
131 } // namespace Traits
132 } // namespace GridFormat
133
134 #endif // GRIDFORMAT_VTK_VTI_WRITER_HPP_
135