Skip to content

Commit

Permalink
GRIDEDIT-680: render curvilinear grid (#48)
Browse files Browse the repository at this point in the history
DisposableMesh2D and DisposableCurvilinearGrid both implement a new interface IReadOnly2DMesh.
  • Loading branch information
andreasbuykx authored Feb 19, 2024
1 parent 340d92d commit 97291ac
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 51 deletions.
74 changes: 73 additions & 1 deletion src/MeshKernelNET/Api/DisposableCurvilinearGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace MeshKernelNET.Api
{
[ProtoContract(AsReferenceDefault = true)]
public sealed class DisposableCurvilinearGrid : DisposableNativeObject<CurvilinearGridNative>
public sealed class DisposableCurvilinearGrid : DisposableNativeObject<CurvilinearGridNative>, IReadOnly2DMesh
{
[ProtoMember(1)]
private double[] nodeX;
Expand Down Expand Up @@ -61,6 +61,78 @@ public int NumN
set { numN = value; }
}

#region IReadOnly2DMesh
/// <inheritdoc/>
public int CellCount()
{
return (NumM - 1) * (NumN-1);
}

/// <inheritdoc/>
/// <remarks>Note that there is no trivial relationship between cell index and edge indexes</remarks>
public int GetCellEdgeCount(int cellIndex)
{
return 4;
}

/// <inheritdoc/>
public int EdgeCount()
{
return (NumM-1)*NumN + // row-oriented edges
NumM*(NumN - 1); // column-oriented edges
}

/// <remarks>The edge numbering is equal to CurvilinearGrid::ConvertCurvilinearToNodesAndEdges.
/// First column-oriented edges, then row-oriented edges, in row major order</remarks>
private (int first, int last) GetEdgeNodes(int edgeIndex)
{
int numNodesInRow = NumN;
int numNodesInColumn = NumM;
int numColumnOrientedEdges = numNodesInRow * (numNodesInColumn - 1);
if (edgeIndex < numColumnOrientedEdges)
{
return (edgeIndex, edgeIndex + numNodesInRow);
}

edgeIndex -= numColumnOrientedEdges;

int numRowOrientedEdgesInRow = (numNodesInRow - 1);
int row = edgeIndex / numRowOrientedEdgesInRow;
int first = row*numNodesInRow + edgeIndex % numRowOrientedEdgesInRow;
return (first, first + 1);
}

/// <inheritdoc/>
public int GetFirstNode(int edgeIndex)
{
return GetEdgeNodes(edgeIndex).first;
}

/// <inheritdoc/>
public int GetLastNode(int edgeIndex)
{
return GetEdgeNodes(edgeIndex).last;
}

/// <inheritdoc/>
public int NodeCount()
{
return NumM*NumN;
}

/// <inheritdoc/>
public double GetNodeX(int nodeIndex)
{
return NodeX[nodeIndex];
}

/// <inheritdoc/>
public double GetNodeY(int nodeIndex)
{
return NodeY[nodeIndex];
}
#endregion

protected override void SetNativeObject(ref CurvilinearGridNative nativeObject)
{
nativeObject.node_x = GetPinnedObjectPointer(NodeX);
Expand Down
53 changes: 52 additions & 1 deletion src/MeshKernelNET/Api/DisposableMesh2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace MeshKernelNET.Api
{
[ProtoContract(AsReferenceDefault = true)]
public sealed class DisposableMesh2D : DisposableNativeObject<Mesh2DNative>
public sealed class DisposableMesh2D : DisposableNativeObject<Mesh2DNative>, IReadOnly2DMesh
{
[ProtoMember(1)]
private int[] edgeFaces;
Expand Down Expand Up @@ -170,6 +170,57 @@ public int NumFaceNodes
set { numFaceNodes = value; }
}


#region IReadOnly2DMesh
/// <inheritdoc/>
public int CellCount()
{
return NumFaces;
}

/// <inheritdoc/>
public int GetCellEdgeCount(int cellIndex)
{
return NodesPerFace[cellIndex];
}

/// <inheritdoc/>
public int EdgeCount()
{
return NumEdges;
}

/// <inheritdoc/>
public int GetFirstNode(int edgeIndex)
{
return EdgeNodes[2 * edgeIndex];
}

/// <inheritdoc/>
public int GetLastNode(int edgeIndex)
{
return EdgeNodes[2 * edgeIndex + 1];
}

/// <inheritdoc/>
public int NodeCount()
{
return NumNodes;
}

/// <inheritdoc/>
public double GetNodeX(int nodeIndex)
{
return NodeX[nodeIndex];
}

/// <inheritdoc/>
public double GetNodeY(int nodeIndex)
{
return NodeY[nodeIndex];
}
#endregion

protected override void SetNativeObject(ref Mesh2DNative nativeObject)
{
nativeObject.edge_faces = GetPinnedObjectPointer(EdgeFaces);
Expand Down
61 changes: 61 additions & 0 deletions src/MeshKernelNET/Api/IReadOnly2DMesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace MeshKernelNET.Api
{
/// <summary>
/// Topology and node locations of a general 2D mesh
/// </summary>
public interface IReadOnly2DMesh
{
/// <summary>
/// The number of cells
/// </summary>
/// <returns>a nonnegative number representing the number of cells</returns>
int CellCount();

/// <summary>
/// The number of edges of a cell
/// </summary>
/// <param name="cellIndex">the index of the cell</param>
/// <returns>a nonnegative number representing the number of edges of the cell</returns>
int GetCellEdgeCount(int cellIndex);

/// <summary>
/// The total number of edges in the mesh
/// </summary>
/// <returns>a nonnegative number representing the number of edges</returns>
int EdgeCount();

/// <summary>
/// The index of the first node of an edge
/// </summary>
/// <returns>a nonnegative number representing the node index</returns>
int GetFirstNode(int edgeIndex);

/// <summary>
/// The index of the second node of an edge
/// </summary>
/// <param name="edgeIndex">the index of the cell</param>
/// <returns>a nonnegative number representing the node index</returns>
int GetLastNode(int edgeIndex);


/// <summary>
/// The total number of nodes in the mesh
/// </summary>
/// <returns>a nonnegative number representing the number of nodes</returns>
int NodeCount();

/// <summary>
/// The x coordinate of a node
/// </summary>
/// <param name="nodeIndex">the index of the cell</param>
/// <returns>the x-coordinate value of the node</returns>
double GetNodeX(int nodeIndex);

/// <summary>
/// The y coordinate of a node
/// </summary>
/// <param name="nodeIndex">the index of the cell</param>
/// <returns>the y-coordinate value of the node</returns>
double GetNodeY(int nodeIndex);
}
}
Loading

0 comments on commit 97291ac

Please sign in to comment.