Skip to content
Snippets Groups Projects
Commit 9e4bf142 authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

Merge pull request #645 from TomFischer/MeshEditElementValueModificationUsingNewPropertiesImpl

Using new mesh interface for Properties in class ElementValueModification 
parents fe2eb332 7399650c
No related branches found
No related tags found
No related merge requests found
...@@ -21,88 +21,105 @@ ...@@ -21,88 +21,105 @@
#include "MeshLib/Mesh.h" #include "MeshLib/Mesh.h"
#include "MeshLib/Elements/Element.h" #include "MeshLib/Elements/Element.h"
namespace MeshLib { namespace MeshLib {
std::vector<unsigned> ElementValueModification::getMeshValues(const MeshLib::Mesh &mesh) bool ElementValueModification::replace(MeshLib::Mesh &mesh,
std::string const& property_name, unsigned old_value, unsigned new_value,
bool replace_if_exists)
{ {
const std::size_t nElements (mesh.getNElements()); boost::optional<MeshLib::PropertyVector<int> &> optional_property_value_vec(
std::vector<unsigned> value_mapping; mesh.getProperties().getPropertyVector<int>(property_name)
for (unsigned i=0; i<nElements; ++i) );
{
bool exists(false);
unsigned value (mesh.getElement(i)->getValue());
const unsigned nValues (value_mapping.size());
for (unsigned j=0; j<nValues; ++j)
{
if (value == value_mapping[j])
{
exists = true;
break;
}
}
if (!exists)
value_mapping.push_back(value);
}
std::sort(value_mapping.begin(), value_mapping.end()); if (!optional_property_value_vec) {
return value_mapping; return false;
} }
bool ElementValueModification::replace(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists) MeshLib::PropertyVector<int> & property_value_vec(
{ optional_property_value_vec.get()
std::vector<unsigned> value_mapping (ElementValueModification::getMeshValues(mesh)); );
const std::size_t n_property_values(property_value_vec.size());
if (!replace_if_exists)
{ if (!replace_if_exists) {
const unsigned nValues (value_mapping.size()); for (std::size_t i=0; i<n_property_values; ++i) {
for (unsigned j=0; j<nValues; ++j) if (property_value_vec[i] == new_value) {
{ WARN ("ElementValueModification::replaceElementValue() "
if (new_value == value_mapping[j]) "- Replacement value \"%d\" is already taken, "
{ "no changes have been made.", new_value);
WARN ("ElementValueModification::replaceElementValue() - Replacement value is already taken, no changes have been made.");
return false; return false;
} }
} }
} }
const std::size_t nElements (mesh.getNElements());
std::vector<MeshLib::Element*> &elements (const_cast<std::vector<MeshLib::Element*>&>(mesh.getElements())); for (std::size_t i=0; i<n_property_values; ++i) {
for (unsigned i=0; i<nElements; ++i) if (property_value_vec[i] == old_value)
{ property_value_vec[i] = new_value;
if (elements[i]->getValue() == old_value)
elements[i]->setValue(new_value);
} }
return true; return true;
} }
bool ElementValueModification::replace(MeshLib::Mesh &mesh,
unsigned old_value, unsigned new_value, bool replace_if_exists)
{
return replace(mesh, "MaterialIDs", old_value, new_value, replace_if_exists);
}
unsigned ElementValueModification::condense(MeshLib::Mesh &mesh) unsigned ElementValueModification::condense(MeshLib::Mesh &mesh)
{ {
std::vector<unsigned> value_mapping (ElementValueModification::getMeshValues(mesh)); boost::optional<MeshLib::PropertyVector<int> &>
std::vector<unsigned> reverse_mapping(value_mapping.back()+1, 0); optional_property_value_vec(
mesh.getProperties().getPropertyVector<int>("MaterialIDs")
);
if (!optional_property_value_vec) {
return 0;
}
MeshLib::PropertyVector<int> & property_value_vector(
optional_property_value_vec.get()
);
std::vector<int> value_mapping(
getSortedPropertyValues(property_value_vector)
);
std::vector<int> reverse_mapping(value_mapping.back()+1, 0);
const unsigned nValues (value_mapping.size()); const unsigned nValues (value_mapping.size());
for (unsigned i=0; i<nValues; ++i) for (unsigned i=0; i<nValues; ++i)
reverse_mapping[value_mapping[i]] = i; reverse_mapping[value_mapping[i]] = i;
const std::size_t nElements (mesh.getNElements()); std::size_t const n_property_values(property_value_vector.size());
std::vector<MeshLib::Element*> &elements (const_cast<std::vector<MeshLib::Element*>&>(mesh.getElements())); for (std::size_t i=0; i<n_property_values; ++i)
for (unsigned i=0; i<nElements; ++i) property_value_vector[i] = reverse_mapping[property_value_vector[i]];
elements[i]->setValue(reverse_mapping[elements[i]->getValue()]);
return nValues; return nValues;
} }
unsigned ElementValueModification::setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, unsigned new_value) unsigned ElementValueModification::setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, unsigned new_value)
{ {
std::vector<MeshLib::Element*> &elements (const_cast<std::vector<MeshLib::Element*>&>(mesh.getElements())); boost::optional<MeshLib::PropertyVector<unsigned> &>
unsigned nValues = 0; optional_property_value_vec(
for (auto e : elements) { mesh.getProperties().getPropertyVector<unsigned>("MaterialIDs")
if (e->getGeomType()!=ele_type) );
if (!optional_property_value_vec) {
return 0;
}
MeshLib::PropertyVector<unsigned> & property_value_vector(
optional_property_value_vec.get()
);
std::vector<MeshLib::Element*> const& elements(mesh.getElements());
std::size_t cnt(0);
for (std::size_t k(0); k<elements.size(); k++) {
if (elements[k]->getGeomType()!=ele_type)
continue; continue;
e->setValue(new_value); property_value_vector[k] = new_value;
nValues++; cnt++;
} }
return nValues; return cnt;
} }
} // end namespace MeshLib } // end namespace MeshLib
...@@ -17,7 +17,11 @@ ...@@ -17,7 +17,11 @@
#include <vector> #include <vector>
#include <boost/optional.hpp>
#include "MeshLib/MeshEnums.h" #include "MeshLib/MeshEnums.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/PropertyVector.h"
namespace MeshLib { namespace MeshLib {
// forward declarations // forward declarations
...@@ -37,13 +41,40 @@ public: ...@@ -37,13 +41,40 @@ public:
/// Returns true if successful or false if the value is already taken. /// Returns true if successful or false if the value is already taken.
static bool replace(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists = false); static bool replace(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists = false);
static bool replace(MeshLib::Mesh &mesh, std::string const& property_name,
unsigned old_value, unsigned new_value, bool replace_if_exists = false);
/// Sets new value for all elements having the given element type /// Sets new value for all elements having the given element type
/// Returns the number of elements having the given element type /// Returns the number of elements having the given element type
static unsigned setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, unsigned new_value); static unsigned setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, unsigned new_value);
private: private:
/// Returns the values of elements within the mesh /// Returns sorted values of properties within the PropertyVector
static std::vector<unsigned> getMeshValues(const MeshLib::Mesh &mesh); /// These values are stored in a vector.
template <typename T>
static std::vector<T> getSortedPropertyValues(
MeshLib::PropertyVector<T> const& property_vector)
{
std::vector<T> value_mapping;
const std::size_t n_property_values(property_vector.size());
for (std::size_t i=0; i<n_property_values; ++i) {
bool exists(false);
T const& value (property_vector[i]);
std::size_t const size(value_mapping.size());
for (unsigned j=0; j<size; ++j) {
if (value == value_mapping[j]) {
exists = true;
break;
}
}
if (!exists)
value_mapping.push_back(value);
}
std::sort(value_mapping.begin(), value_mapping.end());
return value_mapping;
}
}; };
} // end namespace MeshLib } // end namespace MeshLib
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment