Search in sources :

Example 1 with FinalRouteSegment

use of net.osmand.router.BinaryRoutePlanner.FinalRouteSegment in project Osmand by osmandapp.

the class BinaryRoutePlannerOld method processIntersections.

private boolean processIntersections(RoutingContext ctx, PriorityQueue<RouteSegment> graphSegments, TLongObjectHashMap<RouteSegment> visitedSegments, TLongObjectHashMap<RouteSegment> oppositeSegments, float distFromStart, float distToFinalPoint, RouteSegment segment, int segmentEnd, RouteSegment inputNext, boolean reverseWay) {
    boolean thereAreRestrictions = proccessRestrictions(ctx, segment.road, inputNext, reverseWay);
    Iterator<RouteSegment> nextIterator = null;
    if (thereAreRestrictions) {
        nextIterator = ctx.segmentsToVisitPrescripted.iterator();
    }
    // Calculate possible ways to put into priority queue
    RouteSegment next = inputNext;
    boolean hasNext = nextIterator == null || nextIterator.hasNext();
    while (hasNext) {
        if (nextIterator != null) {
            next = nextIterator.next();
        }
        long nts = (next.road.getId() << ROUTE_POINTS) + next.getSegmentStart();
        // 1. Check if opposite segment found so we can stop calculations
        if (oppositeSegments.contains(nts) && oppositeSegments.get(nts) != null) {
            // restrictions checked
            RouteSegment opposite = oppositeSegments.get(nts);
            // additional check if opposite way not the same as current one
            if (next.getSegmentStart() != segmentEnd || opposite.getRoad().getId() != segment.getRoad().getId()) {
                FinalRouteSegment frs = new FinalRouteSegment(segment.getRoad(), segment.getSegmentStart());
                float distStartObstacles = segment.distanceFromStart;
                frs.setParentRoute(segment.getParentRoute());
                frs.setParentSegmentEnd(segment.getParentSegmentEnd());
                frs.reverseWaySearch = reverseWay;
                frs.distanceFromStart = opposite.distanceFromStart + distStartObstacles;
                RouteSegment op = new RouteSegment(segment.getRoad(), segmentEnd);
                op.setParentRoute(opposite);
                op.setParentSegmentEnd(next.getSegmentStart());
                frs.distanceToEnd = 0;
                frs.opposite = op;
                ctx.finalRouteSegment = frs;
                return true;
            }
        }
        // road.id could be equal on roundabout, but we should accept them
        boolean alreadyVisited = visitedSegments.contains(nts);
        if (!alreadyVisited) {
            float distanceToEnd = (float) h(ctx, distToFinalPoint, next);
            if (next.parentRoute == null || ctx.roadPriorityComparator(next.distanceFromStart, next.distanceToEnd, distFromStart, distanceToEnd) > 0) {
                if (next.parentRoute != null) {
                    // already in queue remove it
                    if (!graphSegments.remove(next)) {
                        // exist in different queue!
                        next = new RouteSegment(next.getRoad(), next.getSegmentStart());
                    }
                }
                next.distanceFromStart = distFromStart;
                next.distanceToEnd = distanceToEnd;
                // put additional information to recover whole route after
                next.setParentRoute(segment);
                next.setParentSegmentEnd(segmentEnd);
                graphSegments.add(next);
            }
            if (ctx.visitor != null) {
            // ctx.visitor.visitSegment(next, false);
            }
        } else {
            // that is very strange situation and almost exception (it can happen when we underestimate distnceToEnd)
            if (distFromStart < next.distanceFromStart && next.road.id != segment.road.id) {
                // That code is incorrect (when segment is processed itself,
                // then it tries to make wrong u-turn) -
                // this situation should be very carefully checked in future (seems to be fixed)
                // System.out.println(segment.getRoad().getName() + " " + next.getRoad().getName());
                // System.out.println(next.distanceFromStart + " ! " + distFromStart);
                next.distanceFromStart = distFromStart;
                next.setParentRoute(segment);
                next.setParentSegmentEnd(segmentEnd);
                if (ctx.visitor != null) {
                // ctx.visitor.visitSegment(next, next.getSegmentStart(), false);
                }
            }
        }
        // iterate to next road
        if (nextIterator == null) {
            next = next.next;
            hasNext = next != null;
        } else {
            hasNext = nextIterator.hasNext();
        }
    }
    return false;
}
Also used : FinalRouteSegment(net.osmand.router.BinaryRoutePlanner.FinalRouteSegment) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment) FinalRouteSegment(net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)

Example 2 with FinalRouteSegment

use of net.osmand.router.BinaryRoutePlanner.FinalRouteSegment in project Osmand by osmandapp.

the class TestRouting method runTestSpecialTest.

private static void runTestSpecialTest(NativeLibrary lib, BinaryMapIndexReader[] rs, RoutingConfiguration rconfig, RoutePlannerFrontEnd router, LatLon start, LatLon end, final float calcRoutingTime, String msg) throws IOException, InterruptedException {
    RoutingContext ctx = router.buildRoutingContext(rconfig, lib, rs);
    router.searchRoute(ctx, start, end, null);
    FinalRouteSegment frs = ctx.finalRouteSegment;
    if (frs == null || !equalPercent(calcRoutingTime, frs.distanceFromStart, 0.5f)) {
        throw new IllegalArgumentException(MessageFormat.format(msg, calcRoutingTime + "", frs == null ? "0" : frs.distanceFromStart + ""));
    }
}
Also used : FinalRouteSegment(net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)

Example 3 with FinalRouteSegment

use of net.osmand.router.BinaryRoutePlanner.FinalRouteSegment in project Osmand by osmandapp.

the class RouteResultPreparation method convertFinalSegmentToResults.

private List<RouteSegmentResult> convertFinalSegmentToResults(RoutingContext ctx, FinalRouteSegment finalSegment) {
    List<RouteSegmentResult> result = new ArrayList<RouteSegmentResult>();
    if (finalSegment != null) {
        ctx.routingTime = finalSegment.distanceFromStart;
        println("Routing calculated time distance " + finalSegment.distanceFromStart);
        // Get results from opposite direction roads
        RouteSegment segment = finalSegment.reverseWaySearch ? finalSegment : finalSegment.opposite.getParentRoute();
        int parentSegmentStart = finalSegment.reverseWaySearch ? finalSegment.opposite.getSegmentStart() : finalSegment.opposite.getParentSegmentEnd();
        float parentRoutingTime = -1;
        while (segment != null) {
            RouteSegmentResult res = new RouteSegmentResult(segment.road, parentSegmentStart, segment.getSegmentStart());
            parentRoutingTime = calcRoutingTime(parentRoutingTime, finalSegment, segment, res);
            parentSegmentStart = segment.getParentSegmentEnd();
            segment = segment.getParentRoute();
            addRouteSegmentToResult(ctx, result, res, false);
        }
        // reverse it just to attach good direction roads
        Collections.reverse(result);
        segment = finalSegment.reverseWaySearch ? finalSegment.opposite.getParentRoute() : finalSegment;
        int parentSegmentEnd = finalSegment.reverseWaySearch ? finalSegment.opposite.getParentSegmentEnd() : finalSegment.opposite.getSegmentStart();
        parentRoutingTime = -1;
        while (segment != null) {
            RouteSegmentResult res = new RouteSegmentResult(segment.road, segment.getSegmentStart(), parentSegmentEnd);
            parentRoutingTime = calcRoutingTime(parentRoutingTime, finalSegment, segment, res);
            parentSegmentEnd = segment.getParentSegmentEnd();
            segment = segment.getParentRoute();
            // happens in smart recalculation
            addRouteSegmentToResult(ctx, result, res, true);
        }
        Collections.reverse(result);
    // checkTotalRoutingTime(result);
    }
    return result;
}
Also used : TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) RouteSegment(net.osmand.router.BinaryRoutePlanner.RouteSegment) FinalRouteSegment(net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)

Aggregations

FinalRouteSegment (net.osmand.router.BinaryRoutePlanner.FinalRouteSegment)3 RouteSegment (net.osmand.router.BinaryRoutePlanner.RouteSegment)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ArrayList (java.util.ArrayList)1