Skip to content

Separate algorithms from strategies - #1529

Open
tinko92 wants to merge 9 commits into
boostorg:developfrom
tinko92:refactor/algorithm-strategy-separation
Open

tinko92 wants to merge 9 commits into
boostorg:developfrom
tinko92:refactor/algorithm-strategy-separation

Conversation

@tinko92

@tinko92 tinko92 commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

This PR is part of the larger program outlined in #1520 and separates strategies and algorithms in the following sense:

The strategy supplies predicates and constructions. The predicates take geometries with specific coordinates as input and return answers in topological terms: How triangles are oriented (side strategy), whether two points are the equal or not, sorting intersections along segments and so on.

The algorithm then only reasons about topology, based on the return values of predicates and does not access coordinates directly in any way.

Whether, for example, clusters are compared with a tolerance or not, is the strategy's concern. If it's an exact cartesian strategy, there is no tolerance because it can be resolved exactly, if it's the current default strategy there is one. This should be decided within the strategy to be consistent with other computations also supplied by the strategy. How and with which guarantees those other computations are implemented depends on the specific choice of formulas, calculation types and coordinate system packaged within the strategy. The algorithm side only needs the answer (whether two clusters are the same or not) to carry on.

Out of the PRs proposed on the way towards both faster runtime and robustness guarantees, this is probably the step that touches the most existing code and contains the most software design decision (many interface choices between strategies and algorithms).

After the separation is clean, adding exact strategies introduces a lot of arithmetic code but this will be well-separated from the algorithm code and behind well-defined interfaces, such that it can be reviewed without too much reasoning about unrelated/existing code.



#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/algorithms/convert.hpp>

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.

the strategies folder should actually not depend on algorithms
(though it currently already does, so this is not a painpoint)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree. It is changed to use the point to point conversion utility, which is moved from algorithms to util, which should fit since it only copies coordinates.


#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/algorithms/convert.hpp>
#include <boost/geometry/strategies/cartesian/clip.hpp>

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.

this causes all files using umbrella_strategy to include the "line clipping algorithm after Liang Barsky"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Include removed in favor of a template forward declaration.

{

template <typename Strategy>
class segment_topology

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.

Is this filename right? Shouldn't it be segment_topology.hpp ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Filename updated.

@barendgehrels

Copy link
Copy Markdown
Collaborator

Thanks, I asked AI for an outline, might be useful.


Boost.Geometry · overlay robustness · status 2026-11-29

Robust Overlay Roadmap

How Tinko's program for exact and fast Cartesian strategies (#1520) is cut into PRs, and what the code looks like once step 2, #1529 "Separate algorithms from strategies", is in. Drawn from the #1520 description and the #1529 diff.

The six steps

Each step is one PR (or a few). Steps 3 to 6 only add new strategy code; step 2 is the one that changes existing algorithm code.

step 1: Fixes on the current code merged

The split of #1491: relate fixes #1514, linear overlay #1515, buffer winding #1516, areal graph fixes #1517. The set-ops robustness run is at 0 errors; one buffer regression from #1517 is documented for later.

step 2 · #1529 : Separate algorithms from strategies in review

Algorithms stop reading coordinates. Every decision that compares or computes coordinates goes through an accessor on the umbrella strategy. Today's behavior is kept: the default accessors contain the existing code, tolerances included. 62 files, about +1300 / −1070.

step 3 : Simple Cartesian strategy proposed

Plain double, no tolerances, no consistency repair. The fastest option (about 0.36 to 0.58 of develop's run time in Tinko's benchmarks), but not robust. An opt-in.

step 4: Exact Cartesian strategies proposed

Filtered predicates: compute in double, compare with a cheap error bound, and only recompute with extended precision when the bound can't decide. Predicates also run on the implicit, exact intersection points. Three output policies: approximate output (proposed default), output within N ULP, correctly rounded.

step 5: Decimal strategy proposed

An option for users who expect WKT decimals to be taken literally (#1201, #704, #663, #669). Reuses the step 4 machinery.

step 6 Buffer work proposed

Using the new strategies in buffer.

The layers after #1529

Three layers. The algorithms ask questions in topological terms (same point or not, left or right, inside or outside, which comes first). The seam is a set of accessors on the umbrella strategy. Behind the seam sits one implementation per strategy. Today all three coordinate systems inherit the same default implementations from umbrella_strategy; the new strategies of steps 3 to 5 would override them.

algorithm code (topology only) strategy seam (new in #1529) existing strategies planned strategies (steps 3 to 5)

Accessor names as in #1529. segments() is implemented by segment_topology (in strategies/detail/segment_contacts.hpp); contacts forwards to the existing relate() segment intersection.

Where the overlay pipeline crosses the seam

The overlay runs in six phases. Below each phase: the accessor it calls since #1529, and what used to be inline code before. This is where the exact strategies of step 4 would change answers; nothing in the algorithm boxes needs to change for that.

Buffer uses the same pipeline and adds buffer_corner() (corner of two offset lines), section().preceding in piece turns, and ring_topology().area_sign when selecting rings.

Every accessor, by caller

Accessor Called from Default implementation Kind
segments().contacts get_turn_info, get_intersection_points, intersection_insert, linear disjoint, closest points the existing relate() segment intersection predicate + construction
segments().direction, position, signed_distance turn info helpers, graph (counts, edges, difference paths) direction_code, get_position_code, get_distance_measure (moved to strategies/detail) predicate
segments().same_turn_point, same_source_vertex, coincident select_edge, clustered difference paths, turn verification approximately_equals at least in double; exact compare; squared distance equals 0 predicate
section() sectionalize, get_turns, buffer piece turns section_with_tolerance predicate
clustering() get_clusters via handle_colocations sweep_clustering (tolerance, exact for integers) predicate
ring_topology() assign_parents, add_rings, buffer ring selection area_ring_topology predicate
box_topology(), box_intersection() overlaps, touches, box/box intersection, rtree content code moved from the algorithms predicate + construction
clip(), convert_output() linestring/box clipping, convert_ring, intersection_insert Liang–Barsky, geometry::convert construction
buffer_corner() buffer_inserter former line_line_intersection construction
points() hook relate/disjoint/within/equals on points falls back to relate(a, b) and geometry::less predicate
location() hook relate point/geometry falls back to point_in_geometry predicate

"Hook" means the algorithm checks at compile time whether the strategy has the accessor, and otherwise uses the old code. In #1529 no strategy provides them yet; they are there for steps 3 and 4.

Why this split matters for exactness

Predicates decide, constructions produce

A predicate answers a yes/no or left/right question; a construction computes new coordinates. Wrong overlay results come from predicates that disagree with each other, such as an intersection ratio that orders two turns differently from the side tests (as in #1522). Exact predicates make every decision consistent, even when the output coordinates are rounded.

Filtered, so mostly fast

Compute in double, check the result against an error bound, and only fall back to extended precision when the bound can't decide. Shewchuk's orient2d in util/precise_math.hpp already works this way for the side test; step 4 extends it to intersection points that exist only implicitly.

What #1529 does not do

  • No new arithmetic: answers should be identical to develop.
  • Spherical and geographic still use the same defaults as Cartesian.
  • The tolerances (approximately_equals, scaled epsilon) remain, now inside the strategy.

Superseded side tracks

Sources: PR #1520 description (roadmap and benchmarks) and the #1529 diff at ec22f18 (accessor names and call sites). Benchmarks are Tinko's, not reproduced here.

@barendgehrels

Copy link
Copy Markdown
Collaborator

With some visuals


Screenshot 2026-11-29 at 18 41 15
Screenshot 2026-11-29 at 18 41 23

@barendgehrels

Copy link
Copy Markdown
Collaborator

Thanks a lot and great it is separated into steps now.

I'll continue my review tomorrow. Basically it works without any regression, and the separation looks very useful.

Just have to think a bit more about the namings.

@tinko92
tinko92 force-pushed the refactor/algorithm-strategy-separation branch from ec22f18 to 4681637 Compare November 30, 2026 05:19
namespace boost { namespace geometry { namespace strategies { namespace detail
{

enum class location { exterior = -1, boundary = 0, interior = 1 };

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.

👍

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.

2 participants