8#ifndef GRIDFORMAT_VTK_ATTRIBUTES_HPP_
9#define GRIDFORMAT_VTK_ATTRIBUTES_HPP_
13#include <gridformat/common/precision.hpp>
14#include <gridformat/common/exceptions.hpp>
15#include <gridformat/common/callable_overload_set.hpp>
19namespace GridFormat {
class DynamicPrecision; }
20namespace GridFormat::Compression {
class LZMA;
class ZLIB;
class LZ4; }
21namespace GridFormat::Encoding {
struct Ascii;
struct Base64;
struct RawBinary; }
24namespace GridFormat::VTK {
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);
34std::string attribute_name(std::endian e) {
35 return e == std::endian::little ?
"LittleEndian" :
"BigEndian";
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"; }
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"; };
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"; }
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"; }
58 const std::string other_format_name = Overload{
59 [] (
const DataFormat::Appended&) {
return "GridFormat::VTK::inlined"; },
60 [] (
const DataFormat::Inlined&) {
return "GridFormat::VTK::appended"; }
63 "VTK's '" + format_name +
"' data format cannot be used with " + encoder_name
64 +
" encoding. Please choose '" + other_format_name +
"' or a different encoder."
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 +
"'");
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>{}};
96 throw ValueError(
"Cannot parse precision from '" + prec +
"'");
Common functionality for VTK writers.