Skip to content

Fix ReqHL producing a mainland smaller or larger than it should - #250

Merged
Gonzalo-Mier merged 5 commits into
Fields2Cover:developfrom
u33549:dev/umutc/v2.1.0-devel/intersection-tolerance
Sep 22, 2026
Merged

Gonzalo-Mier merged 5 commits into
Fields2Cover:developfrom
u33549:dev/umutc/v2.1.0-devel/intersection-tolerance

Conversation

@u33549

@u33549 u33549 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

ReqHL::generateHeadlands has an invariant: ConstHL(worst case) <= ReqHL <= field. On real, digitized field boundaries it broke both directions -- a mainland collapsing to near nothing, or growing past the field it came from. Both traced back to the same operation: offsetting a ring by a different distance per segment (LinearRing::bufferOutwards, used only by ReqHL).

Overflow: Point::intersectionOfLines on near-parallel lines

Each new vertex is the intersection of two offset segments' lines. The parallel check compared the determinant to exactly 0, but a redundant vertex on an otherwise straight, digitized edge gives two collinear segments whose determinant is 0 only up to rounding -- dividing by that sent the vertex arbitrarily far from either line.

double den = det(v1, v2);
if (fabs(den) <= 1e-9 * sqrt((v1 * v1) * (v2 * v2))) {
  return l1_s;  // was: if (den == 0)
}

One real field's mainland came out larger than the field it was cut from -- bufferOutwards sent a single vertex tens of metres from the rest of the ring.

Collapse: LinearRing::filterSelfIntersections on a heavily digitized border

Offsetting hundreds of segments by widely different amounts crosses the resulting ring many times over. Cleaning that up fell to filterSelfIntersections, which resolves one crossing at a time and drops every point between the two segments involved -- most of a real border, if that crossing is a distant one. A 274-vertex field collapsed from 6875 m2 to 1 m2 this way.

The fix moves cleanup out of the offsetting step and into a GEOS buffer, which resolves the same self-intersections properly and can come back as more than one mainland cell where the ring legitimately splits -- something a method built to return a single LinearRing never could:

namespace {
F2CCells offsetRing(const F2CLinearRing& ring, const std::vector<double>& dist) {
  F2CLinearRing offset = ring;
  offset.bufferOutwards(dist);
  return F2CCells::buffer(F2CCells::buffer(F2CCell(offset), 1e-5), -1e-5);
}
}  // namespace

getParallelLine no longer calls filterSelfIntersections itself; the one caller that needed cleanup now does its own. filterSelfIntersections is untouched and still does what its own tests ask of it.

Copilot AI lite review requested due to automatic review settings September 1, 2026 20:34

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.

🟡 Changes recommended

The new regression test can throw via flatten().at(0) when no swaths are returned, which would crash the test instead of failing it cleanly.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes cases where ReqHL::generateHeadlands could violate its invariant bounds (collapsing the mainland almost to nothing or expanding it beyond the source field) on real, digitized field boundaries by making per-segment offsetting more numerically robust and by delegating self-intersection cleanup to GEOS buffering.

Changes:

  • Harden Point::intersectionOfLines against near-parallel/collinear numeric overflow by using a scale-aware determinant tolerance.
  • Remove implicit self-intersection filtering from LinearRing::getParallelLine and instead clean ReqHL’s per-segment offsets via GEOS buffering, allowing legitimate splits into multiple mainland polygons.
  • Add regression tests (including a real WKT fixture) and document the fixes in the changelog.
File summaries
File Description
tests/cpp/types/Point_test.cpp Adds a regression test for near-parallel line intersection behavior.
tests/cpp/types/LinearRing_test.cpp Adds a regression test ensuring buffering past a redundant vertex doesn’t explode coordinates.
tests/cpp/headland_generator/req_hl_test.cpp Adds a real-world regression test ensuring ReqHL doesn’t collapse or exceed the field area.
src/fields2cover/types/Point.cpp Updates intersection logic to treat near-parallel lines as parallel using a scaled tolerance.
src/fields2cover/types/LinearRing.cpp Stops auto-calling filterSelfIntersections() inside getParallelLine.
src/fields2cover/headland_generator/required_headland.cpp Refactors ReqHL headland generation to offset the exterior, subtract buffered obstacles, and clean via GEOS buffer.
data/ee_field_103.wkt Adds a digitized real-world field boundary fixture for regression testing.
CHANGELOG.md Documents the two fixes in the “Fixed” section.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 1
  • Review effort level: Lite

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

@u33549

u33549 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

i found new bugs on there i will fix them

@u33549

u33549 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Added a third fix on top of the other two: LinearRing::getParallelLine's corner join had no limit on how far the plain line-line miter intersection could land. As a corner sharpens, or as the two sides' offsets pull further apart, that point recedes arbitrarily far from the original vertex -- the same effect stroke-miterlimit exists to cap in SVG/Cairo stroke rendering.

On real, digitized field boundaries this sent ReqHL's mainland outside the field it came from by up to 273 m, occasionally taking a generated coverage swath with it. Past 4 times the larger of the two offsets at a corner, the join now bevels (both offset segment endpoints, connected directly) instead of following the miter out. Existing callers with equal offsets on both sides, including every pre-existing unit test, stay on the miter path unchanged.

Verified against an independent sweep of 350 real field boundaries: 15 of 17 previously-escaping runs (8 of 9 fields) are fixed. The ninth traces back to a decomposition issue upstream of both headland generators, not this join, and is being tracked separately.

@u33549

u33549 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

It's done rn

@Gonzalo-Mier

Copy link
Copy Markdown
Member

A new great PR! Thanks @u33549

The parallel check compared the determinant to exactly 0. Two real-world
collinear borders -- a redundant vertex on an otherwise straight,
digitized field edge -- produce a determinant that is 0 only up to
rounding, and dividing by that sends the result arbitrarily far from
either line instead of near where they meet.

Fixes the overflow class of ReqHL::generateHeadlands producing a
mainland larger than its input field. A second, distinct failure mode
(area collapsing towards 0 on densely digitized cells) survives this
fix and is still open.
Offsetting hundreds of segments by widely different amounts crosses the
resulting ring many times over. Cleaning that up fell to
LinearRing::filterSelfIntersections, which resolves one crossing at a
time and drops every point between the two segments involved -- most
of a real border, if that crossing is a distant one. A 274-vertex
field collapsed from 6875 m2 to 1 m2 this way.

The exterior ring's offset is cleaned up with a GEOS buffer instead,
which handles the same self-intersections properly and can come back
as more than one mainland cell where the ring legitimately splits --
something a method built to return a single LinearRing never could.
filterSelfIntersections() itself is untouched; getParallelLine() no
longer calls it, since the one caller that needs cleanup now does its
own.
LinearRing::getParallelLine joined each corner with the plain
line-line miter intersection, which has no bound on how far it can
land as the corner sharpens or the two sides' offsets diverge. On
real fields this sent ReqHL's mainland up to 273 m outside the field
it came from, occasionally taking a coverage swath with it. Past 4
times the larger of the two offsets, bevel instead.
@u33549
u33549 changed the base branch from main to develop September 4, 2026 17:20
@pascalzauberzeug pascalzauberzeug added this to the 2.2.0 milestone Sep 8, 2026
@u33549
u33549 force-pushed the dev/umutc/v2.1.0-devel/intersection-tolerance branch from 3bafe0b to ae11ea0 Compare September 18, 2026 17:38
@u33549

u33549 commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Force-pushed while tidying up my fork. The only difference is in CHANGELOG.md: the three entries were accidentally placed under the released [2.1.0] heading and are now under [Unreleased]. No source or test changes.

@Gonzalo-Mier
Gonzalo-Mier merged commit 1ef352c into Fields2Cover:develop Sep 22, 2026
69 of 70 checks passed
@Gonzalo-Mier

Copy link
Copy Markdown
Member

Thank you @u33549 for the PR

@u33549
u33549 deleted the dev/umutc/v2.1.0-devel/intersection-tolerance branch September 22, 2026 13:21
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.

4 participants