Skip to content

Faster strict intersection tests of polygons with holes - #150

Open
fontanf wants to merge 2 commits into
mainfrom
intersection-tree-shape-aabbs
Open

fontanf wants to merge 2 commits into
mainfrom
intersection-tree-shape-aabbs

Conversation

@fontanf

@fontanf fontanf commented Oct 22, 2026

Copy link
Copy Markdown
Owner

Summary

Speeds up the strict intersection tests between shapes with holes made of line segments, used e.g. by PackingSolver's Solution::compute_overlapping_items.

On a solution of the alma_02 instance (15 bins, 542 polygons of up to 1044 vertices), compute_overlapping_items goes from 3.33 s to 0.165 s.

Changes

IntersectionTree: the bounding boxes of the shapes, computed when building the tree, are now stored and used to skip the pairs of shapes which can't intersect, in compute_intersecting_shapes and in the intersect queries. For strict intersections, the interiors of the boxes must overlap; otherwise, boxes closer than the tolerance are still tested. alma_02: 3.33 s → 2.19 s.

intersect(ShapeWithHoles, ShapeWithHoles, strict = true): it computed the boolean intersection of the shapes. For shapes made of line segments (outline and holes), it now:

  1. returns false if the bounding boxes don't intersect, or if an edge of the convex hull of an outline separates it from the other one (exact test);
  2. splits each boundary (outline and holes) of each shape at its contacts with the boundary of the other shape (compute_intersections on the pairs of edges whose boxes intersect), and returns true if the middle of a part is strictly inside the other shape (ShapeWithHoles::contains(point, true)); two consecutive edges without contacts are on the same side, only one of them is tested;
  3. otherwise, the interiors are either disjoint or equal: tests a point strictly inside each shape (find_point_strictly_inside).

The shapes with circular arcs still use the boolean intersection. alma_02: 2.19 s → 0.165 s (2.8 ms → 0.17 ms per pair).

Tests

  • New test StrictPolygonsSameAsBooleanIntersection: 20000 random pairs of polygons with holes on a grid (rectangles, L, U and comb shapes, triangles, a diamond, shapes with one or two holes, a frame), rotated by multiples of 90 degrees and shifted by half units, so that touching edges, shared vertices and colinear overlapping edges are frequent. The result is compared with the boolean intersection: no mismatch (2993 intersecting pairs). Also run once with 200000 pairs and another seed: no mismatch (29471 intersecting pairs).
  • All the tests pass. PackingSolver's irregular tests pass with this version.

… intersect

The tree computed the bounding boxes of the shapes when built, but didn't
keep them: all the pairs of shapes of the same leaf were then tested with
'intersect', which is expensive. The boxes are now stored and used to skip
the pairs of shapes which can't intersect, in 'compute_intersecting_shapes'
and in the 'intersect' queries. For strict intersections, the interiors of
the boxes must overlap; otherwise, boxes closer than the tolerance are
still tested.
The strict intersection test of two shapes with holes computed their boolean
intersection. For shapes made of line segments, it now:
- returns false if the bounding boxes don't intersect, or if an edge of the
  convex hull of an outline separates it from the other one;
- splits each boundary (outline and holes) of each shape at its contacts with
  the boundary of the other shape, and returns true if the middle of a part
  is strictly inside the other shape;
- otherwise, the interiors are either disjoint or equal: tests a point
  strictly inside each shape.

The shapes with circular arcs still use the boolean intersection.

Add a test comparing it with the boolean intersection on random pairs of
polygons with holes on a grid (touching edges, shared vertices and colinear
overlapping edges).
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.

1 participant