Skip to content

Commit

Permalink
Fix performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato committed Jan 8, 2024
1 parent df419b7 commit c4117d3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
15 changes: 14 additions & 1 deletion libs/MeshKernel/include/MeshKernel/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ namespace meshkernel
/// @param[in] mergingDistance The distance below which two nodes will be merged
void MergeNodesInPolygon(const Polygons& polygons, double mergingDistance);

/// @brief Connect two existing nodes, forming a new edge (connectdbn)
/// @brief Connect two existing nodes, checking if the nodes are already connected.
/// If the nodes are not connected a new edge is formed, otherwise UInt invalid value is returned. (connectdbn)
/// @param[in] startNode The start node index
/// @param[in] endNode The end node index
/// @return The index of the new edge
Expand All @@ -181,6 +182,12 @@ namespace meshkernel
/// @return The index of the new node
UInt InsertNode(const Point& newPoint);

/// @brief Insert a new edge, assuming two nodes are not already connected
/// @param[in] startNode The start node index
/// @param[in] endNode The end node index
/// @return The index of the new edge
UInt InsertEdge(UInt startNode, UInt endNode);

/// @brief Delete a node
/// @param[in] node The index of the node to delete
void DeleteNode(UInt node);
Expand All @@ -191,6 +198,12 @@ namespace meshkernel
/// @return The edge index
[[nodiscard]] UInt FindEdge(UInt firstNodeIndex, UInt secondNodeIndex) const;

/// @brief Find the edge using a linear search, without connectivity information (much slower than FindEdge)
/// @param[in] firstNodeIndex The index of the first node
/// @param[in] secondNodeIndex The index of the second node
/// @return The edge index
[[nodiscard]] UInt FindEdgeWithLinearSearch(UInt firstNodeIndex, UInt secondNodeIndex) const;

/// @brief Move a node to a new location
/// @param[in] newPoint The new location
/// @param[in] nodeindex The index of the node to move
Expand Down
42 changes: 23 additions & 19 deletions libs/MeshKernel/src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ meshkernel::UInt Mesh::ConnectNodes(UInt startNode, UInt endNode)
if (edgeIndex != constants::missing::uintValue)
return constants::missing::uintValue;

return InsertEdge(startNode, endNode);
}

meshkernel::UInt Mesh::InsertEdge(UInt startNode, UInt endNode)
{
// increment the edges container
const auto newEdgeIndex = GetNumEdges();
m_edges.resize(newEdgeIndex + 1);
Expand Down Expand Up @@ -493,31 +498,30 @@ meshkernel::UInt Mesh::FindEdge(UInt firstNodeIndex, UInt secondNodeIndex) const
throw std::invalid_argument("Mesh::FindEdge: Invalid node index.");
}

const auto nunEdgesFirstNode = m_nodesNumEdges[firstNodeIndex];

if (nunEdgesFirstNode > 0)
for (UInt n = 0; n < m_nodesNumEdges[firstNodeIndex]; n++)
{
for (UInt n = 0; n < nunEdgesFirstNode; n++)
const auto edgeIndex = m_nodesEdges[firstNodeIndex][n];
const auto firstEdgeOtherNode = OtherNodeOfEdge(m_edges[edgeIndex], firstNodeIndex);
const auto edgeFound = firstEdgeOtherNode == secondNodeIndex;
if (edgeFound)
{
const auto edgeIndex = m_nodesEdges[firstNodeIndex][n];
const auto firstEdgeOtherNode = OtherNodeOfEdge(m_edges[edgeIndex], firstNodeIndex);
if (firstEdgeOtherNode == secondNodeIndex)
{
return edgeIndex;
}
return edgeIndex;
}
}
else

return constants::missing::uintValue;
}

meshkernel::UInt Mesh::FindEdgeWithLinearSearch(UInt firstNodeIndex, UInt secondNodeIndex) const
{
for (UInt edgeIndex = 0; edgeIndex < GetNumEdges(); edgeIndex++)
{
for (UInt edgeIndex = 0; edgeIndex < GetNumEdges(); edgeIndex++)
const auto& [firstNode, secondNode] = m_edges[edgeIndex];
const auto edgeFound = firstNode == firstNodeIndex && secondNode == secondNodeIndex ||
secondNode == firstNodeIndex && firstNode == secondNodeIndex;
if (edgeFound)
{
const auto& [firstNode, secondNode] = m_edges[edgeIndex];
const auto edgeFound = firstNode == firstNodeIndex && secondNode == secondNodeIndex ||
secondNode == firstNodeIndex && firstNode == secondNodeIndex;
if (edgeFound)
{
return edgeIndex;
}
return edgeIndex;
}
}

Expand Down
8 changes: 7 additions & 1 deletion libs/MeshKernel/src/Mesh2DGenerateGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ void Mesh2DGenerateGlobal::AddFace(Mesh& mesh,
{
nextNode = 0;
}
mesh.ConnectNodes(nodeIndices[n], nodeIndices[nextNode]);
const auto& firstNodeIndex = nodeIndices[n];
const auto& secondNodeIndex = nodeIndices[nextNode];

if (mesh.FindEdgeWithLinearSearch(firstNodeIndex, secondNodeIndex) == constants::missing::uintValue)
{
mesh.ConnectNodes(firstNodeIndex, secondNodeIndex);
}
}
}

Expand Down

0 comments on commit c4117d3

Please sign in to comment.