OGS
MeshToolsLib::ElementValueModification Class Reference

Detailed Description

A set of methods for manipulating mesh element values.

Definition at line 25 of file ElementValueModification.h.

#include <ElementValueModification.h>

Static Public Member Functions

static std::size_t condense (MeshLib::Mesh &mesh)
static bool replace (MeshLib::Mesh &mesh, int const old_value, int const new_value, bool replace_if_exists=false)
static bool replace (MeshLib::Mesh &mesh, std::string const &property_name, int const old_value, int const new_value, bool replace_if_exists=false)
static std::size_t setByElementType (MeshLib::Mesh &mesh, MeshLib::MeshElemType ele_type, int const new_value)

Static Private Member Functions

template<typename T>
static std::vector< T > getSortedPropertyValues (MeshLib::PropertyVector< T > const &property_vector)

Member Function Documentation

◆ condense()

std::size_t MeshToolsLib::ElementValueModification::condense ( MeshLib::Mesh & mesh)
static

Reduces the values assigned the elements of mesh to the smallest possible range. Returns the number of different values.

Definition at line 69 of file ElementValueModification.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Properties & getProperties()
Definition Mesh.h:125
PropertyVector< T > const * getPropertyVector(std::string_view name) const
constexpr void assign(R &&r)
static std::vector< T > getSortedPropertyValues(MeshLib::PropertyVector< T > const &property_vector)

References MeshLib::PropertyVector< PROP_VAL_TYPE >::assign(), MeshLib::Cell, ERR(), MeshLib::Mesh::getProperties(), MeshLib::Properties::getPropertyVector(), and getSortedPropertyValues().

Referenced by MeshValueEditDialog::accept(), main(), and FileIO::GMSH::readGMSHMesh().

◆ getSortedPropertyValues()

template<typename T>
std::vector< T > MeshToolsLib::ElementValueModification::getSortedPropertyValues ( MeshLib::PropertyVector< T > const & property_vector)
inlinestaticprivate

Returns sorted values of properties within the PropertyVector These values are stored in a vector.

Definition at line 52 of file ElementValueModification.h.

54 {
55 std::vector<T> value_mapping;
56 const std::size_t n_property_values(property_vector.size());
57 for (std::size_t i = 0; i < n_property_values; ++i)
58 {
59 bool exists(false);
60 T const& value(property_vector[i]);
61 std::size_t const size(value_mapping.size());
62 for (std::size_t j = 0; j < size; ++j)
63 {
64 if (value == value_mapping[j])
65 {
66 exists = true;
67 break;
68 }
69 }
70 if (!exists)
71 {
72 value_mapping.push_back(value);
73 }
74 }
75
76 std::sort(value_mapping.begin(), value_mapping.end());
77 return value_mapping;
78 }
constexpr std::size_t size() const
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.

References MeshLib::PropertyVector< PROP_VAL_TYPE >::size().

Referenced by condense().

◆ replace() [1/2]

bool MeshToolsLib::ElementValueModification::replace ( MeshLib::Mesh & mesh,
int const old_value,
int const new_value,
bool replace_if_exists = false )
static

Replaces for all elements of mesh with the value old_value with new_value if possible. Returns true if successful or false if the value is already taken.

Definition at line 61 of file ElementValueModification.cpp.

64{
65 return replace(mesh, "MaterialIDs", old_value, new_value,
66 replace_if_exists);
67}
static bool replace(MeshLib::Mesh &mesh, int const old_value, int const new_value, bool replace_if_exists=false)

References replace().

Referenced by MeshValueEditDialog::accept(), main(), and replace().

◆ replace() [2/2]

bool MeshToolsLib::ElementValueModification::replace ( MeshLib::Mesh & mesh,
std::string const & property_name,
int const old_value,
int const new_value,
bool replace_if_exists = false )
static

Definition at line 18 of file ElementValueModification.cpp.

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}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
constexpr std::size_t getNumberOfTuples() const

References MeshLib::Cell, ERR(), MeshLib::PropertyVector< PROP_VAL_TYPE >::getNumberOfTuples(), MeshLib::Mesh::getProperties(), MeshLib::Properties::getPropertyVector(), and WARN().

◆ setByElementType()

std::size_t MeshToolsLib::ElementValueModification::setByElementType ( MeshLib::Mesh & mesh,
MeshLib::MeshElemType ele_type,
int const new_value )
static

Sets new value for all elements having the given element type Returns the number of elements having the given element type

Definition at line 101 of file ElementValueModification.cpp.

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}
virtual MeshElemType getGeomType() const =0
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:216

References MeshLib::Cell, ERR(), MeshLib::Mesh::getElements(), MeshLib::Mesh::getProperties(), MeshLib::Properties::getPropertyVector(), and MeshLib::views::ids.

Referenced by main().


The documentation for this class was generated from the following files: