Repository navigation
PolygonAlgorithms v0.5.0 Release Notes
This release rewrites how the Martinez-Rueda algorithm turns segments back into polygons, adds fill rules for self-intersecting polygons, fixes many robustness and tolerance bugs and implements several small refactors to optimise code.
The new segment to polygons algorithm uses graph face computation which has the following benefits:
- More predictable: exteriors are always counter-clockwise, holes are always clockwise.
- More flexible: there are 2 main modes,
SPLIT_FACESandMERGE_FACES. - Removes the "There are still open chains" error that was a major pain point of this package.
The boolean operations now pass all 195 tests from the Clipper2 test suite [#33]. Previously on v0.4 they passed 152 tests, which was 94% of the EVEN_ODD fill rule tests and 78% overall.
New features
- Fill rules for self-intersecting polygons, via the
fill_rulekeyword of the boolean operations:EVEN_ODD(default),NON_ZERO,POSITIVEandNEGATIVE. For example,union_geometry(poly; fill_rule=PolygonAlgorithms.NON_ZERO). [#42] - Face selection strategies for the output, via the
face_selectionkeyword [#35]:SPLIT_FACES(default): smaller polygons with fewer explicit holes.MERGE_FACES: fewer, larger polygons with holes.CLOCKWISE_FACESandCOUNTER_CLOCKWISE_FACES.
PolygonAlgorithms.segments_to_pathsandPolygonAlgorithms.segments_to_polygonsconvert annotated segments to paths and polygons.segments_to_pathsreturns(exteriors, holes, lines), wherelinesare the zero-area faces. These are returned after the polygons in boolean results.any_intersectis now exported.- Algorithm structs are now exported.
atolis accepted and passed through byPolygon,validate_polygon,fully_contains,on_border,on_segment,intersect_edges,is_clockwiseandis_counter_clockwise.- For testing,
PolygonAlgorithms.are_equivalent_polygonscompares polygons, or collections of polygons, up to rotation. It uses the new functionsPolygonAlgorithms.cyclic_equalityandPolygonAlgorithms.cyclic_set_equality. [#39, #41]
Breaking changes
- Boolean operations (
intersect_geometry,union_geometry,difference_geometry,xor_geometry) build their output with graph face computation instead of segment chaining. Results are equivalent, but:- Exteriors are always counter-clockwise and holes are always clockwise.
- Polygons may start at a different vertex, and regions may come out in a different order. The order of vertices and regions can also differ between Julia versions, so compare results up to rotation (e.g. with
PolygonAlgorithms.are_equivalent_polygons), not with==.
x_coordsandy_coordsnow return views. Backwards compatible.- Tolerances [#44, #46, #49]:
- The
fuzzy_rtolkeyword of the Martinez-Rueda algorithm has been removed. Fuzzy closing of chains is no longer needed. rtolandPolygonAlgorithms.default_rtolhave been removed.- There is a single absolute tolerance
atoland it is always a distance. The new tolerances are as follows. (See README.md or docstrings for geometric interpretations):get_orientation(p, q, r): the points are colinear ifcross² ≤ atol² max(|pq|², |qr|², |rp|²). Previously it was|cross| ≤ atol.intersect_geometry(segment1, segment2): segments are parallel ifdet² ≤ atol² * max(|ΔP|², |ΔQ|²). Previously it was|det| < atol. Then an intersection may be up toatolpast either end of each segment. Previously it was a proportionrtolof the segment length, and only before the start. The intersection point is now clamped tosegment1.on_segment(point, segment): the point is on the segment if it is withinatolof the closest point on the segment. Previously it had to be colinear and inside the segment's bounding box expanded byatol, so a point could be up to√2 * atolpast the end.is_clockwiseandis_counter_clockwise: a polygon is neither if|first_moment| ≤ atol * perimeter / 2. Previously a polygon with exactly zero area was both.
The same check is used when converting segments to polygons, to separate zero-area faces (lines) from exteriors and holes.
- Vertical segments are detected with
epsinstead ofatol. intersect_geometry(line1::Line2D, line2::Line2D): no longer acceptsatol. The lines are parallel if the determinant is at mosteps(T). Previouslyatol.
- The
Internal:
convert_to_event_queueis renamed toconvert_to_eventsand no longer returns a sorted queue.any_intersectsorts its input queue in place.SegmentAnnotationsnow stores the winding information for fill rules. Backwards compatible.Polygonstruct uses concrete types for the holes now. Backwards compatible.validate_polygon(andPolygon(...; validate=true)) now also rejects holes that intersect each other. Hole intersection errors are reported with a single message: "Invalid holes: holes intersect with the exterior or themselves."
Bug fixes
- Martinez-Rueda gave wrong fills for polygons with edges sticking out, e.g. spikes [#43, #45, #47].
bounds(::Vector{<:Polygon})always threw an error. [#46]- A warning is now given when a hole is not inside any polygon and is dropped from the output.
convex_hull(GrahamScanAlg(), points)threw for fewer than 3 points. [#46]- Comparison functions used for sorting by angle are now strict, as
sortrequires. [#46] - Weiler-Atherton now works with non-
Float64inputs. [#46] first_moment,area_polygonandcentroid_polygonnow return the input's float type (e.g.Float32) instead of alwaysFloat64.do_intersectno longer reports an intersection in the general case when one of the points is colinear within tolerance; these are handled by the on-segment checks instead. [#49]intersect_geometryfor segments now agrees withdo_intersect. See Tolerances section above.[#49]- Converting segments to polygons could give wrong regions when end points that differ only by floating point error were rounded in different directions, e.g.
0.0078125 ± 5.6e-17to 6 decimal places. End points are now snapped to a neighbouring grid point if one is within half a grid spacing. This fixes the Hilbert curve example for orders 6 and 7. [#49] popfirst!inevent_loop!could result in a failure of the whole loop if an event was inserted in front of the head. This is an edge case that is more common for larger atol.
Refactoring Optimisations
Quick wins to optimise code, assisted by Claude AI, with a few minor fixes along the way. [#48]
is_same_pointnow compares the square magnitudes instead of square rooting.contains(point in polygon): added an initial bounding box check which is fast and also fixes a tolerance bug.boundsdrops redundant min and max arguments.WeilerAthertonAlg- remove vector allocation in loops (vcat). This reduces memory allocation by a factor of 1x - 65x . The Hilbert Order 6 saw the biggest changes, from ~260MB to <10MB. Computation time did not change much.MartinezRuedaAlg: overall memory and speed gains is 2x - 10x.- line sweep: sort the event queue once rather than insert in order on queue creation. This replaces
add_segment_event!withcreate_segment_pairand removesadd_annotated_segment!. This reduces memory by ~2x and improves time by 1-2x. - segment event: copy events without
deepcopy. This reduces the memory requirement in the benchmarks by 1.5x to 2x. It results in speed ups of up 1.5x as well. - Check for object inequality with the head
!==instead of field inequality!=. This is more correct as well: the check is to see if the head was altered. - use binary search for
pop_key!.
- line sweep: sort the event queue once rather than insert in order on queue creation. This replaces
- validate polygon now does one sweep for all holes. This is made possible by sorting the queue once.
- minor improvements to
Polygonstruct and code.
Known issues
- Results can depend on the tolerance. See the robustness section in the README.