From 1ad35ab9571d72695cd5125f28476353e0e7a84c Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Mon, 30 Nov 2020 12:41:35 +0100
Subject: [PATCH] [MeL/Elements] Impl. hasNeighbor as free fct. areNeighbors.

---
 MeshLib/Elements/Element.cpp      | 27 +++++++++++++--------------
 MeshLib/Elements/Element.h        |  3 ---
 Tests/MeshLib/TestLineMesh.cpp    |  8 ++++----
 Tests/MeshLib/TestQuadMesh.cpp    |  4 ++--
 Tests/MeshLib/TestTriLineMesh.cpp | 12 ++++++------
 5 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/MeshLib/Elements/Element.cpp b/MeshLib/Elements/Element.cpp
index 522134decdf..8045642f363 100644
--- a/MeshLib/Elements/Element.cpp
+++ b/MeshLib/Elements/Element.cpp
@@ -52,7 +52,7 @@ boost::optional<unsigned> Element::addNeighbor(Element* e)
         return boost::optional<unsigned>();
     }
 
-    if (this->hasNeighbor(e))
+    if (areNeighbors(this, e))
     {
         return boost::optional<unsigned>();
     }
@@ -149,19 +149,6 @@ std::size_t Element::getNodeIndex(unsigned i) const
 #endif
 }
 
-bool Element::hasNeighbor(Element* elem) const
-{
-    unsigned nNeighbors (this->getNumberOfNeighbors());
-    for (unsigned i = 0; i < nNeighbors; i++)
-    {
-        if (this->_neighbors[i] == elem)
-        {
-            return true;
-        }
-    }
-    return false;
-}
-
 bool Element::isBoundaryElement() const
 {
     return std::any_of(_neighbors, _neighbors + this->getNumberOfNeighbors(),
@@ -188,6 +175,18 @@ std::ostream& operator<<(std::ostream& os, Element const& e)
 }
 #endif  // NDEBUG
 
+bool areNeighbors(Element const* const element, Element const* const other)
+{
+    unsigned nNeighbors(element->getNumberOfNeighbors());
+    for (unsigned i = 0; i < nNeighbors; i++)
+    {
+        if (element->getNeighbor(i) == other)
+        {
+            return true;
+        }
+    }
+    return false;
+}
 
 bool hasZeroVolume(MeshLib::Element const& element)
 {
diff --git a/MeshLib/Elements/Element.h b/MeshLib/Elements/Element.h
index dd3c212483a..42322ffa299 100644
--- a/MeshLib/Elements/Element.h
+++ b/MeshLib/Elements/Element.h
@@ -147,9 +147,6 @@ public:
      */
     virtual ElementErrorCode validate() const = 0;
 
-    /// Returns true if elem is a neighbour of this element and false otherwise.
-    bool hasNeighbor(Element* elem) const;
-
     /// Destructor
     virtual ~Element();
 
diff --git a/Tests/MeshLib/TestLineMesh.cpp b/Tests/MeshLib/TestLineMesh.cpp
index 5b1afa05a7e..8bfe67ed133 100644
--- a/Tests/MeshLib/TestLineMesh.cpp
+++ b/Tests/MeshLib/TestLineMesh.cpp
@@ -71,14 +71,14 @@ TEST_F(MeshLibLineMesh, ElementNeigbors)
     // elements.
     ASSERT_EQ(1u, count_neighbors(elements.front()));
     ASSERT_EQ(1u, count_neighbors(elements.back()));
-    ASSERT_TRUE(elements.front()->hasNeighbor(elements[1]));
-    ASSERT_TRUE(elements.back()->hasNeighbor(elements[elements.size()-2]));
+    ASSERT_TRUE(areNeighbors(elements.front(), elements[1]));
+    ASSERT_TRUE(areNeighbors(elements.back(), elements[elements.size()-2]));
 
     for (std::size_t i = 1; i < elements.size() - 1; ++i)
     {
         ASSERT_EQ(2u, count_neighbors(elements[i]));
-        ASSERT_TRUE(elements[i]->hasNeighbor(elements[i-1]));
-        ASSERT_TRUE(elements[i]->hasNeighbor(elements[i+1]));
+        ASSERT_TRUE(areNeighbors(elements[i], elements[i-1]));
+        ASSERT_TRUE(areNeighbors(elements[i], elements[i+1]));
     }
 }
 
diff --git a/Tests/MeshLib/TestQuadMesh.cpp b/Tests/MeshLib/TestQuadMesh.cpp
index 47eee78feaf..0dcb60afa68 100644
--- a/Tests/MeshLib/TestQuadMesh.cpp
+++ b/Tests/MeshLib/TestQuadMesh.cpp
@@ -221,12 +221,12 @@ TEST_F(MeshLibQuadMesh, ElementNeighbors)
                                 std::pair<Indices, Indices> const& neighbors) {
         for (auto i_neighbor : neighbors.first)
         {
-            ASSERT_TRUE(e->hasNeighbor(getElement(i_neighbor, j)));
+            ASSERT_TRUE(areNeighbors(e, getElement(i_neighbor, j)));
         }
 
         for (auto j_neighbor : neighbors.second)
         {
-            ASSERT_TRUE(e->hasNeighbor(getElement(i, j_neighbor)));
+            ASSERT_TRUE(areNeighbors(e, getElement(i, j_neighbor)));
         }
     };
 
diff --git a/Tests/MeshLib/TestTriLineMesh.cpp b/Tests/MeshLib/TestTriLineMesh.cpp
index 86d2f683f7f..befad4afa75 100644
--- a/Tests/MeshLib/TestTriLineMesh.cpp
+++ b/Tests/MeshLib/TestTriLineMesh.cpp
@@ -107,13 +107,13 @@ TEST_F(MeshLibTriLineMesh, NodeToElementConnectivity)
 TEST_F(MeshLibTriLineMesh, ElementToElementConnectivity)
 {
     // Triangles have other triangles as neighbors only.
-    EXPECT_TRUE(elements[0]->hasNeighbor(elements[1]));
-    EXPECT_TRUE(elements[1]->hasNeighbor(elements[0]));
+    EXPECT_TRUE(areNeighbors(elements[0], elements[1]));
+    EXPECT_TRUE(areNeighbors(elements[1], elements[0]));
 
-    EXPECT_FALSE(elements[0]->hasNeighbor(elements[2]));
-    EXPECT_FALSE(elements[1]->hasNeighbor(elements[2]));
+    EXPECT_FALSE(areNeighbors(elements[0], elements[2]));
+    EXPECT_FALSE(areNeighbors(elements[1], elements[2]));
 
     // Line has no neighbors
-    EXPECT_FALSE(elements[2]->hasNeighbor(elements[0]));
-    EXPECT_FALSE(elements[2]->hasNeighbor(elements[1]));
+    EXPECT_FALSE(areNeighbors(elements[2], elements[0]));
+    EXPECT_FALSE(areNeighbors(elements[2], elements[1]));
 }
-- 
GitLab