From cfd0b47bf76c548889b13af3b4447bfa688b84a0 Mon Sep 17 00:00:00 2001 From: BillSenior <89970704+BillSenior@users.noreply.github.com> Date: Wed, 18 Dec 2024 17:35:27 +0100 Subject: [PATCH] Fixed bug in node classification and updated unit test (#384 | GRIDEDIT-1516) --- libs/MeshKernel/CMakeLists.txt | 1 + libs/MeshKernel/src/Mesh2D.cpp | 9 +- libs/MeshKernel/src/Utilities/Utilities.cpp | 92 +++++++++++++++++++ .../tests/src/OrthogonalizationTests.cpp | 89 +++++++++--------- 4 files changed, 145 insertions(+), 46 deletions(-) create mode 100644 libs/MeshKernel/src/Utilities/Utilities.cpp diff --git a/libs/MeshKernel/CMakeLists.txt b/libs/MeshKernel/CMakeLists.txt index 5faf05ebb..1ef6ebad0 100644 --- a/libs/MeshKernel/CMakeLists.txt +++ b/libs/MeshKernel/CMakeLists.txt @@ -281,6 +281,7 @@ target_sources( PRIVATE ${SRC_LIST} ${UNDO_SRC_LIST} + ${UTILITIES_SRC_LIST} ${AVERAGING_STRATEGIES_SRC_LIST} ${CURVILINEAR_GRID_SRC_LIST} ${CURVILINEAR_GRID_UNDO_ACTION_SRC_LIST} diff --git a/libs/MeshKernel/src/Mesh2D.cpp b/libs/MeshKernel/src/Mesh2D.cpp index d41972e64..cdaa6eace 100644 --- a/libs/MeshKernel/src/Mesh2D.cpp +++ b/libs/MeshKernel/src/Mesh2D.cpp @@ -670,7 +670,7 @@ void Mesh2D::InitialiseBoundaryNodeClassification() const auto firstNode = m_edges[e].first; const auto secondNode = m_edges[e].second; - if (firstNode == constants::missing::uintValue || secondNode == constants::missing::uintValue) + if (firstNode == constants::missing::uintValue || secondNode == constants::missing::uintValue) [[unlikely]] { continue; } @@ -714,7 +714,7 @@ void Mesh2D::ClassifyNode(const UInt nodeId) continue; } - if (firstNode == 0) + if (firstNode == constants::missing::uintValue) { firstNode = OtherNodeOfEdge(m_edges[edgeIndex], nodeId); } @@ -751,6 +751,11 @@ void Mesh2D::ClassifyNodes() for (UInt n = 0; n < GetNumNodes(); n++) { + if (!Node(n).IsValid()) [[unlikely]] + { + continue; + } + if (m_nodesTypes[n] == 1 || m_nodesTypes[n] == 2) { ClassifyNode(n); diff --git a/libs/MeshKernel/src/Utilities/Utilities.cpp b/libs/MeshKernel/src/Utilities/Utilities.cpp new file mode 100644 index 000000000..5d2b34ee9 --- /dev/null +++ b/libs/MeshKernel/src/Utilities/Utilities.cpp @@ -0,0 +1,92 @@ +//---- GPL --------------------------------------------------------------------- +// +// Copyright (C) Stichting Deltares, 2011-2024. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation version 3. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// contact: delft3d.support@deltares.nl +// Stichting Deltares +// P.O. Box 177 +// 2600 MH Delft, The Netherlands +// +// All indications and logos of, and references to, "Delft3D" and "Deltares" +// are registered trademarks of Stichting Deltares, and remain the property of +// Stichting Deltares. All rights reserved. +// +//------------------------------------------------------------------------------ + +#include "MeshKernel/Utilities/Utilities.hpp" +#include "MeshKernel/Exceptions.hpp" + +void meshkernel::Print(const std::vector& nodes, const std::vector& edges, std::ostream& out) +{ + out << "nullId = " << constants::missing::uintValue << ";" << std::endl; + out << "nullValue = " << constants::missing::doubleValue << ";" << std::endl; + out << "nodex = zeros ( " << nodes.size() << ", 1);" << std::endl; + out << "nodey = zeros ( " << nodes.size() << ", 1);" << std::endl; + out << "edges = zeros ( " << edges.size() << ", 2);" << std::endl; + + for (UInt i = 0; i < nodes.size(); ++i) + { + out << "nodex (" << i + 1 << " ) = " << nodes[i].x << ";" << std::endl; + } + + for (UInt i = 0; i < nodes.size(); ++i) + { + out << "nodey (" << i + 1 << " ) = " << nodes[i].y << ";" << std::endl; + } + + out << "edges = zeros ( " << edges.size() << ", 2 );" << std::endl; + + for (UInt i = 0; i < edges.size(); ++i) + { + out << "edges ( " << i + 1 << ", 1 ) = " << edges[i].first + 1 << ";" << std::endl; + out << "edges ( " << i + 1 << ", 2 ) = " << edges[i].second + 1 << ";" << std::endl; + } +} + +void meshkernel::Print(const std::vector& xNodes, + const std::vector& yNodes, + const std::vector& edges, + std::ostream& out) +{ + // xNodes and yNodes should be the same size + if (xNodes.size() != yNodes.size()) + { + throw ConstraintError("x-node and y-nodes are not the same size, {} /= {}", xNodes.size(), yNodes.size()); + } + + out << "nullId = " << constants::missing::uintValue << ";" << std::endl; + out << "nullValue = " << constants::missing::doubleValue << ";" << std::endl; + out << "nodex = zeros ( " << xNodes.size() << ", 1);" << std::endl; + out << "nodey = zeros ( " << xNodes.size() << ", 1);" << std::endl; + out << "edges = zeros ( " << edges.size() << ", 2);" << std::endl; + + for (UInt i = 0; i < xNodes.size(); ++i) + { + out << "nodex (" << i + 1 << " ) = " << xNodes[i] << ";" << std::endl; + } + + for (UInt i = 0; i < xNodes.size(); ++i) + { + out << "nodey (" << i + 1 << " ) = " << yNodes[i] << ";" << std::endl; + } + + out << "edges = zeros ( " << edges.size() / 2 << ", 2 );" << std::endl; + + for (UInt i = 0; i < edges.size() / 2; ++i) + { + out << "edges ( " << i + 1 << ", 1 ) = " << edges[2 * i] + 1 << ";" << std::endl; + out << "edges ( " << i + 1 << ", 2 ) = " << edges[2 * i + 1] + 1 << ";" << std::endl; + } +} diff --git a/libs/MeshKernel/tests/src/OrthogonalizationTests.cpp b/libs/MeshKernel/tests/src/OrthogonalizationTests.cpp index bec025a04..92d8b402a 100644 --- a/libs/MeshKernel/tests/src/OrthogonalizationTests.cpp +++ b/libs/MeshKernel/tests/src/OrthogonalizationTests.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -797,54 +798,54 @@ TEST(OrthogonalizationAndSmoothing, OrthogonalizationWithGapsInNodeAndEdgeLists) projectToLandBoundaryOption, orthogonalizationParameters); - const std::vector expectedX{28.197593689053271, - 45.122094904304319, - 67.434041291301682, - 91.65019268878028, - 121.18012351239659, + const std::vector expectedX{25.0800705723722, + 42.317662650388, + 65.4076069697485, + 90.1058019292434, + 120.558686253752, 150.0, - 121.65514995517655, - 96.144810647136211, - 73.392601318383058, - 49.383970314068847, - 27.985698958218652, - 9.2519807557372626, - -13.113266174727251, - 8.0469903108904433, - 18.108865376000267, - 30.057404995092867, - 81.572189207388078, - 100.72370484044434, - 44.08035834002014, - 62.494201377406981, - 56.221541072263449, - 80.952825997664704, - 39.468493116531533, + 121.059743084289, + 95.1726727500558, + 72.1931604613651, + 47.4190144307411, + 25.2372847856225, + 5.68099647107239, + -20, + 4.58504074822491, + 14.9038474210351, + 27.1345108996488, + 80.0992228898113, + 99.6419675416531, + 41.6528740558509, + 60.6207362548768, + 54.0359431801395, + 79.4201977834984, + 36.6509101374463, -999.0}; - const std::vector expectedY{5.6395187378106542, - 9.0244189808608635, - 13.486808258260337, - 18.330038537756057, - 32.708074107437959, + const std::vector expectedY{5.01601411447445, + 8.4635325300776, + 13.0815213939497, + 18.0211603858487, + 32.3352117522512, 50.0, - 59.448283348274487, - 67.951729784287934, - 74.238600624497238, - 62.866091201401034, - 52.730067927577252, - 43.856201410612385, - 33.262137075129196, - 1.6093980621780888, - 22.269932674539941, - 35.917300713638298, - 51.865232389386854, - 45.127820397816109, - 43.164232704286427, - 48.110596487644287, - 28.375434217690007, - 34.073083926222459, - 23.663968978263402, + 59.6467523052371, + 68.2757757499814, + 73.6704444290677, + 61.9353226250879, + 51.4281875300317, + 42.164682538929, + 30.0, + 0.917008149644981, + 21.0550841422499, + 34.910733978114, + 51.5236060545154, + 44.9951979924611, + 42.223424366405, + 47.4725501580611, + 27.7287089020759, + 33.7364202545338, + 22.785807215113, -999.0}; const std::vector edgeFirst{13, 14, 12, 13, 0, 22, 0, 0, 20, 18, 22, 15,