OGS
MeshLib::ElementValueModification Class Reference

Detailed Description

A set of methods for manipulating mesh element values.

Definition at line 31 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, 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 MeshLib::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(char const *fmt, Args const &... args)
Definition: Logging.h:42
static std::vector< T > getSortedPropertyValues(MeshLib::PropertyVector< T > const &property_vector)
Properties & getProperties()
Definition: Mesh.h:123
PropertyVector< T > const * getPropertyVector(std::string const &name) const
std::size_t size() const

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> MeshLib::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 53 of file ElementValueModification.h.

55  {
56  std::vector<T> value_mapping;
57  const std::size_t n_property_values(property_vector.size());
58  for (std::size_t i=0; i<n_property_values; ++i) {
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  if (value == value_mapping[j]) {
64  exists = true;
65  break;
66  }
67  }
68  if (!exists)
69  {
70  value_mapping.push_back(value);
71  }
72  }
73 
74  std::sort(value_mapping.begin(), value_mapping.end());
75  return value_mapping;
76  }

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

Referenced by condense().

◆ replace() [1/2]

bool MeshLib::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)

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

◆ replace() [2/2]

bool MeshLib::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(char const *fmt, Args const &... args)
Definition: Logging.h:37
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 MeshLib::ElementValueModification::setByElementType ( MeshLib::Mesh mesh,
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.

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

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: