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