Skip to content

Commit

Permalink
GRIDEDIT-768: Add Mesh2dMakeGlobal api function (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato authored Jan 24, 2024
1 parent 68b5ed1 commit 81f4914
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 30 deletions.
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="3.0.2.2796-dev" />
<PackageVersion Include="Deltares.MeshKernel" Version="3.0.2.2814-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 @@ -1077,6 +1077,15 @@ int Mesh2dIntersectionsFromPolygon(int meshKernelId,
ref int[] faceNumEdges,
ref int[] faceEdgeIndex);

/// <summary>
/// Compute the global mesh with a given number of points along the longitude and latitude directions.
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="numLongitudeNodes">The number of points along the longitude</param>
/// <param name="numLatitudeNodes">The number of points along the latitude (half hemisphere)</param>
/// <returns>Error code</returns>
int Mesh2dMakeGlobal(int meshKernelId, int numLongitudeNodes, int numLatitudeNodes);

/// <summary>
/// Make a triangular grid in a polygon
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/MeshKernelNET/Api/MeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,11 @@ public int Mesh2dIntersectionsFromPolygon(int meshKernelId,
return successful;
}

public int Mesh2dMakeGlobal(int meshKernelId, int numLongitudeNodes, int numLatitudeNodes)
{
return MeshKernelDll.Mesh2dMakeGlobal(meshKernelId, numLongitudeNodes, numLatitudeNodes);
}

public int Mesh2dMakeTriangularMeshFromPolygon(int meshKernelId, in DisposableGeometryList disposableGeometryList)
{
GeometryListNative geometryListNative = disposableGeometryList.CreateNativeObject();
Expand Down
12 changes: 11 additions & 1 deletion src/MeshKernelNET/Native/MeshKernelDll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,17 @@ internal static extern int Mesh2dIntersectionsFromPolygon([In] int meshKernelId,
[In][Out] IntPtr segmentIndexes,
[In][Out] IntPtr faceIndexes,
[In][Out] IntPtr faceNumEdges,
[In][Out] IntPtr faceEdgeIndex);
[In][Out] IntPtr faceEdgeIndex);

/// <summary>
/// Compute the global mesh with a given number of points along the longitude and latitude directions.
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <param name="numLongitudeNodes">The number of points along the longitude</param>
/// <param name="numLatitudeNodes">The number of points along the latitude (half hemisphere)</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_mesh2d_make_global", CallingConvention = CallingConvention.Cdecl)]
internal static extern int Mesh2dMakeGlobal(int meshKernelId, int numLongitudeNodes, int numLatitudeNodes);

/// <summary>
/// Make a triangular grid in a polygon
Expand Down
86 changes: 58 additions & 28 deletions test/MeshKernelNETTest/Api/MeshKernelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ public void Mesh2dComputeOrthogonalizationThroughApi()
using (var landBoundaries = new DisposableGeometryList())
{
var id = 0;
var mesh2D = new DisposableMesh2D();
DisposableMesh2D mesh2D = null;
try
{
id = api.AllocateState(0);
Expand All @@ -1688,14 +1688,13 @@ public void Mesh2dComputeOrthogonalizationThroughApi()
orthogonalizationParametersList,
polygon,
landBoundaries));

Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2D));
Assert.AreEqual(16, mesh2D.NumNodes);
}
finally
{
api.DeallocateState(id);
mesh2D.Dispose();
mesh2D?.Dispose();
}
}
}
Expand All @@ -1708,7 +1707,7 @@ public void Mesh2dConvertProjectionThroughApi()
using (var api = new MeshKernelApi())
{
var id = 0;
var meshInitial = new DisposableMesh2D();
DisposableMesh2D meshInitial = null;
var meshFinal = new DisposableMesh2D();
try
{
Expand All @@ -1717,6 +1716,7 @@ public void Mesh2dConvertProjectionThroughApi()
Assert.AreEqual(0, api.Mesh2dSet(id, mesh));

// Get data of initial mesh
meshInitial = new DisposableMesh2D();
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshInitial));
Assert.IsNotNull(meshInitial);

Expand Down Expand Up @@ -1777,7 +1777,7 @@ public void Mesh2dConvertProjectionThroughApi()
finally
{
api.DeallocateState(id);
meshInitial.Dispose();
meshInitial?.Dispose();
meshFinal.Dispose();
}
}
Expand All @@ -1791,14 +1791,15 @@ public void Mesh2dConvertProjectionThroughApiFails()
using (var api = new MeshKernelApi())
{
var id = 0;
var meshInitial = new DisposableMesh2D();
DisposableMesh2D meshInitial = null;
try
{
id = api.AllocateState(0);

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

// Get data of initial mesh
meshInitial = new DisposableMesh2D();
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshInitial));
Assert.IsNotNull(meshInitial);

Expand Down Expand Up @@ -1867,7 +1868,7 @@ public void Mesh2dGetSmallFlowEdgeCentersThroughApi()
using (var api = new MeshKernelApi())
{
var id = 0;
var disposableGeometryList = new DisposableGeometryList();
DisposableGeometryList disposableGeometryList = null;
try
{
// Prepare
Expand All @@ -1877,6 +1878,7 @@ public void Mesh2dGetSmallFlowEdgeCentersThroughApi()
int numSmallFlowEdges = -1;

Assert.AreEqual(0, api.Mesh2dCountSmallFlowEdgeCenters(id, smallFlowEdgesLengthThreshold, ref numSmallFlowEdges));
disposableGeometryList = new DisposableGeometryList();
disposableGeometryList.XCoordinates = new double[numSmallFlowEdges];
disposableGeometryList.YCoordinates = new double[numSmallFlowEdges];
disposableGeometryList.NumberOfCoordinates = numSmallFlowEdges;
Expand All @@ -1889,7 +1891,7 @@ public void Mesh2dGetSmallFlowEdgeCentersThroughApi()
finally
{
api.DeallocateState(id);
disposableGeometryList.Dispose();
disposableGeometryList?.Dispose();
}
}
}
Expand All @@ -1901,7 +1903,7 @@ public void Mesh2dDeleteHangingEdgesThroughApi()
using (var api = new MeshKernelApi())
{
var id = 0;
var mesh2D = new DisposableMesh2D();
DisposableMesh2D mesh2D = null;
try
{
id = api.AllocateState(0);
Expand All @@ -1917,7 +1919,7 @@ public void Mesh2dDeleteHangingEdgesThroughApi()
finally
{
api.DeallocateState(id);
mesh2D.Dispose();
mesh2D?.Dispose();
}
}
}
Expand All @@ -1929,7 +1931,7 @@ public void Mesh2dDeleteSmallFlowEdgesAndSmallTrianglesThroughApi()
using (var api = new MeshKernelApi())
{
var id = 0;
var mesh2D = new DisposableMesh2D();
DisposableMesh2D mesh2D = null;
try
{
// Prepare
Expand All @@ -1947,7 +1949,7 @@ public void Mesh2dDeleteSmallFlowEdgesAndSmallTrianglesThroughApi()
finally
{
api.DeallocateState(id);
mesh2D.Dispose();
mesh2D?.Dispose();
}
}
}
Expand Down Expand Up @@ -1990,7 +1992,7 @@ public void Mesh2dIntersectionsFromPolygonThroughApi()
using (var disposableGeometryList = new DisposableGeometryList())
{
var id = 0;
var mesh2D = new DisposableMesh2D();
DisposableMesh2D mesh2D = null;
try
{
id = api.AllocateState(0);
Expand Down Expand Up @@ -2033,7 +2035,7 @@ public void Mesh2dIntersectionsFromPolygonThroughApi()
finally
{
api.DeallocateState(id);
mesh2D.Dispose();
mesh2D?.Dispose();
}
}
}
Expand All @@ -2045,7 +2047,7 @@ public void Mesh2dMakeRectangularMeshThroughAPI()
using (var api = new MeshKernelApi())
{
var id = 0;
var mesh2d = new DisposableMesh2D();
DisposableMesh2D mesh2d = null;
try
{
id = api.AllocateState(0);
Expand All @@ -2069,7 +2071,7 @@ public void Mesh2dMakeRectangularMeshThroughAPI()
finally
{
api.DeallocateState(id);
mesh2d.Dispose();
mesh2d?.Dispose();
}
}
}
Expand All @@ -2082,7 +2084,6 @@ public void Mesh2dMakeRectangularMeshFromPolygonThroughAPIFails()
using (var polygon = new DisposableGeometryList())
{
var id = 0;
var mesh2d = new DisposableMesh2D();
try
{
id = api.AllocateState(0);
Expand All @@ -2101,7 +2102,6 @@ public void Mesh2dMakeRectangularMeshFromPolygonThroughAPIFails()
finally
{
api.DeallocateState(id);
mesh2d.Dispose();
}
}
}
Expand All @@ -2114,7 +2114,7 @@ public void Mesh2dMakeRectangularMeshFromPolygonThroughAPI()
using (var polygon = new DisposableGeometryList())
{
var id = 0;
var mesh2d = new DisposableMesh2D();
DisposableMesh2D mesh2d = null;
try
{
id = api.AllocateState(0);
Expand All @@ -2138,7 +2138,7 @@ public void Mesh2dMakeRectangularMeshFromPolygonThroughAPI()
finally
{
api.DeallocateState(id);
mesh2d.Dispose();
mesh2d?.Dispose();
}
}
}
Expand All @@ -2149,7 +2149,7 @@ public void Mesh2dMakeRectangularMeshOnExtensionThroughAPI()
using (var api = new MeshKernelApi())
{
var id = 0;
var mesh2D = new DisposableMesh2D();
DisposableMesh2D mesh2D = null;
try
{
// Setup
Expand All @@ -2175,7 +2175,7 @@ public void Mesh2dMakeRectangularMeshOnExtensionThroughAPI()
finally
{
api.DeallocateState(id);
mesh2D.Dispose();
mesh2D?.Dispose();
}
}
}
Expand All @@ -2187,7 +2187,7 @@ public void Mesh2dRefineBasedOnGriddedSamplesThroughAPI()
using (DisposableMesh2D mesh = CreateMesh2D(4, 4, 100, 200))
{
var id = 0;
var meshOut = new DisposableMesh2D();
DisposableMesh2D meshOut = null;
var griddedSamples = new DisposableGriddedSamples();
try
{
Expand Down Expand Up @@ -2227,6 +2227,7 @@ public void Mesh2dRefineBasedOnGriddedSamplesThroughAPI()
griddedSamples,
meshRefinementParameters,
true));
meshOut = new DisposableMesh2D();
Assert.AreEqual(0, api.Mesh2dGetData(id, out meshOut));
// Assert
Assert.NotNull(meshOut);
Expand All @@ -2235,7 +2236,7 @@ public void Mesh2dRefineBasedOnGriddedSamplesThroughAPI()
finally
{
api.DeallocateState(id);
meshOut.Dispose();
meshOut?.Dispose();
griddedSamples.Dispose();
}
}
Expand All @@ -2251,7 +2252,7 @@ public void Mesh2dTriangulationInterpolationThroughAPI()
{
var id = 0;
var results = new DisposableGeometryList();
var mesh2D = new DisposableMesh2D();
DisposableMesh2D mesh2D = null;
try
{
// Setup
Expand All @@ -2260,6 +2261,7 @@ public void Mesh2dTriangulationInterpolationThroughAPI()
int locationType = -1;
Assert.AreEqual(0, api.GetNodesLocationType(ref locationType));
Assert.AreEqual(0, api.Mesh2dSet(id, mesh));

Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2D));
samples.XCoordinates = new[] { 1.0, 2.0, 3.0, 1.0 };
samples.YCoordinates = new[] { 1.0, 3.0, 2.0, 4.0 };
Expand All @@ -2279,7 +2281,7 @@ public void Mesh2dTriangulationInterpolationThroughAPI()
finally
{
api.DeallocateState(id);
mesh2D.Dispose();
mesh2D?.Dispose();
results.Dispose();
}
}
Expand Down Expand Up @@ -2332,7 +2334,7 @@ public void Mesh2dDeleteInsidePolygonAndIntersectedThroughApi()
using (var api = new MeshKernelApi())
{
var id = 0;
var mesh2d = new DisposableMesh2D();
DisposableMesh2D mesh2d = null;
var polygon = new DisposableGeometryList();
try
{
Expand All @@ -2354,7 +2356,35 @@ public void Mesh2dDeleteInsidePolygonAndIntersectedThroughApi()
finally
{
api.DeallocateState(id);
mesh2d.Dispose();
mesh2d?.Dispose();
}
}
}

[Test]
public void Mesh2dMakeGlobaThroughApi()
{
// Generates a mesh in spherical coordinates around the globe

// Setup
using (var api = new MeshKernelApi())
{
var id = 0;
DisposableMesh2D mesh2d = null;
try
{
int projectionType = 1;
id = api.AllocateState(projectionType);
Assert.AreEqual(0, api.Mesh2dMakeGlobal(id, 19,25));

Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2d));
Assert.AreEqual(1233, mesh2d.NumEdges);
Assert.AreEqual(629, mesh2d.NumNodes);
}
finally
{
api.DeallocateState(id);
mesh2d?.Dispose();
}
}
}
Expand Down

0 comments on commit 81f4914

Please sign in to comment.