Skip to content

Commit

Permalink
GRIDEDIT-1566 Minor refactoring, can now pass span, or vector to merg…
Browse files Browse the repository at this point in the history
…e function
  • Loading branch information
BillSenior committed Dec 19, 2024
1 parent 2d34278 commit f65b5c3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 85 deletions.
8 changes: 4 additions & 4 deletions libs/MeshKernel/include/MeshKernel/Mesh2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ namespace meshkernel
static std::unique_ptr<Mesh2D> Merge(const Mesh2D& mesh1, const Mesh2D& mesh2);

/// @brief Merges mesh node and edge connectivity into a single mesh.
static std::unique_ptr<Mesh2D> Merge(const std::vector<Point>& mesh1Nodes,
const std::vector<Edge>& mesh1Edges,
const std::vector<Point>& mesh2Nodes,
const std::vector<Edge>& mesh2Edges,
static std::unique_ptr<Mesh2D> Merge(const std::span<const Point>& mesh1Nodes,
const std::span<const Edge>& mesh1Edges,
const std::span<const Point>& mesh2Nodes,
const std::span<const Edge>& mesh2Edges,
const Projection projection);

/// @brief Get the mesh bounding box
Expand Down
2 changes: 1 addition & 1 deletion libs/MeshKernel/src/ConnectMeshes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 7 additions & 41 deletions libs/MeshKernel/src/Mesh2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2471,10 +2471,10 @@ std::unique_ptr<Mesh2D> Mesh2D::Merge(const Mesh2D& mesh1, const Mesh2D& mesh2)
mergedMesh.m_projection);
}

std::unique_ptr<meshkernel::Mesh2D> Mesh2D::Merge(const std::vector<Point>& mesh1Nodes,
const std::vector<Edge>& mesh1Edges,
const std::vector<Point>& mesh2Nodes,
const std::vector<Edge>& mesh2Edges,
std::unique_ptr<meshkernel::Mesh2D> Mesh2D::Merge(const std::span<const Point>& mesh1Nodes,
const std::span<const Edge>& mesh1Edges,
const std::span<const Point>& mesh2Nodes,
const std::span<const Edge>& mesh2Edges,
const Projection projection)
{
std::vector<Point> mergedNodes(mesh1Nodes.size() + mesh2Nodes.size());
Expand All @@ -2499,49 +2499,15 @@ std::unique_ptr<meshkernel::Mesh2D> Mesh2D::Merge(const std::vector<Point>& 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<Edge> mesh2EdgesCopy;
// std::span<const Edge> 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<const Edge>(mesh2EdgesCopy.data(), mesh2EdgesCopy.size());
// }
// else
// {
// mesh2EdgesSpan = std::span<const Edge>(mesh2Edges.data(), mesh2Edges.size());
// }

// // Merge edge array from mesh2 edges
// mergedEdges.insert(mergedEdges.begin() + mesh1Edges.size(), mesh2EdgesSpan.begin(), mesh2EdgesSpan.end());
}

return std::make_unique<Mesh2D>(mergedEdges, mergedNodes, projection);
Expand Down
34 changes: 24 additions & 10 deletions libs/MeshKernel/tests/src/Mesh2DConnectDDTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ TEST(Mesh2DConnectDD, MergeMeshes)
delta = meshkernel::Vector{2.5, 2.5};
std::shared_ptr<meshkernel::Mesh2D> 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<meshkernel::Point> originalNodes(mergedMesh->Nodes());
const std::vector<meshkernel::Edge> originalEdges(mergedMesh->Edges());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -365,7 +371,9 @@ TEST(Mesh2DConnectDD, MergeTwoSameMeshesSmallNegativeOffset)

std::shared_ptr<meshkernel::Mesh2D> 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<meshkernel::Point> originalNodes(mergedMesh->Nodes());
const std::vector<meshkernel::Edge> originalEdges(mergedMesh->Edges());
Expand Down Expand Up @@ -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<meshkernel::Point> originalNodes(mergedMesh->Nodes());
const std::vector<meshkernel::Edge> originalEdges(mergedMesh->Edges());

Expand Down Expand Up @@ -521,7 +527,9 @@ TEST(Mesh2DConnectDD, MergeTwoSameMeshesSmallPositiveOffset)

std::shared_ptr<meshkernel::Mesh2D> 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<meshkernel::Point> originalNodes(mergedMesh->Nodes());
const std::vector<meshkernel::Edge> originalEdges(mergedMesh->Edges());
Expand Down Expand Up @@ -596,7 +604,9 @@ TEST(Mesh2DConnectDD, MergeTwoMeshesWithSmallNegativeOffset)

std::shared_ptr<meshkernel::Mesh2D> 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<meshkernel::Point> originalNodes(mergedMesh->Nodes());
const std::vector<meshkernel::Edge> originalEdges(mergedMesh->Edges());
Expand Down Expand Up @@ -801,7 +811,9 @@ TEST(Mesh2DConnectDD, MergeTwoMeshesWithSmallPositiveOffset)

std::shared_ptr<meshkernel::Mesh2D> 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<meshkernel::Point> originalNodes(mergedMesh->Nodes());
const std::vector<meshkernel::Edge> originalEdges(mergedMesh->Edges());
Expand Down Expand Up @@ -1008,7 +1020,9 @@ TEST(Mesh2DConnectDD, MergeTwoMeshesErrorInSeparationFraction)

std::shared_ptr<meshkernel::Mesh2D> 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);
Expand Down
33 changes: 4 additions & 29 deletions libs/MeshKernelApi/src/MeshKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -3820,33 +3818,10 @@ namespace meshkernelapi
mesh2d.node_x,
mesh2d.node_y);

std::unique_ptr<meshkernel::Mesh2D> 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<meshkernel::UInt> 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<meshkernel::UInt>(mesh2d.nodes_per_face[n]));
}

meshToConnect = std::make_unique<meshkernel::Mesh2D>(edges2d,
nodes2d,
face_nodes,
num_face_nodes,
meshKernelState[meshKernelId].m_projection);
}
else
{
meshToConnect = std::make_unique<meshkernel::Mesh2D>(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.
Expand Down

0 comments on commit f65b5c3

Please sign in to comment.