diff --git a/libs/MeshKernel/include/MeshKernel/Mesh2D.hpp b/libs/MeshKernel/include/MeshKernel/Mesh2D.hpp index b39c51583..70a4a6ae9 100644 --- a/libs/MeshKernel/include/MeshKernel/Mesh2D.hpp +++ b/libs/MeshKernel/include/MeshKernel/Mesh2D.hpp @@ -349,10 +349,10 @@ namespace meshkernel static std::unique_ptr Merge(const Mesh2D& mesh1, const Mesh2D& mesh2); /// @brief Merges mesh node and edge connectivity into a single mesh. - static std::unique_ptr Merge(const std::vector& mesh1Nodes, - const std::vector& mesh1Edges, - const std::vector& mesh2Nodes, - const std::vector& mesh2Edges, + static std::unique_ptr Merge(const std::span& mesh1Nodes, + const std::span& mesh1Edges, + const std::span& mesh2Nodes, + const std::span& mesh2Edges, const Projection projection); /// @brief Get the mesh bounding box diff --git a/libs/MeshKernel/src/ConnectMeshes.cpp b/libs/MeshKernel/src/ConnectMeshes.cpp index 424047f4c..af4371461 100644 --- a/libs/MeshKernel/src/ConnectMeshes.cpp +++ b/libs/MeshKernel/src/ConnectMeshes.cpp @@ -124,7 +124,7 @@ void meshkernel::ConnectMeshes::GetQuadrilateralElementsOnDomainBoundary(const M mesh.m_projection); // Only store edge info for edges that have a size strictly greater than EdgeLengthTolerance - if (edgeLength != constants::missing::doubleValue && edgeLength > EdgeLengthTolerance) + if (edgeLength != constants::missing::doubleValue && edgeLength > EdgeLengthTolerance) [[likely]] { elementsOnDomainBoundary.push_back(faceId); edgesOnDomainBoundary.push_back(i); diff --git a/libs/MeshKernel/src/Mesh2D.cpp b/libs/MeshKernel/src/Mesh2D.cpp index 4dc90d5a9..35cb98cbd 100644 --- a/libs/MeshKernel/src/Mesh2D.cpp +++ b/libs/MeshKernel/src/Mesh2D.cpp @@ -2471,10 +2471,10 @@ std::unique_ptr Mesh2D::Merge(const Mesh2D& mesh1, const Mesh2D& mesh2) mergedMesh.m_projection); } -std::unique_ptr Mesh2D::Merge(const std::vector& mesh1Nodes, - const std::vector& mesh1Edges, - const std::vector& mesh2Nodes, - const std::vector& mesh2Edges, +std::unique_ptr Mesh2D::Merge(const std::span& mesh1Nodes, + const std::span& mesh1Edges, + const std::span& mesh2Nodes, + const std::span& mesh2Edges, const Projection projection) { std::vector mergedNodes(mesh1Nodes.size() + mesh2Nodes.size()); @@ -2499,49 +2499,15 @@ std::unique_ptr Mesh2D::Merge(const std::vector& mesh if (mesh1Nodes.size() > 0) { - const UInt edgeOffset = mesh1Edges.size(); const UInt nodeOffset = mesh1Nodes.size(); // Re-assign the node ids for the second mesh data set - for (size_t i = 0; i < mesh2Edges.size(); ++i) + for (size_t i = mesh1Edges.size(); i < mergedEdges.size(); ++i) { - IncrementValidValue(mergedEdges[i + edgeOffset].first, nodeOffset); - IncrementValidValue(mergedEdges[i + edgeOffset].second, nodeOffset); + IncrementValidValue(mergedEdges[i].first, nodeOffset); + IncrementValidValue(mergedEdges[i].second, nodeOffset); } } - - // // A copy is needed so that the node connectivity ids can be reassigned - // // The copy is needed only if mesh1Nodes.size() > 0 (most likelly to be true) - // std::vector mesh2EdgesCopy; - // std::span mesh2EdgesSpan; - - // if (mesh1Nodes.size() > 0) - // { - // mesh2EdgesCopy = mesh2Edges; - - // // Re-assign the node ids for the second mesh data set - // for (size_t i = 0; i < mesh2Edges.size(); ++i) - // { - // if (mesh2EdgesCopy[i].first != constants::missing::uintValue) - // { - // mesh2EdgesCopy[i].first += numberOfMesh1Nodes; - // } - - // if (mesh2EdgesCopy[i].second != constants::missing::uintValue) - // { - // mesh2EdgesCopy[i].second += numberOfMesh1Nodes; - // } - // } - - // mesh2EdgesSpan = std::span(mesh2EdgesCopy.data(), mesh2EdgesCopy.size()); - // } - // else - // { - // mesh2EdgesSpan = std::span(mesh2Edges.data(), mesh2Edges.size()); - // } - - // // Merge edge array from mesh2 edges - // mergedEdges.insert(mergedEdges.begin() + mesh1Edges.size(), mesh2EdgesSpan.begin(), mesh2EdgesSpan.end()); } return std::make_unique(mergedEdges, mergedNodes, projection); diff --git a/libs/MeshKernel/tests/src/Mesh2DConnectDDTest.cpp b/libs/MeshKernel/tests/src/Mesh2DConnectDDTest.cpp index 15470539a..e0a3e8adc 100644 --- a/libs/MeshKernel/tests/src/Mesh2DConnectDDTest.cpp +++ b/libs/MeshKernel/tests/src/Mesh2DConnectDDTest.cpp @@ -254,7 +254,9 @@ TEST(Mesh2DConnectDD, MergeMeshes) delta = meshkernel::Vector{2.5, 2.5}; std::shared_ptr mesh2 = generateMesh(origin, delta, 9, 9); - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2->Nodes(), mesh2->Edges(), + mesh1->m_projection); const std::vector originalNodes(mergedMesh->Nodes()); const std::vector originalEdges(mergedMesh->Edges()); @@ -310,13 +312,17 @@ TEST(Mesh2DConnectDD, MergeOneEmptyMesh) // The projection needs to be set, as there is no default mesh2.m_projection = meshkernel::Projection::cartesian; - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2.Nodes(), mesh2.Edges(), + mesh1->m_projection); EXPECT_EQ(mergedMesh->GetNumFaces(), mesh1->GetNumFaces()); EXPECT_EQ(mergedMesh->GetNumNodes(), mesh1->GetNumNodes()); // This time with the parameters reversed - const auto anotherMergedMesh = meshkernel::Mesh2D::Merge(mesh2, *mesh1); + const auto anotherMergedMesh = meshkernel::Mesh2D::Merge(mesh2.Nodes(), mesh2.Edges(), + mesh1->Nodes(), mesh1->Edges(), + mesh1->m_projection); EXPECT_EQ(anotherMergedMesh->GetNumFaces(), mesh1->GetNumFaces()); EXPECT_EQ(anotherMergedMesh->GetNumValidNodes(), mesh1->GetNumNodes()); @@ -365,7 +371,9 @@ TEST(Mesh2DConnectDD, MergeTwoSameMeshesSmallNegativeOffset) std::shared_ptr mesh2 = generateMesh(origin, delta, nx, ny); - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2->Nodes(), mesh2->Edges(), + mesh1->m_projection); const std::vector originalNodes(mergedMesh->Nodes()); const std::vector originalEdges(mergedMesh->Edges()); @@ -445,8 +453,6 @@ TEST(Mesh2DConnectDD, MergeTwoSameMeshesNoOffset) mesh2->Nodes(), mesh2->Edges(), mesh1->m_projection); - // const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); - const std::vector originalNodes(mergedMesh->Nodes()); const std::vector originalEdges(mergedMesh->Edges()); @@ -521,7 +527,9 @@ TEST(Mesh2DConnectDD, MergeTwoSameMeshesSmallPositiveOffset) std::shared_ptr mesh2 = generateMesh(origin, delta, nx, ny); - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2->Nodes(), mesh2->Edges(), + mesh1->m_projection); const std::vector originalNodes(mergedMesh->Nodes()); const std::vector originalEdges(mergedMesh->Edges()); @@ -596,7 +604,9 @@ TEST(Mesh2DConnectDD, MergeTwoMeshesWithSmallNegativeOffset) std::shared_ptr mesh2 = generateMesh(origin, delta, nx, ny); - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2->Nodes(), mesh2->Edges(), + mesh1->m_projection); const std::vector originalNodes(mergedMesh->Nodes()); const std::vector originalEdges(mergedMesh->Edges()); @@ -801,7 +811,9 @@ TEST(Mesh2DConnectDD, MergeTwoMeshesWithSmallPositiveOffset) std::shared_ptr mesh2 = generateMesh(origin, delta, nx, ny); - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2->Nodes(), mesh2->Edges(), + mesh1->m_projection); const std::vector originalNodes(mergedMesh->Nodes()); const std::vector originalEdges(mergedMesh->Edges()); @@ -1008,7 +1020,9 @@ TEST(Mesh2DConnectDD, MergeTwoMeshesErrorInSeparationFraction) std::shared_ptr mesh2 = generateMesh(origin, delta, nx, ny); - const auto mergedMesh = meshkernel::Mesh2D::Merge(*mesh1, *mesh2); + const auto mergedMesh = meshkernel::Mesh2D::Merge(mesh1->Nodes(), mesh1->Edges(), + mesh2->Nodes(), mesh2->Edges(), + mesh1->m_projection); EXPECT_THROW([[maybe_unused]] auto undoAction = meshkernel::ConnectMeshes::Compute(*mergedMesh, 0.5), meshkernel::RangeError); EXPECT_THROW([[maybe_unused]] auto undoAction = meshkernel::ConnectMeshes::Compute(*mergedMesh, 1.5), meshkernel::RangeError); diff --git a/libs/MeshKernelApi/src/MeshKernel.cpp b/libs/MeshKernelApi/src/MeshKernel.cpp index 00e4833aa..1e2a4f22f 100644 --- a/libs/MeshKernelApi/src/MeshKernel.cpp +++ b/libs/MeshKernelApi/src/MeshKernel.cpp @@ -3810,8 +3810,6 @@ namespace meshkernelapi throw meshkernel::MeshKernelError("The selected mesh kernel id does not exist."); } - // Check mesh has been initialised - // convert raw arrays to containers const auto edges2d = meshkernel::ConvertToEdgeNodesVector(mesh2d.num_edges, mesh2d.edge_nodes); @@ -3820,33 +3818,10 @@ namespace meshkernelapi mesh2d.node_x, mesh2d.node_y); - std::unique_ptr meshToConnect; - - if (mesh2d.num_faces > 0 && mesh2d.face_nodes != nullptr && mesh2d.nodes_per_face != nullptr) - { - - const auto face_nodes = meshkernel::ConvertToFaceNodesVector(mesh2d.num_faces, mesh2d.face_nodes, mesh2d.nodes_per_face); - - std::vector num_face_nodes; - num_face_nodes.reserve(mesh2d.num_faces); - - for (auto n = 0; n < mesh2d.num_faces; n++) - { - num_face_nodes.emplace_back(static_cast(mesh2d.nodes_per_face[n])); - } - - meshToConnect = std::make_unique(edges2d, - nodes2d, - face_nodes, - num_face_nodes, - meshKernelState[meshKernelId].m_projection); - } - else - { - meshToConnect = std::make_unique(edges2d, nodes2d, meshKernelState[meshKernelId].m_projection); - } - - const auto mergedMeshes = meshkernel::Mesh2D::Merge(*meshKernelState[meshKernelId].m_mesh2d, *meshToConnect); + const auto mergedMeshes = meshkernel::Mesh2D::Merge(meshKernelState[meshKernelId].m_mesh2d->Nodes(), + meshKernelState[meshKernelId].m_mesh2d->Edges(), + nodes2d, edges2d, + meshKernelState[meshKernelId].m_projection); // Keep existing mesh to restore with undo auto undoAction = meshkernel::FullUnstructuredGridUndo::Create(*meshKernelState[meshKernelId].m_mesh2d); // The undo information collected from the ConnectMeshes::Compute is not needed here.