Skip to content

Commit

Permalink
Add mkernel_mesh2d_convert_to_curvilinear API unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato committed Apr 26, 2024
1 parent 6f458ed commit 6394384
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/MeshKernelApi/include/MeshKernelApi/MeshKernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,14 @@ namespace meshkernelapi
/// @returns Error code
MKERNEL_API int mkernel_mesh2d_convert_projection(int meshKernelId, int projectionType, const char* const zoneString);

/// @brief Converts a mesh to a curvilinear mesh
///
/// @param[in] meshKernelId The id of the mesh state
/// @param[in] xPointCoordinate The x coordinate of the point where to start the conversion point coordinate
/// @param[in] yPointCoordinate The y point coordinate
/// @returns Error code
MKERNEL_API int mkernel_mesh2d_convert_to_curvilinear(int meshKernelId, double xPointCoordinate, double yPointCoordinate);

/// @brief Count the number of hanging edges in a mesh2d.
/// An hanging edge is an edge where one of the two nodes is not connected.
/// @param[in] meshKernelId The id of the mesh state
Expand Down
23 changes: 23 additions & 0 deletions libs/MeshKernelApi/src/MeshKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "MeshKernel/CurvilinearGrid/CurvilinearGridDeleteExterior.hpp"
#include "MeshKernel/Mesh2DIntersections.hpp"
#include "MeshKernel/Mesh2DToCurvilinear.hpp"
#include "MeshKernel/SamplesHessianCalculator.hpp"

#include "MeshKernel/CurvilinearGrid/CurvilinearGridDeleteInterior.hpp"
Expand Down Expand Up @@ -770,6 +771,28 @@ namespace meshkernelapi
return lastExitCode;
}

MKERNEL_API int mkernel_mesh2d_convert_to_curvilinear(int meshKernelId, double xPointCoordinate, double yPointCoordinate)
{
lastExitCode = meshkernel::ExitCode::Success;
try
{
if (!meshKernelState.contains(meshKernelId))
{
throw meshkernel::MeshKernelError("The selected mesh kernel id does not exist.");
}

meshkernel::Mesh2DToCurvilinear mesh2DToCurvilinear(*meshKernelState[meshKernelId].m_mesh2d);

meshKernelState[meshKernelId].m_curvilinearGrid = mesh2DToCurvilinear.Compute({xPointCoordinate, yPointCoordinate});
meshKernelState[meshKernelId].m_mesh2d = std::make_unique<meshkernel::Mesh2D>(meshKernelState[meshKernelId].m_projection);
}
catch (...)
{
lastExitCode = HandleException();
}
return lastExitCode;
}

MKERNEL_API int mkernel_mesh2d_count_hanging_edges(int meshKernelId, int& numHangingEdges)
{
lastExitCode = meshkernel::ExitCode::Success;
Expand Down
41 changes: 41 additions & 0 deletions libs/MeshKernelApi/tests/src/ApiTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3248,3 +3248,44 @@ TEST_P(MeshLocationIndexTests, GetLocationIndex_OnAMesh_ShouldGetTheLocationInde
ASSERT_EQ(locationIndex, expectedIndex);
}
INSTANTIATE_TEST_SUITE_P(LocationIndexParametrizedTests, MeshLocationIndexTests, ::testing::ValuesIn(MeshLocationIndexTests::GetData()));

TEST(Mesh2D, ConvertToCurvilinear_ShouldConvertMeshToCurvilinear)
{
// create first mesh
auto [num_nodes, num_edges, node_x, node_y, edge_nodes] = MakeRectangularMeshForApiTesting(10, 10, 1.0, meshkernel::Point(0.0, 0.0));

meshkernelapi::Mesh2D mesh2d{};
mesh2d.num_nodes = static_cast<int>(num_nodes);
mesh2d.num_edges = static_cast<int>(num_edges);
mesh2d.node_x = node_x.data();
mesh2d.node_y = node_y.data();
mesh2d.edge_nodes = edge_nodes.data();

// allocate state
int meshKernelId = 0;
int errorCode = meshkernelapi::mkernel_allocate_state(0, meshKernelId);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

// first initialise using the first mesh, mesh2d
errorCode = mkernel_mesh2d_set(meshKernelId, mesh2d);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

// Execute
errorCode = meshkernelapi::mkernel_mesh2d_convert_to_curvilinear(meshKernelId, 5.0, 5.0);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

meshkernelapi::Mesh2D mesh2dOut{};
errorCode = mkernel_mesh2d_get_dimensions(meshKernelId, mesh2dOut);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

meshkernelapi::CurvilinearGrid curvilinearOut{};
errorCode = mkernel_curvilinear_get_dimensions(meshKernelId, curvilinearOut);
ASSERT_EQ(meshkernel::ExitCode::Success, errorCode);

// Assert
ASSERT_EQ(0, mesh2dOut.num_nodes);
ASSERT_EQ(0, mesh2dOut.num_edges);

ASSERT_EQ(11, curvilinearOut.num_m);
ASSERT_EQ(11, curvilinearOut.num_n);
}

0 comments on commit 6394384

Please sign in to comment.