Skip to content

Commit

Permalink
Improve encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato committed Nov 19, 2024
1 parent 2bc3039 commit 0389665
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 62 deletions.
11 changes: 7 additions & 4 deletions libs/MeshKernel/include/MeshKernel/LandBoundaries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace meshkernel
DoNotProject = 0,
ToOriginalMeshBoundary = 1,
OuterMeshBoundaryToLandBoundaries = 2,
InnerAndOuterMeshBoundaryToLandboundaries = 3
InnerAndOuterMeshBoundariesToLandboundaries = 3
};

/// @brief Default constructor
Expand All @@ -79,8 +79,8 @@ namespace meshkernel
void Administrate();

/// @brief Find the mesh boundary line closest to the land boundary (find_nearest_meshline).
/// @param[in] projectToLandBoundaryOption The option describing the projection to the land boundary.
void FindNearestMeshBoundary(ProjectionsOptions projectToLandBoundaryOption);
/// @param[in] projectionOption The option describing the projection to the land boundary.
void FindNearestMeshBoundary(ProjectionsOptions projectionOption);

/// @brief Snap the mesh nodes to land boundaries (snap_to_landboundary)
[[nodiscard]] std::unique_ptr<UndoAction> SnapMeshToLandBoundaries() const;
Expand All @@ -89,7 +89,9 @@ namespace meshkernel
/// @return The number of land boundary nodes.
auto GetNumNodes() const { return m_landBoundary.GetNumNodes(); }

std::vector<UInt> m_meshNodesLandBoundarySegments; ///< Mesh nodes to land boundary mapping (lanseg_map)
/// @brief Gets the land boundary segment index
/// @param[in] node The mesh node index
const UInt& LandBoundarySegment(UInt node) const { return m_meshNodesLandBoundarySegments[node]; }

private:
/// @brief Build an additional boundary for not assigned nodes (connect_boundary_paths)
Expand Down Expand Up @@ -183,6 +185,7 @@ namespace meshkernel
Mesh2D& m_mesh; ///< A reference to mesh
const Polygons m_polygons; ///< A copy of the polygons
LandBoundary m_landBoundary; ///< The nodes on the land boundary
std::vector<UInt> m_meshNodesLandBoundarySegments; ///< Mesh nodes to land boundary mapping (lanseg_map)
std::vector<Point> m_polygonNodesCache; ///< Array of points (e.g. points of a face)
std::vector<std::pair<UInt, UInt>> m_validLandBoundaries; ///< Start and end indices of valid land boundaries (lanseg_startend)
std::vector<UInt> m_nodeFaceIndices; ///< For each node, the indices of the faces including them
Expand Down
12 changes: 6 additions & 6 deletions libs/MeshKernel/src/FlipEdges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ int FlipEdges::ComputeTopologyFunctional(UInt edge,

if (m_projectToLandBoundary && m_landBoundaries.GetNumNodes() > 0)
{
if (m_landBoundaries.m_meshNodesLandBoundarySegments[firstNode] != constants::missing::uintValue && m_landBoundaries.m_meshNodesLandBoundarySegments[secondNode] != constants::missing::uintValue)
if (m_landBoundaries.LandBoundarySegment(firstNode) != constants::missing::uintValue && m_landBoundaries.LandBoundarySegment(secondNode) != constants::missing::uintValue)
{
// Edge is associated with a land boundary -> keep the edge
return largeTopologyFunctionalValue;
Expand Down Expand Up @@ -390,7 +390,7 @@ int FlipEdges::ComputeTopologyFunctional(UInt edge,

int FlipEdges::DifferenceFromOptimum(UInt nodeIndex, UInt firstNode, UInt secondNode) const
{
if (m_landBoundaries.m_meshNodesLandBoundarySegments[nodeIndex] == constants::missing::uintValue)
if (m_landBoundaries.LandBoundarySegment(nodeIndex) == constants::missing::uintValue)
{
return static_cast<int>(m_mesh.m_nodesNumEdges[nodeIndex]) - static_cast<int>(OptimalNumberOfConnectedNodes(nodeIndex));
}
Expand Down Expand Up @@ -448,7 +448,7 @@ int FlipEdges::DifferenceFromOptimum(UInt nodeIndex, UInt firstNode, UInt second
auto otherNode = OtherNodeOfEdge(m_mesh.GetEdge(edgeIndex), nodeIndex);

UInt num = 1;
while (m_landBoundaries.m_meshNodesLandBoundarySegments[otherNode] == constants::missing::uintValue &&
while (m_landBoundaries.LandBoundarySegment(otherNode) == constants::missing::uintValue &&
!m_mesh.IsEdgeOnBoundary(edgeIndex) &&
currentEdgeIndexInNodeEdges != edgeIndexConnectingSecondNode)
{
Expand All @@ -459,7 +459,7 @@ int FlipEdges::DifferenceFromOptimum(UInt nodeIndex, UInt firstNode, UInt second
}

UInt firstEdgeInPathIndex = constants::missing::uintValue;
if (m_landBoundaries.m_meshNodesLandBoundarySegments[otherNode] != constants::missing::uintValue ||
if (m_landBoundaries.LandBoundarySegment(otherNode) != constants::missing::uintValue ||
m_mesh.IsEdgeOnBoundary(edgeIndex))
{
firstEdgeInPathIndex = edgeIndex;
Expand All @@ -473,7 +473,7 @@ int FlipEdges::DifferenceFromOptimum(UInt nodeIndex, UInt firstNode, UInt second
edgeIndex = m_mesh.m_nodesEdges[nodeIndex][currentEdgeIndexInNodeEdges];
otherNode = OtherNodeOfEdge(m_mesh.GetEdge(edgeIndex), nodeIndex);
num = num + 1;
while (m_landBoundaries.m_meshNodesLandBoundarySegments[otherNode] == constants::missing::uintValue &&
while (m_landBoundaries.LandBoundarySegment(otherNode) == constants::missing::uintValue &&
!m_mesh.IsEdgeOnBoundary(edgeIndex) &&
currentEdgeIndexInNodeEdges != edgeIndexConnectingFirstNode &&
edgeIndex != firstEdgeInPathIndex)
Expand All @@ -488,7 +488,7 @@ int FlipEdges::DifferenceFromOptimum(UInt nodeIndex, UInt firstNode, UInt second
}
}

if ((m_landBoundaries.m_meshNodesLandBoundarySegments[otherNode] != constants::missing::uintValue ||
if ((m_landBoundaries.LandBoundarySegment(otherNode) != constants::missing::uintValue ||
m_mesh.IsEdgeOnBoundary(edgeIndex)) &&
edgeIndex != firstEdgeInPathIndex)
{
Expand Down
8 changes: 4 additions & 4 deletions libs/MeshKernel/src/LandBoundaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ void LandBoundaries::Administrate()
}
}

void LandBoundaries::FindNearestMeshBoundary(ProjectionsOptions projectToLandBoundaryOption)
void LandBoundaries::FindNearestMeshBoundary(ProjectionsOptions projectionOption)
{
if (m_landBoundary.IsEmpty())
{
return;
}

if (projectToLandBoundaryOption != ProjectionsOptions::OuterMeshBoundaryToLandBoundaries &&
projectToLandBoundaryOption != ProjectionsOptions::InnerAndOuterMeshBoundaryToLandboundaries)
if (projectionOption != ProjectionsOptions::OuterMeshBoundaryToLandBoundaries &&
projectionOption != ProjectionsOptions::InnerAndOuterMeshBoundariesToLandboundaries)
{
return;
}
Expand All @@ -126,7 +126,7 @@ void LandBoundaries::FindNearestMeshBoundary(ProjectionsOptions projectToLandBou
{
const auto [_, numRejectedPaths] = MakePath(landBoundarySegment);

if (numRejectedPaths > 0 && projectToLandBoundaryOption == ProjectionsOptions::InnerAndOuterMeshBoundaryToLandboundaries)
if (numRejectedPaths > 0 && projectionOption == ProjectionsOptions::InnerAndOuterMeshBoundariesToLandboundaries)
{
m_findOnlyOuterMeshBoundary = false;
MakePath(landBoundarySegment);
Expand Down
2 changes: 1 addition & 1 deletion libs/MeshKernel/src/OrthogonalizationAndSmoothing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ std::unique_ptr<meshkernel::UndoAction> OrthogonalizationAndSmoothing::Initializ

//
if (m_projectOptions == LandBoundaries::ProjectionsOptions::OuterMeshBoundaryToLandBoundaries ||
m_projectOptions == LandBoundaries::ProjectionsOptions::InnerAndOuterMeshBoundaryToLandboundaries)
m_projectOptions == LandBoundaries::ProjectionsOptions::InnerAndOuterMeshBoundariesToLandboundaries)
{
m_landBoundaries->FindNearestMeshBoundary(m_projectOptions);
}
Expand Down
92 changes: 46 additions & 46 deletions libs/MeshKernel/tests/src/LandBoundaryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ TEST(LandBoundaries, OneLandBoundary)
landboundaries.FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions ::OuterMeshBoundaryToLandBoundaries);

// Checks
EXPECT_EQ(1, landboundaries.m_meshNodesLandBoundarySegments[0]);
EXPECT_EQ(0, landboundaries.m_meshNodesLandBoundarySegments[1]);
EXPECT_EQ(2, landboundaries.m_meshNodesLandBoundarySegments[2]);
EXPECT_EQ(2, landboundaries.m_meshNodesLandBoundarySegments[3]);
EXPECT_EQ(3, landboundaries.m_meshNodesLandBoundarySegments[4]);
EXPECT_EQ(1, landboundaries.m_meshNodesLandBoundarySegments[5]);
EXPECT_EQ(1, landboundaries.m_meshNodesLandBoundarySegments[6]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.m_meshNodesLandBoundarySegments[7]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.m_meshNodesLandBoundarySegments[8]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.m_meshNodesLandBoundarySegments[9]);
EXPECT_EQ(1, landboundaries.LandBoundarySegment(0));
EXPECT_EQ(0, landboundaries.LandBoundarySegment(1));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(2));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(3));
EXPECT_EQ(3, landboundaries.LandBoundarySegment(4));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(5));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(6));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(7));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(8));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(9));
}

TEST(LandBoundaries, TwoLandBoundaries)
Expand All @@ -61,20 +61,20 @@ TEST(LandBoundaries, TwoLandBoundaries)
auto polygons = meshkernel::Polygons();

// Execute
auto landboundaries = std::make_shared<meshkernel::LandBoundaries>(landBoundaryPolygon, *mesh, polygons);
landboundaries->FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions::OuterMeshBoundaryToLandBoundaries);
auto landboundaries = meshkernel::LandBoundaries(landBoundaryPolygon, *mesh, polygons);
landboundaries.FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions::OuterMeshBoundaryToLandBoundaries);

// Checks
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[0]);
EXPECT_EQ(1, landboundaries->m_meshNodesLandBoundarySegments[1]);
EXPECT_EQ(1, landboundaries->m_meshNodesLandBoundarySegments[2]);
EXPECT_EQ(3, landboundaries->m_meshNodesLandBoundarySegments[3]);
EXPECT_EQ(3, landboundaries->m_meshNodesLandBoundarySegments[4]);
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[5]);
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[6]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[7]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[8]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[9]);
EXPECT_EQ(2, landboundaries.LandBoundarySegment(0));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(1));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(2));
EXPECT_EQ(3, landboundaries.LandBoundarySegment(3));
EXPECT_EQ(3, landboundaries.LandBoundarySegment(4));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(5));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(6));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(7));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(8));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(9));
}

TEST(LandBoundaries, OneCrossingLandBoundary)
Expand All @@ -93,20 +93,20 @@ TEST(LandBoundaries, OneCrossingLandBoundary)
auto polygons = meshkernel::Polygons();

// Execute
auto landboundaries = std::make_shared<meshkernel::LandBoundaries>(landBoundaryPolygon, *mesh, polygons);
landboundaries->FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions ::OuterMeshBoundaryToLandBoundaries);
auto landboundaries = meshkernel::LandBoundaries(landBoundaryPolygon, *mesh, polygons);
landboundaries.FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions ::OuterMeshBoundaryToLandBoundaries);

// Checks
EXPECT_EQ(0, landboundaries->m_meshNodesLandBoundarySegments[0]);
EXPECT_EQ(0, landboundaries->m_meshNodesLandBoundarySegments[1]);
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[2]);
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[3]);
EXPECT_EQ(1, landboundaries->m_meshNodesLandBoundarySegments[4]);
EXPECT_EQ(1, landboundaries->m_meshNodesLandBoundarySegments[5]);
EXPECT_EQ(1, landboundaries->m_meshNodesLandBoundarySegments[6]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[7]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[8]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[9]);
EXPECT_EQ(0, landboundaries.LandBoundarySegment(0));
EXPECT_EQ(0, landboundaries.LandBoundarySegment(1));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(2));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(3));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(4));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(5));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(6));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(7));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(8));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(9));
}

TEST(LandBoundaries, TwoCrossingLandBoundary)
Expand All @@ -129,20 +129,20 @@ TEST(LandBoundaries, TwoCrossingLandBoundary)
auto polygons = meshkernel::Polygons();

// Execute
auto landboundaries = std::make_shared<meshkernel::LandBoundaries>(landBoundaryPolygon, *mesh, polygons);
landboundaries->FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions ::OuterMeshBoundaryToLandBoundaries);
auto landboundaries = meshkernel::LandBoundaries(landBoundaryPolygon, *mesh, polygons);
landboundaries.FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions ::OuterMeshBoundaryToLandBoundaries);

// Checks
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[0]);
EXPECT_EQ(0, landboundaries->m_meshNodesLandBoundarySegments[1]);
EXPECT_EQ(1, landboundaries->m_meshNodesLandBoundarySegments[2]);
EXPECT_EQ(3, landboundaries->m_meshNodesLandBoundarySegments[3]);
EXPECT_EQ(3, landboundaries->m_meshNodesLandBoundarySegments[4]);
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[5]);
EXPECT_EQ(2, landboundaries->m_meshNodesLandBoundarySegments[6]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[7]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[8]);
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries->m_meshNodesLandBoundarySegments[9]);
EXPECT_EQ(2, landboundaries.LandBoundarySegment(0));
EXPECT_EQ(0, landboundaries.LandBoundarySegment(1));
EXPECT_EQ(1, landboundaries.LandBoundarySegment(2));
EXPECT_EQ(3, landboundaries.LandBoundarySegment(3));
EXPECT_EQ(3, landboundaries.LandBoundarySegment(4));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(5));
EXPECT_EQ(2, landboundaries.LandBoundarySegment(6));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(7));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(8));
EXPECT_EQ(meshkernel::constants::missing::uintValue, landboundaries.LandBoundarySegment(9));
}

TEST(LandBoundaries, LandBoundaryConstructorTestSinglePolyline)
Expand Down
2 changes: 1 addition & 1 deletion libs/MeshKernelApi/src/MeshKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ namespace meshkernelapi
// Construct all dependencies
const auto polygon = meshkernel::Polygons(polygonNodes, meshKernelState[meshKernelId].m_projection);
auto landBoundary = meshkernel::LandBoundaries(landBoundariesPoints, *meshKernelState[meshKernelId].m_mesh2d, polygon);
landBoundary.FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions::InnerAndOuterMeshBoundaryToLandboundaries);
landBoundary.FindNearestMeshBoundary(meshkernel::LandBoundaries::ProjectionsOptions::InnerAndOuterMeshBoundariesToLandboundaries);

// Execute algorithm
meshKernelUndoStack.Add(landBoundary.SnapMeshToLandBoundaries());
Expand Down

0 comments on commit 0389665

Please sign in to comment.