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::VTUReader | ||
7 | */ | ||
8 | #ifndef GRIDFORMAT_VTK_VTU_READER_HPP_ | ||
9 | #define GRIDFORMAT_VTK_VTU_READER_HPP_ | ||
10 | |||
11 | #include <string> | ||
12 | #include <optional> | ||
13 | #include <iterator> | ||
14 | #include <algorithm> | ||
15 | #include <utility> | ||
16 | |||
17 | #include <gridformat/common/string_conversion.hpp> | ||
18 | #include <gridformat/common/field.hpp> | ||
19 | |||
20 | #include <gridformat/grid/reader.hpp> | ||
21 | #include <gridformat/vtk/xml.hpp> | ||
22 | |||
23 | namespace GridFormat { | ||
24 | |||
25 | /*! | ||
26 | * \ingroup VTK | ||
27 | * \brief Reader for .vtu file format | ||
28 | */ | ||
29 | class VTUReader : public GridReader { | ||
30 | private: | ||
31 | 309 | void _open(const std::string& filename, typename GridReader::FieldNames& fields) override { | |
32 |
2/2✓ Branch 2 taken 300 times.
✓ Branch 3 taken 9 times.
|
309 | auto helper = VTK::XMLReaderHelper::make_from(filename, "UnstructuredGrid"); |
33 | |||
34 |
3/6✓ Branch 2 taken 300 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 300 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 300 times.
✗ Branch 10 not taken.
|
300 | _num_points = from_string<std::size_t>(helper.get("UnstructuredGrid/Piece").get_attribute("NumberOfPoints")); |
35 |
3/6✓ Branch 2 taken 300 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 300 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 300 times.
✗ Branch 10 not taken.
|
300 | _num_cells = from_string<std::size_t>(helper.get("UnstructuredGrid/Piece").get_attribute("NumberOfCells")); |
36 |
2/4✓ Branch 2 taken 300 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 300 times.
✗ Branch 6 not taken.
|
300 | VTK::XMLDetail::copy_field_names_from(helper.get("UnstructuredGrid"), fields); |
37 | 300 | _helper.emplace(std::move(helper)); | |
38 | 300 | } | |
39 | |||
40 | 53 | void _close() override { | |
41 | 53 | _helper.reset(); | |
42 | 53 | _num_points = 0; | |
43 | 53 | _num_cells = 0; | |
44 | 53 | } | |
45 | |||
46 | 43 | std::string _name() const override { | |
47 |
1/2✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
|
86 | return "VTUReader"; |
48 | } | ||
49 | |||
50 | 8339 | std::size_t _number_of_cells() const override { | |
51 | 8339 | return _num_cells; | |
52 | } | ||
53 | |||
54 | 1455 | std::size_t _number_of_points() const override { | |
55 | 1455 | return _num_points; | |
56 | } | ||
57 | |||
58 | 10 | std::size_t _number_of_pieces() const override { | |
59 | 10 | return 1; | |
60 | } | ||
61 | |||
62 | 87 | bool _is_sequence() const override { | |
63 | 87 | return false; | |
64 | } | ||
65 | |||
66 | 173 | FieldPtr _points() const override { | |
67 |
1/2✓ Branch 4 taken 173 times.
✗ Branch 5 not taken.
|
173 | return _helper.value().make_points_field("UnstructuredGrid/Piece/Points", _number_of_points()); |
68 | } | ||
69 | |||
70 | 281 | void _visit_cells(const typename GridReader::CellVisitor& visitor) const override { | |
71 |
2/4✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 281 times.
✗ Branch 7 not taken.
|
843 | const auto types = _helper.value().make_data_array_field( |
72 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
281 | "types", "UnstructuredGrid/Piece/Cells", _number_of_cells() |
73 |
1/2✓ Branch 2 taken 281 times.
✗ Branch 3 not taken.
|
281 | )->template export_to<std::vector<std::uint_least8_t>>(); |
74 |
2/4✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 281 times.
✗ Branch 7 not taken.
|
843 | const auto offsets = _helper.value().make_data_array_field( |
75 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
281 | "offsets", "UnstructuredGrid/Piece/Cells", _number_of_cells() |
76 |
1/2✓ Branch 2 taken 281 times.
✗ Branch 3 not taken.
|
281 | )->template export_to<std::vector<std::size_t>>(); |
77 |
2/4✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 7 taken 281 times.
✗ Branch 8 not taken.
|
562 | const auto connectivity = _helper.value().make_data_array_field( |
78 | "connectivity", "UnstructuredGrid/Piece/Cells" | ||
79 |
1/2✓ Branch 2 taken 281 times.
✗ Branch 3 not taken.
|
281 | )->template export_to<std::vector<std::size_t>>(); |
80 | 281 | std::vector<std::size_t> corners; | |
81 |
3/4✓ Branch 1 taken 6761 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6480 times.
✓ Branch 4 taken 281 times.
|
6761 | for (std::size_t i = 0; i < _number_of_cells(); ++i) { |
82 | 6480 | corners.clear(); | |
83 |
3/4✓ Branch 0 taken 6199 times.
✓ Branch 1 taken 281 times.
✓ Branch 3 taken 6199 times.
✗ Branch 4 not taken.
|
6480 | const std::size_t offset_begin = (i == 0 ? 0 : offsets.at(i-1)); |
84 |
1/2✓ Branch 1 taken 6480 times.
✗ Branch 2 not taken.
|
6480 | const std::size_t offset_end = offsets.at(i); |
85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6480 times.
|
6480 | if (connectivity.size() < offset_end) |
86 | ✗ | throw SizeError("Connectivity array read from the file is too small"); | |
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6480 times.
|
6480 | if (offset_end < offset_begin) |
88 | ✗ | throw ValueError("Invalid offset array"); | |
89 |
2/4✓ Branch 1 taken 6480 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 6480 times.
✗ Branch 7 not taken.
|
6480 | std::copy_n(connectivity.begin() + offset_begin, offset_end - offset_begin, std::back_inserter(corners)); |
90 |
3/6✓ Branch 1 taken 6480 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6480 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6480 times.
✗ Branch 8 not taken.
|
6480 | visitor(VTK::cell_type(types.at(i)), corners); |
91 | } | ||
92 | 281 | } | |
93 | |||
94 | 691 | FieldPtr _cell_field(std::string_view name) const override { | |
95 |
2/4✓ Branch 2 taken 691 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 691 times.
✗ Branch 8 not taken.
|
691 | return _helper.value().make_data_array_field(name, "UnstructuredGrid/Piece/CellData", _number_of_cells()); |
96 | } | ||
97 | |||
98 | 706 | FieldPtr _point_field(std::string_view name) const override { | |
99 |
2/4✓ Branch 2 taken 706 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
|
706 | return _helper.value().make_data_array_field(name, "UnstructuredGrid/Piece/PointData", _number_of_points()); |
100 | } | ||
101 | |||
102 | 57 | FieldPtr _meta_data_field(std::string_view name) const override { | |
103 |
1/2✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
|
57 | return _helper.value().make_data_array_field(name, "UnstructuredGrid/FieldData"); |
104 | } | ||
105 | |||
106 | std::optional<VTK::XMLReaderHelper> _helper; | ||
107 | std::size_t _num_points; | ||
108 | std::size_t _num_cells; | ||
109 | }; | ||
110 | |||
111 | } // namespace GridFormat | ||
112 | |||
113 | #endif // GRIDFORMAT_VTK_VTU_READER_HPP_ | ||
114 |