Skip to content

Commit

Permalink
reorganized test cases and added assertion of point close to vertex
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasbuykx committed May 1, 2024
1 parent a59a4a8 commit 453a9b0
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions test/MeshKernelNETTest/Api/MeshKernelCurvilinearTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using MeshKernelNET.Api;
using NUnit.Framework;

Expand Down Expand Up @@ -1044,47 +1046,52 @@ public void CreateGridSucceeds()
{
CreateGrid(5,4,1.0,1.0,1.0,1.0);

(double x, double y)[] expectedVertices = {
(double x, double y)[] expectedVertices_5x4 = {
(1, 1), (2, 1), (3, 1), (4, 1), // 0 - 3
(1, 2), (2, 2), (3, 2), (4, 2), // 4 - 7
(1, 3), (2, 3), (3, 3), (4, 3), // 8 - 11
(1, 4), (2, 4), (3, 4), (4, 4), // 12 - 15
(1, 5), (2, 5), (3, 5), (4, 5) }; // 16 - 19
Assert.That(vertices,Is.EquivalentTo(expectedVertices));

Assert.That(vertices,Is.EquivalentTo(expectedVertices_5x4));
}

private void AssertThatPointIsCloseToExpectedVertex(double x, double y, int expectedIndex)
{
// Check that point is closest to expected vertex
Coordinate vertex = new Coordinate(vertices[expectedIndex].x, vertices[expectedIndex].y);
Coordinate point = new Coordinate(x, y);
Assert.That(vertex.Distance(point),Is.LessThan(Math.Sqrt(0.5*0.5 + 0.5*0.5)));
}

[TestCase(1.0,1.0, 0)]
[TestCase(2.0,2.0, 5)]
[TestCase(3.0,3.0, 10)]
[TestCase(.99, .95, 0)] // slightly below and left of vertex (outside mesh perimeter)
[TestCase(1.01,1.01, 0)] // slightly above and right of vertex

[TestCase(2.0,2.0, 5)] // on vertex
[TestCase(1.97,1.999, 5)] // slightly below and left of vertex
[TestCase(2.01,2.01, 5)] // slightly above and right of vertex

[TestCase(4.0,4.0, 15)]
[TestCase(4.01,3.99, 15)] // slightly below and right of vertex (outside mesh perimeter)
[TestCase(3.99,4.04, 15)] // slightly above and right of vertex

[TestCase(4.0,1.0,3)]
[TestCase(1.0,5.0,16)]
[TestCase(4.0,5.0,19)]
[TestCase(3.99,0.99,3)] // slightly below and left of vertex (outside mesh perimeter)
[TestCase(4.01,1.01,3)] // slightly above and right of vertex (outside mesh perimeter)

// slightly above and right of vertex
[TestCase(1.01,1.01, 0)]
[TestCase(2.01,2.01, 5)]
[TestCase(3.01,3.01, 10)]
[TestCase(4.01,4.01, 15)]

[TestCase(4.01,1.01,3)]
[TestCase(1.01,5.01,16)]
[TestCase(4.01,5.01,19)]

// slightly below and left of vertex
[TestCase(.99, .95, 0)]
[TestCase(1.97,1.999, 5)]
[TestCase(2.9501, 2.99, 10)]
[TestCase(3.99,3.99, 15)]
[TestCase(1.0,5.0,16)]
[TestCase(1.01,5.01,16)] // slightly above and right of vertex (outside mesh perimeter)
[TestCase(0.99,4.99,16)] // slightly below and left of vertex (outside mesh perimeter)

[TestCase(3.99,0.99,3)]
[TestCase(0.99,4.99,16)]
[TestCase(3.99,4.99,19)]
[TestCase(4.0,5.0,19)]
[TestCase(4.01,5.01,19)] // slightly above and right of vertex (outside mesh perimeter)
[TestCase(3.99,4.99,19)] // slightly below and left of vertex (outside mesh perimeter)
public void CurvilinearGetNodeLocationIndex_AtOrCloseToVertexPositionFindsNearest(double x, double y, int expectedIndex)
{
CreateGrid(6,5,1.0,1.0,1.0,1.0);
// Setup
CreateGrid(5,4,1.0,1.0,1.0,1.0);
AssertThatPointIsCloseToExpectedVertex(x,y,expectedIndex);

// Call
var box = new BoundingBox() // box around grid with 1.0 margin
Expand All @@ -1098,7 +1105,6 @@ public void CurvilinearGetNodeLocationIndex_AtOrCloseToVertexPositionFindsNeares
int returnCode = api.CurvilinearGetNodeLocationIndex(id,x,y,box, ref actualIndex);

// Assert
Assert.That(returnCode,Is.EqualTo(0));
Assert.That(actualIndex,Is.EqualTo(expectedIndex));
}

Expand All @@ -1118,7 +1124,9 @@ public void CurvilinearGetNodeLocationIndex_AtOrCloseToVertexPositionFindsNeares
[TestCase(3.0,3.4)]
public void CurvilinearGetNodeLocationIndex_OnOrInBoundingBox_FindsNearest(double x, double y)
{
CreateGrid(6,5,1.0,1.0,1.0,1.0);
const int expectedIndex = 10;
CreateGrid(5,4,1.0,1.0,1.0,1.0);
AssertThatPointIsCloseToExpectedVertex(x, y, expectedIndex);

// Call
var box = new BoundingBox() // box (w=0.8,h=0.8) around point (3,3) index 10
Expand All @@ -1132,8 +1140,7 @@ public void CurvilinearGetNodeLocationIndex_OnOrInBoundingBox_FindsNearest(doubl
int returnCode = api.CurvilinearGetNodeLocationIndex(id,x,y,box, ref actualIndex);

// Assert
Assert.That(returnCode,Is.EqualTo(0));
Assert.That(actualIndex,Is.EqualTo(10));
Assert.That(actualIndex,Is.EqualTo(expectedIndex));
}

[TestCase(2.6,3.0)]
Expand All @@ -1142,8 +1149,10 @@ public void CurvilinearGetNodeLocationIndex_OnOrInBoundingBox_FindsNearest(doubl
[TestCase(3.0,3.4)]
public void CurvilinearGetNodeLocationIndex_OutsideBoundingBox_DoesNotFindNearest(double x, double y)
{
CreateGrid(6,5,1.0,1.0,1.0,1.0);

const int expectedIndex = 10;
CreateGrid(5,4,1.0,1.0,1.0,1.0);
AssertThatPointIsCloseToExpectedVertex(x, y, 10);

// Call
var box = new BoundingBox() // box (w=0.4,h=0.4) around point (3,3) index 10
{
Expand All @@ -1152,11 +1161,10 @@ public void CurvilinearGetNodeLocationIndex_OutsideBoundingBox_DoesNotFindNeares
xUpperRight = 3.2,
yUpperRight = 3.2
};
int actualIndex = -1;
int actualIndex = expectedIndex; // should be reset to -1
int returnCode = api.CurvilinearGetNodeLocationIndex(id,x,y,box, ref actualIndex);

// Assert
Assert.That(returnCode,Is.Not.EqualTo(0)); // not a 0 return code?
Assert.That(actualIndex,Is.EqualTo(-1));
}
}
Expand Down

0 comments on commit 453a9b0

Please sign in to comment.