Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRIDEDIT-1020 Snap mesh to landboundary #76

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Deltares.MeshKernel" Version="4.3.0.3353" />
<ItemGroup>
<PackageVersion Include="Deltares.MeshKernel" Version="4.3.0.3355-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
9 changes: 9 additions & 0 deletions src/MeshKernelNET/Api/IMeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public interface IMeshKernelApi : IDisposable
/// <returns>Error code</returns>
int ContactsGetData(int meshKernelId, out DisposableContacts disposableContacts);

/// <summary>
/// Snaps a mesh to a land boundary
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="selectingPolygon">The polygon where to perform the snapping</param>
/// <param name="landBoundaries"> The input land boundaries</param>
/// <returns>Error code</returns>
int Mesh2dSnapToLandBoundary(int meshKernelId, in DisposableGeometryList selectingPolygon, in DisposableGeometryList landBoundaries);

/// <summary>
/// Make curvilinear grid from splines
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/MeshKernelNET/Api/MeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public int ContactsGetData(int meshKernelId, out DisposableContacts disposableCo
return exitCode;
}

public int Mesh2dSnapToLandBoundary(int meshKernelId, in DisposableGeometryList selectingPolygon, in DisposableGeometryList landBoundaries)
{
var selectingPolygonNative = selectingPolygon.CreateNativeObject();
var landBoundariesNative = landBoundaries.CreateNativeObject();
return MeshKernelDll.Mesh2dSnapToLandBoundary(meshKernelId, ref selectingPolygonNative, ref landBoundariesNative);
}

public int CurvilinearComputeTransfiniteFromSplines(int meshKernelId,
in DisposableGeometryList disposableGeometryListIn,
in CurvilinearParameters curvilinearParameters)
Expand Down
10 changes: 10 additions & 0 deletions src/MeshKernelNET/Native/MeshKernelDll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,16 @@ internal static extern int Mesh2dTranslate([In] int meshKernelId,
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_mesh2d_set", CallingConvention = CallingConvention.Cdecl)]
internal static extern int Mesh2dSet([In] int meshKernelId, [In] ref Mesh2DNative mesh2DNative);

/// <summary>
/// Snaps a mesh to a land boundary
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="selectingPolygon">The polygon where to perform the snapping</param>
/// <param name="landBoundaries"> The input land boundaries</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_mesh2d_snap_to_landboundary", CallingConvention = CallingConvention.Cdecl)]
internal static extern int Mesh2dSnapToLandBoundary([In] int meshKernelId, [In] ref GeometryListNative selectingPolygon, [In] ref GeometryListNative landBoundaries);

/// <summary>
/// Get the double value used in the back-end library as separator and missing value
/// </summary>
Expand Down
148 changes: 114 additions & 34 deletions test/MeshKernelNETTest/Api/MeshKernelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3120,40 +3120,120 @@ public void CurvilinearGetNodeLocationIndexThroughApi(double x, double y, int ex
}
}

[Test]
public void Mesh2dConvertCurvilinearThroughApi()
{
// Setup
using (DisposableMesh2D mesh = CreateMesh2D(10, 10, 10, 10))
using (var api = new MeshKernelApi())
{
var id = 0;
var curvilinearGrid = new DisposableCurvilinearGrid();
var meshOut = new DisposableMesh2D();
try
{
id = api.AllocateState(0);

Assert.AreEqual(0, api.Mesh2dSet(id, mesh));


api.Mesh2dConvertCurvilinear(id, 5, 5);

// Get curvilinear grid data after conversion
Assert.AreEqual(0, api.CurvilinearGridGetData(id, out curvilinearGrid));
Assert.AreEqual((10, 10), (curvilinearGrid.NumM, curvilinearGrid.NumN));

// Get mesh data after conversion
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshOut));
Assert.AreEqual(0, meshOut.NumNodes);
}
finally
{
api.DeallocateState(id);
curvilinearGrid?.Dispose();
meshOut?.Dispose();
}
}
[Test]
public void Mesh2dConvertCurvilinearThroughApi()
{
// Setup
using (DisposableMesh2D mesh = CreateMesh2D(10, 10, 10, 10))
using (var api = new MeshKernelApi())
{
var id = 0;
var curvilinearGrid = new DisposableCurvilinearGrid();
var meshOut = new DisposableMesh2D();
try
{
id = api.AllocateState(0);

Assert.AreEqual(0, api.Mesh2dSet(id, mesh));


api.Mesh2dConvertCurvilinear(id, 5, 5);

// Get curvilinear grid data after conversion
Assert.AreEqual(0, api.CurvilinearGridGetData(id, out curvilinearGrid));
Assert.AreEqual((10, 10), (curvilinearGrid.NumM, curvilinearGrid.NumN));

// Get mesh data after conversion
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshOut));
Assert.AreEqual(0, meshOut.NumNodes);
}
finally
{
api.DeallocateState(id);
curvilinearGrid?.Dispose();
meshOut?.Dispose();
}
}
}

[Test]
public void Mesh2dSnapToAnEmptyLandBoundaryThroughApi()
{
// Setup
using (DisposableMesh2D mesh = CreateMesh2D(10, 10, 10, 10))
using (var selectingPolygon = new DisposableGeometryList())
using (var landBoundaries = new DisposableGeometryList())
using (var api = new MeshKernelApi())
{
var id = 0;
var meshOut = new DisposableMesh2D();
try
{
id = api.AllocateState(0);

Assert.AreEqual(0, api.Mesh2dSet(id, mesh));

api.Mesh2dSnapToLandBoundary(id, in selectingPolygon, in landBoundaries);

// Get mesh data after conversion
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshOut));
Assert.AreEqual(100, meshOut.NumNodes);
Assert.AreEqual(0.0, meshOut.NodeX[0]);
Assert.AreEqual(0.0, meshOut.NodeX[1]);
Assert.AreEqual(0.0, meshOut.NodeY[0]);
Assert.AreEqual(10.0, meshOut.NodeY[1]);
}
finally
{
api.DeallocateState(id);
meshOut?.Dispose();
}
}
}

[Test]
public void Mesh2dSnapToLandBoundaryThroughApi()
{
// Setup
using (DisposableMesh2D mesh = CreateMesh2D(10, 10, 10, 10))
using (var selectingPolygon = new DisposableGeometryList())
using (var landBoundaries = new DisposableGeometryList())
using (var api = new MeshKernelApi())
{
var id = 0;
var meshOut = new DisposableMesh2D();
try
{
// prepare
id = api.AllocateState(0);

Assert.AreEqual(0, api.Mesh2dSet(id, mesh));

selectingPolygon.XCoordinates = new[] { -10.0, 11.0, 15.0, -10.0, -10.0 };
selectingPolygon.YCoordinates = new[] { -10.0, -10.0, 15.0, 15.0, -10.0 };
selectingPolygon.NumberOfCoordinates = selectingPolygon.XCoordinates.Length;

landBoundaries.XCoordinates = new[] { -1.0, 11.0 };
landBoundaries.YCoordinates = new[] { -1.0, 11.0 };
landBoundaries.NumberOfCoordinates = landBoundaries.XCoordinates.Length;

// execute
api.Mesh2dSnapToLandBoundary(id, in selectingPolygon, in landBoundaries);

// assert
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshOut));
Assert.AreEqual(100, meshOut.NumNodes);
Assert.AreEqual(0.0, meshOut.NodeX[0]);
Assert.AreEqual(0.0, meshOut.NodeX[1]);
Assert.AreEqual(0.0, meshOut.NodeY[0]);
Assert.AreEqual(10.0, meshOut.NodeY[1]);
}
finally
{
api.DeallocateState(id);
meshOut?.Dispose();
}
}
}
}

Expand Down