8#ifndef GRIDFORMAT_VTK_VTI_READER_HPP_
9#define GRIDFORMAT_VTK_VTI_READER_HPP_
19#include <gridformat/common/exceptions.hpp>
20#include <gridformat/common/precision.hpp>
21#include <gridformat/common/ranges.hpp>
23#include <gridformat/common/lazy_field.hpp>
38 std::array<std::size_t, 6> extents;
39 std::array<double, 3> spacing;
40 std::array<double, 3> origin;
41 std::array<double, 9> direction;
45 auto helper = VTK::XMLReaderHelper::make_from(
filename,
"ImageData");
46 auto specs = ImageSpecs{};
47 specs.extents = Ranges::array_from_string<std::size_t, 6>(helper.get(
"ImageData/Piece").get_attribute(
"Extent"));
48 specs.spacing = Ranges::array_from_string<double, 3>(helper.get(
"ImageData").get_attribute(
"Spacing"));
49 specs.origin = Ranges::array_from_string<double, 3>(helper.get(
"ImageData").get_attribute(
"Origin"));
50 specs.direction = Ranges::array_from_string<double, 9>(helper.get(
"ImageData").get_attribute_or(
51 std::string{
"1 0 0 0 1 0 0 0 1"},
"Direction"
54 VTK::XMLDetail::copy_field_names_from(helper.get(
"ImageData"), fields);
55 _helper.emplace(std::move(helper));
56 _image_specs.emplace(std::move(specs));
59 void _close()
override {
64 std::string _name()
const override {
68 std::size_t _number_of_cells()
const override {
69 return VTK::CommonDetail::number_of_entities(_specs().
extents);
72 std::size_t _number_of_points()
const override {
73 return VTK::CommonDetail::number_of_entities(_point_extents());
76 std::size_t _number_of_pieces()
const override {
80 typename GridReader::Vector _origin()
const override {
81 return _specs().origin;
84 typename GridReader::Vector _spacing()
const override {
85 return _specs().spacing;
88 typename GridReader::Vector _basis_vector(
unsigned int i)
const override {
89 const auto specs = _specs();
91 specs.direction.at(i),
92 specs.direction.at(i+3),
93 specs.direction.at(i+6)
98 const auto& specs = _specs();
100 result.lower_left = {specs.extents[0], specs.extents[2], specs.extents[4]};
101 result.upper_right = {specs.extents[1], specs.extents[3], specs.extents[5]};
105 std::vector<double> _ordinates(
unsigned int direction)
const override {
106 const auto& specs = _specs();
107 const auto extents = _point_extents();
108 const auto extent_begin =
extents[direction*2];
109 const std::size_t num_ordinates =
extents[2*direction + 1] - extent_begin;
110 std::vector<double>
ordinates(num_ordinates);
112 std::views::iota(std::size_t{0}, num_ordinates) | std::views::transform([&] (std::size_t i) {
113 return specs.origin[direction] + (extent_begin + i)*specs.spacing[direction];
120 bool _is_sequence()
const override {
125 const auto pextents = _point_extents();
126 const auto num_points = VTK::CommonDetail::number_of_entities(pextents);
129 MDLayout{{num_points, std::size_t{3}}},
131 [specs=_specs(), pextents=pextents] (
const int&) {
132 return VTK::CommonDetail::serialize_structured_points(
142 void _visit_cells(
const typename GridReader::CellVisitor& visitor)
const override {
143 VTK::CommonDetail::visit_structured_cells(visitor, _specs().
extents);
146 FieldPtr _cell_field(std::string_view
name)
const override {
147 return _helper.value().make_data_array_field(
name,
"ImageData/Piece/CellData", _number_of_cells());
150 FieldPtr _point_field(std::string_view
name)
const override {
151 return _helper.value().make_data_array_field(
name,
"ImageData/Piece/PointData", _number_of_points());
154 FieldPtr _meta_data_field(std::string_view
name)
const override {
155 return _helper.value().make_data_array_field(
name,
"ImageData/FieldData");
158 std::array<std::size_t, 6> _point_extents()
const {
159 auto result = _specs().extents;
166 const ImageSpecs& _specs()
const {
167 if (!_image_specs.has_value())
168 throw ValueError(
"No data has been read");
169 return _image_specs.value();
172 std::optional<VTK::XMLReaderHelper> _helper;
173 std::optional<ImageSpecs> _image_specs;
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
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.