OGS
ElementValueModification.cpp
Go to the documentation of this file.
1
16
17#include <algorithm>
18
19#include "BaseLib/Logging.h"
21#include "MeshLib/Mesh.h"
23
24namespace MeshToolsLib
25{
27 std::string const& property_name,
28 int const old_value, int const new_value,
29 bool replace_if_exists)
30{
31 MeshLib::PropertyVector<int>* property_value_vec = nullptr;
32 try
33 {
34 property_value_vec = mesh.getProperties().getPropertyVector<int>(
35 property_name, MeshLib::MeshItemType::Cell, 1);
36 }
37 catch (std::runtime_error const& e)
38 {
39 ERR("{:s}", e.what());
40 return false;
41 }
42
43 const std::size_t n_property_tuples(
44 property_value_vec->getNumberOfTuples());
45
46 if (!replace_if_exists)
47 {
48 for (std::size_t i = 0; i < n_property_tuples; ++i)
49 {
50 if ((*property_value_vec)[i] == new_value)
51 {
52 WARN(
53 "ElementValueModification::replaceElementValue() - "
54 "Replacement value '{:d}' is already taken, no changes "
55 "have been made.",
56 new_value);
57 return false;
58 }
59 }
60 }
61
62 for (std::size_t i = 0; i < n_property_tuples; ++i)
63 {
64 if ((*property_value_vec)[i] == old_value)
65 {
66 (*property_value_vec)[i] = new_value;
67 }
68 }
69
70 return true;
71}
72
73bool ElementValueModification::replace(MeshLib::Mesh& mesh, int const old_value,
74 int const new_value,
75 bool replace_if_exists)
76{
77 return replace(mesh, "MaterialIDs", old_value, new_value,
78 replace_if_exists);
79}
80
82{
83 MeshLib::PropertyVector<int>* property_value_vector = nullptr;
84 try
85 {
86 property_value_vector = mesh.getProperties().getPropertyVector<int>(
87 "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
88 }
89 catch (std::runtime_error const& e)
90 {
91 ERR("{:s}", e.what());
92 return 0;
93 }
94
95 std::vector<int> value_mapping(
96 getSortedPropertyValues(*property_value_vector));
97
98 std::vector<int> reverse_mapping(value_mapping.back() + 1, 0);
99 std::size_t const nValues(value_mapping.size());
100 for (std::size_t i = 0; i < nValues; ++i)
101 {
102 reverse_mapping[value_mapping[i]] = i;
103 }
104
105 std::size_t const n_property_values(property_value_vector->size());
106 for (std::size_t i = 0; i < n_property_values; ++i)
107 {
108 (*property_value_vector)[i] =
109 reverse_mapping[(*property_value_vector)[i]];
110 }
111
112 return nValues;
113}
114
116 MeshLib::Mesh& mesh, MeshLib::MeshElemType ele_type, int const new_value)
117{
118 MeshLib::PropertyVector<int>* property_value_vector = nullptr;
119 try
120 {
121 property_value_vector = mesh.getProperties().getPropertyVector<int>(
122 "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
123 }
124 catch (std::runtime_error const& e)
125 {
126 ERR("{:s}", e.what());
127 return 0;
128 }
129
130 std::vector<MeshLib::Element*> const& elements(mesh.getElements());
131 std::size_t cnt(0);
132 for (std::size_t k(0); k < elements.size(); k++)
133 {
134 if (elements[k]->getGeomType() != ele_type)
135 {
136 continue;
137 }
138 (*property_value_vector)[k] = new_value;
139 cnt++;
140 }
141
142 return cnt;
143}
144
145} // namespace MeshToolsLib
Definition of the ElementValueModification class.
Definition of the Element class.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the Mesh class.
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
Properties & getProperties()
Definition Mesh.h:134
PropertyVector< T > const * getPropertyVector(std::string_view name) const
std::size_t getNumberOfTuples() const
std::size_t size() const
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)
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:27