GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
attributes.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_ATTRIBUTES_HPP_
9#define GRIDFORMAT_VTK_ATTRIBUTES_HPP_
10
11#include <bit>
12
13#include <gridformat/common/precision.hpp>
14#include <gridformat/common/exceptions.hpp>
15#include <gridformat/common/callable_overload_set.hpp>
17
18// forward declarations
19namespace GridFormat { class DynamicPrecision; }
20namespace GridFormat::Compression { class LZMA; class ZLIB; class LZ4; }
21namespace GridFormat::Encoding { struct Ascii; struct Base64; struct RawBinary; }
22// end forward declarations
23
24namespace GridFormat::VTK {
25
28
29std::string attribute_name(const DynamicPrecision& prec) {
30 std::string prefix = prec.is_integral() ? (prec.is_signed() ? "Int" : "UInt") : "Float";
31 return prefix + std::to_string(prec.size_in_bytes()*8);
32}
33
34std::string attribute_name(std::endian e) {
35 return e == std::endian::little ? "LittleEndian" : "BigEndian";
36}
37
38std::string attribute_name(const Encoding::Ascii&) { return "ascii"; }
39std::string attribute_name(const Encoding::Base64&) { return "base64"; }
40std::string attribute_name(const Encoding::RawBinary&) { return "raw"; }
41
42std::string attribute_name(const Compression::LZMA&) { return "vtkLZMADataCompressor"; };
43std::string attribute_name(const Compression::ZLIB&) { return "vtkZLibDataCompressor"; };
44std::string attribute_name(const Compression::LZ4&) { return "vtkLZ4DataCompressor"; };
45
46std::string data_format_name(const Encoding::RawBinary&, const DataFormat::Appended&) { return "appended"; }
47std::string data_format_name(const Encoding::Base64&, const DataFormat::Appended&) { return "appended"; }
48std::string data_format_name(const Encoding::Base64&, const DataFormat::Inlined&) { return "binary"; }
49std::string data_format_name(const Encoding::Ascii&, const DataFormat::Inlined&) { return "ascii"; }
50
51template<typename Enc, typename Format>
52std::string data_format_name(const Enc& e, const Format& format) {
53 const std::string encoder_name = attribute_name(e);
54 const std::string format_name = Overload{
55 [] (const DataFormat::Appended&) { return "appended"; },
56 [] (const DataFormat::Inlined&) { return "inlined"; }
57 } (format);
58 const std::string other_format_name = Overload{
59 [] (const DataFormat::Appended&) { return "GridFormat::VTK::inlined"; },
60 [] (const DataFormat::Inlined&) { return "GridFormat::VTK::appended"; }
61 } (format);
62 throw ValueError(
63 "VTK's '" + format_name + "' data format cannot be used with " + encoder_name
64 + " encoding. Please choose '" + other_format_name + "' or a different encoder."
65 );
66}
67
68std::endian from_endian_attribute(const std::string& endian) {
69 if (endian == "LittleEndian")
70 return std::endian::little;
71 if (endian == "BigEndian")
72 return std::endian::big;
73 throw ValueError("Unsupported endian attribute: '" + endian + "'");
74}
75
76DynamicPrecision from_precision_attribute(const std::string& prec) {
77 if (prec.starts_with("Int")) {
78 const auto bytes = prec.substr(3);
79 if (bytes == "8") return int8;
80 if (bytes == "16") return int16;
81 if (bytes == "32") return int32;
82 if (bytes == "64") return int64;
83 } else if (prec.starts_with("UInt")) {
84 const auto bytes = prec.substr(4);
85 if (bytes == "8") return uint8;
86 if (bytes == "16") return uint16;
87 if (bytes == "32") return uint32;
88 if (bytes == "64") return uint64;
89 } else if (prec.starts_with("Float")) {
90 const auto bytes = prec.substr(5);
91 if (bytes == "32") return float32;
92 if (bytes == "64") return float64;
93 } else if (prec == "String") {
94 return {Precision<char>{}};
95 }
96 throw ValueError("Cannot parse precision from '" + prec + "'");
97}
98
100
101} // namespace GridFormat::VTK
102
103#endif // GRIDFORMAT_VTK_ATTRIBUTES_HPP_
Common functionality for VTK writers.