| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup VTK | ||
| 6 | * \brief Helper functions to get the VTK-specific names of things. | ||
| 7 | */ | ||
| 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> | ||
| 16 | #include <gridformat/vtk/common.hpp> | ||
| 17 | |||
| 18 | // forward declarations | ||
| 19 | namespace GridFormat { class DynamicPrecision; } | ||
| 20 | namespace GridFormat::Compression { class LZMA; class ZLIB; class LZ4; } | ||
| 21 | namespace GridFormat::Encoding { struct Ascii; struct Base64; struct RawBinary; } | ||
| 22 | // end forward declarations | ||
| 23 | |||
| 24 | namespace GridFormat::VTK { | ||
| 25 | |||
| 26 | //! \addtogroup VTK | ||
| 27 | //! \{ | ||
| 28 | |||
| 29 | 140483 | std::string attribute_name(const DynamicPrecision& prec) { | |
| 30 |
7/10✓ Branch 1 taken 140483 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19005 times.
✓ Branch 4 taken 121478 times.
✓ Branch 6 taken 19005 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7238 times.
✓ Branch 9 taken 11767 times.
✓ Branch 11 taken 140483 times.
✗ Branch 12 not taken.
|
140483 | std::string prefix = prec.is_integral() ? (prec.is_signed() ? "Int" : "UInt") : "Float"; |
| 31 |
3/6✓ Branch 1 taken 140483 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 140483 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 140483 times.
✗ Branch 8 not taken.
|
280966 | return prefix + std::to_string(prec.size_in_bytes()*8); |
| 32 | 140483 | } | |
| 33 | |||
| 34 | 8244 | std::string attribute_name(std::endian e) { | |
| 35 |
2/4✓ Branch 0 taken 8244 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8244 times.
✗ Branch 4 not taken.
|
16488 | return e == std::endian::little ? "LittleEndian" : "BigEndian"; |
| 36 | } | ||
| 37 | |||
| 38 |
1/2✓ Branch 1 taken 1329 times.
✗ Branch 2 not taken.
|
3987 | std::string attribute_name(const Encoding::Ascii&) { return "ascii"; } |
| 39 |
1/2✓ Branch 1 taken 6446 times.
✗ Branch 2 not taken.
|
19338 | std::string attribute_name(const Encoding::Base64&) { return "base64"; } |
| 40 |
1/2✓ Branch 1 taken 3844 times.
✗ Branch 2 not taken.
|
11532 | std::string attribute_name(const Encoding::RawBinary&) { return "raw"; } |
| 41 | |||
| 42 |
1/2✓ Branch 1 taken 1772 times.
✗ Branch 2 not taken.
|
5316 | std::string attribute_name(const Compression::LZMA&) { return "vtkLZMADataCompressor"; }; |
| 43 |
1/2✓ Branch 1 taken 2215 times.
✗ Branch 2 not taken.
|
6645 | std::string attribute_name(const Compression::ZLIB&) { return "vtkZLibDataCompressor"; }; |
| 44 |
1/2✓ Branch 1 taken 4674 times.
✗ Branch 2 not taken.
|
14022 | std::string attribute_name(const Compression::LZ4&) { return "vtkLZ4DataCompressor"; }; |
| 45 | |||
| 46 |
1/2✓ Branch 1 taken 35757 times.
✗ Branch 2 not taken.
|
107271 | std::string data_format_name(const Encoding::RawBinary&, const DataFormat::Appended&) { return "appended"; } |
| 47 |
1/2✓ Branch 1 taken 81730 times.
✗ Branch 2 not taken.
|
245190 | std::string data_format_name(const Encoding::Base64&, const DataFormat::Appended&) { return "appended"; } |
| 48 |
1/2✓ Branch 1 taken 8935 times.
✗ Branch 2 not taken.
|
26805 | std::string data_format_name(const Encoding::Base64&, const DataFormat::Inlined&) { return "binary"; } |
| 49 |
1/2✓ Branch 1 taken 18200 times.
✗ Branch 2 not taken.
|
54600 | std::string data_format_name(const Encoding::Ascii&, const DataFormat::Inlined&) { return "ascii"; } |
| 50 | |||
| 51 | template<typename Enc, typename Format> | ||
| 52 | 1188 | std::string data_format_name(const Enc& e, const Format& format) { | |
| 53 |
1/2✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
|
1188 | const std::string encoder_name = attribute_name(e); |
| 54 |
1/2✓ Branch 2 taken 594 times.
✗ Branch 3 not taken.
|
1188 | const std::string format_name = Overload{ |
| 55 | 297 | [] (const DataFormat::Appended&) { return "appended"; }, | |
| 56 | 297 | [] (const DataFormat::Inlined&) { return "inlined"; } | |
| 57 | } (format); | ||
| 58 |
1/2✓ Branch 2 taken 594 times.
✗ Branch 3 not taken.
|
1188 | const std::string other_format_name = Overload{ |
| 59 | 297 | [] (const DataFormat::Appended&) { return "GridFormat::VTK::inlined"; }, | |
| 60 | 297 | [] (const DataFormat::Inlined&) { return "GridFormat::VTK::appended"; } | |
| 61 | } (format); | ||
| 62 |
1/2✓ Branch 3 taken 594 times.
✗ Branch 4 not taken.
|
2376 | throw ValueError( |
| 63 |
3/6✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 594 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 594 times.
✗ Branch 8 not taken.
|
2376 | "VTK's '" + format_name + "' data format cannot be used with " + encoder_name |
| 64 |
3/6✓ Branch 1 taken 594 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 594 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 594 times.
✗ Branch 8 not taken.
|
3564 | + " encoding. Please choose '" + other_format_name + "' or a different encoder." |
| 65 | ); | ||
| 66 | 3564 | } | |
| 67 | |||
| 68 | 10206 | std::endian from_endian_attribute(const std::string& endian) { | |
| 69 |
2/2✓ Branch 1 taken 9868 times.
✓ Branch 2 taken 338 times.
|
10206 | if (endian == "LittleEndian") |
| 70 | 9868 | return std::endian::little; | |
| 71 |
1/2✓ Branch 1 taken 338 times.
✗ Branch 2 not taken.
|
338 | if (endian == "BigEndian") |
| 72 | 338 | return std::endian::big; | |
| 73 | ✗ | throw ValueError("Unsupported endian attribute: '" + endian + "'"); | |
| 74 | } | ||
| 75 | |||
| 76 | 21724 | DynamicPrecision from_precision_attribute(const std::string& prec) { | |
| 77 |
2/2✓ Branch 1 taken 797 times.
✓ Branch 2 taken 20927 times.
|
21724 | if (prec.starts_with("Int")) { |
| 78 |
1/2✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
|
797 | const auto bytes = prec.substr(3); |
| 79 |
2/4✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 797 times.
|
797 | if (bytes == "8") return int8; |
| 80 |
2/4✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 797 times.
|
797 | if (bytes == "16") return int16; |
| 81 |
3/4✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
✓ Branch 4 taken 702 times.
|
797 | if (bytes == "32") return int32; |
| 82 |
2/4✓ Branch 1 taken 702 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 702 times.
✗ Branch 4 not taken.
|
702 | if (bytes == "64") return int64; |
| 83 |
3/4✗ Branch 1 not taken.
✓ Branch 2 taken 797 times.
✓ Branch 4 taken 11701 times.
✓ Branch 5 taken 9226 times.
|
21724 | } else if (prec.starts_with("UInt")) { |
| 84 |
1/2✓ Branch 1 taken 11701 times.
✗ Branch 2 not taken.
|
11701 | const auto bytes = prec.substr(4); |
| 85 |
3/4✓ Branch 1 taken 11701 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 283 times.
✓ Branch 4 taken 11418 times.
|
11701 | if (bytes == "8") return uint8; |
| 86 |
2/4✓ Branch 1 taken 11418 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 11418 times.
|
11418 | if (bytes == "16") return uint16; |
| 87 |
3/4✓ Branch 1 taken 11418 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1352 times.
✓ Branch 4 taken 10066 times.
|
11418 | if (bytes == "32") return uint32; |
| 88 |
2/4✓ Branch 1 taken 10066 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10066 times.
✗ Branch 4 not taken.
|
10066 | if (bytes == "64") return uint64; |
| 89 |
3/4✗ Branch 1 not taken.
✓ Branch 2 taken 11701 times.
✓ Branch 4 taken 8664 times.
✓ Branch 5 taken 562 times.
|
20927 | } else if (prec.starts_with("Float")) { |
| 90 |
1/2✓ Branch 1 taken 8664 times.
✗ Branch 2 not taken.
|
8664 | const auto bytes = prec.substr(5); |
| 91 |
3/4✓ Branch 1 taken 8664 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4027 times.
✓ Branch 4 taken 4637 times.
|
8664 | if (bytes == "32") return float32; |
| 92 |
2/4✓ Branch 1 taken 4637 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4637 times.
✗ Branch 4 not taken.
|
4637 | if (bytes == "64") return float64; |
| 93 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 8664 times.
✓ Branch 4 taken 562 times.
✗ Branch 5 not taken.
|
9226 | } else if (prec == "String") { |
| 94 | 562 | return {Precision<char>{}}; | |
| 95 | } | ||
| 96 | ✗ | throw ValueError("Cannot parse precision from '" + prec + "'"); | |
| 97 | } | ||
| 98 | |||
| 99 | //! \} group VTK | ||
| 100 | |||
| 101 | } // namespace GridFormat::VTK | ||
| 102 | |||
| 103 | #endif // GRIDFORMAT_VTK_ATTRIBUTES_HPP_ | ||
| 104 |