Skip to content

fix(area): try every area holding a coordinate, not just the first - #7718

Merged
DennisOSRM merged 1 commit into
masterfrom
fix/area-every-area-holding-coordinate
Sep 8, 2026
Merged

DennisOSRM merged 1 commit into
masterfrom
fix/area-every-area-holding-coordinate

Conversation

@DennisOSRM

Copy link
Copy Markdown
Collaborator

Issue

Refs #7683, the first of its known defects.

Problem

A plaza route could leave by a corner and come back, and a plaza request cost about 288 ms against 0.8 ms for a comparable route touching no area. Those are one defect seen from two sides: the engine gave up on an area rather than trying the next, and did so only after paying the full price of deciding it could not use that one.

Areas overlap, and one holding a coordinate can have nothing to offer. The extractor records a polygon into .osrm.openareas before the AreaMesher::max_vertices valve decides whether to mesh it, so a plaza too large to mesh is stored with no way on any of its vertices. The r-tree also answers with bounding boxes, so a coordinate is filed under areas it is not inside at all. Three places then gave up on the first candidate:

  • SnapInsideOpenArea returned inside its loop over the areas, so only the first one containing the coordinate was considered; the request fell back to ordinary snapping onto the perimeter, which is the route that goes out and comes back.
  • commonArea returned the first shared area, and the geodesic replacement moved on when geodesic_between declined it.
  • geodesic_between built the visibility graph before testing containment, so every bounding-box near miss was charged the cubic price of an answer already lost.

crosses_ring and on_any_ring also scanned a whole ring after the answer was known, once per vertex per ring on the snapping path.

Fix

Every area holding the coordinate is tried before giving up, commonAreas and geodesicAcross try each shared area in turn, containment is tested before the graph is built, and the ring scans stop early.

Verification

Neither area_snapping.cpp nor area_route.cpp had a test. Both have one now, sharing a facade mock that carries polygons: an area whose vertices carry no way, standing for one the mesher declined, and a triangle whose bounding box claims coordinates it does not hold. Reverting the three source files fails exactly the three new cases. Area scenarios pass on CH and MLD.

Tasklist

  • self-review code for correctness and following the coding guidelines
  • add tests (see testing)
  • review
  • adjust for comments

Requirements / Relations

None.

🤖 Claude Code, Claude Fable 5.1

A plaza route could leave by a corner and come back, and a plaza request
cost about 288 ms against 0.8 ms for a comparable route touching no area.
Those are the same defect seen from two sides: the engine gave up on an
area rather than trying the next, and it did so only after paying the
full price of deciding it could not use that one.

Areas overlap, and one holding a coordinate can have nothing to offer.
The extractor records a polygon into `.osrm.openareas` sixteen lines
before the `AreaMesher::max_vertices` valve decides whether to mesh it,
so a plaza too large to mesh is stored with no way on any of its
vertices. The r-tree also answers with bounding boxes, so a coordinate is
filed under areas it is not inside at all.

Three places then gave up on the first candidate:

* `SnapInsideOpenArea` returned inside its own loop over the areas, so
  only the first one containing the coordinate was ever considered. The
  `return` after the loop, which exists for "none of them had anything",
  was unreachable whenever any area contained it. The request fell back
  to ordinary snapping, which projects onto the perimeter, and that is
  the route that goes out and comes back.
* `commonArea` returned the first shared area, and `useGeodesicWhereShorter`
  and `useGeodesicInTable` moved on when `geodesic_between` declined it.
  It is now `commonAreas`, and `geodesicAcross` tries each in turn.
* `geodesic_between` built the visibility graph before testing
  containment, deliberately, so a run of queries against one area paid
  for the graph once. That only holds while exactly one area is ever
  asked about. Building is cubic in the vertex count and the containment
  test is linear, so every bounding-box near miss was charged the full
  price of an answer already lost -- and trying several areas would have
  multiplied it. The test now comes first. No answer changes; only when
  the cache is populated does.

`crosses_ring` and `on_any_ring` also scanned an entire ring after the
answer was known, because `for_each_edge` cannot stop early.
`visible_vertices` calls them once per vertex per ring, so that was a
factor of the ring's length on the snapping path.

Neither `area_snapping.cpp` nor `area_route.cpp` had any test, which is
how this survived. Both have one now, sharing a facade mock that carries
polygons: an area whose vertices carry no way, standing for one the
mesher declined, and a triangle whose bounding box claims coordinates it
does not hold. Reverting the three source files fails exactly the three
new regression cases and nothing else.

Refs #7683
Copilot AI lite review requested due to automatic review settings September 8, 2026 08:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

This PR fixes open-area routing/snapping regressions by iterating over all candidate open areas instead of prematurely giving up on the first match, and by avoiding expensive geodesic work for bounding-box near misses.

Changes:

  • Update snapping and table/route geodesic selection to try all overlapping/shared areas before falling back.
  • Reorder geodesic computation to test containment before building/caching the visibility graph; add early-exit ring scans.
  • Add unit tests and a new mock facade to reproduce the reported regressions.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
unit_tests/mocks/mock_area_facade.hpp Adds a lightweight facade mock to supply open areas/rings/phantoms for new tests.
unit_tests/engine/area_snapping.cpp Adds snapping regression coverage for overlapping areas and “unmeshed” areas.
unit_tests/engine/area_route.cpp Adds table/geodesic regression coverage for bounding-box near misses and shared-area iteration.
unit_tests/engine/area_geodesic.cpp Adds coverage to ensure near misses don’t populate the geodesic cache.
src/engine/area_visibility.cpp Reworks ring scanning to stop early to reduce snapping-path overhead.
src/engine/area_snapping.cpp Ensures snapping continues to later areas if an earlier one yields no candidates.
src/engine/area_route.cpp Replaces “first common area” logic with “try all common areas” geodesic selection.
src/engine/area_geodesic.cpp Avoids building/caching the visibility graph until after containment is confirmed.
include/engine/area_snapping.hpp Updates docs to reflect trying all areas holding the coordinate.
Suppressed comments (1)

src/engine/area_geodesic.cpp:1

  • build_adjacency now operates on a pre-existing SolvedArea, but it only resizes area.adjacency. If this helper is ever called on an area that already has adjacency lists populated, the existing per-vertex lists won’t be cleared, risking duplicated edges and incorrect paths. Consider resetting adjacency with assign(area.size(), {}) (or explicitly clearing each list) before repopulating.
#include "engine/area_geodesic.hpp"

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/engine/area_route.cpp
Comment on lines 34 to 43
auto here = facade.GetOpenAreasAt(from);
if (here.empty())
{
return std::nullopt;
return {};
}
const auto there = facade.GetOpenAreasAt(to);
if (there.empty())
{
return std::nullopt;
return {};
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two lists are the areas whose bounding boxes hold one coordinate, which is one or two in practice and a handful where plazas nest; the product is a few comparisons against a partition lookup per relaxed node in the search that follows. A sort would cost more than it saves at these sizes, so the plain loop stays.

Comment thread src/engine/area_route.cpp
Comment on lines +51 to +63
std::vector<extractor::AreaPolygonSegment> shared;
for (const auto &area : here)
{
const auto shared = std::any_of(there.begin(),
there.end(),
[&](const auto &other)
{ return other.vertices_offset == area.vertices_offset; });
if (!shared)
if (std::any_of(there.begin(),
there.end(),
[&](const auto &other)
{ return other.vertices_offset == area.vertices_offset; }))
{
continue;
// a bounding box is not the area; geodesic_between checks containment properly
shared.push_back(area);
}
}
return shared;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two lists are the areas whose bounding boxes hold one coordinate, which is one or two in practice and a handful where plazas nest; the product is a few comparisons against a partition lookup per relaxed node in the search that follows. A sort would cost more than it saves at these sizes, so the plain loop stays.

Comment on lines +84 to +106
std::vector<engine::PhantomNodeWithDistance>
NearestPhantomNodes(const util::Coordinate coordinate,
const size_t /*max_results*/,
const std::optional<double> /*max_distance*/,
const std::optional<engine::Bearing> /*bearing*/,
const engine::Approach /*approach*/) const override
{
std::vector<engine::PhantomNodeWithDistance> found;
NodeID id = 1;
for (const auto &area : areas)
{
for (const auto vertex : area.outer)
{
if (area.reachable &&
util::coordinate_calculation::greatCircleDistance(vertex, coordinate) < 0.5)
{
found.push_back({standing_at(vertex, id), 0.0});
}
id += 2;
}
}
return found;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deliberate: the mock answers by construction, a phantom on a vertex exactly when the mesher gave that vertex a way, and the tests here want every one of them so that the claim per vertex can be checked. Truncation and the radius belong to the r-tree, which has its own tests; honouring them here would make the mock a second implementation of it.

Comment on lines +24 to +27
constexpr double AREA_METRE = 1.0 / 111319.49;

inline util::Coordinate at(double x, double y)
{ return {util::FloatLongitude{x * AREA_METRE}, util::FloatLatitude{y * AREA_METRE}}; }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a conversion factor, and the name is chosen for the one place it is read: at(x, y) multiplies metres by it, so x * AREA_METRE reads as x metres of area coordinate. The same convention is in unit_tests/engine/area_geodesic.cpp; changing it in one file would leave the two disagreeing, so it stays.

@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.47368% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.84%. Comparing base (807122b) to head (aa503a3).

Files with missing lines Patch % Lines
unit_tests/mocks/mock_area_facade.hpp 98.57% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7718      +/-   ##
==========================================
+ Coverage   94.79%   94.84%   +0.04%     
==========================================
  Files         526      529       +3     
  Lines       42085    42219     +134     
==========================================
+ Hits        39896    40042     +146     
+ Misses       2189     2177      -12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@DennisOSRM
DennisOSRM requested a lite review from Copilot September 8, 2026 13:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Comment on lines +43 to +47
for (std::size_t i = 0; i < ring.size(); ++i)
{
if (distance_squared_to_segment(point, ring[i], ring[(i + 1) % ring.size()]) <= limit)
return true;
}
Comment on lines +59 to +62
for (std::size_t i = 0; i < ring.size(); ++i)
{
const Point &a = ring[i];
const Point &b = ring[(i + 1) % ring.size()];
Comment thread src/engine/area_route.cpp
Comment on lines +51 to +63
std::vector<extractor::AreaPolygonSegment> shared;
for (const auto &area : here)
{
const auto shared = std::any_of(there.begin(),
there.end(),
[&](const auto &other)
{ return other.vertices_offset == area.vertices_offset; });
if (!shared)
if (std::any_of(there.begin(),
there.end(),
[&](const auto &other)
{ return other.vertices_offset == area.vertices_offset; }))
{
continue;
// a bounding box is not the area; geodesic_between checks containment properly
shared.push_back(area);
}
}
return shared;
Comment on lines +84 to +106
std::vector<engine::PhantomNodeWithDistance>
NearestPhantomNodes(const util::Coordinate coordinate,
const size_t /*max_results*/,
const std::optional<double> /*max_distance*/,
const std::optional<engine::Bearing> /*bearing*/,
const engine::Approach /*approach*/) const override
{
std::vector<engine::PhantomNodeWithDistance> found;
NodeID id = 1;
for (const auto &area : areas)
{
for (const auto vertex : area.outer)
{
if (area.reachable &&
util::coordinate_calculation::greatCircleDistance(vertex, coordinate) < 0.5)
{
found.push_back({standing_at(vertex, id), 0.0});
}
id += 2;
}
}
return found;
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It changes core routing/snapshot/geodesic selection behavior in performance-sensitive engine paths, and needs a final human review for broader algorithmic and edge-case assurance despite the added unit tests.

Review details
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@DennisOSRM
DennisOSRM merged commit 31df8cd into master Sep 8, 2026
45 of 46 checks passed
@DennisOSRM
DennisOSRM deleted the fix/area-every-area-holding-coordinate branch September 8, 2026 19:58
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.

2 participants