Skip to content

v0.5.0

Latest

Choose a tag to compare

@LiorSinai LiorSinai released this 24 Oct 09:07
· 2 commits to main since this release
64382c3

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_FACES and MERGE_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_rule keyword of the boolean operations: EVEN_ODD (default), NON_ZERO, POSITIVE and NEGATIVE. For example, union_geometry(poly; fill_rule=PolygonAlgorithms.NON_ZERO). [#42]
  • Face selection strategies for the output, via the face_selection keyword [#35]:
    • SPLIT_FACES (default): smaller polygons with fewer explicit holes.
    • MERGE_FACES: fewer, larger polygons with holes.
    • CLOCKWISE_FACES and COUNTER_CLOCKWISE_FACES.
  • PolygonAlgorithms.segments_to_paths and PolygonAlgorithms.segments_to_polygons convert annotated segments to paths and polygons. segments_to_paths returns (exteriors, holes, lines), where lines are the zero-area faces. These are returned after the polygons in boolean results.
  • any_intersect is now exported.
  • Algorithm structs are now exported.
  • atol is accepted and passed through by Polygon, validate_polygon, fully_contains, on_border, on_segment, intersect_edges, is_clockwise and is_counter_clockwise.
  • For testing, PolygonAlgorithms.are_equivalent_polygons compares polygons, or collections of polygons, up to rotation. It uses the new functions PolygonAlgorithms.cyclic_equality and PolygonAlgorithms.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_coords and y_coords now return views. Backwards compatible.
  • Tolerances [#44, #46, #49]:
    • The fuzzy_rtol keyword of the Martinez-Rueda algorithm has been removed. Fuzzy closing of chains is no longer needed.
    • rtol and PolygonAlgorithms.default_rtol have been removed.
    • There is a single absolute tolerance atol and 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 if cross² ≤ atol² max(|pq|², |qr|², |rp|²). Previously it was |cross| ≤ atol.
      • intersect_geometry(segment1, segment2): segments are parallel if det² ≤ atol² * max(|ΔP|², |ΔQ|²). Previously it was |det| < atol. Then an intersection may be up to atol past either end of each segment. Previously it was a proportion rtol of the segment length, and only before the start. The intersection point is now clamped to segment1.
      • on_segment(point, segment): the point is on the segment if it is within atol of the closest point on the segment. Previously it had to be colinear and inside the segment's bounding box expanded by atol, so a point could be up to √2 * atol past the end.
      • is_clockwise and is_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 eps instead of atol.
    • intersect_geometry(line1::Line2D, line2::Line2D): no longer accepts atol. The lines are parallel if the determinant is at most eps(T). Previously atol.

Internal:

  • convert_to_event_queue is renamed to convert_to_events and no longer returns a sorted queue. any_intersect sorts its input queue in place.
  • SegmentAnnotations now stores the winding information for fill rules. Backwards compatible.
  • Polygon struct uses concrete types for the holes now. Backwards compatible.
  • validate_polygon (and Polygon(...; 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 sort requires. [#46]
  • Weiler-Atherton now works with non-Float64 inputs. [#46]
  • first_moment, area_polygon and centroid_polygon now return the input's float type (e.g. Float32) instead of always Float64.
  • do_intersect no 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_geometry for segments now agrees with do_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-17 to 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! in event_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_point now 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.
  • bounds drops 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! with create_segment_pair and removes add_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!.
  • validate polygon now does one sweep for all holes. This is made possible by sorting the queue once.
  • minor improvements to Polygon struct and code.

Known issues

  • Results can depend on the tolerance. See the robustness section in the README.