Skip to content

Add simplify (Douglas-Peucker) and is_closed_polygon#33

Open
gistrec wants to merge 1 commit into
feature/polyline-encodingfrom
feature/simplify
Open

Add simplify (Douglas-Peucker) and is_closed_polygon#33
gistrec wants to merge 1 commit into
feature/polyline-encodingfrom
feature/simplify

Conversation

@gistrec

@gistrec gistrec commented Jun 11, 2026

Copy link
Copy Markdown
Owner

Summary

Ports the remaining two PolyUtil functions from android-maps-utils, completing upstream parity:

  • geo::simplify(poly, tolerance) — Douglas-Peucker decimation: keeps vertices farther than tolerance meters from the simplified shape, always keeps the endpoints, returns only original input points. Closed polygons (is_closed_polygon) are simplified including the closing segment via the upstream trick of nudging the last point by 1e-11° on a working copy — the input is never modified (unlike the Java original, which mutates and restores the input list).
  • geo::is_closed_polygon(poly) — non-empty path with equal first and last points. Deliberate divergence: equality is the library's approximate LatLng comparison (longitudes modulo 360°), so a ring closing across the antimeridian — (10, 180)(10, -180) — counts as closed; Java compares exactly.

Distances come from distance_to_segment, so its approximation limits apply (documented). Worst-case complexity is O(n²).

Stacked on #32 (feature/polyline-encoding): the simplify tests decode the upstream test fixtures (95-point LINE, OVAL_POLYGON) with geo::decode. CI will run once this is retargeted to master after #32 merges.

Parity validation

All expected sizes from upstream PolyUtilTest reproduce exactly:

Fixture Tolerance (m) Expected size Result
LINE (95 pts) 5 / 10 / 15 / 20 / 50 / 500 / 1000 20 / 14 / 10 / 8 / 6 / 3 / 2
Triangle, open + closed 88 4 / 4
Oval, open + closed 10 13 / 13

Plus the upstream invariants as a shared helper: endpoints preserved, output is an ordered subset of the input, path_length(simplified) ≤ path_length(input).

Docs

  • docs/api.md: new is_closed_polygon and simplify sections, both added to the Path list; error-handling convention extended (simplify returns an owning container, can throw std::bad_alloc).
  • README.md: Polygon utilities bullet mentions Douglas-Peucker simplification; header size claim refreshed (40 → 44 KB).

Test plan

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