OGS
MeshToolsLib::ElementValueModification Class Reference

Detailed Description

A set of methods for manipulating mesh element values.

Definition at line 36 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 81 of file ElementValueModification.cpp.

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

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

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

◆ getSortedPropertyValues()

template<typename T >
static 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 63 of file ElementValueModification.h.

65 {
66 std::vector<T> value_mapping;
67 const std::size_t n_property_values(property_vector.size());
68 for (std::size_t i = 0; i < n_property_values; ++i)
69 {
70 bool exists(false);
71 T const& value(property_vector[i]);
72 std::size_t const size(value_mapping.size());
73 for (std::size_t j = 0; j < size; ++j)
74 {
75 if (value == value_mapping[j])
76 {
77 exists = true;
78 break;
79 }
80 }
81 if (!exists)
82 {
83 value_mapping.push_back(value);
84 }
85 }
86
87 std::sort(value_mapping.begin(), value_mapping.end());
88 return value_mapping;
89 }
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 73 of file ElementValueModification.cpp.

76{
77 return replace(mesh, "MaterialIDs", old_value, new_value,
78 replace_if_exists);
79}
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 26 of file ElementValueModification.cpp.

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}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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 115 of file ElementValueModification.cpp.

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}
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109

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

Referenced by main().


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