From 4012c047d9a203e5bd5e8f35ff5405df2de8d252 Mon Sep 17 00:00:00 2001 From: Luca Carniato Date: Wed, 31 Jul 2024 14:59:17 +0200 Subject: [PATCH] aDD PolygonSnapToLandBoundaryThroughAPI --- src/MeshKernelNET/Api/IMeshKernelApi.cs | 22 ++++- src/MeshKernelNET/Api/MeshKernelApi.cs | 11 +++ src/MeshKernelNET/Native/MeshKernelDll.cs | 16 ++++ test/MeshKernelNETTest/Api/MeshKernelTest.cs | 87 ++++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) diff --git a/src/MeshKernelNET/Api/IMeshKernelApi.cs b/src/MeshKernelNET/Api/IMeshKernelApi.cs index c77b0a5..cf343ea 100644 --- a/src/MeshKernelNET/Api/IMeshKernelApi.cs +++ b/src/MeshKernelNET/Api/IMeshKernelApi.cs @@ -1631,8 +1631,26 @@ int PolygonEquidistantRefine(int meshKernelId, in DisposableGeometryList disposa /// The index of the second vertex /// The refined polygon /// Error code - int PolygonLinearRefine(int meshKernelId, in DisposableGeometryList disposableGeometryListIn, int firstIndex, - int secondIndex, ref DisposableGeometryList disposableGeometryListOut); + int PolygonLinearRefine(int meshKernelId, + in DisposableGeometryList disposableGeometryListIn, + int firstIndex, + int secondIndex, + ref DisposableGeometryList disposableGeometryListOut); + + /// + /// Snaps part of a polygon to a land boundary + /// + /// Id of the mesh stateThe land boundaries + /// The input polygon + /// The index of the first vertex + /// The index of the second vertex + /// Error code + int PolygonSnapToLandBoundary(int meshKernelId, + in DisposableGeometryList landboundaries, + ref DisposableGeometryList polygon, + int firstIndex, + int secondIndex); /// /// Redo editing action diff --git a/src/MeshKernelNET/Api/MeshKernelApi.cs b/src/MeshKernelNET/Api/MeshKernelApi.cs index 1efb389..e981f84 100644 --- a/src/MeshKernelNET/Api/MeshKernelApi.cs +++ b/src/MeshKernelNET/Api/MeshKernelApi.cs @@ -1514,6 +1514,17 @@ public int PolygonLinearRefine(int meshKernelId, in DisposableGeometryList dispo return MeshKernelDll.PolygonLinearRefine(meshKernelId, ref geometryListNativeIn, firstIndex, secondIndex, ref geometryListNativeOut); } + public int PolygonSnapToLandBoundary(int meshKernelId, + in DisposableGeometryList landboundaries, + ref DisposableGeometryList polygon, + int firstIndex, + int secondIndex) + { + GeometryListNative landboundariesNative = landboundaries.CreateNativeObject(); + GeometryListNative polygonNative = polygon.CreateNativeObject(); // Create an instance for the out parameter + return MeshKernelDll.PolygonSnapToLandBoundary(meshKernelId, ref landboundariesNative, ref polygonNative, firstIndex, secondIndex); + } + /// public int RedoState(ref bool redone) { diff --git a/src/MeshKernelNET/Native/MeshKernelDll.cs b/src/MeshKernelNET/Native/MeshKernelDll.cs index bf58937..d5e113f 100644 --- a/src/MeshKernelNET/Native/MeshKernelDll.cs +++ b/src/MeshKernelNET/Native/MeshKernelDll.cs @@ -1880,6 +1880,22 @@ internal static extern int PolygonCountOffset([In] int meshKernelId, [DllImport(MeshKernelDllName, EntryPoint = "mkernel_polygon_linear_refine", CallingConvention = CallingConvention.Cdecl)] internal static extern int PolygonLinearRefine([In] int meshKernelId, [In] ref GeometryListNative geometryListIn, [In] int firstIndex, [In] int secondIndex, [In][Out] ref GeometryListNative geometryListOut); + /// + /// Snaps part of a polygon to a land boundary + /// + /// Id of the mesh stateThe land boundaries + /// The input polygon + /// The index of the first vertex + /// The index of the second vertex + /// Error code + [DllImport(MeshKernelDllName, EntryPoint = "mkernel_polygon_snap_to_landboundary", CallingConvention = CallingConvention.Cdecl)] + internal static extern int PolygonSnapToLandBoundary([In] int meshKernelId, + [In] ref GeometryListNative landboundaries, + [In][Out] ref GeometryListNative polygon, + [In] int firstIndex, + [In] int secondIndex); + /// /// Redo editing action /// diff --git a/test/MeshKernelNETTest/Api/MeshKernelTest.cs b/test/MeshKernelNETTest/Api/MeshKernelTest.cs index 8ebbe11..396d9ba 100644 --- a/test/MeshKernelNETTest/Api/MeshKernelTest.cs +++ b/test/MeshKernelNETTest/Api/MeshKernelTest.cs @@ -704,6 +704,93 @@ public void PolygonLinearRefineThroughAPI() } } + [Test] + public void PolygonSnapToLandBoundaryThroughAPI() + { + // Setup + using (var api = new MeshKernelApi()) + using (DisposableMesh2D mesh = CreateMesh2D(11, 11, 100, 100)) + using (var landboundaries = new DisposableGeometryList()) + { + var id = 0; + var polygon = new DisposableGeometryList(); + try + { + id = api.AllocateState(0); + + double geometrySeparator = api.GetSeparator(); + landboundaries.GeometrySeparator = geometrySeparator; + landboundaries.NumberOfCoordinates = 4; + landboundaries.XCoordinates = new[] { 139.251465, 527.753906, 580.254211, 194.001801 }; + landboundaries.YCoordinates = new[] { 497.630615, 499.880676, 265.878296, 212.627762 }; + landboundaries.NumberOfCoordinates = landboundaries.XCoordinates.Length; + + polygon.XCoordinates = new[] { 170.001648, 263.002228, 344.002747, + 458.753448, 515.753845, 524.753906, + 510.503754, 557.754089, 545.004028, + 446.003387, 340.252716, 242.752106, + 170.001648 }; + polygon.YCoordinates = new[] { 472.880371, 472.880371, 475.130432, + 482.630493, 487.130554, 434.630005, + 367.129333, 297.378601, 270.378357, + 259.128235, 244.128067, 226.877884, + 472.880371 }; + polygon.NumberOfCoordinates = polygon.XCoordinates.Length; + + + var firstIndex = 0; + var secondIndex = 2; + int numberOfPolygonVertices = -1; + var result = api.PolygonSnapToLandBoundary(id, + landboundaries, + ref polygon, + firstIndex, + secondIndex); + Assert.That(result, Is.EqualTo(0)); + + + const double tolerance = 1.0e-3; + var expectedXCoordinates = new[] { 169.85727722422831, + 262.854737816309, + 343.86557098779792, + 458.753448, + 515.753845, + 524.753906, + 510.503754, + 557.754089, + 545.004028, + 446.003387, + 340.252716, + 242.752106, + 170.001648 }; + + var expectedYCoordinates = new[] + { 497.80787243056284, + 498.34647897995455, + 498.81566346133775, + 482.630493, + 487.130554, + 434.630005, + 367.129333, + 297.378601, + 270.378357, + 259.128235, + 244.128067, + 226.877884, + 472.880371}; + + Assert.That(polygon.XCoordinates, Is.EqualTo(expectedXCoordinates).Within(tolerance)); + Assert.That(polygon.YCoordinates, Is.EqualTo(expectedYCoordinates).Within(tolerance)); + + } + finally + { + api.DeallocateState(id); + polygon.Dispose(); + } + } + } + [Test] public void Mesh2dRefineBasedOnSamplesThroughAPI() {