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