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"
22 #include "MeshLib/PropertyVector.h"
23 
24 namespace MeshLib
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 
73 bool 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  MeshElemType ele_type,
117  int const new_value)
118 {
119  MeshLib::PropertyVector<int>* property_value_vector = nullptr;
120  try
121  {
122  property_value_vector = mesh.getProperties().getPropertyVector<int>(
123  "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
124  }
125  catch (std::runtime_error const& e)
126  {
127  ERR("{:s}", e.what());
128  return 0;
129  }
130 
131  std::vector<MeshLib::Element*> const& elements(mesh.getElements());
132  std::size_t cnt(0);
133  for (std::size_t k(0); k < elements.size(); k++)
134  {
135  if (elements[k]->getGeomType() != ele_type)
136  {
137  continue;
138  }
139  (*property_value_vector)[k] = new_value;
140  cnt++;
141  }
142 
143  return cnt;
144 }
145 
146 } // end namespace MeshLib
Definition of the ElementValueModification class.
Definition of the Element class.
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Definition of the Mesh class.
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)
static std::size_t setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, int const new_value)
static std::vector< T > getSortedPropertyValues(MeshLib::PropertyVector< T > const &property_vector)
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
PropertyVector< T > const * getPropertyVector(std::string const &name) const
std::size_t getNumberOfTuples() const
std::size_t size() const
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition: MeshEnums.h:27