Fix critical issue native routing

This commit is contained in:
Victor Shcherb 2012-07-11 19:49:51 +02:00
parent 1c0c25db38
commit 83966b070f
4 changed files with 67 additions and 9 deletions

View file

@ -43,6 +43,8 @@ public class BinaryRoutePlanner {
protected static final Log log = LogUtil.getLog(BinaryRoutePlanner.class);
private static final int ROUTE_POINTS = 11;
public BinaryRoutePlanner(NativeLibrary nativeLib, BinaryMapIndexReader... map) {
this.nativeLib = nativeLib;
@ -175,7 +177,7 @@ public class BinaryRoutePlanner {
ctx.visitedSegments = 0;
ctx.timeToCalculate = System.nanoTime();
if(ctx.config.initialDirection != null) {
ctx.firstRoadId = (start.getRoad().id << 8) + start.getSegmentStart();
ctx.firstRoadId = (start.getRoad().id << ROUTE_POINTS) + start.getSegmentStart();
double plusDir = start.getRoad().directionRoute(start.segmentStart, true);
double diff = plusDir - ctx.config.initialDirection;
if(Math.abs(MapUtils.alignAngleDifference(diff)) <= Math.PI / 3) {
@ -208,6 +210,23 @@ public class BinaryRoutePlanner {
TLongObjectHashMap<RouteSegment> visitedDirectSegments = new TLongObjectHashMap<RouteSegment>();
TLongObjectHashMap<RouteSegment> visitedOppositeSegments = new TLongObjectHashMap<RouteSegment>();
if(ctx.previouslyCalculatedRoute != null && ctx.previouslyCalculatedRoute.size() >0) {
RouteSegment previous = null;
int previousSegmentEnd = 0;
for(RouteSegmentResult rr : ctx.previouslyCalculatedRoute) {
RouteSegment segment = new RouteSegment(rr.getObject(), rr.getEndPointIndex());
if(previous != null) {
segment.parentRoute = previous;
segment.parentSegmentEnd = previousSegmentEnd;
}
previous = segment;
previousSegmentEnd = rr.getStartPointIndex();
long t = rr.getObject().getId() << ROUTE_POINTS + rr.getEndPointIndex();
visitedOppositeSegments.put(t, segment);
}
end = previous;
}
// for start : f(start) = g(start) + h(start) = 0 + h(start) = h(start)
int targetEndX = end.road.getPoint31XTile(end.segmentStart);
int targetEndY = end.road.getPoint31YTile(end.segmentStart);
@ -247,7 +266,9 @@ public class BinaryRoutePlanner {
if (graphReverseSegments.isEmpty() || graphDirectSegments.isEmpty() || routeFound) {
break;
}
if (!init) {
if(ctx.previouslyCalculatedRoute != null){
// nothing change
} else if (!init) {
inverse = !inverse;
init = true;
} else if (ctx.planRouteIn2Directions()) {
@ -543,7 +564,7 @@ public class BinaryRoutePlanner {
double obstacleMinusTime = 0;
// 0. mark route segment as visited
long nt = (road.getId() << 8l) + middle;
long nt = (road.getId() << 11l) + middle;
// avoid empty segments to connect but mark the point as visited
visitedSegments.put(nt, null);
@ -600,7 +621,7 @@ public class BinaryRoutePlanner {
}
// if we found end point break cycle
long nts = (road.getId() << 8l) + segmentEnd;
long nts = (road.getId() << ROUTE_POINTS) + segmentEnd;
visitedSegments.put(nts, segment);
// 2. calculate point and try to load neighbor ways if they are not loaded
@ -757,7 +778,7 @@ public class BinaryRoutePlanner {
if(nextIterator != null) {
next = nextIterator.next();
}
long nts = (next.road.getId() << 8l) + next.segmentStart;
long nts = (next.road.getId() << ROUTE_POINTS) + next.segmentStart;
// 1. Check if opposite segment found so we can stop calculations
if (oppositeSegments.contains(nts) && oppositeSegments.get(nts) != null) {

View file

@ -9,6 +9,7 @@ import gnu.trove.set.hash.TLongHashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.osmand.binary.RouteDataObject;
import net.osmand.router.BinaryRoutePlanner.RouteSegment;
@ -25,6 +26,8 @@ public class RoutingContext {
public long firstRoadId = 0;
public int firstRoadDirection = 0;;
public List<RouteSegmentResult> previouslyCalculatedRoute;
// 2. Routing memory cache (big objects)
TIntObjectHashMap<RoutingTile> tiles = new TIntObjectHashMap<RoutingContext.RoutingTile>();

View file

@ -73,6 +73,8 @@ public class MapRouterLayer implements MapPanelLayer {
private JButton nextTurn;
private JButton playPauseButton;
private JButton stopButton;
private List<RouteSegmentResult> previousRoute;
@Override
@ -172,7 +174,36 @@ public class MapRouterLayer implements MapPanelLayer {
new Thread() {
@Override
public void run() {
List<Way> ways = selfRoute(startRoute, endRoute);
List<Way> ways = selfRoute(startRoute, endRoute, null);
if (ways != null) {
DataTileManager<Way> points = new DataTileManager<Way>();
points.setZoom(11);
for (Way w : ways) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
map.setPoints(points);
}
}
}.start();
}
};
Action recalculate = new AbstractAction("Recalculate OsmAnd route") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public boolean isEnabled() {
// return previousRoute != null;
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
new Thread() {
@Override
public void run() {
List<Way> ways = selfRoute(startRoute, endRoute, previousRoute);
if (ways != null) {
DataTileManager<Way> points = new DataTileManager<Way>();
points.setZoom(11);
@ -188,6 +219,7 @@ public class MapRouterLayer implements MapPanelLayer {
};
menu.add(selfRoute);
menu.add(recalculate);
Action route_YOURS = new AbstractAction("Calculate YOURS route") {
private static final long serialVersionUID = 507156107455281238L;
@ -507,7 +539,7 @@ public class MapRouterLayer implements MapPanelLayer {
return res;
}
public List<Way> selfRoute(LatLon start, LatLon end) {
public List<Way> selfRoute(LatLon start, LatLon end, List<RouteSegmentResult> previousRoute) {
List<Way> res = new ArrayList<Way>();
long time = System.currentTimeMillis();
List<File> files = new ArrayList<File>();
@ -556,6 +588,7 @@ public class MapRouterLayer implements MapPanelLayer {
BinaryRoutePlanner router = new BinaryRoutePlanner(NativeSwingRendering.getDefaultFromSettings(), rs);
RoutingConfiguration config = builder.build(m, true);
RoutingContext ctx = new RoutingContext(config);
ctx.previouslyCalculatedRoute = previousRoute;
log.info("Use " + config.routerName + "mode for routing");
// int dir = DataExtractionSettings.getSettings().getRouteDirection();
@ -631,6 +664,7 @@ public class MapRouterLayer implements MapPanelLayer {
});
List<RouteSegmentResult> searchRoute = router.searchRoute(ctx, st, e, false);
previousRoute = searchRoute;
if (animateRoutingCalculation) {
playPauseButton.setVisible(false);
nextTurn.setText("FINISH");

View file

@ -441,8 +441,8 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_net_osmand_NativeLibrary_loadRout
RoutingIndex ind;
ind.filePointer = filepointer;
ind.name = getString(ienv, regName);
jclass jclIntArray = ienv->FindClass("[I");
jclass jclstring = ienv->FindClass("Ljava/lang/String;");
jclass jclIntArray = findClass(ienv, "[I");
jclass jclstring = findClass(ienv, "java/lang/String");
std::vector<RouteDataObject*> result;
SearchQuery q(left, right, top, bottom);