Skip to content

Commit

Permalink
Create a MeshKernelAPI for generating curvilinear boundary polygons (#72
Browse files Browse the repository at this point in the history
 | GRIDEDIT 1317)
  • Loading branch information
lucacarniato authored Jul 24, 2024
1 parent fe9438c commit 67614ea
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Deltares.MeshKernel" Version="4.3.0.3304" />
<PackageVersion Include="Deltares.MeshKernel" Version="4.3.0.3323-dev" />
<PackageVersion Include="DHYDRO.SharedConfigurations" Version="1.0.0.27" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageVersion Include="NetTopologySuite" Version="1.15.0" />
Expand Down
12 changes: 12 additions & 0 deletions src/MeshKernelNET/Api/IMeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ int CurvilinearComputeTransfiniteFromTriangle(int meshKernelId,
/// <returns>Error code</returns>
int CurvilinearGridGetData(int meshKernelId, out DisposableCurvilinearGrid disposableCurvilinearGrid);

/// <summary>
/// Gets the boundary polygon of a curvilinear grid, nodes with invalid coordinates are excluded
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="lowerLeftN">The n index of the lower left corner</param>
/// <param name="lowerLeftM">The m index of the lower left corner</param>
/// <param name="upperRightN">The n index of the upper right corner</param>
/// <param name="upperRightM">The m index of the upper right corner</param>
/// <param name="boundaryPolygons">The geometry list containing the boundary polygons. If multiple polygons are present, a separator is used</param>
/// <returns>Error code</returns>
int CurvilinearGetBoundariesAsPolygons(int meshKernelId, int lowerLeftN, int lowerLeftM, int upperRightN, int upperRightM, out DisposableGeometryList boundaryPolygons);

/// <summary>
/// Gets the index of the closest curvilinear edge
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions src/MeshKernelNET/Api/MeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,47 @@ public int CurvilinearGridGetData(int meshKernelId, out DisposableCurvilinearGri
return exitCode;
}

public int CurvilinearGetBoundariesAsPolygons(int meshKernelId, int lowerLeftN, int lowerLeftM, int upperRightN, int upperRightM, out DisposableGeometryList boundaryPolygons)
{
int numberOfPolygonNodes = 0;
int exitCode = MeshKernelDll.CurvilinearCountGetBoundariesAsPolygons(meshKernelId,
lowerLeftN,
lowerLeftM,
upperRightN,
upperRightM,
ref numberOfPolygonNodes);
if (exitCode != 0)
{
boundaryPolygons = new DisposableGeometryList();
return exitCode;
}

boundaryPolygons = new DisposableGeometryList();
boundaryPolygons.XCoordinates = new double[numberOfPolygonNodes];
boundaryPolygons.YCoordinates = new double[numberOfPolygonNodes];
double geometrySeparator = GetSeparator();
boundaryPolygons.GeometrySeparator = geometrySeparator;
boundaryPolygons.NumberOfCoordinates = numberOfPolygonNodes;

var geometryListNative = boundaryPolygons.CreateNativeObject();
exitCode = MeshKernelDll.CurvilinearGetBoundariesAsPolygons(meshKernelId,
lowerLeftN,
lowerLeftM,
upperRightN,
upperRightM,
ref geometryListNative);

if (exitCode != 0)
{
boundaryPolygons = new DisposableGeometryList();
return exitCode;
}

boundaryPolygons.XCoordinates = geometryListNative.xCoordinates.CreateValueArray<double>(numberOfPolygonNodes);
boundaryPolygons.YCoordinates = geometryListNative.yCoordinates.CreateValueArray<double>(numberOfPolygonNodes);
return exitCode;
}

public int CurvilinearGetEdgeLocationIndex(int meshKernelId, double xCoordinate, double yCoordinate, BoundingBox boundingBox, ref int locationIndex)
{
int locationType = -1;
Expand Down
26 changes: 26 additions & 0 deletions src/MeshKernelNET/Native/MeshKernelDll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,32 @@ public static extern int CurvilinearDerefine([In] int meshKernelId,
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_curvilinear_get_data", CallingConvention = CallingConvention.Cdecl)]
internal static extern int CurvilinearGetData([In] int meshKernelId, [In][Out] ref CurvilinearGridNative curvilinearGridNative);

/// <summary>
/// Counts the number of nodes in curvilinear grid boundary polygons.
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="lowerLeftN">The n index of the lower left corner</param>
/// <param name="lowerLeftM">The m index of the lower left corner</param>
/// <param name="upperRightN">The n index of the upper right corner</param>
/// <param name="upperRightM">The m index of the upper right corner</param>
/// <param name="numberOfPolygonNodes">The number of polygon nodes</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_curvilinear_count_boundaries_as_polygons", CallingConvention = CallingConvention.Cdecl)]
internal static extern int CurvilinearCountGetBoundariesAsPolygons([In] int meshKernelId, [In] int lowerLeftN, [In] int lowerLeftM, [In] int upperRightN, [In] int upperRightM, [In][Out] ref int numberOfPolygonNodes);

/// <summary>
/// Gets the boundary polygon of a curvilinear grid, nodes with invalid coordinates are excluded.
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="lowerLeftN">The n index of the lower left corner</param>
/// <param name="lowerLeftM">The m index of the lower left corner</param>
/// <param name="upperRightN">The n index of the upper right corner</param>
/// <param name="upperRightM">The m index of the upper right corner</param>
/// <param name="boundaryPolygons">The geometry list containing the boundary polygons. If multiple polygons are present, a separator is used</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_curvilinear_get_boundaries_as_polygons", CallingConvention = CallingConvention.Cdecl)]
internal static extern int CurvilinearGetBoundariesAsPolygons([In] int meshKernelId, [In] int lowerLeftN, [In] int lowerLeftM, [In] int upperRightN, [In] int upperRightM, [In][Out] ref GeometryListNative boundaryPolygons);

/// <summary>
/// Gets the curvilinear grid dimensions as a CurvilinearGrid struct (converted as set of edges and nodes).
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions test/MeshKernelNETTest/Api/MeshKernelCurvilinearTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,48 @@ public void CurvilinearSetAndCovertThroughApi()
}
}
}

[Test]
public void CurvilinearGetBoundariesAsPolygons()
{
// Setup
using (var api = new MeshKernelApi())
{
var id = 0;
try
{
id = api.AllocateState(0);

var makeGridParameters = MakeGridParameters.CreateDefault();

makeGridParameters.GridType = 0;
makeGridParameters.NumberOfColumns = 3;
makeGridParameters.NumberOfRows = 3;
makeGridParameters.GridAngle = 0.0;
makeGridParameters.OriginXCoordinate = 0.0;
makeGridParameters.OriginYCoordinate = 0.0;
makeGridParameters.XGridBlockSize = 1.0;
makeGridParameters.YGridBlockSize = 1.0;
makeGridParameters.UpperRightCornerXCoordinate = 0.0;
makeGridParameters.UpperRightCornerYCoordinate = 0.0;

Assert.AreEqual(0, api.CurvilinearComputeRectangularGrid(id, makeGridParameters));
Assert.AreEqual(0, api.CurvilinearGetBoundariesAsPolygons(id, 0, 0, 3, 3, out DisposableGeometryList boundaryPolygons));

var expectedPolygonXCoordinates = new[] { 0, 1, 2, 3, 3, 3, 3, 2, 1, 0, 0, 0, 0 };
var expectedPolygonYCoordinates = new[] { 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 2, 1, 0 };

Assert.That(boundaryPolygons.XCoordinates, Is.EquivalentTo(expectedPolygonXCoordinates));
Assert.That(boundaryPolygons.YCoordinates, Is.EquivalentTo(expectedPolygonYCoordinates));

boundaryPolygons.Dispose();
}
finally
{
api.DeallocateState(id);
}
}
}
}

[TestFixture]
Expand Down

0 comments on commit 67614ea

Please sign in to comment.