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 80 of file ElementValueModification.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Properties & getProperties()
Definition Mesh.h:136
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< T >::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 >
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 std::size_t size() const
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.

References MeshLib::PropertyVector< T >::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 72 of file ElementValueModification.cpp.

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

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

References MeshLib::Cell, ERR(), MeshLib::PropertyVector< T >::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 112 of file ElementValueModification.cpp.

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

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: