Skip to content

Commit

Permalink
GRIDEDIT-1336 Added cache for hanging edges
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior committed Oct 21, 2024
1 parent 2f44c3e commit ec747c1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
4 changes: 4 additions & 0 deletions libs/MeshKernelApi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ set(VERSION_INC_DIR ${CMAKE_SOURCE_DIR}/tools)
# list of target sources
set(SRC_LIST
${SRC_DIR}/BoundariesAsPolygonCache.cpp
${SRC_DIR}/CachedIntegerValues.cpp
${SRC_DIR}/CachedPointValues.cpp
${SRC_DIR}/FacePolygonPropertyCache.cpp
${SRC_DIR}/HangingEdgeCache.cpp
${SRC_DIR}/MKStateUndoAction.cpp
${SRC_DIR}/MeshKernel.cpp
${SRC_DIR}/NodeInPolygonCache.cpp
Expand All @@ -36,12 +38,14 @@ set(
INC_LIST
${DOMAIN_INC_DIR}/BoundariesAsPolygonCache.hpp
${DOMAIN_INC_DIR}/BoundingBox.hpp
${DOMAIN_INC_DIR}/CachedIntegerValues.hpp
${DOMAIN_INC_DIR}/CachedPointValues.hpp
${DOMAIN_INC_DIR}/Contacts.hpp
${DOMAIN_INC_DIR}/CurvilinearGrid.hpp
${DOMAIN_INC_DIR}/FacePolygonPropertyCache.hpp
${DOMAIN_INC_DIR}/GeometryList.hpp
${DOMAIN_INC_DIR}/GriddedSamples.hpp
${DOMAIN_INC_DIR}/HangingEdgeCache.hpp
${DOMAIN_INC_DIR}/MKStateUndoAction.hpp
${DOMAIN_INC_DIR}/Mesh1D.hpp
${DOMAIN_INC_DIR}/Mesh2D.hpp
Expand Down
17 changes: 2 additions & 15 deletions libs/MeshKernelApi/include/MeshKernelApi/NodeInPolygonCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@

#include "MeshKernel/Point.hpp"

#include "MeshKernelApi/CachedIntegerValues.hpp"
#include "MeshKernelApi/GeometryList.hpp"

namespace meshkernelapi
{

/// @brief Cache node indices contained in a polygon
class NodeInPolygonCache
class NodeInPolygonCache : public CachedIntegerValues
{
public:
/// @brief Constructor
Expand All @@ -49,26 +50,12 @@ namespace meshkernelapi
/// @brief Determine if current options match those used to construct the object
bool ValidOptions(const std::vector<meshkernel::Point>& polygonPoints, const int inside) const;

/// @brief Get the number of values being cached.
int Size() const;

/// @brief Copy cached values to array
void Copy(int* selectedNodes) const;

private:
/// @brief Points making up the polygon
std::vector<meshkernel::Point> m_polygonPoints;

/// @brief Indicates if the points are inside or outside of the polygon
int m_inside = -1;

/// @brief Indices of nodes in the polygon
std::vector<int> m_nodeIndices;
};

} // namespace meshkernelapi

inline int meshkernelapi::NodeInPolygonCache::Size() const
{
return static_cast<int>(m_nodeIndices.size());
}
2 changes: 2 additions & 0 deletions libs/MeshKernelApi/include/MeshKernelApi/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "MeshKernelApi/BoundariesAsPolygonCache.hpp"
#include "MeshKernelApi/CachedPointValues.hpp"
#include "MeshKernelApi/FacePolygonPropertyCache.hpp"
#include "MeshKernelApi/HangingEdgeCache.hpp"
#include "MeshKernelApi/NodeInPolygonCache.hpp"
#include "MeshKernelApi/PolygonRefinementCache.hpp"
#include "MeshKernelApi/SmallFlowEdgeCentreCache.hpp"
Expand Down Expand Up @@ -87,6 +88,7 @@ namespace meshkernelapi
std::shared_ptr<PolygonRefinementCache> m_polygonRefinementCache; ///< Cache for polygon refinement
std::shared_ptr<NodeInPolygonCache> m_nodeInPolygonCache; ///< Cache for node in polygon
std::shared_ptr<SmallFlowEdgeCentreCache> m_smallFlowEdgeCentreCache; ///< Cache for small flow edge centres
std::shared_ptr<HangingEdgeCache> m_hangingEdgeCache; ///< Cache for hanging edge ids
};

} // namespace meshkernelapi
19 changes: 15 additions & 4 deletions libs/MeshKernelApi/src/MeshKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,9 +1173,17 @@ namespace meshkernelapi
{
throw meshkernel::MeshKernelError("The selected mesh kernel id does not exist.");
}

if (meshKernelState[meshKernelId].m_hangingEdgeCache != nullptr)
{
meshKernelState[meshKernelId].m_hangingEdgeCache.reset();
throw meshkernel::MeshKernelError("Polygon Hanging edge has already been cached. Cached values will be delelted.");
}

meshKernelState[meshKernelId].m_mesh2d->Administrate();
const auto hangingEdges = meshKernelState[meshKernelId].m_mesh2d->GetHangingEdges();
numHangingEdges = static_cast<int>(hangingEdges.size());
meshKernelState[meshKernelId].m_hangingEdgeCache = std::make_shared<HangingEdgeCache>(hangingEdges);
numHangingEdges = meshKernelState[meshKernelId].m_hangingEdgeCache->Size();
}
catch (...)
{
Expand All @@ -1193,11 +1201,14 @@ namespace meshkernelapi
{
throw meshkernel::MeshKernelError("The selected mesh kernel id does not exist.");
}
const auto hangingEdges = meshKernelState[meshKernelId].m_mesh2d->GetHangingEdges();
for (size_t i = 0; i < hangingEdges.size(); ++i)

if (meshKernelState[meshKernelId].m_hangingEdgeCache == nullptr)
{
edges[i] = static_cast<int>(hangingEdges[i]);
throw meshkernel::MeshKernelError("Hanging edge data has not been cached");
}

meshKernelState[meshKernelId].m_hangingEdgeCache->Copy(edges);
meshKernelState[meshKernelId].m_hangingEdgeCache.reset();
}
catch (...)
{
Expand Down
19 changes: 5 additions & 14 deletions libs/MeshKernelApi/src/NodeInPolygonCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ meshkernelapi::NodeInPolygonCache::NodeInPolygonCache(const std::vector<int>& no
const int inside)
: m_polygonPoints(polygonPoints), m_inside(inside)
{
std::vector<int> nodeIndices;

m_nodeIndices.reserve(m_polygonPoints.size());
nodeIndices.reserve(m_polygonPoints.size());

for (size_t i = 0; i < nodeMask.size(); ++i)
{
if (nodeMask[i] > 0)
{
m_nodeIndices.push_back(static_cast<int>(i));
nodeIndices.push_back(static_cast<int>(i));
}
}

Reset(std::move(nodeIndices));
}

bool meshkernelapi::NodeInPolygonCache::ValidOptions(const std::vector<meshkernel::Point>& polygonPoints, const int inside) const
Expand All @@ -56,15 +59,3 @@ bool meshkernelapi::NodeInPolygonCache::ValidOptions(const std::vector<meshkerne
polygonPoints.size() == m_polygonPoints.size() &&
std::equal(polygonPoints.begin(), polygonPoints.end(), m_polygonPoints.begin());
}

void meshkernelapi::NodeInPolygonCache::Copy(int* selectedNodes) const
{
if (selectedNodes == nullptr)
{
throw meshkernel::MeshKernelError("Selected nodes array is null.");
}

size_t valueCount = sizeof(int) * m_nodeIndices.size();

std::memcpy(selectedNodes, m_nodeIndices.data(), valueCount);
}
30 changes: 30 additions & 0 deletions libs/MeshKernelApi/tests/src/ApiTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,36 @@ TEST_F(CartesianApiTestFixture, GetHangingEdgesMesh2D_WithOneHangingEdges_Should
ASSERT_EQ(hangingEdges[0], 9);
}

TEST_F(CartesianApiTestFixture, GetHangingEdgesMesh2D_WithOneHangingEdges_GetOneHangingEdgesFailures)
{
// Prepare
MakeMesh();
auto const meshKernelId = GetMeshKernelId();

// delete an edge at the lower left corner to create a new hanging edge
auto errorCode = meshkernelapi::mkernel_mesh2d_delete_edge(meshKernelId, 0.5, 0.0, 0.0, 0.0, 1.0, 1.0);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

int numHangingEdges;
errorCode = meshkernelapi::mkernel_mesh2d_count_hanging_edges(meshKernelId, numHangingEdges);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

// Already cached
errorCode = meshkernelapi::mkernel_mesh2d_count_hanging_edges(meshKernelId, numHangingEdges);
ASSERT_EQ(meshkernel::ExitCode::MeshKernelErrorCode, errorCode);

// Re-cache
errorCode = meshkernelapi::mkernel_mesh2d_count_hanging_edges(meshKernelId, numHangingEdges);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

std::vector<int> hangingEdges(numHangingEdges);
errorCode = meshkernelapi::mkernel_mesh2d_get_hanging_edges(meshKernelId, hangingEdges.data());
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

errorCode = meshkernelapi::mkernel_mesh2d_get_hanging_edges(meshKernelId, hangingEdges.data());
ASSERT_EQ(meshkernel::ExitCode::MeshKernelErrorCode, errorCode);
}

TEST_F(CartesianApiTestFixture, DeleteHangingEdgesMesh2D_WithOneHangingEdges_ShouldDeleteOneHangingEdges)
{
// Prepare
Expand Down

0 comments on commit ec747c1

Please sign in to comment.