From 7a23e40d0788d6dbfb7a28ccc17a923b8aae8e80 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 26 Apr 2024 11:18:48 +0200 Subject: [PATCH 1/3] Implementing == and != operators --- .../custom_utilities/closest_points.cpp | 13 +++++++++++-- .../tests/cpp_tests/test_closest_points.cpp | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/applications/MappingApplication/custom_utilities/closest_points.cpp b/applications/MappingApplication/custom_utilities/closest_points.cpp index 5057ce2dbbe9..c70686ccd2dd 100644 --- a/applications/MappingApplication/custom_utilities/closest_points.cpp +++ b/applications/MappingApplication/custom_utilities/closest_points.cpp @@ -32,8 +32,17 @@ PointWithId::PointWithId(const PointWithId& rOther) bool PointWithId::operator<(const PointWithId& rOther) const { - if (Point::operator==(rOther)) return false; - return mDistance < rOther.mDistance; + return (!Point::operator==(rOther)) && (mDistance < rOther.mDistance); +} + +bool PointWithId::operator==(const PointWithId& rOther) const +{ + return ( Point::operator==(rOther)) && std::abs(mDistance - rOther.mDistance) < std::numeric_limits::epsilon(); +} + +bool PointWithId::operator!=(const PointWithId& rOther) const +{ + return (!Point::operator==(rOther)) || std::abs(mDistance - rOther.mDistance) > std::numeric_limits::epsilon(); } void PointWithId::save(Serializer &rSerializer) const diff --git a/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp b/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp index 2d0f96bc9667..3375ed23205d 100644 --- a/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp +++ b/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp @@ -63,17 +63,17 @@ KRATOS_TEST_CASE_IN_SUITE(PointWithIdEqualComparison, KratosMappingApplicationSe PointWithId point_2(id, coords, dist); PointWithId point_3(id, coords2, dist); - // only the position aka the coordinates are used for the equal comparison! PointWithId point_4(id+1, coords, dist); PointWithId point_5(id, coords, dist+1.0); PointWithId point_6(id+1, coords, dist+1.0); + // Coordinates and Distance (id excluded) are used for the equal comparison! KRATOS_EXPECT_EQ(point_1, point_2); KRATOS_EXPECT_NE(point_1, point_3); KRATOS_EXPECT_EQ(point_1, point_4); - KRATOS_EXPECT_EQ(point_1, point_5); - KRATOS_EXPECT_EQ(point_1, point_6); + KRATOS_EXPECT_NE(point_1, point_5); + KRATOS_EXPECT_NE(point_1, point_6); } KRATOS_TEST_CASE_IN_SUITE(PointWithIdLessComparison, KratosMappingApplicationSerialTestSuite) From fa4c9a43b0099b75b2b29e0f77ff8f228fb850d1 Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 26 Apr 2024 12:13:04 +0200 Subject: [PATCH 2/3] Missing header --- .../MappingApplication/custom_utilities/closest_points.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/applications/MappingApplication/custom_utilities/closest_points.h b/applications/MappingApplication/custom_utilities/closest_points.h index 94638e73e638..388ecc8772eb 100644 --- a/applications/MappingApplication/custom_utilities/closest_points.h +++ b/applications/MappingApplication/custom_utilities/closest_points.h @@ -44,6 +44,10 @@ class KRATOS_API(MAPPING_APPLICATION) PointWithId : public IndexedObject, public bool operator<(const PointWithId& rOther) const; + bool operator!=(const PointWithId& rOther) const; + + bool operator==(const PointWithId& rOther) const; + double GetDistance() const { return mDistance; } private: From fde830edc97007505091f632d54166bc65c5c91e Mon Sep 17 00:00:00 2001 From: Carlos Roig Date: Fri, 3 May 2024 12:23:55 +0200 Subject: [PATCH 3/3] Restoring non-distant check behaviour --- .../MappingApplication/custom_utilities/closest_points.cpp | 4 ++-- .../tests/cpp_tests/test_closest_points.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/applications/MappingApplication/custom_utilities/closest_points.cpp b/applications/MappingApplication/custom_utilities/closest_points.cpp index c70686ccd2dd..1a9ab670743b 100644 --- a/applications/MappingApplication/custom_utilities/closest_points.cpp +++ b/applications/MappingApplication/custom_utilities/closest_points.cpp @@ -37,12 +37,12 @@ bool PointWithId::operator<(const PointWithId& rOther) const bool PointWithId::operator==(const PointWithId& rOther) const { - return ( Point::operator==(rOther)) && std::abs(mDistance - rOther.mDistance) < std::numeric_limits::epsilon(); + return ( Point::operator==(rOther)); } bool PointWithId::operator!=(const PointWithId& rOther) const { - return (!Point::operator==(rOther)) || std::abs(mDistance - rOther.mDistance) > std::numeric_limits::epsilon(); + return (!Point::operator==(rOther)); } void PointWithId::save(Serializer &rSerializer) const diff --git a/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp b/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp index 3375ed23205d..1809392abc94 100644 --- a/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp +++ b/applications/MappingApplication/tests/cpp_tests/test_closest_points.cpp @@ -67,13 +67,13 @@ KRATOS_TEST_CASE_IN_SUITE(PointWithIdEqualComparison, KratosMappingApplicationSe PointWithId point_5(id, coords, dist+1.0); PointWithId point_6(id+1, coords, dist+1.0); - // Coordinates and Distance (id excluded) are used for the equal comparison! + // Only the position (aka the coordinates) are used for the EQ and NE comparison KRATOS_EXPECT_EQ(point_1, point_2); KRATOS_EXPECT_NE(point_1, point_3); KRATOS_EXPECT_EQ(point_1, point_4); - KRATOS_EXPECT_NE(point_1, point_5); - KRATOS_EXPECT_NE(point_1, point_6); + KRATOS_EXPECT_EQ(point_1, point_5); + KRATOS_EXPECT_EQ(point_1, point_6); } KRATOS_TEST_CASE_IN_SUITE(PointWithIdLessComparison, KratosMappingApplicationSerialTestSuite)