Skip to content

refactor(engine): carry the approach cost on the phantom, not in its offset - #7688

Merged
DennisOSRM merged 1 commit into
masterfrom
area-approach-cost
Aug 12, 2026
Merged

DennisOSRM merged 1 commit into
masterfrom
area-approach-cost

Conversation

@DennisOSRM

@DennisOSRM DennisOSRM commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes the sign half of defect 1 in #7683, and the two follow-ups recorded under it: the uncharged via point and the uncharged trip.

The problem

Snapping a coordinate that falls inside an open area charges the walk to the vertex it snapped to. That cost went into the phantom's weight offset, and a search seeds a source with the negation of the offset and a target with the offset itself, so one stored number had to mean both signs. Which one it got was decided by the coordinate's place in the request: first is a departure, last an arrival.

That works for a route and for nothing else.

  • A table. Every coordinate is a source and a destination at once, so half the matrix was charged backwards.
  • A via point. Charged as a departure, the leg arriving there seeds a negative key, which CH asserts against. It was made uncharged to stop the crash, at the cost of accuracy on both legs either side.
  • A trip. The same case for all of its coordinates, since the tour returns to the first, so it had to opt out of the charge entirely with ApproachCharge::NeverCharge.

The change

The walk lives on the phantom now, in three fields of its own, and the seeding accessors add it in either role:

EdgeWeight GetForwardWeightAsSource() const
{ return approach_weight - GetForwardWeightPlusOffset(); }
EdgeWeight GetForwardWeightAsTarget() const
{ return approach_weight + GetForwardWeightPlusOffset(); }

Nothing has to know which end of a journey a coordinate is in order to cost it. The sign is fixed by the accessor rather than by the caller, a target's key can no longer go negative, and the trip opt-out is gone.

They are deliberately not folded into the Get*PlusOffset accessors, which leg assembly reads to work out what a route costs. The walk is already drawn into the leg as a stretch of its own, so a route's reported distance and duration are unchanged.

Size

sizeof(PhantomNode) goes from 80 to 92 bytes. Nothing public moves with it. #7649 replaced the PhantomNode inside SegmentHint with a fixed 80 byte placeholder, so the hint string stays 112 characters. #7683 says this fix costs a longer hint; that was written before #7649 landed and is no longer true.

What this does not fix

A table cell with one end on a plaza. The remaining error is not the sign, and it took some digging to see why:

CELL r=1 c=0 heap_d=-49.98 bucket_d=111.76 new_d=61.78

A candidate departed from stands at an edge-based node's start; one arrived at stands at its end. A coordinate that is both can only have one shape, so when the departing shape is used as a target it sits before a source further along the same node, and the cell comes out short by the stretch in between. Worse, the many-to-many code detects that degenerate case by the accumulated weight going negative (addLoopWeight), and a positive approach cost masks it.

The numbers, from the node coordinates in the generated .osm rather than from OSRM: a is 50 m from e, and m is 111.8 m from a straight across the plaza, so the walk is 161.8 m. Round by d would be 270.7 m.

table says correct
today 161.7 one way, 61.8 the other 161.7 both
offering both shapes 61.8 both ways 161.7 both

161.74 - 61.78 is 99.96, exactly twice the 49.98 m e -> a segment: the source seed subtracts its own segment and nothing adds it back.

Offering both shapes is the obvious fix and I tried it. It makes the matrix symmetric, which is the right shape for a walking cost function and worth having, but both cells then come out 100 m short instead of one being exact, so it is not an improvement on its own. It needs addLoopWeight to fire on the graph part of the weight rather than on the total, and then both shapes becomes the right default. That is its own piece of work; #7683 carries the diagnosis.

Worth noting for whoever picks it up: the asymmetry is the tell. A foot profile should never produce an asymmetric matrix, so the current output announces the bug on its own.

Verified with ENABLE_ASSERTIONS=1: all five unit suites, and the full cucumber matrix over ch/mld and mmap/directly/datastore, 1471 scenarios each.

Written with AI assistance: Claude Code, Claude Opus 5 🤖

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.

Pull request overview

This PR refactors open-area snapping “approach” costs (walk from requested coordinate to snapped phantom location) so they are stored explicitly on PhantomNode and applied via role-specific accessors (*AsSource() / *AsTarget()) during heap seeding, fixing incorrect sign handling across routing modes (notably table/via/trip).

Changes:

  • Add approach_weight/approach_duration/approach_distance to PhantomNode and introduce heap-seeding accessors that apply approach costs with correct source/target semantics.
  • Update MLD many-to-many, shortest-path, and routing-base seeding to use the new accessors instead of relying on offset sign tricks.
  • Remove the /trip workaround (ApproachCharge::NeverCharge) now that approach costs no longer depend on coordinate position.

Reviewed changes

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

Show a summary per file
File Description
src/engine/routing_algorithms/many_to_many_mld.cpp Switches many-to-many MLD seeding to *AsSource/*AsTarget accessors for consistent approach charging.
src/engine/plugins/trip.cpp Removes the “never charge” special-casing for trip phantom generation.
src/engine/area_snapping.cpp Moves approach walk computation onto PhantomNode fields instead of encoding it in offsets/sign.
include/engine/routing_algorithms/shortest_path_impl.hpp Updates heap seeding to new accessors (but currently has source/target accessor mismatches in waypoint-continuation seeding).
include/engine/routing_algorithms/routing_base.hpp Updates generic heap seeding helpers (weights/durations/distances) to new accessors.
include/engine/routing_algorithms/routing_base_mld.hpp Updates MLD getNetworkDistance seeding to include approach via new accessors.
include/engine/plugins/plugin_base.hpp Simplifies GetPhantomNodes API by removing ApproachCharge mode.
include/engine/phantom_node.hpp Adds approach fields + seeding accessors; updates sizeof(PhantomNode) static_assert.
include/engine/area_snapping.hpp Updates ApproachRole documentation to reflect that role affects shape, not cost sign.
Suppressed comments (3)

include/engine/routing_algorithms/shortest_path_impl.hpp:116

  • Same issue for reverse-source seeding: this path checks IsValidReverseSource() but subtracts GetReverseWeightAsTarget(). This should use GetReverseWeightAsSource() so open-area approach costs are charged with the correct sign/semantics when initializing the heap from a source phantom.
                forward_heap.Insert(candidate.reverse_segment_id.id,
                                    total_weight_to_reverse[i] -
                                        candidate.GetReverseWeightAsTarget(),
                                    candidate.reverse_segment_id.id);

include/engine/routing_algorithms/shortest_path_impl.hpp:147

  • In the reverse-target branch, forward-source candidates are again seeded using GetForwardWeightAsTarget(). Since these candidates are treated as sources (IsValidForwardSource()), use GetForwardWeightAsSource() to avoid incorrect totals for open-area phantoms under waypoint continuation.
                forward_heap.Insert(candidate.forward_segment_id.id,
                                    total_weight_to_forward[i] -
                                        candidate.GetForwardWeightAsTarget(),
                                    candidate.forward_segment_id.id);

include/engine/routing_algorithms/shortest_path_impl.hpp:155

  • In the reverse-target branch, reverse-source candidates are seeded using GetReverseWeightAsTarget() even though they are sources (IsValidReverseSource()). Use GetReverseWeightAsSource() so approach charging remains consistent and correct for open-area snapped waypoints.
                forward_heap.Insert(candidate.reverse_segment_id.id,
                                    total_weight_to_reverse[i] -
                                        candidate.GetReverseWeightAsTarget(),
                                    candidate.reverse_segment_id.id);

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

Comment on lines 105 to 108
forward_heap.Insert(candidate.forward_segment_id.id,
total_weight_to_forward[i] -
candidate.GetForwardWeightPlusOffset(),
candidate.GetForwardWeightAsTarget(),
candidate.forward_segment_id.id);

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.

Right, and thanks: all four are fixed. These are the continue-straight seeding sites, where the original read total_weight_to_forward[i] - candidate.GetForwardWeightPlusOffset(). The rewrite recognised the negation only in the EdgeWeight{0} - x form, so these fell through to the target accessor and ended up subtracting the approach walk instead of adding it, out by twice the walk.

They now read total_weight_to_forward[i] + candidate.GetForwardWeightAsSource(), which is the same identity as everywhere else: total + approach - PlusOffset.

I audited every use of the new accessors against the validity guard above it while I was at it. These four were the only mismatch; the other 38 line up.

Comment thread include/engine/phantom_node.hpp Resolved
@codecov

codecov Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.47%. Comparing base (a91adf1) to head (8efaafc).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7688   +/-   ##
=======================================
  Coverage   94.46%   94.47%           
=======================================
  Files         516      516           
  Lines       40406    40418   +12     
=======================================
+ Hits        38170    38185   +15     
+ Misses       2236     2233    -3     

☔ 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.

…offset

Snapping a coordinate that falls inside an open area charges the walk to the vertex it
snapped to.  That cost was folded into the phantom's weight offset, and a search seeds a
source with the negation of the offset and a target with the offset itself, so one stored
number had to mean both signs.  Which sign it got was decided by the coordinate's place
in the request: first is a departure, last an arrival.

That holds for a route and nothing else.  Every coordinate of a table is a source and a
destination at once, so half the matrix was charged the wrong way round, and so is a via
point, where charging it as a departure made the leg arriving there seed a negative key
that CH asserts against.  A trip is the same case for all of its coordinates, since the
tour returns to the first, and had to opt out of the charge entirely to avoid the crash.

The walk now lives on the phantom in three fields of its own, and the seeding accessors
add it in either role.  Nothing has to know which end of a journey a coordinate is in
order to cost it, the sign is fixed by the accessor rather than by the caller, and the
opt-out in the trip plugin is gone.

`sizeof(PhantomNode)` goes from 80 to 92 bytes.  Nothing public moves with it: #7649
replaced the PhantomNode inside SegmentHint with a fixed 80 byte placeholder, so the hint
string stays 112 characters.

The candidate's *shape* still follows its position, and still has to: a phantom departed
from stands at an edge-based node's start, one arrived at at its end, and a coordinate
that is both can only have one.  A table cell with one end on a plaza is therefore still
short by the stretch between the candidate and the node boundary.  Tracked in #7683 with
what it would take to finish.
@DennisOSRM
DennisOSRM merged commit ad6a8cb into master Aug 12, 2026
23 checks passed
@DennisOSRM
DennisOSRM deleted the area-approach-cost branch August 12, 2026 06:16
DennisOSRM added a commit that referenced this pull request Aug 13, 2026
…hort

The last of the sign problems from #7683, and the one that survived #7688 because it was
never about the sign.

A candidate departed from stands at an edge-based node's start. One arrived at stands at
its end. Snapping into an open area picks the shape from the coordinate's position in the
request, which works for a route. It does not work for a table, where every coordinate is
a source and a destination at once. Used in the role it was not shaped for, the candidate
sits before the source on the same node, and the cell comes out short by the stretch
between them: 61.8 m where /route says 161.7 m. The matrix was asymmetric for a walking
profile, which is the defect announcing itself.

A target that sits before the source on one edge-based node is unreachable that way and
needs a loop to get to. The weight going negative is what says so. But the walk into an
open area is charged at both ends and is not travel, so it lifts the sum back above zero
and the degenerate pairing is accepted. Both many-to-many implementations now carry how
much of the weight is that walk, from the seed through the relaxation into the buckets,
and test the graph part rather than the whole.

The two implementations hide the same case in different places. CH hides it in
addLoopWeight behind new_weight < 0. MLD hides it in the acceptance test new_weight >= 0
that otherwise keeps a target in the search to be found later by a real path. The new
scenario fails on either algorithm if only one is fixed.
DennisOSRM added a commit that referenced this pull request Aug 13, 2026
…hort (#7689)

The last of the sign problems from #7683, and the one that survived #7688 because it was
never about the sign.

A candidate departed from stands at an edge-based node's start. One arrived at stands at
its end. Snapping into an open area picks the shape from the coordinate's position in the
request, which works for a route. It does not work for a table, where every coordinate is
a source and a destination at once. Used in the role it was not shaped for, the candidate
sits before the source on the same node, and the cell comes out short by the stretch
between them: 61.8 m where /route says 161.7 m. The matrix was asymmetric for a walking
profile, which is the defect announcing itself.

A target that sits before the source on one edge-based node is unreachable that way and
needs a loop to get to. The weight going negative is what says so. But the walk into an
open area is charged at both ends and is not travel, so it lifts the sum back above zero
and the degenerate pairing is accepted. Both many-to-many implementations now carry how
much of the weight is that walk, from the seed through the relaxation into the buckets,
and test the graph part rather than the whole.

The two implementations hide the same case in different places. CH hides it in
addLoopWeight behind new_weight < 0. MLD hides it in the acceptance test new_weight >= 0
that otherwise keeps a target in the search to be found later by a real path. The new
scenario fails on either algorithm if only one is fixed.
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