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/iota.hpp>
10#include <range/v3/view/unique.hpp>
11#include <set>
12
13#include "BaseLib/Logging.h"
14#include "BaseLib/StringTools.h"
16
17namespace MaterialLib
18{
19
22void checkForWhitespaces(std::string_view const part)
23{
24 if (std::ranges::any_of(
25 part,
26 [](char c) { return std::isspace(static_cast<unsigned char>(c)); }))
27 {
29 "Whitespace is not allowed in ranges. Use 'start:end' without "
30 "spaces around the colon.");
31 }
32}
33
36auto expandRange(int const start, int const end)
37{
38 if (end < start)
39 {
41 "Invalid range '{}:{}'. The end must be greater than or equal "
42 "to the start.",
43 start, end);
44 }
45 return ranges::views::iota(start, end + 1);
46}
47
48std::vector<int> splitMaterialIdString(std::string const& material_id_string,
49 char const separator)
50{
51 if (separator == ':')
52 {
54 "Invalid separator ':'. Character ':' is reserved for range "
55 "specification.");
56 }
57 auto const material_ids_strings =
58 BaseLib::splitString(material_id_string, separator);
59
60 // Pre-allocate with estimated capacity (simplified heuristic)
61 std::vector<int> material_ids;
62 material_ids.reserve(material_ids_strings.size());
63
64 for (std::string mid_str : material_ids_strings)
65 {
66 // Trim leading and trailing whitespace
67 BaseLib::trim(mid_str);
68
69 auto const parts =
70 BaseLib::splitString(mid_str, ':') | ranges::to_vector;
71 if (parts.size() == 2)
72 {
73 checkForWhitespaces(parts[0]);
74 auto const start_id = BaseLib::parseInteger(parts[0]);
75 if (!start_id)
76 {
77 OGS_FATAL("Could not parse material ID: {}", start_id.error());
78 }
79 checkForWhitespaces(parts[1]);
80 auto const end_id = BaseLib::parseInteger(parts[1]);
81 if (!end_id)
82 {
83 OGS_FATAL("Could not parse material ID: {}", end_id.error());
84 }
85 ranges::copy(expandRange(*start_id, *end_id),
86 std::back_inserter(material_ids));
87 }
88 else if (parts.size() == 1)
89 {
90 auto const material_id = BaseLib::parseInteger(mid_str);
91 if (!material_id)
92 {
93 OGS_FATAL("Could not parse material ID: {}",
94 material_id.error());
95 }
96 material_ids.push_back(*material_id);
97 }
98 else
99 {
100 OGS_FATAL(
101 "Could not parse material ID from '{}'. Invalid range format. "
102 "Use 'start:end' for ranges or a single integer.",
103 mid_str);
104 }
105 }
106
107 return material_ids;
108}
109
110std::vector<int> parseMaterialIdString(
111 std::string const& material_id_string,
112 MeshLib::PropertyVector<int> const* const material_ids,
113 char const separator, bool const validate)
114{
115 if (material_id_string == "*")
116 {
117 if (material_ids == nullptr)
118 {
119 OGS_FATAL(
120 "MaterialIDs property is not defined in the mesh but it is "
121 "required to parse '*' definition.");
122 }
123
124 std::vector<int> const material_ids_of_this_medium =
125 *material_ids | ranges::views::unique | ranges::to_vector |
126 ranges::actions::sort | ranges::actions::unique |
127 ranges::to<std::vector>;
128 DBUG("Catch all medium definition for material ids {}.",
129 fmt::join(material_ids_of_this_medium, ", "));
130 return material_ids_of_this_medium;
131 }
132
133 // Usual case of ids or ranges separated by comma.
134 auto const parsed_ids =
135 splitMaterialIdString(material_id_string, separator);
136
137 if (material_ids == nullptr || !validate)
138 {
139 return parsed_ids;
140 }
141
142 // Create a set of valid material IDs for efficient lookup
143 std::set<int> const valid_material_ids(material_ids->begin(),
144 material_ids->end());
145
146 for (int id : parsed_ids)
147 {
148 if (valid_material_ids.find(id) == valid_material_ids.end())
149 {
150 OGS_FATAL(
151 "Material ID {} specified in material_id_string '{}' does "
152 "not exist in the mesh. Available material IDs are: {}.",
153 id, material_id_string, fmt::join(valid_material_ids, ", "));
154 }
155 }
156 return parsed_ids;
157}
158
159} // namespace MaterialLib
#define OGS_FATAL(...)
Definition Error.h:10
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
constexpr PROP_VAL_TYPE * begin()
constexpr PROP_VAL_TYPE * end()
void trim(std::string &str, char ch)
std::expected< int, std::string > parseInteger(std::string_view const str)
std::vector< std::string > splitString(std::string const &str)
std::vector< int > parseMaterialIdString(std::string const &material_id_string, MeshLib::PropertyVector< int > const *const material_ids, char const separator, bool const validate)
auto expandRange(int const start, int const end)
std::vector< int > splitMaterialIdString(std::string const &material_id_string, char const separator)
void checkForWhitespaces(std::string_view const part)