From 98c908664cbb5267c199b087494787d4048ef02c Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Mon, 13 Apr 2015 14:44:09 +0200
Subject: [PATCH] [MeL/ME] ElemValMod: Rewrote replace().

---
 .../MeshEditing/ElementValueModification.cpp  | 48 ++++++++++++-------
 .../MeshEditing/ElementValueModification.h    |  3 ++
 2 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/MeshLib/MeshEditing/ElementValueModification.cpp b/MeshLib/MeshEditing/ElementValueModification.cpp
index acefe0d011c..dee36a008a6 100644
--- a/MeshLib/MeshEditing/ElementValueModification.cpp
+++ b/MeshLib/MeshEditing/ElementValueModification.cpp
@@ -49,32 +49,48 @@ std::vector<unsigned> ElementValueModification::getMeshValues(const MeshLib::Mes
 	return value_mapping;
 }
 
-bool ElementValueModification::replace(MeshLib::Mesh &mesh, unsigned old_value, unsigned new_value, bool replace_if_exists)
+bool ElementValueModification::replace(MeshLib::Mesh &mesh,
+	std::string const& property_name, unsigned old_value, unsigned new_value,
+	bool replace_if_exists)
 {
-	std::vector<unsigned> value_mapping (ElementValueModification::getMeshValues(mesh));
+	boost::optional<MeshLib::PropertyVector<int> &> optional_property_value_vec(
+		mesh.getProperties().getPropertyVector<int>(property_name)
+	);
 
-	if (!replace_if_exists)
-	{
-		const unsigned nValues (value_mapping.size());
-		for (unsigned j=0; j<nValues; ++j)
-		{
-			if (new_value == value_mapping[j])
-			{
-				WARN ("ElementValueModification::replaceElementValue() - Replacement value is already taken, no changes have been made.");
+	if (!optional_property_value_vec) {
+		return false;
+	}
+
+	MeshLib::PropertyVector<int> & property_value_vec(
+		optional_property_value_vec.get()
+	);
+	const std::size_t n_property_values(property_value_vec.size());
+
+	if (!replace_if_exists) {
+		for (std::size_t i=0; i<n_property_values; ++i) {
+			if (property_value_vec[i] == new_value) {
+				WARN ("ElementValueModification::replaceElementValue() "
+					"- Replacement value \"%d\" is already taken, "
+					"no changes have been made.", new_value);
 				return false;
 			}
 		}
 	}
-	const std::size_t nElements (mesh.getNElements());
-	std::vector<MeshLib::Element*> &elements (const_cast<std::vector<MeshLib::Element*>&>(mesh.getElements()));
-	for (unsigned i=0; i<nElements; ++i)
-	{
-		if (elements[i]->getValue() == old_value)
-			elements[i]->setValue(new_value);
+
+	for (std::size_t i=0; i<n_property_values; ++i) {
+		if (property_value_vec[i] == old_value)
+			property_value_vec[i] = new_value;
 	}
+
 	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)
 {
 	boost::optional<MeshLib::PropertyVector<int> &>
diff --git a/MeshLib/MeshEditing/ElementValueModification.h b/MeshLib/MeshEditing/ElementValueModification.h
index 9d38002eb29..2464c43b574 100644
--- a/MeshLib/MeshEditing/ElementValueModification.h
+++ b/MeshLib/MeshEditing/ElementValueModification.h
@@ -41,6 +41,9 @@ public:
 	/// 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, 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
 	/// Returns the number of elements having the given element type
 	static unsigned setByElementType(MeshLib::Mesh &mesh, MeshElemType ele_type, unsigned new_value);
-- 
GitLab