Repository navigation
feat(area): solve a plaza at query time when both ends are on it - #7685
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends OSRM’s open-area routing so that when both endpoints of a leg lie within the same open area (e.g., a plaza), the engine can compute an in-area shortest path at query time (geodesic within the polygon), instead of relying on a mesh that is primarily optimized for “get out to an entry point”.
Changes:
- Change open-area meshing to keep the union of shortest-path trees rooted at entry points (pruned-by-default), with the full visibility graph still optionally available.
- Add a query-time open-area geodesic solver with a per-thread bounded LRU cache, and use it in the route plugin to rewrite eligible legs.
- Fix geometry assembly deduplication so it only drops points when both node id and coordinate repeat; update tests, docs, benchmarks, and debug tooling accordingly.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| unit_tests/extractor/area/area_mesher.cpp | Updates unit test expectations for pruned-vs-whole visibility graph behavior. |
| unit_tests/engine/area_geodesic.cpp | Adds unit coverage for query-time geodesic computation and caching behavior. |
| src/extractor/area/area_mesher.cpp | Changes pruning to keep full shortest-path trees (not just entry-to-entry paths). |
| src/engine/plugins/viaroute.cpp | Applies open-area geodesic leg rewriting before leg collapsing. |
| src/engine/area_route.cpp | Implements leg rewriting to geodesic paths and adjusts waypoint/phantom handling. |
| src/engine/area_geodesic.cpp | Implements visibility-graph build + Dijkstra geodesic solver with per-thread cache. |
| src/benchmarks/CMakeLists.txt | Adds a dedicated benchmark target for the geodesic solver. |
| src/benchmarks/area_geodesic.cpp | Benchmarks visibility computation, graph build, and Dijkstra costs. |
| scripts/debug/area_snapping_report.py | Updates debug report tooling and adds an “inside-to-inside” visualization case. |
| scripts/debug/area_pruning_stages.py | New debug tool to visualize pruning stages and compare quality/cost. |
| include/extractor/profile_properties.hpp | Changes default to pruned mesh; clarifies meaning of area_emit_visibility_graph. |
| include/extractor/area/area_mesher.hpp | Updates mesher documentation and default for emit_visibility_graph. |
| include/engine/guidance/assemble_geometry.hpp | Makes point deduplication require both repeated node id and coordinate. |
| include/engine/area_route.hpp | Declares the geodesic-based leg rewrite entry point and documents behavior. |
| include/engine/area_geodesic.hpp | Declares geodesic solver API and exposes cache/limit constants. |
| features/foot/area_snapping.feature | Switches assertions to encoded geometry and adds inside-to-inside scenarios. |
| docs/areas.md | Updates documentation to reflect pruned-by-default mesh and when whole graph is needed. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| route.shortest_path_weight = | ||
| route.shortest_path_weight + route.leg_endpoints[leg].target_phantom.forward_weight; |
| std::unordered_map<std::uint64_t, std::list<Entry>::iterator> index; | ||
|
|
||
| static std::uint64_t hash(const Key &key) | ||
| { return (static_cast<std::uint64_t>(key.first) << 32) ^ (key.second * 0x9e3779b97f4a7c15ULL); } | ||
|
|
||
| const SolvedArea *find(const Key &key) | ||
| { | ||
| const auto found = index.find(hash(key)); | ||
| if (found == index.end() || found->second->key != key) | ||
| { | ||
| return nullptr; | ||
| } | ||
| entries.splice(entries.begin(), entries, found->second); | ||
| return &entries.front().area; | ||
| } | ||
|
|
||
| const SolvedArea &put(const Key &key, SolvedArea area) | ||
| { | ||
| entries.push_front(Entry{key, std::move(area)}); | ||
| index[hash(key)] = entries.begin(); | ||
| while (entries.size() > GEODESIC_CACHE_SIZE) | ||
| { | ||
| index.erase(hash(entries.back().key)); | ||
| entries.pop_back(); | ||
| } |
94a69a7 to
a55fabb
Compare
a0387d5 to
aea31fc
Compare
a55fabb to
722ab49
Compare
aea31fc to
3e76217
Compare
722ab49 to
cb9fb70
Compare
3e76217 to
1b93bc2
Compare
Meshing an area kept the whole visibility graph, because the pruning it
had was not good enough to route from a coordinate inside the area: it
kept only the edges carrying a shortest path between two entry points,
and a coordinate inside may want a chord no pair of entry points had a
reason to keep.
But the whole graph is more than is needed. Such a coordinate sets off
towards a vertex it can see and then wants the shortest way out, so the
baked mesh has to hold a shortest path from every vertex to every entry
point -- not between every pair of vertices. That is a shortest-path
tree rooted at each entry point, and run_dijkstra already computes one
per entry point; it simply discarded everything except the paths joining
them. Walking the predecessor array back from every vertex rather than
only from the other entry points keeps the tree, at no extra search
cost, and is exactly as good as the whole graph for any journey with one
end at an entry point.
The forest grows as entry points times vertices where the whole graph
grows as vertices squared, so it is smaller and gets relatively smaller
as areas get bigger:
vertices 8 20 40 68 104 148
whole 21 104 375 1012 2063 3176
forest 21 67 146 257 399 573
On Monaco, 359 mesh ways against 420, and 10 762 edge-expanded nodes
against 10 906. On the report fixtures the worst excess over the
a-priori optimum is 0.1 m either way -- the same number the whole graph
gives.
area_emit_visibility_graph therefore defaults to false. It now buys
exactly one thing: a journey that begins and ends inside the same area,
which wants a path between two arbitrary vertices rather than a path
out. On plaza 400x400m with two obstacles that is worth 112 m -- 385.7 m
against 497.3 m. That journey is approximate for a second reason anyway,
since neither endpoint inserts an edge to the other, so it routes by way
of a shared vertex whichever mesh is baked.
features/foot/area_snapping.feature pins both halves: p to g asserts the
exact route, p to s the longer one, so closing the gap will show up as a
failing expectation rather than a silent change.
scripts/debug/area_pruning_stages.py draws the three options for a
40-vertex area, reading each edge back out of the built graph rather
than modelling it.
A journey that begins and ends on one plaza is the case the mesh cannot
answer. It holds shortest paths *out* of an area, and this journey never
leaves, so it was routed by way of whichever vertex both ends happened to
share -- and where the two could simply see each other, which on a plaza
with nothing in it is every pair, the route left by a corner and came
back. Neither the pruned mesh nor the whole visibility graph fixes that:
the fault is that both endpoints are joined to the graph and never to
each other.
So the answer is worked out when asked, from the polygon the engine
already carries for snapping. The straight line when the two points can
see each other, otherwise Dijkstra over the area's visibility graph.
Measured against the a-priori optimum:
before now optimum
no obstacles 536.1 m 320.3 m 320.6 m
centre obstacle 386.5 m 386.5 m 386.5 m
two obstacles 497.3 m 385.8 m 385.7 m
The graph for an area is built on first use and kept, because a plaza
asked about once tends to be asked about again. The cache is per thread,
following SearchEngineData, so no request waits on another and a thread
that never touches an area pays nothing. It is keyed on the facade's
checksum as well as the area, so a reloaded dataset cannot reuse a stale
graph, holds 32 areas, and declines an area of more than 40 vertices.
src/benchmarks/area_geodesic.cpp puts that limit in context: 7.5 us for
the eight-vertex areas that are typical, 96 us at twenty, half a
millisecond at forty against a budget of one. Monaco's largest meshed
area has twenty-four.
The route is written into the leg directly, because it runs along lines
of sight that are deliberately not edges of the graph and so cannot be
unpacked from one. Two things that needs care about:
* assembleGeometry names the first node of a leg from the source
phantom's own segment and then drops any path point that repeats it.
For a leg beginning at a free point that name is arbitrary, and when it
landed on the first bend the bend was dropped and the drawn line cut
the very corner it was there to turn -- reported 500.2 m against 461.9
m drawn. The phantom is now stood on a different segment nearby, which
supplies only a name and a node id.
* PathData::from_edge_based_node is fed to GetNameIndex, so it holds an
edge-based node and not the node-based id of the bend. The plaza's own
way names every stretch of the leg.
The geodesic is taken without comparing it against the leg the search
found, because the two do not measure the same journey: a routed leg runs
between the *snapped* points and leaves the walk to each of them out of
its distance, and when both ends snap to one vertex it reports nothing at
all. A route that left the area to come back could only win on ways
faster than the area itself, which is not a case that arises for a
profile that meshes plazas.
Sixteen unit tests cover the solver, including a point to itself, points
half a metre apart, points outside and inside an obstacle, rings of two
and of no vertices, an area one vertex over the limit and one exactly at
it, repeated and collinear vertices, points on a corner and on an edge, a
concave boundary whose inner corner is the only bend, a wall with a
one-metre gap at each end, symmetry, cache eviction, and that the length
reported always matches the path reported. Five scenarios in
features/foot/area_snapping.feature cover it end to end.
A route that begins or ends inside an open area is snapped to a vertex of it and then walks there in a straight line, sometimes for hundreds of metres. That walk was charged to the search but drawn nowhere: the line started at the vertex, the distance left the walk out, and the response reported it as a snapping error instead. It is now a leg of the journey like any other. The vertex becomes a point of the path rather than a moved endpoint -- the walk and the ways beyond it run in different directions, so a line straight from the request to the far end would cut across whatever lies between -- and the waypoint reports the coordinate that was asked for, with no offset. That needed a correction in assembleGeometry, which skipped any path point repeating the node before it. The name alone is the wrong test. A leg can begin at a free point, and there the first node is a place the line genuinely travels to, however the phantom's own segment happens to be named; comparing names deleted the first turn of the route, and the drawn line then cut the corner it was there to turn -- 500.2 m reported against 461.9 m drawn on one fixture. Comparing positions alone is wrong too: map matching leans on segments of no length between distinct nodes to carry their annotations, and collapsing those changed the matched geometry of features/car/ferry.feature and the annotations of features/testbot/matching.feature. A point is skipped when it repeats the one before it in both senses, which leaves every existing route as it was. With that in place the phantom no longer has to be stood on a different segment to keep its first bend, so that workaround goes, and with it the dependence of a leg's end labels on which phantom the search happened to keep. Also fixes an assertion in the mesher tests that had gone stale: since the pruning became shortest-path trees the pruned mesh can equal the whole visibility graph on a small area, which is the point of it. Both now assert that every obstacle corner keeps an edge. Verified across the full cucumber matrix -- both algorithms, all three load methods, 8808 scenarios.
The area snapping scenarios asserted a:nodes, which turned out to be the weakest thing available. The first and last entries of that list name the segments the source and target phantoms sit on, and a leg that begins or ends at a free point keeps whichever phantom the search settled on -- so the list differed between CH and MLD for the same route, and four rows had to be weakened to a distance check to keep both passing. The geometry does not have that problem. It is a property of the route rather than of the search that found it, both algorithms agree on it, and it is the thing these scenarios are actually about: that a route starts where it was asked to, turns where it has to, and nowhere else. Every row now pins the encoded line, with the distance beside it to be read by a human. The rows weakened earlier are back to asserting something. Requests ask for overview=full, so the line is the one the router produced rather than a simplified rendering of it, and annotations are no longer requested at all.
geodesic_between keeps an area's visibility graph under a key of the dataset's checksum and the area's offset. A checksum says what a dataset contains, not which dataset it is, and one process can serve several in turn -- osrm-datastore swaps them under a running server. Two areas from two datasets can therefore arrive wearing the same key, and the second was answered from the first one's graph: a plaza with a block across it returning the straight line that belongs to an empty one. It only showed under the datastore load method, because mmap and directly start a fresh process per scenario and so a fresh thread-local cache. It presented as flakiness -- features/foot/ failing four scenarios, then three, then none -- and was twice mistaken for shared-memory contention. The key is now only a way of finding a candidate. The cached area's own vertices are compared against the rings that were passed in, and a mismatch rebuilds; that costs a walk over at most GEODESIC_MAX_VERTICES coordinates against building the graph again. features/foot/ under ch datastore now passes three runs out of three.
1b93bc2 to
758d0d2
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7685 +/- ##
========================================
Coverage 94.46% 94.46%
========================================
Files 512 516 +4
Lines 39823 40336 +513
========================================
+ Hits 37619 38105 +486
- Misses 2204 2231 +27 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Second of three. #7684 has merged, so this now targets master and is its own five commits.
#7684 lets a route start or end inside an area. It cannot yet answer a journey with both
ends inside one area, because the mesh only holds paths that reach an entry point. This one
does, without making the mesh any larger.
2145 lines, of which 851 are engine code:
src+includePrune the mesh, and prune by default
The mesher stored the whole visibility graph, O(n^2) edges. It now keeps the union of the
shortest-path trees rooted at the entry points, O(k*n).
This is not an approximation. Any journey with one end at an entry point has a shortest path
that lies entirely within the tree rooted there, so the forest answers every question the full
graph could. Measured over 390 interior points against 4 exits on a 400x400 m plaza: 0.000000 m
difference. Emitting the full graph remains available behind
emit_visibility_graph, and theprune is now the default.
Solve the plaza at query time
For the case the forest genuinely cannot answer, both ends inside, the engine builds the
visibility graph for that one area when it is asked, and reuses it from a small per-thread
LRU cache. The straight line is tried first and the graph is only built when something is in
the way.
The budget is a millisecond and the measurements are well inside it:
Bounded at
GEODESIC_MAX_VERTICES = 40; the largest area in Monaco has 24. Above the boundthe leg is left as the search returned it.
Draw the walk
assembleGeometrydropped a coordinate that repeated the previous one, which silently ate thefirst and last points of a leg the engine had written itself. It now needs the node id and
the coordinate to repeat before it drops a point. Map matching depends on the old behaviour
for zero-length segments between distinct nodes, which is why the node id stays in the test:
loosening it to compare coordinates alone breaks
features/car/ferry.featureandfeatures/testbot/matching.feature.Do not answer one area from another area's cache
The cache was keyed on
(dataset checksum, area offset). A checksum says what a datasetcontains, not which one it is, and
osrm-datastoreswaps datasets under a running server, sotwo areas could arrive wearing the same key and the second was answered from the first one's
graph: a plaza with a block across it returning the straight line that belongs to an empty
one. The key now only finds a candidate, and the cached area's own vertices are checked
against the rings passed in.
It only showed under the
datastoreload method, sincemmapanddirectlystart a freshprocess per scenario. It presented as flakiness in
features/foot/and was twice written offas shared-memory contention.
Tests
The cucumber scenarios moved from asserting node lists to asserting encoded geometry, so they
no longer depend on which algorithm answered. See #7683 for why the node ids at the ends of a
leg are not stable. 17 unit cases on the solver, including degenerate rings, a coordinate on a
vertex, a coordinate outside the area, and the two-areas-one-key case above.
Written with AI assistance: Claude Code, Claude Opus 5 🤖