Skip to content

fix: correct ring-in-geometry check - #1521

Merged
tinko92 merged 2 commits into
boostorg:developfrom
gastald88:hotfix/bad_ring_inside_geometry_classification
Nov 30, 2026
Merged

tinko92 merged 2 commits into
boostorg:developfrom
gastald88:hotfix/bad_ring_inside_geometry_classification

Conversation

@gastald88

@gastald88 gastald88 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

This is a ai-aided fix on a case I ran into:

if the point of the ring checked is on the boundary of the other geometry, the other points of the same ring are checked (not those of the whole input, which can be located elsewhere).

Reproduction

A hole of A lies inside B and touches B at one vertex, (10 10):

#include <boost/geometry.hpp>
#include <iostream>

namespace bg = boost::geometry;
using point = bg::model::d2::point_xy<double>;
using polygon = bg::model::polygon<point>;

int main()
{
   polygon a, b;
   bg::read_wkt("POLYGON((0 0,0 100,100 100,100 0,0 0),(10 10,30 20,20 30,10 10))", a);
   bg::read_wkt("POLYGON((10 10,15 50,50 50,50 15,10 10))", b);
   bg::correct(a);
   bg::correct(b);

   std::string reason;
   std::cout << bg::is_valid(a, reason) << " " << reason << "\n";   // 1 Geometry is valid
   std::cout << bg::is_valid(b, reason) << " " << reason << "\n";   // 1 Geometry is valid

   bg::model::multi_polygon<polygon> result;
   bg::difference(a, b, result);
   std::cout << bg::area(result) << " " << bg::is_valid(result, reason) << " " << reason << "\n";
}
  • Before: area 8450, invalid (self-intersection at (10, 10)). A's hole is kept, nested inside the hole cut by B.
  • After: area 8600, valid: the box with B as its only hole.

I added the corresponding test too in the test suite.

Let me know if you need me to also create an issue about this problem or if this PR is ok as is.

@gastald88
gastald88 marked this pull request as ready for review November 20, 2026 11:32
@barendgehrels

Copy link
Copy Markdown
Collaborator

Thanks for the PR.
Can you please first call bg::correct for both geometries in your reproduction scenario, and check if they are valid?

@gastald88

Copy link
Copy Markdown
Contributor Author

Thanks for the PR. Can you please first call bg::correct for both geometries in your reproduction scenario, and check if they are valid?

sure, both inputs are valid, I added the calls in the repro example.

@tinko92

tinko92 commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the PR!

This does not reproduce on #1517 for me, so we may already have a fix currently under review.

@barendgehrels barendgehrels left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for the PR.

In the meantime #1517 is merged, which contains the same fix (nearly identical) in select_rings.hpp, dated from September 9
So with current develop your case gives area 8600 and a valid result.

The test case is still valuable. Could you rebase on develop, keeping only case_hole_touching_at_vertex?

@gastald88
gastald88 force-pushed the hotfix/bad_ring_inside_geometry_classification branch from 198e86f to d9350e6 Compare November 30, 2026 09:13
@gastald88

Copy link
Copy Markdown
Contributor Author

Thanks again for the PR.

In the meantime #1517 is merged, which contains the same fix (nearly identical) in select_rings.hpp, dated from September 9 So with current develop your case gives area 8600 and a valid result.

The test case is still valuable. Could you rebase on develop, keeping only case_hole_touching_at_vertex?

Sure, just rebased on itself keeping only the test case.

@tinko92 tinko92 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick update. Because the fix is already merged, I'm in favor of merging this. I think, the conflict can be resolved via web editor by chosing "Accept both" (the editor shows the merge to be towards your repo, so I think it requires you to do it).

Edit: Thanks, will merge after CI completes.

@tinko92
tinko92 merged commit a276c67 into boostorg:develop Nov 30, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants