From 2c88301afac2aba5fb28b480d2cc6e45bb51a1b9 Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Tue, 14 Sep 2021 14:37:44 +0200
Subject: [PATCH] [A/U/ME] Add switch arg to tool.

A part of an element is inside of a polygon if at
least one node is in the (convex) polygon. Sometimes
it is useful to forster that all nodes of an
element have to be in the polygon. With the new
implemented switch argument the user can choose the
behaviour of the algorithm.
---
 .../ResetPropertiesInPolygonalRegion.cpp      | 25 +++++++++------
 .../MeshEditing/ResetMeshElementProperty.h    | 32 +++++++++++--------
 2 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp b/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp
index 6bed6f4e44f..3cff11e9fce 100644
--- a/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp
+++ b/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp
@@ -55,6 +55,13 @@ int main(int argc, char* argv[])
         "format)",
         true, "", "file name");
     cmd.add(geometry_fname);
+    TCLAP::SwitchArg any_of_arg(
+        "", "any_of",
+        "all nodes of an element has to be inside the polygon (default "
+        "behaviour without switch) or any node of an element has to be inside "
+        "(switch is given)",
+        false);
+    cmd.add(any_of_arg);
     TCLAP::ValueArg<char> char_property_arg(
         "c", "char-property-value", "new property value (data type char)",
         false, 'A', "character");
@@ -135,23 +142,23 @@ int main(int argc, char* argv[])
 
     if (char_property_arg.isSet())
     {
-        MeshGeoToolsLib::resetMeshElementProperty(*mesh, polygon, property_name,
-                                                  char_property_arg.getValue(),
-                                                  restrict_arg.getValue());
+        MeshGeoToolsLib::resetMeshElementProperty(
+            *mesh, polygon, property_name, char_property_arg.getValue(),
+            restrict_arg.getValue(), any_of_arg.getValue());
     }
 
     if (int_property_arg.isSet())
     {
-        MeshGeoToolsLib::resetMeshElementProperty(*mesh, polygon, property_name,
-                                                  int_property_arg.getValue(),
-                                                  restrict_arg.getValue());
+        MeshGeoToolsLib::resetMeshElementProperty(
+            *mesh, polygon, property_name, int_property_arg.getValue(),
+            restrict_arg.getValue(), any_of_arg.getValue());
     }
 
     if (bool_property_arg.isSet())
     {
-        MeshGeoToolsLib::resetMeshElementProperty(*mesh, polygon, property_name,
-                                                  bool_property_arg.getValue(),
-                                                  restrict_arg.getValue());
+        MeshGeoToolsLib::resetMeshElementProperty(
+            *mesh, polygon, property_name, bool_property_arg.getValue(),
+            restrict_arg.getValue(), any_of_arg.getValue());
     }
 
     MeshLib::MeshInformation::writePropertyVectorInformation(*mesh);
diff --git a/MeshGeoToolsLib/MeshEditing/ResetMeshElementProperty.h b/MeshGeoToolsLib/MeshEditing/ResetMeshElementProperty.h
index ed9fab401e5..f526a681ade 100644
--- a/MeshGeoToolsLib/MeshEditing/ResetMeshElementProperty.h
+++ b/MeshGeoToolsLib/MeshEditing/ResetMeshElementProperty.h
@@ -14,17 +14,14 @@
 #include <cstdlib>
 #include <vector>
 
-#include "MeshLib/IO/readMeshFromFile.h"
-#include "MeshLib/IO/writeMeshToFile.h"
-
 #include "GeoLib/GEOObjects.h"
 #include "GeoLib/Polygon.h"
-
 #include "MeshGeoToolsLib/MeshEditing/MarkNodesOutsideOfPolygon.h"
-
+#include "MeshLib/Elements/Element.h"
+#include "MeshLib/IO/readMeshFromFile.h"
+#include "MeshLib/IO/writeMeshToFile.h"
 #include "MeshLib/Mesh.h"
 #include "MeshLib/Node.h"
-#include "MeshLib/Elements/Element.h"
 
 namespace MeshGeoToolsLib
 {
@@ -33,7 +30,8 @@ void resetMeshElementProperty(MeshLib::Mesh& mesh,
                               GeoLib::Polygon const& polygon,
                               std::string const& property_name,
                               PT new_property_value,
-                              int restrict_to_material_id)
+                              int restrict_to_material_id,
+                              bool const any_of)
 {
     auto* const pv = MeshLib::getOrCreateMeshProperty<PT>(
         mesh, property_name, MeshLib::MeshItemType::Cell, 1);
@@ -44,9 +42,10 @@ void resetMeshElementProperty(MeshLib::Mesh& mesh,
         return;
     }
 
-    auto is_node_outside =
-        [outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon)](
-            auto const* node_ptr) { return outside[node_ptr->getID()]; };
+    auto const outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon);
+
+    auto is_node_outside = [&outside](auto const* node_ptr)
+    { return outside[node_ptr->getID()]; };
 
     auto const* material_ids =
         mesh.getProperties().getPropertyVector<int>("MaterialIDs");
@@ -59,17 +58,22 @@ void resetMeshElementProperty(MeshLib::Mesh& mesh,
             "mesh.");
     }
 
-    auto has_element_required_material_id = [&](int const element_id) {
+    auto has_element_required_material_id = [&](int const element_id)
+    {
         return restrict_to_material_id == -1 ||
                (*material_ids)[element_id] == restrict_to_material_id;
     };
 
+    auto is_element_outside =
+        any_of ? std::all_of<MeshLib::Node* const*, decltype(is_node_outside)>
+               : std::any_of<MeshLib::Node* const*, decltype(is_node_outside)>;
+
     for (std::size_t j(0); j < mesh.getElements().size(); ++j)
     {
         MeshLib::Element const* const elem(mesh.getElements()[j]);
-        if (std::all_of(elem->getNodes(),
-                        elem->getNodes() + elem->getNumberOfNodes(),
-                        is_node_outside))
+        if (is_element_outside(elem->getNodes(),
+                               elem->getNodes() + elem->getNumberOfNodes(),
+                               is_node_outside))
         {
             continue;
         }
-- 
GitLab