2014-01-19 21:29:11 +01:00
|
|
|
package net.osmand.router;
|
|
|
|
|
|
|
|
import gnu.trove.list.array.TIntArrayList;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.osmand.binary.RouteDataObject;
|
2014-03-02 17:24:02 +01:00
|
|
|
import net.osmand.data.LatLon;
|
2014-01-28 21:03:25 +01:00
|
|
|
import net.osmand.data.QuadPoint;
|
|
|
|
import net.osmand.data.QuadRect;
|
|
|
|
import net.osmand.data.QuadTree;
|
|
|
|
import net.osmand.util.MapUtils;
|
2014-01-19 21:29:11 +01:00
|
|
|
|
|
|
|
public class PrecalculatedRouteDirection {
|
|
|
|
|
2014-01-31 01:43:52 +01:00
|
|
|
private int[] pointsX;
|
|
|
|
private int[] pointsY;
|
2014-02-07 02:38:42 +01:00
|
|
|
private float minSpeed;
|
|
|
|
private float maxSpeed;
|
2014-01-19 21:29:11 +01:00
|
|
|
private float[] tms;
|
2014-03-03 14:55:32 +01:00
|
|
|
private boolean followNext;
|
2014-01-28 21:03:25 +01:00
|
|
|
private static final int SHIFT = (1 << (31 - 17));
|
|
|
|
private static final int[] SHIFTS = new int[]{1 << (31 - 15), 1 << (31 - 13), 1 << (31 - 12),
|
|
|
|
1 << (31 - 11), 1 << (31 - 7)};
|
2014-01-19 21:29:11 +01:00
|
|
|
|
|
|
|
private List<Integer> cachedS = new ArrayList<Integer>();
|
|
|
|
|
2014-01-28 21:03:25 +01:00
|
|
|
private long startPoint = 0;
|
|
|
|
private long endPoint = 0;
|
|
|
|
// private DataTileManager<Integer> indexedPoints = new DataTileManager<Integer>(17);
|
|
|
|
QuadTree<Integer> quadTree = new QuadTree<Integer>(new QuadRect(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE),
|
2014-02-07 02:38:42 +01:00
|
|
|
8, 0.55f);
|
|
|
|
private float startFinishTime;
|
|
|
|
private float endFinishTime;
|
2014-01-19 21:29:11 +01:00
|
|
|
|
2014-02-07 02:38:42 +01:00
|
|
|
public PrecalculatedRouteDirection(TIntArrayList px, TIntArrayList py, List<Float> speedSegments, float maxSpeed) {
|
|
|
|
this.maxSpeed = maxSpeed;
|
|
|
|
init(px, py, speedSegments);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PrecalculatedRouteDirection(List<RouteSegmentResult> ls, float maxSpeed) {
|
|
|
|
this.maxSpeed = maxSpeed;
|
2014-01-19 21:29:11 +01:00
|
|
|
init(ls);
|
|
|
|
}
|
|
|
|
|
2014-03-02 17:24:02 +01:00
|
|
|
private PrecalculatedRouteDirection(LatLon[] ls, float maxSpeed) {
|
|
|
|
this.maxSpeed = maxSpeed;
|
|
|
|
init(ls);
|
|
|
|
}
|
|
|
|
|
2014-01-19 21:57:46 +01:00
|
|
|
private PrecalculatedRouteDirection(PrecalculatedRouteDirection parent, int s1, int s2) {
|
2014-02-07 02:38:42 +01:00
|
|
|
this.minSpeed = parent.minSpeed;
|
|
|
|
this.maxSpeed = parent.maxSpeed;
|
2014-01-19 21:57:46 +01:00
|
|
|
tms = new float[s2 - s1 + 1];
|
2014-01-31 01:43:52 +01:00
|
|
|
pointsX = new int[s2 - s1 + 1];
|
|
|
|
pointsY = new int[s2 - s1 + 1];
|
2014-01-19 21:57:46 +01:00
|
|
|
for (int i = s1; i <= s2; i++) {
|
2014-02-01 02:06:36 +01:00
|
|
|
int shiftInd = i - s1;
|
|
|
|
pointsX[shiftInd] = parent.pointsX[i];
|
|
|
|
pointsY[shiftInd] = parent.pointsY[i];
|
2014-01-28 21:03:25 +01:00
|
|
|
// indexedPoints.registerObjectXY(parent.pointsX.get(i), parent.pointsY.get(i), pointsX.size() - 1);
|
2014-02-01 02:06:36 +01:00
|
|
|
quadTree.insert(shiftInd, parent.pointsX[i], parent.pointsY[i]);
|
|
|
|
tms[shiftInd] = parent.tms[i] - parent.tms[s2];
|
2014-01-19 21:57:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:38:42 +01:00
|
|
|
public static PrecalculatedRouteDirection build(List<RouteSegmentResult> ls, float cutoffDistance, float maxSpeed){
|
2014-01-19 21:29:11 +01:00
|
|
|
int begi = 0;
|
|
|
|
float d = cutoffDistance;
|
|
|
|
for (; begi < ls.size(); begi++) {
|
|
|
|
d -= ls.get(begi).getDistance();
|
|
|
|
if (d < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 21:03:25 +01:00
|
|
|
int endi = ls.size();
|
2014-01-19 21:29:11 +01:00
|
|
|
d = cutoffDistance;
|
2014-01-28 21:03:25 +01:00
|
|
|
for (; endi > 0; endi--) {
|
|
|
|
d -= ls.get(endi - 1).getDistance();
|
2014-01-19 21:29:11 +01:00
|
|
|
if (d < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(begi < endi) {
|
2014-02-07 02:38:42 +01:00
|
|
|
return new PrecalculatedRouteDirection(ls.subList(begi, endi), maxSpeed);
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-03-02 17:24:02 +01:00
|
|
|
public static PrecalculatedRouteDirection build(LatLon[] ls, float maxSpeed){
|
|
|
|
return new PrecalculatedRouteDirection(ls, maxSpeed);
|
|
|
|
}
|
|
|
|
|
2014-01-19 21:29:11 +01:00
|
|
|
|
|
|
|
private void init(List<RouteSegmentResult> ls) {
|
2014-01-31 01:43:52 +01:00
|
|
|
TIntArrayList px = new TIntArrayList();
|
|
|
|
TIntArrayList py = new TIntArrayList();
|
2014-02-07 02:38:42 +01:00
|
|
|
List<Float> speedSegments = new ArrayList<Float>();
|
2014-01-19 21:29:11 +01:00
|
|
|
for (RouteSegmentResult s : ls) {
|
|
|
|
boolean plus = s.getStartPointIndex() < s.getEndPointIndex();
|
|
|
|
int i = s.getStartPointIndex();
|
|
|
|
RouteDataObject obj = s.getObject();
|
2014-02-07 02:38:42 +01:00
|
|
|
float routeSpd = (s.getRoutingTime() == 0 || s.getDistance() == 0) ? maxSpeed :
|
|
|
|
(s.getDistance() / s.getRoutingTime());
|
2014-01-19 21:29:11 +01:00
|
|
|
while (true) {
|
|
|
|
i = plus? i + 1 : i -1;
|
2014-01-31 01:43:52 +01:00
|
|
|
px.add(obj.getPoint31XTile(i));
|
|
|
|
py.add(obj.getPoint31YTile(i));
|
2014-02-07 02:38:42 +01:00
|
|
|
speedSegments.add(routeSpd);
|
2014-01-19 21:29:11 +01:00
|
|
|
if (i == s.getEndPointIndex()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 02:38:42 +01:00
|
|
|
init(px, py, speedSegments);
|
|
|
|
}
|
2014-03-02 17:24:02 +01:00
|
|
|
|
|
|
|
private void init(LatLon[] ls) {
|
|
|
|
TIntArrayList px = new TIntArrayList();
|
|
|
|
TIntArrayList py = new TIntArrayList();
|
|
|
|
List<Float> speedSegments = new ArrayList<Float>();
|
|
|
|
for (LatLon s : ls) {
|
|
|
|
float routeSpd = maxSpeed; // (s.getDistance() / s.getRoutingTime())
|
|
|
|
px.add(MapUtils.get31TileNumberX(s.getLongitude()));
|
|
|
|
py.add(MapUtils.get31TileNumberY(s.getLatitude()));
|
|
|
|
speedSegments.add(routeSpd);
|
|
|
|
}
|
|
|
|
init(px, py, speedSegments);
|
|
|
|
}
|
2014-02-07 02:38:42 +01:00
|
|
|
|
|
|
|
private void init(TIntArrayList px, TIntArrayList py, List<Float> speedSegments) {
|
|
|
|
float totaltm = 0;
|
|
|
|
List<Float> times = new ArrayList<Float>();
|
|
|
|
for (int i = 0; i < px.size(); i++) {
|
|
|
|
// MapUtils.measuredDist31 vs BinaryRoutePlanner.squareRootDist
|
|
|
|
// use measuredDist31 because we use precise s.getDistance() to calculate routeSpd
|
|
|
|
int ip = i == 0 ? 0 : i - 1;
|
|
|
|
float dist = (float) MapUtils.measuredDist31(px.get(ip), py.get(ip), px.get(i), py.get(i));
|
|
|
|
float tm = dist / speedSegments.get(i);// routeSpd;
|
|
|
|
times.add(tm);
|
|
|
|
quadTree.insert(i, px.get(i), py.get(i));
|
|
|
|
// indexedPoints.registerObjectXY();
|
|
|
|
totaltm += tm;
|
|
|
|
}
|
2014-01-31 01:43:52 +01:00
|
|
|
pointsX = px.toArray();
|
|
|
|
pointsY = py.toArray();
|
2014-01-19 21:29:11 +01:00
|
|
|
tms = new float[times.size()];
|
|
|
|
float totDec = totaltm;
|
|
|
|
for(int i = 0; i < times.size(); i++) {
|
|
|
|
totDec -= times.get(i);
|
|
|
|
tms[i] = totDec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float timeEstimate(int sx31, int sy31, int ex31, int ey31) {
|
2014-01-28 21:03:25 +01:00
|
|
|
long l1 = calc(sx31, sy31);
|
|
|
|
long l2 = calc(ex31, ey31);
|
|
|
|
int x31;
|
|
|
|
int y31;
|
|
|
|
boolean start;
|
|
|
|
if(l1 == startPoint || l1 == endPoint) {
|
|
|
|
start = l1 == startPoint;
|
|
|
|
x31 = ex31;
|
|
|
|
y31 = ey31;
|
|
|
|
} else if(l2 == startPoint || l2 == endPoint) {
|
|
|
|
start = l2 == startPoint;
|
|
|
|
x31 = sx31;
|
|
|
|
y31 = sy31;
|
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
int ind = getIndex(x31, y31);
|
|
|
|
if(ind == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if((ind == 0 && start) ||
|
2014-01-31 01:43:52 +01:00
|
|
|
(ind == pointsX.length - 1 && !start)) {
|
2014-01-28 21:03:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
float distToPoint = getDeviationDistance(x31, y31, ind);
|
2014-02-07 02:38:42 +01:00
|
|
|
float deviationPenalty = distToPoint / minSpeed;
|
|
|
|
float finishTime = (start? startFinishTime : endFinishTime);
|
2014-01-28 21:03:25 +01:00
|
|
|
if(start) {
|
2014-02-07 02:38:42 +01:00
|
|
|
return (tms[0] - tms[ind]) + deviationPenalty + finishTime;
|
2014-01-28 21:03:25 +01:00
|
|
|
} else {
|
2014-02-07 02:38:42 +01:00
|
|
|
return tms[ind] + deviationPenalty + finishTime;
|
2014-01-28 21:03:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getDeviationDistance(int x31, int y31) {
|
|
|
|
int ind = getIndex(x31, y31);
|
|
|
|
if(ind == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return getDeviationDistance(x31, y31, ind);
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 21:03:25 +01:00
|
|
|
public float getDeviationDistance(int x31, int y31, int ind) {
|
|
|
|
float distToPoint = 0; //BinaryRoutePlanner.squareRootDist(x31, y31, pointsX.get(ind), pointsY.get(ind));
|
2014-01-31 01:43:52 +01:00
|
|
|
if(ind < pointsX.length - 1 && ind != 0) {
|
|
|
|
double nx = BinaryRoutePlanner.squareRootDist(x31, y31, pointsX[ind + 1], pointsY[ind + 1]);
|
|
|
|
double pr = BinaryRoutePlanner.squareRootDist(x31, y31, pointsX[ind - 1], pointsY[ind - 1]);
|
2014-01-28 21:03:25 +01:00
|
|
|
int nind = nx > pr ? ind -1 : ind +1;
|
2014-01-31 01:43:52 +01:00
|
|
|
QuadPoint proj = MapUtils.getProjectionPoint31(x31, y31, pointsX[ind], pointsY[ind], pointsX[nind], pointsX[nind]);
|
2014-01-28 21:03:25 +01:00
|
|
|
distToPoint = (float) BinaryRoutePlanner.squareRootDist(x31, y31, (int)proj.x, (int)proj.y) ;
|
|
|
|
}
|
|
|
|
return distToPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getIndex(int x31, int y31) {
|
|
|
|
int ind = -1;
|
|
|
|
cachedS.clear();
|
|
|
|
// indexedPoints.getObjects(x31 - SHIFT, y31 - SHIFT, x31 + SHIFT, y31 + SHIFT, cachedS);
|
|
|
|
quadTree.queryInBox(new QuadRect(x31 - SHIFT, y31 - SHIFT, x31 + SHIFT, y31 + SHIFT), cachedS);
|
|
|
|
if (cachedS.size() == 0) {
|
|
|
|
for (int k = 0; k < SHIFTS.length; k++) {
|
|
|
|
quadTree.queryInBox(new QuadRect(x31 - SHIFTS[k], y31 - SHIFTS[k], x31 + SHIFTS[k], y31 + SHIFTS[k]), cachedS);
|
|
|
|
// indexedPoints.getObjects(x31 - SHIFTS[k], y31 - SHIFTS[k], x31 + SHIFTS[k], y31 + SHIFTS[k],cachedS);
|
|
|
|
if (cachedS.size() != 0) {
|
|
|
|
break;
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-28 21:03:25 +01:00
|
|
|
if (cachedS.size() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double minDist = 0;
|
|
|
|
for (int i = 0; i < cachedS.size(); i++) {
|
|
|
|
Integer n = cachedS.get(i);
|
2014-01-31 01:43:52 +01:00
|
|
|
double ds = BinaryRoutePlanner.squareRootDist(x31, y31, pointsX[n], pointsY[n]);
|
2014-01-28 21:03:25 +01:00
|
|
|
if (ds < minDist || i == 0) {
|
|
|
|
ind = n;
|
|
|
|
minDist = ds;
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ind;
|
|
|
|
}
|
2014-01-28 21:03:25 +01:00
|
|
|
|
|
|
|
private long calc(int x31, int y31) {
|
|
|
|
return ((long) x31) << 32l + ((long)y31);
|
|
|
|
}
|
2014-01-19 21:57:46 +01:00
|
|
|
|
2014-03-03 14:55:32 +01:00
|
|
|
public void setFollowNext(boolean followNext) {
|
|
|
|
this.followNext = followNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFollowNext() {
|
|
|
|
return followNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-19 21:57:46 +01:00
|
|
|
public PrecalculatedRouteDirection adopt(RoutingContext ctx) {
|
2014-01-28 21:03:25 +01:00
|
|
|
int ind1 = getIndex(ctx.startX, ctx.startY);
|
|
|
|
int ind2 = getIndex(ctx.targetX, ctx.targetY);
|
2014-02-07 02:38:42 +01:00
|
|
|
minSpeed = ctx.getRouter().getMinDefaultSpeed();
|
|
|
|
maxSpeed = ctx.getRouter().getMaxDefaultSpeed();
|
2014-01-28 21:03:25 +01:00
|
|
|
if(ind1 == -1) {
|
|
|
|
throw new IllegalArgumentException();
|
2014-01-19 21:57:46 +01:00
|
|
|
}
|
2014-01-28 21:03:25 +01:00
|
|
|
if(ind2 == -1) {
|
|
|
|
throw new IllegalArgumentException();
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|
2014-01-28 21:03:25 +01:00
|
|
|
PrecalculatedRouteDirection routeDirection = new PrecalculatedRouteDirection(this, ind1, ind2);
|
|
|
|
routeDirection.startPoint = calc(ctx.startX, ctx.startY);
|
2014-02-07 02:38:42 +01:00
|
|
|
routeDirection.startFinishTime = (float) BinaryRoutePlanner.squareRootDist(pointsX[ind1], pointsY[ind1], ctx.startX, ctx.startY) / maxSpeed;
|
2014-01-28 21:03:25 +01:00
|
|
|
// routeDirection.startX31 = ctx.startX;
|
|
|
|
// routeDirection.startY31 = ctx.startY;
|
2014-10-07 23:53:04 +02:00
|
|
|
routeDirection.endPoint = calc(ctx.targetX, ctx.targetY);
|
2014-02-07 02:38:42 +01:00
|
|
|
routeDirection.endFinishTime = (float) BinaryRoutePlanner.squareRootDist(pointsX[ind2], pointsY[ind2], ctx.targetX, ctx.targetY) / maxSpeed;
|
2014-03-03 14:55:32 +01:00
|
|
|
routeDirection.followNext = followNext;
|
2014-02-07 02:38:42 +01:00
|
|
|
|
2014-01-28 21:03:25 +01:00
|
|
|
// routeDirection.endX31 = ctx.targetX;
|
|
|
|
// routeDirection.endY31 = ctx.targetY;
|
|
|
|
return routeDirection;
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 21:03:25 +01:00
|
|
|
|
2014-01-19 21:57:46 +01:00
|
|
|
|
|
|
|
|
2014-01-19 21:29:11 +01:00
|
|
|
}
|