Skip to content

Commit

Permalink
bug fix, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato committed Oct 23, 2023
1 parent 7c095b6 commit 4b02a5d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
4 changes: 2 additions & 2 deletions libs/MeshKernel/include/MeshKernel/Polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ namespace meshkernel

/// @brief Determine if the polygon contains the point for Cartesian coordinate system
///
/// Also for spherical coordinates
bool ContainsCartesian(const Point& point) const;
/// For cartesian and spherical coordinates
bool ContainsCartesianOrSpherical(const Point& point) const;

/// @brief Determine if the polygon contains the point for accurate spherical coordinate system
bool ContainsSphericalAccurate(const Point& point) const;
Expand Down
16 changes: 5 additions & 11 deletions libs/MeshKernel/src/Polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,13 @@ void meshkernel::Polygon::Reset(const std::vector<Point>& points,
Initialise();
}

bool meshkernel::Polygon::ContainsCartesian(const Point& point) const
bool meshkernel::Polygon::ContainsCartesianOrSpherical(const Point& point) const
{
int windingNumber = 0;

for (size_t n = 0; n < m_nodes.size() - 1; n++)
{
// TODO always Cartesian
// So Dx and Dy can be simplified (no branching)
// Then for 2 or more points, return multiple cross product values
const auto crossProductValue = crossProduct(m_nodes[n], m_nodes[n + 1], m_nodes[n], point, Projection::cartesian);
const auto crossProductValue = crossProduct(m_nodes[n], m_nodes[n + 1], m_nodes[n], point, m_projection);

if (IsEqual(crossProductValue, 0.0))
{
Expand Down Expand Up @@ -206,13 +203,10 @@ bool meshkernel::Polygon::Contains(const Point& pnt) const

if (m_projection == Projection::cartesian || m_projection == Projection::spherical)
{
return ContainsCartesian(pnt);
}
else
{
// projection = Projection::sphericalAccurate
return ContainsSphericalAccurate(pnt);
return ContainsCartesianOrSpherical(pnt);
}
// projection = Projection::sphericalAccurate
return ContainsSphericalAccurate(pnt);
}

// TODO does this need start and end points, probably not all the polygon is to be snapped
Expand Down
26 changes: 26 additions & 0 deletions libs/MeshKernel/tests/src/Mesh2DTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,29 @@ TEST(Mesh2D, DeleteMesh_WhenFacesAreIntersected_ShouldNotDeleteFaces)
// Assert
EXPECT_EQ(mesh->GetNumFaces(), 9);
}

TEST(Mesh2D, DeleteMesh_WhenFacesAreIntersectedSpherical_ShouldNotDeleteFaces)
{
// Prepare
const auto mesh = MakeRectangularMeshForTesting(4, 4, 3, 3, meshkernel::Projection::spherical, meshkernel::Point{0, 0});

// a polygon including all nodes of a face, but also intersecting
std::vector<meshkernel::Point> polygonNodes{
{1.87622950819672, -0.299180327868853},
{1.86885245901639, 0.187704918032786},
{3.27049180327869, 0.195081967213114},
{3.27049180327869, 0.320491803278688},
{1.87622950819672, 0.320491803278688},
{1.86147540983607, 1.16147540983606},
{3.54344262295082, 1.18360655737705},
{3.55081967213115, -0.358196721311476},
{1.87622950819672, -0.299180327868853}};

auto polygon = meshkernel::Polygons(polygonNodes, meshkernel::Projection::spherical);

// Execute
mesh->DeleteMesh(polygon, 0, false);

// Assert
EXPECT_EQ(mesh->GetNumFaces(), 9);
}

0 comments on commit 4b02a5d

Please sign in to comment.