OGS
ElementValueModification.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
5
6#include <algorithm>
7#include <range/v3/algorithm/fill.hpp>
8#include <range/v3/view/filter.hpp>
9#include <range/v3/view/transform.hpp>
10
11#include "BaseLib/Logging.h"
13#include "MeshLib/Mesh.h"
15
16namespace MeshToolsLib
17{
19 std::string const& property_name,
20 int const old_value, int const new_value,
21 bool replace_if_exists)
22{
23 MeshLib::PropertyVector<int>* property_value_vec = nullptr;
24 try
25 {
26 property_value_vec = mesh.getProperties().getPropertyVector<int>(
27 property_name, MeshLib::MeshItemType::Cell, 1);
28 }
29 catch (std::runtime_error const& e)
30 {
31 ERR("{:s}", e.what());
32 return false;
33 }
34
35 const std::size_t n_property_tuples(
36 property_value_vec->getNumberOfTuples());
37
38 if (!replace_if_exists)
39 {
40 for (std::size_t i = 0; i < n_property_tuples; ++i)
41 {
42 if ((*property_value_vec)[i] == new_value)
43 {
44 WARN(
45 "ElementValueModification::replaceElementValue() - "
46 "Replacement value '{:d}' is already taken, no changes "
47 "have been made.",
48 new_value);
49 return false;
50 }
51 }
52 }
53
54 auto const old_values_filter = ranges::views::filter(
55 [&old_value](auto const& v) { return v == old_value; });
56 ranges::fill(*property_value_vec | old_values_filter, new_value);
57
58 return true;
59}
60
61bool ElementValueModification::replace(MeshLib::Mesh& mesh, int const old_value,
62 int const new_value,
63 bool replace_if_exists)
64{
65 return replace(mesh, "MaterialIDs", old_value, new_value,
66 replace_if_exists);
67}
68
70{
71 MeshLib::PropertyVector<int>* property_value_vector = nullptr;
72 try
73 {
74 property_value_vector = mesh.getProperties().getPropertyVector<int>(
75 "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
76 }
77 catch (std::runtime_error const& e)
78 {
79 ERR("{:s}", e.what());
80 return 0;
81 }
82
83 std::vector<int> value_mapping(
84 getSortedPropertyValues(*property_value_vector));
85
86 std::vector<int> reverse_mapping(value_mapping.back() + 1, 0);
87 std::size_t const nValues(value_mapping.size());
88 for (std::size_t i = 0; i < nValues; ++i)
89 {
90 reverse_mapping[value_mapping[i]] = i;
91 }
92
93 property_value_vector->assign(
94 *property_value_vector |
95 ranges::views::transform([&](auto const v)
96 { return reverse_mapping[v]; }));
97
98 return nValues;
99}
100
102 MeshLib::Mesh& mesh, MeshLib::MeshElemType ele_type, int const new_value)
103{
104 MeshLib::PropertyVector<int>* property_value_vector = nullptr;
105 try
106 {
107 property_value_vector = mesh.getProperties().getPropertyVector<int>(
108 "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
109 }
110 catch (std::runtime_error const& e)
111 {
112 ERR("{:s}", e.what());
113 return 0;
114 }
115
116 auto const element_type_filter =
117 ranges::views::filter([&](MeshLib::Element const* const e)
118 { return e->getGeomType() == ele_type; });
119
120 auto selected_element_ids =
121 mesh.getElements() | element_type_filter | MeshLib::views::ids;
122
123 ranges::fill(
124 selected_element_ids |
125 ranges::views::transform([&](std::size_t const k) -> auto&
126 { return (*property_value_vector)[k]; }),
127 new_value);
128
129 return ranges::distance(selected_element_ids);
130}
131
132} // namespace MeshToolsLib
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
Properties & getProperties()
Definition Mesh.h:125
PropertyVector< T > const * getPropertyVector(std::string_view name) const
constexpr std::size_t getNumberOfTuples() const
constexpr void assign(R &&r)
static std::vector< T > getSortedPropertyValues(MeshLib::PropertyVector< T > const &property_vector)
static std::size_t setByElementType(MeshLib::Mesh &mesh, MeshLib::MeshElemType ele_type, int const new_value)
static bool replace(MeshLib::Mesh &mesh, int const old_value, int const new_value, bool replace_if_exists=false)
static std::size_t condense(MeshLib::Mesh &mesh)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:216
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:37