From ec747c1ec5fca46e3753f6a4aa24c2228b432890 Mon Sep 17 00:00:00 2001 From: BillSenior Date: Mon, 21 Oct 2024 15:34:12 +0200 Subject: [PATCH] GRIDEDIT-1336 Added cache for hanging edges --- libs/MeshKernelApi/CMakeLists.txt | 4 +++ .../MeshKernelApi/NodeInPolygonCache.hpp | 17 ++--------- .../include/MeshKernelApi/State.hpp | 2 ++ libs/MeshKernelApi/src/MeshKernel.cpp | 19 +++++++++--- libs/MeshKernelApi/src/NodeInPolygonCache.cpp | 19 ++++-------- libs/MeshKernelApi/tests/src/ApiTest.cpp | 30 +++++++++++++++++++ 6 files changed, 58 insertions(+), 33 deletions(-) diff --git a/libs/MeshKernelApi/CMakeLists.txt b/libs/MeshKernelApi/CMakeLists.txt index c23cab058..78374792f 100644 --- a/libs/MeshKernelApi/CMakeLists.txt +++ b/libs/MeshKernelApi/CMakeLists.txt @@ -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 @@ -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 diff --git a/libs/MeshKernelApi/include/MeshKernelApi/NodeInPolygonCache.hpp b/libs/MeshKernelApi/include/MeshKernelApi/NodeInPolygonCache.hpp index bb4bfa185..c0c1fbfa5 100644 --- a/libs/MeshKernelApi/include/MeshKernelApi/NodeInPolygonCache.hpp +++ b/libs/MeshKernelApi/include/MeshKernelApi/NodeInPolygonCache.hpp @@ -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 @@ -49,26 +50,12 @@ namespace meshkernelapi /// @brief Determine if current options match those used to construct the object bool ValidOptions(const std::vector& 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 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 m_nodeIndices; }; } // namespace meshkernelapi - -inline int meshkernelapi::NodeInPolygonCache::Size() const -{ - return static_cast(m_nodeIndices.size()); -} diff --git a/libs/MeshKernelApi/include/MeshKernelApi/State.hpp b/libs/MeshKernelApi/include/MeshKernelApi/State.hpp index 64e470509..55eac4ba0 100644 --- a/libs/MeshKernelApi/include/MeshKernelApi/State.hpp +++ b/libs/MeshKernelApi/include/MeshKernelApi/State.hpp @@ -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" @@ -87,6 +88,7 @@ namespace meshkernelapi std::shared_ptr m_polygonRefinementCache; ///< Cache for polygon refinement std::shared_ptr m_nodeInPolygonCache; ///< Cache for node in polygon std::shared_ptr m_smallFlowEdgeCentreCache; ///< Cache for small flow edge centres + std::shared_ptr m_hangingEdgeCache; ///< Cache for hanging edge ids }; } // namespace meshkernelapi diff --git a/libs/MeshKernelApi/src/MeshKernel.cpp b/libs/MeshKernelApi/src/MeshKernel.cpp index 6f1879671..ab98d18b7 100644 --- a/libs/MeshKernelApi/src/MeshKernel.cpp +++ b/libs/MeshKernelApi/src/MeshKernel.cpp @@ -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(hangingEdges.size()); + meshKernelState[meshKernelId].m_hangingEdgeCache = std::make_shared(hangingEdges); + numHangingEdges = meshKernelState[meshKernelId].m_hangingEdgeCache->Size(); } catch (...) { @@ -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(hangingEdges[i]); + throw meshkernel::MeshKernelError("Hanging edge data has not been cached"); } + + meshKernelState[meshKernelId].m_hangingEdgeCache->Copy(edges); + meshKernelState[meshKernelId].m_hangingEdgeCache.reset(); } catch (...) { diff --git a/libs/MeshKernelApi/src/NodeInPolygonCache.cpp b/libs/MeshKernelApi/src/NodeInPolygonCache.cpp index d8e74d70d..df5564af4 100644 --- a/libs/MeshKernelApi/src/NodeInPolygonCache.cpp +++ b/libs/MeshKernelApi/src/NodeInPolygonCache.cpp @@ -38,16 +38,19 @@ meshkernelapi::NodeInPolygonCache::NodeInPolygonCache(const std::vector& no const int inside) : m_polygonPoints(polygonPoints), m_inside(inside) { + std::vector 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(i)); + nodeIndices.push_back(static_cast(i)); } } + + Reset(std::move(nodeIndices)); } bool meshkernelapi::NodeInPolygonCache::ValidOptions(const std::vector& polygonPoints, const int inside) const @@ -56,15 +59,3 @@ bool meshkernelapi::NodeInPolygonCache::ValidOptions(const std::vector 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