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

Add RedoState, UndoState and ClearUndoState #56

Merged
merged 7 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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.1.0.3005-dev" />
<PackageVersion Include="Deltares.MeshKernel" Version="4.1.0.3027" />
<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
18 changes: 13 additions & 5 deletions src/MeshKernelNET/Api/DisposableMesh1D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public sealed class DisposableMesh1D : DisposableNativeObject<Mesh1DNative>
private int numNodes;

[ProtoMember(5)]
private readonly int numValidNodes;
private int numValidNodes;

[ProtoMember(6)]
private int numEdges;

[ProtoMember(7)]
private readonly int numValidEdges;
private int numValidEdges;

public DisposableMesh1D()
{
Expand Down Expand Up @@ -70,16 +70,24 @@ public int NumNodes
set { numNodes = value; }
}

public int NumValidNodes => numValidNodes;
public int NumValidNodes
{
get { return numValidNodes; }
set { numValidNodes = value; }
}

public int NumEdges
{
get { return numEdges; }
set { numEdges = value; }
}

public int NumValidEdges => numValidEdges;

public int NumValidEdges
{
get { return numValidEdges; }
set { numValidEdges = value; }
}

protected override void SetNativeObject(ref Mesh1DNative nativeObject)
{
nativeObject.edge_nodes = GetPinnedObjectPointer(EdgeNodes);
Expand Down
27 changes: 20 additions & 7 deletions src/MeshKernelNET/Api/DisposableMesh2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,30 @@ public sealed class DisposableMesh2D : DisposableNativeObject<Mesh2DNative>, IRe
private int numNodes;

[ProtoMember(13)]
private readonly int numValidNodes;
private int numValidNodes;

[ProtoMember(14)]
private int numEdges;

[ProtoMember(15)]
private readonly int numValidEdges;
private int numValidEdges;

[ProtoMember(16)]
private int numFaces;

[ProtoMember(17)]
private readonly int numFaceNodes;
private int numFaceNodes;

public DisposableMesh2D()
{
}

public DisposableMesh2D(int nNodes, int nEdges, int nFaces)
public DisposableMesh2D(int nNodes, int nEdges, int nFaces, int nFaceNodes)
{
NumNodes = nNodes;
NumEdges = nEdges;
NumFaces = nFaces;
NumFaceNodes = nFaceNodes;

EdgeFaces = new int[NumEdges * 2];
EdgeNodes = new int[NumEdges * 2];
Expand Down Expand Up @@ -157,23 +158,35 @@ public int NumNodes
set { numNodes = value; }
}

public int NumValidNodes => numValidNodes;
public int NumValidNodes
{
get { return numValidNodes; }
set { numValidNodes = value; }
}

public int NumEdges
{
get { return numEdges; }
set { numEdges = value; }
}

public int NumValidEdges => numValidEdges;
public int NumValidEdges
{
get { return numValidEdges; }
set { numValidEdges = value; }
}

public int NumFaces
{
get { return numFaces; }
set { numFaces = value; }
}

public int NumFaceNodes => numFaceNodes;
public int NumFaceNodes
{
get { return numFaceNodes; }
set { numFaceNodes = value; }
}

#region IReadOnly2DMesh
/// <inheritdoc/>
Expand Down
23 changes: 23 additions & 0 deletions src/MeshKernelNET/Api/IMeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public interface IMeshKernelApi : IDisposable
/// <returns>Generated meshKernelId</returns>
int AllocateState(int projectionType);

/// <summary>
/// Clear the undo state
/// </summary>
/// <param name="meshKernelId">Id of the grid state</param>
/// <returns>Error code</returns>
int ClearUndoState(int meshKernelId);

/// <summary>
/// Computes 1d-2d contacts, where 1d nodes are connected to the closest 2d faces at the boundary
/// (ggeo_make1D2DRiverLinks_dll)
Expand Down Expand Up @@ -1376,5 +1383,21 @@ int PolygonGetOffset(int meshKernelId,
/// <returns>Error code</returns>
int PolygonRefine(int meshKernelId, in DisposableGeometryList disposableGeometryListIn, int firstIndex,
int secondIndex, double distance, ref DisposableGeometryList disposableGeometryListOut);

/// <summary>
/// Redo editing action
/// </summary>
/// <param name="meshKernelId">Id of the grid state</param>
/// <param name="redone">If the editing action has been re-done</param>
/// <returns>Error code</returns>
int RedoState(int meshKernelId, ref bool redone);

/// <summary>
/// Undo editing action
/// </summary>
/// <param name="meshKernelId">Id of the grid state</param>
/// <param name="undone">If the editing action has been un-done</param>
/// <returns>Error code</returns>
int UndoState(int meshKernelId, ref bool undone);
}
}
29 changes: 26 additions & 3 deletions src/MeshKernelNET/Api/MeshKernelApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public int AllocateState(int projectionType)
return meshKernelId;
}

/// <inheritdoc/>
public int ClearUndoState(int meshKernelId)
{
return MeshKernelDll.ClearUndoState(meshKernelId);
}

public int ContactsComputeBoundary(int meshKernelId, in IntPtr oneDNodeMask, in DisposableGeometryList polygons, double searchRadius)
{
GeometryListNative polygonsNative = polygons.CreateNativeObject();
Expand Down Expand Up @@ -797,7 +803,8 @@ public int Mesh2dGetData(int meshKernelId, out DisposableMesh2D disposableMesh2D

disposableMesh2D = new DisposableMesh2D(newMesh2D.num_nodes,
newMesh2D.num_edges,
newMesh2D.num_faces);
newMesh2D.num_faces,
newMesh2D.num_face_nodes);

newMesh2D = disposableMesh2D.CreateNativeObject();

Expand Down Expand Up @@ -1245,6 +1252,18 @@ public int PolygonRefine(int meshKernelId, in DisposableGeometryList disposableG
return MeshKernelDll.PolygonRefine(meshKernelId, ref geometryListNativeIn, firstIndex, secondIndex, distance, ref geometryListNativeOut);
}

/// <inheritdoc/>
public int RedoState(int meshKernelId, ref bool redone)
{
return MeshKernelDll.RedoState(meshKernelId, ref redone);
}

/// <inheritdoc/>
public int UndoState(int meshKernelId, ref bool undone)
{
return MeshKernelDll.UndoState(meshKernelId, ref undone);
}

private DisposableMesh2D CreateDisposableMesh2D(Mesh2DNative newMesh2DNative, bool addCellInformation = false)
{
var disposableMesh2D = new DisposableMesh2D
Expand All @@ -1253,7 +1272,9 @@ private DisposableMesh2D CreateDisposableMesh2D(Mesh2DNative newMesh2DNative, bo
NodeY = newMesh2DNative.node_y.CreateValueArray<double>(newMesh2DNative.num_nodes),
EdgeNodes = newMesh2DNative.edge_nodes.CreateValueArray<int>(newMesh2DNative.num_edges * 2).ToArray(),
NumEdges = newMesh2DNative.num_edges,
NumNodes = newMesh2DNative.num_nodes
NumNodes = newMesh2DNative.num_nodes,
NumValidNodes = newMesh2DNative.num_valid_nodes,
NumValidEdges = newMesh2DNative.num_valid_edges
};

if (addCellInformation && newMesh2DNative.num_faces > 0)
Expand All @@ -1277,7 +1298,9 @@ private DisposableMesh1D CreateDisposableMesh1d(Mesh1DNative newMesh1DNative)
NodeX = newMesh1DNative.node_x.CreateValueArray<double>(newMesh1DNative.num_nodes),
NodeY = newMesh1DNative.node_y.CreateValueArray<double>(newMesh1DNative.num_nodes),
NumNodes = newMesh1DNative.num_nodes,
NumEdges = newMesh1DNative.num_edges
NumEdges = newMesh1DNative.num_edges,
NumValidNodes = newMesh1DNative.num_valid_nodes,
NumValidEdges = newMesh1DNative.num_valid_edges,
};
return disposableMesh1D;
}
Expand Down
24 changes: 24 additions & 0 deletions src/MeshKernelNET/Native/MeshKernelDll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ static MeshKernelDll()
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_allocate_state", CallingConvention = CallingConvention.Cdecl)]
internal static extern int AllocateState([In] int projectionType, [In][Out] ref int meshKernelId);

/// <summary>
/// Clear the undo state
/// </summary>
/// <param name="meshKernelId">The id of the mesh state</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_clear_undo_state", CallingConvention = CallingConvention.Cdecl)]
internal static extern int ClearUndoState([In] int meshKernelId);

/// <summary>
/// Computes 1d-2d contacts, where 1d nodes are connected to the closest 2d faces at the boundary
/// (ggeo_make1D2DRiverLinks_dll)
Expand Down Expand Up @@ -1652,5 +1660,21 @@ internal static extern int PolygonCountOffset([In] int meshKernelId,
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_polygon_refine", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PolygonRefine([In] int meshKernelId, [In] ref GeometryListNative geometryListIn, [In] int firstIndex, [In] int secondIndex, [In] double distance, [In][Out] ref GeometryListNative geometryListOut);

/// <summary>
/// Redo editing action
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_redo_state", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RedoState(int meshKernelId, [In][Out] ref bool redone);

/// <summary>
/// Redo editing action
/// </summary>
/// <param name="meshKernelId">Id of the mesh state</param>
/// <returns>Error code</returns>
[DllImport(MeshKernelDllName, EntryPoint = "mkernel_undo_state", CallingConvention = CallingConvention.Cdecl)]
internal static extern int UndoState(int meshKernelId, [In][Out] ref bool undone);
}
}
34 changes: 34 additions & 0 deletions test/MeshKernelNETTest/Api/MeshKernelCurvilinearTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,5 +969,39 @@ public void CurvilinearGrid_EdgeNodesSerialization(int numM, int numN, IList<(in
new object[] { 2, 3, new[] { (0, 2), (1, 3), (2, 4), (3, 5), (0, 1), (2, 3), (4, 5) } },
new object[] { 3, 2, new[] { (0, 3), (1, 4), (2, 5), (0, 1), (1, 2), (3, 4), (4, 5) } }
};

[Test]
public void CurvilinearSetAndCovertThroughApi()
{

// Setup
using (var api = new MeshKernelApi())
using (DisposableCurvilinearGrid grid = CreateCurvilinearGrid(5, 5, 10, 10))
{
var id = 0;
DisposableCurvilinearGrid curvilinearGrid = null;
var mesh2d = new DisposableMesh2D();
{
try
{
// Prepare
id = api.AllocateState(0);
Assert.AreEqual(0, api.CurvilinearSet(id, grid));
Assert.AreEqual(0, api.CurvilinearConvertToMesh2D(id));
Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2d));

// Assert
Assert.AreEqual(25, mesh2d.NumNodes);
}
finally
{
api.DeallocateState(id);
curvilinearGrid?.Dispose();
mesh2d.Dispose();
}
}
}
}

}
}
53 changes: 52 additions & 1 deletion test/MeshKernelNETTest/Api/MeshKernelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,7 +2467,7 @@ public void Mesh2dDeleteInsidePolygon(DeleteMeshInsidePolygonOptions deleteMeshI
}

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

Expand All @@ -2493,5 +2493,56 @@ public void Mesh2dMakeGlobaThroughApi()
}
}
}


[Test]
public void Mesh2dUndoRedoThroughApi()
lucacarniato marked this conversation as resolved.
Show resolved Hide resolved
{
// Setup
using (DisposableMesh2D mesh = CreateMesh2D(4, 4, 100, 200))
using (var api = new MeshKernelApi())
{
var id = 0;
var mesh2d = new DisposableMesh2D();
try
{
int numberOfVerticesBefore = mesh.NumNodes;
id = api.AllocateState(0);

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

// Do
Assert.AreEqual(0, api.Mesh2dDeleteNode(id, 0));
lucacarniato marked this conversation as resolved.
Show resolved Hide resolved
Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2d));
Assert.AreEqual(numberOfVerticesBefore - 1, mesh2d.NumValidNodes);

Assert.AreEqual(0, api.Mesh2dDeleteNode(id, 6));
Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2d));
Assert.AreEqual(numberOfVerticesBefore - 2, mesh2d.NumValidNodes);

// Un-do
bool undone = false;
Assert.AreEqual(0, api.UndoState(id, ref undone));
Assert.AreEqual(true, undone);

Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2d));
Assert.AreEqual(numberOfVerticesBefore - 1, mesh2d.NumValidNodes);
lucacarniato marked this conversation as resolved.
Show resolved Hide resolved

undone = false;
Assert.AreEqual(0, api.UndoState(id, ref undone));
Assert.AreEqual(true, undone);

Assert.AreEqual(0, api.Mesh2dGetData(id, out mesh2d));
Assert.AreEqual(numberOfVerticesBefore, mesh2d.NumValidNodes);
}
finally
{
api.DeallocateState(id);
mesh2d.Dispose();
}
}
}


lucacarniato marked this conversation as resolved.
Show resolved Hide resolved
}
}