Skip to content

Commit

Permalink
GRIDEDIT-1566 Testing another, simpler mesh merging
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior committed Dec 19, 2024
1 parent 496d2dd commit 0bd19ce
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
7 changes: 7 additions & 0 deletions libs/MeshKernel/include/MeshKernel/Mesh2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ namespace meshkernel
/// it may be required to call Administrate after merging
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,
const Projection projection);

/// @brief Get the mesh bounding box
///
/// @return The mesh bounding box
Expand Down
61 changes: 61 additions & 0 deletions libs/MeshKernel/src/Mesh2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,67 @@ 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,
const Projection projection)
{
std::vector<Point> mergedNodes(mesh1Nodes.size() + mesh2Nodes.size());
std::vector<Edge> mergedEdges(mesh1Edges.size() + mesh2Edges.size());

if (mesh1Nodes.size() > 0)
{
// Merge node array from mesh1 nodes
mergedNodes.insert(mergedNodes.begin(), mesh1Nodes.begin(), mesh1Nodes.end());

// Merge edge array from mesh1 edges
mergedEdges.insert(mergedEdges.begin(), mesh1Edges.begin(), mesh1Edges.end());
}

if (mesh2Nodes.size() > 0)
{
// Merge node array from mesh2 nodes
mergedNodes.insert(mergedNodes.begin() + mesh1Nodes.size(), mesh2Nodes.begin(), mesh2Nodes.end());

// 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)
{

Check failure on line 2502 in libs/MeshKernel/src/Mesh2D.cpp

View workflow job for this annotation

GitHub Actions / Codespell Check

likelly ==> likely
mesh2EdgesCopy = mesh2Edges;
const UInt numberOfMesh1Nodes = mesh1Nodes.size();

// 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);
}

meshkernel::BoundingBox Mesh2D::GetBoundingBox() const
{
Point lowerLeft(std::numeric_limits<double>::max(), std::numeric_limits<double>::max());
Expand Down
5 changes: 4 additions & 1 deletion libs/MeshKernel/tests/src/Mesh2DConnectDDTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ TEST(Mesh2DConnectDD, MergeTwoSameMeshesNoOffset)

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 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

0 comments on commit 0bd19ce

Please sign in to comment.