Skip to content

Store edge BBox in the graph #2625

Open

Description

@otbutz

We currently compute the BBox on-the-fly for custom area checks and only based on the tower nodes of the edge:

public static boolean in(Polygon p, EdgeIteratorState edge) {
BBox bbox = GHUtility.createBBox(edge);
if (!p.getBounds().intersects(bbox))
return false;
if (p.isRectangle())
return true;
return p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); // TODO PERF: cache bbox and edge wayGeometry for multiple area
}

This has a few drawbacks. On the one hand, we lose precision, since the full point list could in fact have a larger BBox than that of only the tower nodes:

image

And it adds computational overhead as discussed previously:

#2338 (comment)
https://discuss.graphhopper.com/t/why-using-areas-is-low-efficient/7374/6

Activity

  1. easbar commented on Jul 18, 2022

    @easbar
    Member

    We currently compute the BBox on-the-fly for custom area checks and only based on the tower nodes of the edge:

    Ok, but the code where we use only the tower nodes to compute the bounding box is this method:

    public static BBox createBBox(EdgeIteratorState edgeState) {
    PointList towerNodes = edgeState.fetchWayGeometry(FetchMode.TOWER_ONLY);
    int secondIndex = towerNodes.size() == 1 ? 0 : 1;
    return BBox.fromPoints(towerNodes.getLat(0), towerNodes.getLon(0),
    towerNodes.getLat(secondIndex), towerNodes.getLon(secondIndex));
    }

    We currently use it for the block_area feature (GraphEdgeIdFinder) and, like you say, for the custom weighting areas. We use the tower node bounding box in if (!p.getBounds().intersects(bbox)) return false;, which is only a performance optimization that skips the more costly detailed intersection check for many edges. We then use the full geometry for the detailed check p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());.

    On the one hand, we lose precision, since the full point list could in fact have a larger BBox than that of only the tower nodes

    I agree the current logic is not very intuitive. For example if p is a rectangle and the tower node bounding box intersects p it can happen that a road is affected by a custom area even though none of the road points lies within the area (false positive). On the other hand, if the roads geometry partly lies outside of the tower node bounding box, like in your picture, it can happen that a road is not affected even though some points are located within the area (false negative).

    The false positives are only possible because of the rectangle optimization introduced here: #2326, right? Without the if (p.isRectangle()) true case we would only ever return true if p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); was true, which is only true if one of the geometry points actually lies within p. And I think this is what we want: A road should be affected by a custom area if and only if at least one point is located within the area?!

    So to actually find out which roads are affected by a custom model area we need to do the full check p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());, but yes we can skip it if the real (geometry) bounding box does not intersect the polygon envelope bounding box. Currently we already skip it if the tower node bounding box does not intersect the polygon envelope bounding box, which is wrong in some cases (the false negatives I mentioned above).

    And it adds computational overhead as discussed previously:

    Actually I don't really think so. Let's say we used the full geometry bounding box in GHUtility.createBBox instead of the tower node bounding box we use currently. This would mean that more edges would be considered for the full p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); check. So overall the area check could potentially be slower. But it would be more precise as we would get rid of the false negatives. I do not expect that reading the stored full bounding box would be any faster (or slower) than reading the tower node bounding box as we do now, because the fetchWayGeometry(FetchMode.TOWER_ONLY) call in GHUtility#createBBox does not 'fetch' the actual geometry anyway:

    private PointList fetchWayGeometry_(long edgePointer, boolean reverse, FetchMode mode, int baseNode, int adjNode) {
    if (mode == FetchMode.TOWER_ONLY) {
    // no reverse handling required as adjNode and baseNode is already properly switched
    PointList pillarNodes = new PointList(2, nodeAccess.is3D());
    pillarNodes.add(nodeAccess, baseNode);
    pillarNodes.add(nodeAccess, adjNode);
    return pillarNodes;
    }

    I think that maybe we should make the custom area check more precise (get rid of false positives and negatives), and this would require using the real geometry bounding box instead of the tower node bounding box, but this will make the custom area feature potentially slower rather than faster.

    The code you quoted does include a (costly) fetchWayGeometry(FetchMode.ALL) call, but we cannot replace it with a stored bounding box, even if it was the full geometry bounding box. For this detailed check we actually want to check if any point lies within the polygon, so the bounding box wouldn't be sufficient.

    So to me it seems like the real question is: Do we want to make the custom areas more precise? If yes we need to use the full bounding box for GHUtility#createBBox and to make sure this does not slow down the custom area feature it is probably a good idea to store the full bounding box. But overall this won't make the feature faster. If anything it will be slower.

    If our goal is rather to make the custom area feature faster as requested in the forum discussion you mentioned storing the full bounding box won't help at all I think (unless we want to increase the precision without slowing it down). To speed up custom area routing we probably need to come up with something else and first we should have a reproducible example that shows how slow the routing really is.

    Regarding actually storing the bounding box for each edge I found this related comment: #2320 (comment)

  2. karussell commented on Jul 18, 2022

    @karussell
    Member

    But overall this won't make the feature faster. If anything it will be slower.

    Not necessarily as we currently fetch two random lat+lon reads in nodeAccess, which would be potentially replaced with a single read. But performance changes are often hard to guess :) (and easy to try)

    I found this related comment: #2320 (comment)
    .. and we could probably even somehow restrict this to 'curved' edges only, because for straight edges using the tower node coordinates as bounding box is often sufficient.

    Yes, if the performance change is 0 this would be a nice storage optimization.

    To speed up custom area routing we probably need to come up with something else and first we should have a reproducible example that shows how slow the routing really is.

    Yes 👍

    Probably we need some typical use cases that should be fast like single edge exclusions. E.g. for that we could fetch the edge-IDs. But first we would have to see how slow they are :)

    And if there are several cases with a big slow down I can imagine we introduce another less precise helper method like likely_in_area instead of in_area :) Because especially for larger polygons the precision down to the edge geometry might not matter.

  3. easbar commented on Jul 18, 2022

    @easbar
    Member

    Because especially for larger polygons the precision down to the edge geometry might not matter.

    Yes it really depends on the size of the areas. For small areas the current precision seems insufficient to me, but for big ones it probably does not really matter and we could even be less precise still, i.e. only check if one of the two tower nodes is in the area or not. By the way this could be an easy optimization: We could briefly check if one of the two tower nodes is in the area before we fetch the geometry. If yes return true and only otherwise do the full p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); check.

  4. otbutz commented on Jul 19, 2022

    @otbutz
    ContributorAuthor

    For small areas the current precision seems insufficient to me

    Agreed. Blocking individual edges is a feature that is still missing in CustomModels, but it can be substituted with small areas.

    Maybe we should also take a step back and get some numbers on what's really causing the slowdown here. Since people seem to be complaining, this should be measurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions