Loading [MathJax]/jax/output/HTML-CSS/config.js
OGS
MediaCreation.cpp
Go to the documentation of this file.
1
10#include <spdlog/fmt/bundled/ranges.h>
11
12#include <range/v3/action/sort.hpp>
13#include <range/v3/action/unique.hpp>
14#include <range/v3/range/conversion.hpp>
15#include <range/v3/view/unique.hpp>
16
17#include "BaseLib/Algorithm.h"
18#include "BaseLib/Logging.h"
19#include "BaseLib/StringTools.h"
21
22namespace MaterialLib
23{
24
25std::vector<int> splitMaterialIdString(std::string const& material_id_string)
26{
27 auto const material_ids_strings =
28 BaseLib::splitString(material_id_string, ',');
29
30 std::vector<int> material_ids;
31 for (auto& mid_str : material_ids_strings)
32 {
33 std::size_t num_chars_processed = 0;
34 int material_id;
35 try
36 {
37 material_id = std::stoi(mid_str, &num_chars_processed);
38 }
39 catch (std::invalid_argument&)
40 {
42 "Could not parse material ID from '{}' to a valid integer.",
43 mid_str);
44 }
45 catch (std::out_of_range&)
46 {
48 "Could not parse material ID from '{}'. The integer value of "
49 "the given string exceeds the permitted range.",
50 mid_str);
51 }
52
53 if (num_chars_processed != mid_str.size())
54 {
55 // Not the whole string has been parsed. Check the rest.
56 if (auto const it = std::find_if_not(
57 begin(mid_str) + num_chars_processed, end(mid_str),
58 [](unsigned char const c) { return std::isspace(c); });
59 it != end(mid_str))
60 {
62 "Could not parse material ID from '{}'. Please separate "
63 "multiple material IDs by comma only. Invalid character: "
64 "'{}' at position {}.",
65 mid_str, *it, distance(begin(mid_str), it));
66 }
67 }
68
69 material_ids.push_back(material_id);
70 };
71
72 return material_ids;
73}
74
75std::vector<int> parseMaterialIdString(
76 std::string const& material_id_string,
77 MeshLib::PropertyVector<int> const* const material_ids)
78{
79 if (material_id_string == "*")
80 {
81 if (material_ids == nullptr)
82 {
84 "MaterialIDs property is not defined in the mesh but it is "
85 "required to parse '*' definition.");
86 }
87
88 std::vector<int> const material_ids_of_this_medium =
89 *material_ids | ranges::views::unique | ranges::to_vector |
90 ranges::actions::sort | ranges::actions::unique |
91 ranges::to<std::vector>;
92 DBUG("Catch all medium definition for material ids {}.",
93 fmt::join(material_ids_of_this_medium, ", "));
94 return material_ids_of_this_medium;
95 }
96
97 // Usual case of ids separated by comma.
98 return splitMaterialIdString(material_id_string);
99}
100
101} // namespace MaterialLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of string helper functions.
std::vector< std::string > splitString(std::string const &str)
std::vector< int > splitMaterialIdString(std::string const &material_id_string)
std::vector< int > parseMaterialIdString(std::string const &material_id_string, MeshLib::PropertyVector< int > const *const material_ids)