From 8804a746f1bf4065fa1547f094f46b02b062fc03 Mon Sep 17 00:00:00 2001
From: rinkk <karsten.rink@ufz.de>
Date: Wed, 9 May 2018 15:16:13 +0200
Subject: [PATCH] streamlined code and added some checks concerning data types,
 components, etc.

---
 .../DataView/MeshElementRemovalDialog.cpp     | 72 +++++++++----------
 .../DataView/MeshElementRemovalDialog.h       |  7 ++
 .../Utils/MeshEdit/removeMeshElements.cpp     | 10 +--
 MeshLib/MeshSearch/ElementSearch.h            | 14 ++--
 4 files changed, 56 insertions(+), 47 deletions(-)

diff --git a/Applications/DataExplorer/DataView/MeshElementRemovalDialog.cpp b/Applications/DataExplorer/DataView/MeshElementRemovalDialog.cpp
index 1c3585972f5..72c85c056a6 100644
--- a/Applications/DataExplorer/DataView/MeshElementRemovalDialog.cpp
+++ b/Applications/DataExplorer/DataView/MeshElementRemovalDialog.cpp
@@ -27,7 +27,6 @@
 #include "MeshLib/MeshSearch/ElementSearch.h"
 #include "MeshLib/Node.h"
 #include "MeshLib/Properties.h"
-#include "MeshLib/PropertyVector.h"
 
 /// Constructor
 MeshElementRemovalDialog::MeshElementRemovalDialog(
@@ -93,8 +92,15 @@ void MeshElementRemovalDialog::accept()
             min_val = this->insideScalarMinEdit->text().toDouble();
             max_val = this->insideScalarMaxEdit->text().toDouble();
         }
-        ex.searchByPropertyValueRange(array_name, min_val, max_val, outside);
-        anything_checked = true;
+        std::size_t n_marked_elements =
+            ex.searchByPropertyValueRange<double>(array_name, min_val, max_val, outside);
+
+        if (n_marked_elements == 0)
+            n_marked_elements =
+                ex.searchByPropertyValueRange<int>(array_name, min_val, max_val, outside);
+
+        if (n_marked_elements > 0)
+            anything_checked = true;
     }
     if (this->boundingBoxCheckBox->isChecked())
     {
@@ -157,11 +163,11 @@ std::size_t MeshElementRemovalDialog::addScalarArrays(MeshLib::Mesh const& mesh)
 {
     MeshLib::Properties const& properties = mesh.getProperties();
     std::vector<std::string> const& names = properties.getPropertyVectorNames();
-    for (auto name : names)
+    for (auto const& name : names)
     {
         if (properties.existsPropertyVector<int>(name))
         {
-            auto const p = properties.getPropertyVector<int>(name);
+            auto const& p = properties.getPropertyVector<int>(name);
             if (p->getMeshItemType() == MeshLib::MeshItemType::Cell)
             {
                 this->scalarArrayComboBox->addItem(QString::fromStdString(name));
@@ -170,7 +176,7 @@ std::size_t MeshElementRemovalDialog::addScalarArrays(MeshLib::Mesh const& mesh)
         }
         if (properties.existsPropertyVector<double>(name))
         {
-            auto const p = properties.getPropertyVector<double>(name);
+            auto const& p = properties.getPropertyVector<double>(name);
             if (p->getMeshItemType() == MeshLib::MeshItemType::Cell)
             {
                 this->scalarArrayComboBox->addItem(QString::fromStdString(name));
@@ -202,10 +208,7 @@ void MeshElementRemovalDialog::toggleScalarEdits(bool outside) const
 
 void MeshElementRemovalDialog::on_insideButton_toggled(bool is_checked)
 {
-    if (this->insideButton->isChecked())
-        toggleScalarEdits(false);
-    else
-        toggleScalarEdits(true);
+    toggleScalarEdits(!this->insideButton->isChecked());
 }
 
 void MeshElementRemovalDialog::on_boundingBoxCheckBox_toggled(bool is_checked)
@@ -267,43 +270,36 @@ void MeshElementRemovalDialog::on_meshNameComboBox_currentIndexChanged(int idx)
     this->outsideScalarMaxEdit->setText("");
     this->insideScalarMinEdit->setText("");
     this->insideScalarMaxEdit->setText("");
-    if (this->scalarArrayCheckBox->isChecked())
-        on_scalarArrayCheckBox_toggled(true);
-    if (this->boundingBoxCheckBox->isChecked())
-        on_boundingBoxCheckBox_toggled(true);
+    on_scalarArrayCheckBox_toggled(this->scalarArrayCheckBox->isChecked());
+    on_boundingBoxCheckBox_toggled(this->boundingBoxCheckBox->isChecked());
 }
 
 void MeshElementRemovalDialog::on_scalarArrayComboBox_currentIndexChanged(int idx)
 {
     Q_UNUSED(idx);
+    std::string const vec_name(scalarArrayComboBox->currentText().toStdString());
+    if (vec_name.empty())
+        return;
+
     MeshLib::Mesh const* const mesh =
         _project.getMesh(meshNameComboBox->currentText().toStdString());
-    MeshLib::Properties const& properties = mesh->getProperties();
-
-    std::string const vec_name(scalarArrayComboBox->currentText().toStdString());
-    if (vec_name == "")
+    if (mesh == nullptr)
         return;
+    MeshLib::Properties const& properties = mesh->getProperties();
 
     if (properties.existsPropertyVector<int>(vec_name))
-    {
-        MeshLib::PropertyVector<int> const& vec =
-            *properties.getPropertyVector<int>(vec_name);
-        auto min = std::min_element(vec.cbegin(), vec.cend());
-        auto max = std::max_element(vec.cbegin(), vec.cend());
-        this->outsideScalarMinEdit->setText(QString::number(*min));
-        this->outsideScalarMaxEdit->setText(QString::number(*max));
-        this->insideScalarMinEdit->setText(QString::number(*min));
-        this->insideScalarMaxEdit->setText(QString::number(*max));
-    }
+        setRangeValues<int>(*properties.getPropertyVector<int>(vec_name));
     else if (properties.existsPropertyVector<double>(vec_name))
-    {
-        MeshLib::PropertyVector<double> const& vec =
-            *properties.getPropertyVector<double>(vec_name);
-        auto min = std::min_element(vec.cbegin(), vec.cend());
-        auto max = std::max_element(vec.cbegin(), vec.cend());
-        this->outsideScalarMinEdit->setText(QString::number(*min));
-        this->outsideScalarMaxEdit->setText(QString::number(*max));
-        this->insideScalarMinEdit->setText(QString::number(*min));
-        this->insideScalarMaxEdit->setText(QString::number(*max));
-    }
+        setRangeValues<double>(*properties.getPropertyVector<double>(vec_name));
+}
+
+template <typename T>
+void MeshElementRemovalDialog::setRangeValues(MeshLib::PropertyVector<T> const& vec)
+{
+    auto min = std::min_element(vec.cbegin(), vec.cend());
+    auto max = std::max_element(vec.cbegin(), vec.cend());
+    this->outsideScalarMinEdit->setText(QString::number(*min));
+    this->outsideScalarMaxEdit->setText(QString::number(*max));
+    this->insideScalarMinEdit->setText(QString::number(*min));
+    this->insideScalarMaxEdit->setText(QString::number(*max));
 }
diff --git a/Applications/DataExplorer/DataView/MeshElementRemovalDialog.h b/Applications/DataExplorer/DataView/MeshElementRemovalDialog.h
index da15ae24f2b..1529b22345c 100644
--- a/Applications/DataExplorer/DataView/MeshElementRemovalDialog.h
+++ b/Applications/DataExplorer/DataView/MeshElementRemovalDialog.h
@@ -19,6 +19,9 @@
 #include <QDialog>
 #include <array>
 
+#include "MeshLib/PropertyVector.h"
+
+
 namespace DataHolderLib
 {
 class Project;
@@ -59,6 +62,10 @@ private slots:
 private:
     std::size_t addScalarArrays(MeshLib::Mesh const& mesh) const;
     void enableScalarArrayWidgets(bool enable) const;
+
+    template <typename T>
+    void setRangeValues(MeshLib::PropertyVector<T> const& vec);
+
     void toggleScalarEdits(bool outside) const;
 
     DataHolderLib::Project const& _project;
diff --git a/Applications/Utils/MeshEdit/removeMeshElements.cpp b/Applications/Utils/MeshEdit/removeMeshElements.cpp
index c35f221bb78..6ab13f629f4 100644
--- a/Applications/Utils/MeshEdit/removeMeshElements.cpp
+++ b/Applications/Utils/MeshEdit/removeMeshElements.cpp
@@ -37,8 +37,9 @@ void searchByPropertyValue(std::string const& property_name,
         std::size_t n_marked_elements = searcher.searchByPropertyValue<double>(
             property_name, property_value);
         if (n_marked_elements == 0)
-            std::size_t n_marked_elements = searcher.searchByPropertyValue<int>(
+            n_marked_elements = searcher.searchByPropertyValue<int>(
                 property_name, property_value);
+
         INFO("%d elements with property value %s found.", n_marked_elements,
              std::to_string(property_value).c_str());
     }
@@ -57,6 +58,8 @@ void searchByPropertyRange(std::string const& property_name,
             property_name, static_cast<int>(min_value),
             static_cast<int>(max_value), outside);
 
+    // add checks for other data types here (if n_marked_elements remains 0)
+
     INFO("%d elements in range [%s, %s] found.", n_marked_elements,
          std::to_string(min_value).c_str(), std::to_string(max_value).c_str());
 }
@@ -192,10 +195,7 @@ int main (int argc, char* argv[])
                 return EXIT_FAILURE;
             }
 
-            bool outside = false;
-            if (outside_property_arg.isSet())
-                outside = true;
-
+            bool const outside = outside_property_arg.isSet();
             searchByPropertyRange(
                 property_name_arg.getValue(), min_property_arg.getValue(),
                 max_property_arg.getValue(), outside, searcher);
diff --git a/MeshLib/MeshSearch/ElementSearch.h b/MeshLib/MeshSearch/ElementSearch.h
index fdb48e49965..d8631bf879e 100644
--- a/MeshLib/MeshSearch/ElementSearch.h
+++ b/MeshLib/MeshSearch/ElementSearch.h
@@ -31,7 +31,7 @@ public:
     /// return marked elements
     const std::vector<std::size_t>& getSearchedElementIDs() const { return _marked_elements; }
 
-    /// @tparam PROPERTY_TYPE integral type of the property
+    /// @tparam PROPERTY_TYPE type of the property
     /// Different properties can be assigned to the elements of the mesh. These
     /// properties can be accessed by the name of the property. The method marks
     /// all elements of the mesh for the property \c property_name with  a
@@ -49,7 +49,7 @@ public:
             property_name, property_value, property_value, false);
     }
 
-    /// @tparam PROPERTY_TYPE integral type of the property
+    /// @tparam PROPERTY_TYPE type of the property
     /// Different properties can be assigned to the elements of the mesh. These
     /// properties can be accessed by the name of the property. The method marks
     /// all elements of the mesh for the property \c property_name with  a
@@ -62,7 +62,7 @@ public:
     /// @param max_property_value maximum value of the given property for the
     /// element not to be marked
     /// @param outside_of if true, all values outside of the given range or
-    /// markedc, if false, all values inside the given range are marked
+    /// marked, if false, all values inside the given range are marked
     /// @return The number of marked elements will be returned. The concrete
     /// element ids can be requested by getSearchedElementIDs().
     template <typename PROPERTY_TYPE>
@@ -70,7 +70,7 @@ public:
         std::string const& property_name,
         PROPERTY_TYPE const min_property_value,
         PROPERTY_TYPE const max_property_value,
-        bool outside_of = true)
+        bool outside_of)
     {
         if (!_mesh.getProperties().existsPropertyVector<PROPERTY_TYPE>(property_name))
         {
@@ -87,6 +87,12 @@ public:
             return 0;
         }
 
+        if (pv->getNumberOfComponents() != 1)
+        {
+            WARN("Value-based element removal currently only works for scalars.");
+            return 0;
+        }
+
         std::vector<std::size_t> matchedIDs;
 
         if (outside_of)
-- 
GitLab