8#ifndef GRIDFORMAT_VTK_VTU_READER_HPP_
9#define GRIDFORMAT_VTK_VTU_READER_HPP_
17#include <gridformat/common/string_conversion.hpp>
32 auto helper = VTK::XMLReaderHelper::make_from(
filename,
"UnstructuredGrid");
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));
40 void _close()
override {
46 std::string _name()
const override {
50 std::size_t _number_of_cells()
const override {
54 std::size_t _number_of_points()
const override {
58 std::size_t _number_of_pieces()
const override {
62 bool _is_sequence()
const override {
67 return _helper.value().make_points_field(
"UnstructuredGrid/Piece/Points", _number_of_points());
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) {
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);
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());
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());
102 FieldPtr _meta_data_field(std::string_view
name)
const override {
103 return _helper.value().make_data_array_field(
name,
"UnstructuredGrid/FieldData");
106 std::optional<VTK::XMLReaderHelper> _helper;
107 std::size_t _num_points;
108 std::size_t _num_cells;
Base class for grid data readers.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
Helper classes and functions for VTK XML-type file format writers & readers.