GridFormat 0.4.0
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
vti_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_VTI_READER_HPP_
9#define GRIDFORMAT_VTK_VTI_READER_HPP_
10
11#include <string>
12#include <optional>
13#include <algorithm>
14#include <utility>
15#include <ranges>
16#include <array>
17#include <cmath>
18
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>
24
28
29namespace GridFormat {
30
35class VTIReader : public GridReader {
36 private:
37 struct ImageSpecs {
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;
42 };
43
44 void _open(const std::string& filename, typename GridReader::FieldNames& fields) override {
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"
52 ));
53
54 VTK::XMLDetail::copy_field_names_from(helper.get("ImageData"), fields);
55 _helper.emplace(std::move(helper));
56 _image_specs.emplace(std::move(specs));
57 }
58
59 void _close() override {
60 _helper.reset();
61 _image_specs.reset();
62 }
63
64 std::string _name() const override {
65 return "VTIReader";
66 }
67
68 std::size_t _number_of_cells() const override {
69 return VTK::CommonDetail::number_of_entities(_specs().extents);
70 }
71
72 std::size_t _number_of_points() const override {
73 return VTK::CommonDetail::number_of_entities(_point_extents());
74 }
75
76 std::size_t _number_of_pieces() const override {
77 return 1;
78 }
79
80 typename GridReader::Vector _origin() const override {
81 return _specs().origin;
82 }
83
84 typename GridReader::Vector _spacing() const override {
85 return _specs().spacing;
86 }
87
88 typename GridReader::Vector _basis_vector(unsigned int i) const override {
89 const auto specs = _specs();
90 return {
91 specs.direction.at(i),
92 specs.direction.at(i+3),
93 specs.direction.at(i+6)
94 };
95 }
96
97 typename GridReader::PieceLocation _location() const override {
98 const auto& specs = _specs();
99 typename GridReader::PieceLocation result;
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]};
102 return result;
103 }
104
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);
111 std::ranges::copy(
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];
114 }),
115 ordinates.begin()
116 );
117 return ordinates;
118 }
119
120 bool _is_sequence() const override {
121 return false;
122 }
123
124 FieldPtr _points() const override {
125 const auto pextents = _point_extents();
126 const auto num_points = VTK::CommonDetail::number_of_entities(pextents);
127 return make_field_ptr(LazyField{
128 int{}, // dummy "source"
129 MDLayout{{num_points, std::size_t{3}}},
130 Precision<double>{},
131 [specs=_specs(), pextents=pextents] (const int&) {
132 return VTK::CommonDetail::serialize_structured_points(
133 pextents,
134 specs.origin,
135 specs.spacing,
136 specs.direction
137 );
138 }
139 });
140 }
141
142 void _visit_cells(const typename GridReader::CellVisitor& visitor) const override {
143 VTK::CommonDetail::visit_structured_cells(visitor, _specs().extents);
144 }
145
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());
148 }
149
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());
152 }
153
154 FieldPtr _meta_data_field(std::string_view name) const override {
155 return _helper.value().make_data_array_field(name, "ImageData/FieldData");
156 }
157
158 std::array<std::size_t, 6> _point_extents() const {
159 auto result = _specs().extents;
160 result[1] += 1;
161 result[3] += 1;
162 result[5] += 1;
163 return result;
164 }
165
166 const ImageSpecs& _specs() const {
167 if (!_image_specs.has_value())
168 throw ValueError("No data has been read");
169 return _image_specs.value();
170 }
171
172 std::optional<VTK::XMLReaderHelper> _helper;
173 std::optional<ImageSpecs> _image_specs;
174};
175
176} // namespace GridFormat
177
178#endif // GRIDFORMAT_VTK_VTI_READER_HPP_
Abstract base class for all readers, defines the common interface.
Definition: reader.hpp:51
std::array< std::size_t, 3 > extents() const
Return the extents of the grid (only available for structured grid formats)
Definition: reader.hpp:113
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
std::vector< double > ordinates(unsigned int direction) const
Return the ordinates of the grid (only available for rectilinear grid formats)
Definition: reader.hpp:128
Reader for .vti file format.
Definition: vti_reader.hpp:35
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
Definition: reader.hpp:266
Describes the location of a piece within a distributed structured grid.
Definition: reader.hpp:57
Common functionality for VTK writers.
Helper classes and functions for VTK XML-type file format writers & readers.