8#ifndef GRIDFORMAT_VTK_VTR_READER_HPP_
9#define GRIDFORMAT_VTK_VTR_READER_HPP_
17#include <gridformat/common/exceptions.hpp>
18#include <gridformat/common/ranges.hpp>
20#include <gridformat/common/lazy_field.hpp>
35 auto helper = VTK::XMLReaderHelper::make_from(
filename,
"RectilinearGrid");
36 _extents = Ranges::array_from_string<std::size_t, 6>(helper.get(
"RectilinearGrid/Piece").get_attribute(
"Extent"));
37 VTK::XMLDetail::copy_field_names_from(helper.get(
"RectilinearGrid"), fields);
38 _helper.emplace(std::move(helper));
41 void _close()
override {
46 std::string _name()
const override {
50 std::size_t _number_of_cells()
const override {
51 return VTK::CommonDetail::number_of_entities(_extents.value());
54 std::size_t _number_of_points()
const override {
55 return VTK::CommonDetail::number_of_entities(_point_extents());
58 std::size_t _number_of_pieces()
const override {
63 const auto& ex = _extents.value();
65 result.lower_left = {ex[0], ex[2], ex[4]};
66 result.upper_right = {ex[1], ex[3], ex[5]};
70 bool _is_sequence()
const override {
74 std::vector<double> _ordinates(
unsigned int i)
const override {
75 std::vector<double> result;
76 unsigned int direction = 0;
77 const XMLElement& coords = _helper.value().get(
"RectilinearGrid/Piece/Coordinates");
79 if (direction++ == i) {
80 FieldPtr ordinates_field = _helper.value().make_data_array_field(da);
81 return ordinates_field->precision().visit([&] <
typename T> (
const Precision<T>& prec) {
82 auto bytes = ordinates_field->serialized();
83 auto values = bytes.as_span_of(prec);
84 result.resize(values.size());
85 std::ranges::copy(values, result.begin());
89 throw IOError(
"Could not read ordinates in direction " + std::to_string(i));
94 const XMLElement& coordinates = _helper.value().get(
"RectilinearGrid/Piece/Coordinates");
98 ordinates.push_back(_helper.value().make_data_array_field(da));
102 throw SizeError(
"Expected 3 data arrays in the 'Coordinates' section");
104 auto precision =
ordinates.front()->precision();
105 if (!std::ranges::all_of(
ordinates, [&] (
const auto f) {
return f->precision() == precision; }))
106 throw ValueError(
"Coordinates must use the same scalar types");
108 auto num_points = _number_of_points();
111 MDLayout{{num_points, std::size_t{3}}},
114 return prec.visit([&] <
typename T> (
const Precision<T>& p) {
115 auto x_data =
ordinates.at(0)->serialized();
116 auto y_data =
ordinates.at(1)->serialized();
117 auto z_data =
ordinates.at(2)->serialized();
118 if (x_data.size() == 0) { x_data.resize(
sizeof(T)); x_data.as_span_of(p)[0] = T{0}; }
119 if (y_data.size() == 0) { y_data.resize(
sizeof(T)); y_data.as_span_of(p)[0] = T{0}; }
120 if (z_data.size() == 0) { z_data.resize(
sizeof(T)); z_data.as_span_of(p)[0] = T{0}; }
122 Serialization result(np*
sizeof(T)*3);
123 auto result_span = result.as_span_of(p);
125 for (
auto z : z_data.as_span_of(p))
126 for (
auto y : y_data.as_span_of(p))
127 for (
auto x : x_data.as_span_of(p)) {
128 assert(i + 3 <= result_span.size());
129 result_span[i + 0] = x;
130 result_span[i + 1] = y;
131 result_span[i + 2] = z;
140 void _visit_cells(
const typename GridReader::CellVisitor& visitor)
const override {
141 VTK::CommonDetail::visit_structured_cells(visitor, _extents.value());
144 FieldPtr _cell_field(std::string_view
name)
const override {
145 return _helper.value().make_data_array_field(
name,
"RectilinearGrid/Piece/CellData", _number_of_cells());
148 FieldPtr _point_field(std::string_view
name)
const override {
149 return _helper.value().make_data_array_field(
name,
"RectilinearGrid/Piece/PointData", _number_of_points());
152 FieldPtr _meta_data_field(std::string_view
name)
const override {
153 return _helper.value().make_data_array_field(
name,
"RectilinearGrid/FieldData");
156 std::array<std::size_t, 6> _point_extents()
const {
157 auto result = _extents.value();
164 std::optional<VTK::XMLReaderHelper> _helper;
165 std::optional<std::array<std::size_t, 6>> _extents;
Base class for grid data readers.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186
FieldPtr make_field_ptr(F &&f)
Factory function for field pointers.
Definition: field.hpp:192
std::ranges::range auto data_arrays(const XMLElement &e)
Return a range over all data array elements in the given xml section.
Definition: xml.hpp:662
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.