Skip to content

Fix Cells::splitByLine throwing on a pinched split - #249

Merged
Gonzalo-Mier merged 4 commits into
Fields2Cover:mainfrom
u33549:dev/umutc/v2.1.0-devel/splitbyline-pinch
Sep 3, 2026
Merged

Gonzalo-Mier merged 4 commits into
Fields2Cover:mainfrom
u33549:dev/umutc/v2.1.0-devel/splitbyline-pinch

Conversation

@u33549

@u33549 u33549 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

A split can leave a piece touching itself at a single point (a pinch). Cells::splitByLine reinflated each split piece through Cell::buffer, which only accepts a single polygon back; a positive buffer on a pinched piece can separate it into two, and Cell(const OGRGeometry*) throws std::invalid_argument on the resulting MultiPolygon instead of decomposing the field.

Reproduction

data/ee_field_130.wkt is a real field. Decomposing it with BoustrophedonDecomp throws at 12 of 180 swept split angles (58, 60, 131, 142, 144, 147, 148, 152, 155, 160, 162, 168 degrees). Added as fields2cover_decomp_boustrophedon.doesNotThrowOnAPinchedSplit, using angle 58.

The fix

Cells Cells::splitByLine(const LineString& line) const {
  return this->difference(this->buffer(line, 1e-8));
}

The reinflate step turns out to have never run:

for (auto&& c : cells) {
  c = Cell::buffer(c, 1e-8 * 0.5);
}

c is the range-for's by-value loop variable; assigning to it never writes back into cells. Verified directly — the container's WKT is byte-identical before and after this loop runs. So removing it fixes the crash and changes nothing for pieces that were never pinched, since the step already had no effect on them. The existing fields2cover_decomp_boustrophedon.decompose test (an unrelated shape, no pinch involved) passes unchanged.

A split can leave a piece touching itself at a single point. The piece
was reinflated through Cell::buffer, which only accepts a single
polygon back; a positive buffer on a pinched piece can separate it
into two, and Cell(const OGRGeometry*) throws std::invalid_argument
on the resulting MultiPolygon instead of decomposing the field.

The reinflate step turns out to have never run: it reassigned the
by-value loop variable of a range-for, never writing back into the
result. Removing it fixes the crash and changes nothing for pieces
that were never pinched, since the step was already a no-op for them.

data/ee_field_130.wkt is a real field that reproduces the crash with
BoustrophedonDecomp at several split angles (58, 60, 131, 142, 144,
147, 148, 152, 155, 160, 162 and 168 degrees, out of a 0-179 sweep).
Copilot AI lite review requested due to automatic review settings September 1, 2026 01:11
Cells::splitByLine's reinflate loop looked fine but never ran: writing
through a range-for's loop variable only reaches the container for the
few types Iterator::update() whitelists. A comment at that whitelist
says which containers do not write back, so the next person who hits
this does not lose an afternoon to it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a crash in Cells::splitByLine when splitting produces a “pinched” piece that can buffer into a MultiPolygon, causing Cell(const OGRGeometry*) to throw. The fix removes an ineffective “reinflate” loop (which previously didn’t write back to the Cells container), and adds a regression test + fixture WKT to cover the pinched-split scenario.

Changes:

  • Simplify Cells::splitByLine(const LineString&) to return the split result directly (removing the no-op reinflation loop).
  • Add a regression test that reproduces the pinched-split case using a real field WKT fixture.
  • Document the iterator “range-for writes back only for some geometry types” pitfall and add a changelog entry.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/cpp/decomposition/boustrophedon_decomp_test.cpp Adds regression test loading a WKT fixture and asserting decomposition doesn’t throw for a pinch case.
src/fields2cover/types/Cells.cpp Removes ineffective reinflation loop from Cells::splitByLine.
include/fields2cover/types/Geometries_impl.hpp Adds clarifying comment about iterator write-back behavior for certain geometry types.
data/ee_field_130.wkt Adds real-world WKT fixture used by the new regression test.
CHANGELOG.md Documents the bugfix behavior change in the Unreleased “Fixed” section.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

loadWktCell used a raw OGRGeometry* with no null check: a missing file
or a WKT parse failure would reach Cell(const OGRGeometry*) as a null
pointer and segfault instead of failing the test. importFromWkt reads
into an already-valid Cell, so there is no pointer to check; a failed
read now leaves the cell empty and an explicit assertion catches it
before the test can silently pass on a field that never loaded.
Comment thread src/fields2cover/types/Cells.cpp Resolved
The reinflate step conserves each piece's size against the 1e-8-wide
strip the cut itself removes -- necessary, not dead code as assumed.
Restored it through Cells::buffer, which unlike Cell::buffer tolerates
a piece coming out pinched into two. Chaining reinflate after every
line in a MultiLineString split let GEOS collapse the next cut into a
no-op, so every line is now buffered and cut together in one pass.
@Gonzalo-Mier

Copy link
Copy Markdown
Member

Nice, thanks @u33549 for finding this bug!

@Gonzalo-Mier
Gonzalo-Mier merged commit 77a9e64 into Fields2Cover:main Sep 3, 2026
70 checks passed
@u33549
u33549 deleted the dev/umutc/v2.1.0-devel/splitbyline-pinch branch September 8, 2026 07:41
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