Skip to content

Commit

Permalink
aDD PolygonSnapToLandBoundaryThroughAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato committed Aug 2, 2024
1 parent b5447d5 commit f488bd2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/MeshKernelNET/Api/IMeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,8 +1631,26 @@ int PolygonEquidistantRefine(int meshKernelId, in DisposableGeometryList disposa
/// <param name="secondIndex">The index of the second vertex</param>
/// <param name="disposableGeometryListOut">The refined polygon</param>
/// <returns>Error code</returns>
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);

/// <summary>
/// Snaps part of a polygon to a land boundary
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param
/// <param name="landboundaries">The land boundaries</param>
/// <param name="polygon">The input polygon</param>
/// <param name="firstIndex">The index of the first vertex</param>
/// <param name="secondIndex">The index of the second vertex</param>
/// <returns>Error code</returns>
int PolygonSnapToLandBoundary(int meshKernelId,
in DisposableGeometryList landboundaries,
ref DisposableGeometryList polygon,
int firstIndex,
int secondIndex);

/// <summary>
/// Redo editing action
Expand Down
11 changes: 11 additions & 0 deletions src/MeshKernelNET/Api/MeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <inheritdoc/>
public int RedoState(ref bool redone)
{
Expand Down
16 changes: 16 additions & 0 deletions src/MeshKernelNET/Native/MeshKernelDll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// Snaps part of a polygon to a land boundary
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param
/// <param name="landboundaries">The land boundaries</param>
/// <param name="polygon">The input polygon</param>
/// <param name="firstIndex">The index of the first vertex</param>
/// <param name="secondIndex">The index of the second vertex</param>
/// <returns>Error code</returns>
[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);

/// <summary>
/// Redo editing action
/// </summary>
Expand Down
87 changes: 87 additions & 0 deletions test/MeshKernelNETTest/Api/MeshKernelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit f488bd2

Please sign in to comment.