diff --git a/MeshLib/Elements/Element.cpp b/MeshLib/Elements/Element.cpp index 522134decdfe644644992a78a989cf7c41f33d47..8045642f363161d30fa521a2057f7b050e31eaa3 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 dd3c212483ac049efdfc4fe6c0a81779fd0fe9de..42322ffa299a484209400a0d0ad8fdc445754f66 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 5b1afa05a7e87eb40b8c3df937ecc8a50dce7d54..8bfe67ed1332a79cba6bc3eecd7f1d4fb31ddb46 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 47eee78feaf2f051d132d690de1b5ec24a25f3d8..0dcb60afa687e0e3512ed31ffc868a7d66dc40e5 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 86d2f683f7fdc93b9436f472684a6d1b3d588911..befad4afa759fc5caa8e97170406d06cdb9b549d 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])); }