Skip to content

Update snap.closestNode when an edge snap gets converted to a tower snap - #3009

Merged
easbar merged 2 commits into
masterfrom
snap_bug
May 16, 2024
Merged

easbar merged 2 commits into
masterfrom
snap_bug

Conversation

@easbar

@easbar easbar commented May 15, 2024

Copy link
Copy Markdown
Member

Fixes #2850

In LocationIndexTree#findClosest we first search for edges close to the query point, but for performance reasons we do not calculate the 'crossing points' for edge snaps yet. Afterwards, in Snap#calcSnappedPoint, we calculate the crossing point only for the closest snap. Once we know the crossing point we checkif it is close to a pillar or tower node. If so, we convert the edge snap to a pillar or tower snap (see also this discussion: 8df34e7#r1115485827).

For very short edges it can be that both tower nodes are considered 'close' to the crossing point. In this case we might move the snap to the one that wasn't set to be the snaps closestNode, because we do not check again which one is closer. In any case if we update the snaps coordinates to match those of a nearby tower node we also need to update the closest node to keep a consistent state. Otherwise we get the error described in #2850 where the route starts at the closest node, but the snaps coordinates are still the ones of the opposite node.

@easbar easbar added the bug label May 15, 2024
@easbar easbar added this to the 10.0 milestone May 15, 2024
if (considerEqual(crossingPoint.lat, crossingPoint.lon, tmpLat, tmpLon)) {
snappedPosition = wayIndex == 0 ? Position.TOWER : Position.PILLAR;
snappedPoint = new GHPoint3D(tmpLat, tmpLon, tmpEle);
closestNode = wayIndex == 0 ? closestEdge.getBaseNode() : closestNode;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the actual fix. Since we update the snappedPoint we need to update the closestNode as well.

wayIndex++;
snappedPosition = wayIndex == fullPL.size() - 1 ? Position.TOWER : Position.PILLAR;
snappedPoint = new GHPoint3D(adjLat, adjLon, adjEle);
closestNode = wayIndex == fullPL.size() - 1 ? closestEdge.getAdjNode() : closestNode;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure if this situation really occurs. If the crossing point is 'close' to the adj node, but not 'close' to the base node the closestNode will probably already be set to the adj node and updating the closestNode again has no effect. Still, I think it is more clean to simply update it here.

class SnapTest {

@Test
void snapToCloseTower() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This test creates the situation where updating of the closest node in the second block is necessary (see my other comment).

}

@Test
void veryShortEdge() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This test simulates the situation that causes the error in #2850

@@ -160,10 +160,12 @@ public void calcSnappedPoint(DistanceCalc distCalc) {
if (considerEqual(crossingPoint.lat, crossingPoint.lon, tmpLat, tmpLon)) {
snappedPosition = wayIndex == 0 ? Position.TOWER : Position.PILLAR;
snappedPoint = new GHPoint3D(tmpLat, tmpLon, tmpEle);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A bit off-topic here: Maybe we should also update the queryDistance. But the exact definition of this field seems a bit unclear to me? First I thought it should be the distance to the crossing point for edge snaps and the distance to the tower/pillar node for tower/pillar snaps. But apparently it isn't and actually we do not really use this distance anywhere? So maybe we can remove it from the Snap class anyway. Or at least state that is only a rough approximation that can be off by quite a bit for edge snaps, in which case we don't need to worry about updating it here, either.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could it be that it is assumed here that we already calculated the queryDistance properly before in the LocationIndex lookup?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That depends on our definition of a 'proper' query distance calculation. If it is meant to be the distance between the crossing point and the query point we cannot calculate it earlier than here because we do not have the crossing point yet. If it is meant to be the distance to the closest tower or pillar node, it is rather imprecise anyway and only a rough approximation for the 'distance' of the snap.

@karussell karussell May 16, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure as it is a long time since I had this in mind but we do calculate the distance to the edge here - i.e. it should be the proper distance from the query point to the crossing point. (Without the need to calculate the crossing point.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, ok. Yes that makes sense.

@@ -160,10 +160,12 @@ public void calcSnappedPoint(DistanceCalc distCalc) {
if (considerEqual(crossingPoint.lat, crossingPoint.lon, tmpLat, tmpLon)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Another fix for #2850 would be to not simply use the first tower node that is 'considered equal' here, but rather test both nodes and use the closer one. But what if their distance is equal? I think I chose the cleaner approach here: It does not matter which node we use, but if we update the snaps coordinates we need to update the closest node as well to keep the snaps state consistent.

@karussell karussell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot!

@easbar
easbar merged commit 4acd99e into master May 16, 2024
@easbar
easbar deleted the snap_bug branch May 16, 2024 08:02

This branch was previously deployed

1 inactive deployment
benchmarks — 206b0e98 Deployed May 15, 2024 by easbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Specific alternative route requests leads to IllegalStateException

2 participants