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