GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
vtu_reader.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_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>
19
22
23namespace GridFormat {
24
29class VTUReader : public GridReader {
30 private:
31 void _open(const std::string& filename, typename GridReader::FieldNames& fields) override {
32 auto helper = VTK::XMLReaderHelper::make_from(filename, "UnstructuredGrid");
33
34 _num_points = from_string<std::size_t>(helper.get("UnstructuredGrid/Piece").get_attribute("NumberOfPoints"));
35 _num_cells = from_string<std::size_t>(helper.get("UnstructuredGrid/Piece").get_attribute("NumberOfCells"));
36 VTK::XMLDetail::copy_field_names_from(helper.get("UnstructuredGrid"), fields);
37 _helper.emplace(std::move(helper));
38 }
39
40 void _close() override {
41 _helper.reset();
42 _num_points = 0;
43 _num_cells = 0;
44 }
45
46 std::string _name() const override {
47 return "VTUReader";
48 }
49
50 std::size_t _number_of_cells() const override {
51 return _num_cells;
52 }
53
54 std::size_t _number_of_points() const override {
55 return _num_points;
56 }
57
58 std::size_t _number_of_pieces() const override {
59 return 1;
60 }
61
62 bool _is_sequence() const override {
63 return false;
64 }
65
66 FieldPtr _points() const override {
67 return _helper.value().make_points_field("UnstructuredGrid/Piece/Points", _number_of_points());
68 }
69
70 void _visit_cells(const typename GridReader::CellVisitor& visitor) const override {
71 const auto types = _helper.value().make_data_array_field(
72 "types", "UnstructuredGrid/Piece/Cells", _number_of_cells()
73 )->template export_to<std::vector<std::uint_least8_t>>();
74 const auto offsets = _helper.value().make_data_array_field(
75 "offsets", "UnstructuredGrid/Piece/Cells", _number_of_cells()
76 )->template export_to<std::vector<std::size_t>>();
77 const auto connectivity = _helper.value().make_data_array_field(
78 "connectivity", "UnstructuredGrid/Piece/Cells"
79 )->template export_to<std::vector<std::size_t>>();
80 std::vector<std::size_t> corners;
81 for (std::size_t i = 0; i < _number_of_cells(); ++i) {
82 corners.clear();
83 const std::size_t offset_begin = (i == 0 ? 0 : offsets.at(i-1));
84 const std::size_t offset_end = offsets.at(i);
85 if (connectivity.size() < offset_end)
86 throw SizeError("Connectivity array read from the file is too small");
87 if (offset_end < offset_begin)
88 throw ValueError("Invalid offset array");
89 std::copy_n(connectivity.begin() + offset_begin, offset_end - offset_begin, std::back_inserter(corners));
90 visitor(VTK::cell_type(types.at(i)), corners);
91 }
92 }
93
94 FieldPtr _cell_field(std::string_view name) const override {
95 return _helper.value().make_data_array_field(name, "UnstructuredGrid/Piece/CellData", _number_of_cells());
96 }
97
98 FieldPtr _point_field(std::string_view name) const override {
99 return _helper.value().make_data_array_field(name, "UnstructuredGrid/Piece/PointData", _number_of_points());
100 }
101
102 FieldPtr _meta_data_field(std::string_view name) const override {
103 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_
Abstract base class for all readers, defines the common interface.
Definition: reader.hpp:51
const std::string & filename() const
Return the name of the opened grid file (empty string until open() is called)
Definition: reader.hpp:92
std::string name() const
Return the name of this reader.
Definition: reader.hpp:73
Reader for .vtu file format.
Definition: vtu_reader.hpp:29
Base class for grid data readers.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Definition: reader.hpp:266
Helper classes and functions for VTK XML-type file format writers & readers.