search with dynamic and extended range

This commit is contained in:
afischerdev 2024-09-12 11:54:55 +02:00
parent 9fc21b0fc8
commit 0d28726ec9
2 changed files with 20 additions and 9 deletions

View File

@ -177,7 +177,8 @@ public final class NodesCache {
} }
MicroCache segment = osmf.getMicroCache(ilon, ilat); MicroCache segment = osmf.getMicroCache(ilon, ilat);
if (segment == null) { // needed for a second chance
if (segment == null || (waypointMatcher != null && ((WaypointMatcherImpl) waypointMatcher).useDynamicRange)) {
checkEnableCacheCleaning(); checkEnableCacheCleaning();
segment = osmf.createMicroCache(ilon, ilat, dataBuffers, expCtxWay, waypointMatcher, directWeaving ? nodesMap : null); segment = osmf.createMicroCache(ilon, ilat, dataBuffers, expCtxWay, waypointMatcher, directWeaving ? nodesMap : null);
@ -282,15 +283,15 @@ public final class NodesCache {
return existing; return existing;
} }
public void matchWaypointsToNodes(List<MatchedWaypoint> unmatchedWaypoints, double maxDistance, OsmNodePairSet islandNodePairs) { public boolean matchWaypointsToNodes(List<MatchedWaypoint> unmatchedWaypoints, double maxDistance, OsmNodePairSet islandNodePairs) {
waypointMatcher = new WaypointMatcherImpl(unmatchedWaypoints, maxDistance, islandNodePairs); waypointMatcher = new WaypointMatcherImpl(unmatchedWaypoints, maxDistance, islandNodePairs);
for (MatchedWaypoint mwp : unmatchedWaypoints) { for (MatchedWaypoint mwp : unmatchedWaypoints) {
int cellsize = 12500; int cellsize = 12500;
preloadPosition(mwp.waypoint, cellsize); preloadPosition(mwp.waypoint, cellsize, 1);
// get a second chance // get a second chance
if (mwp.crosspoint == null) { if (mwp.crosspoint == null) {
cellsize = 1000000 / 32; cellsize = 1000000 / 32;
preloadPosition(mwp.waypoint, cellsize); preloadPosition(mwp.waypoint, cellsize, maxDistance < 0 ? 5 : 2);
} }
} }
@ -305,7 +306,8 @@ public final class NodesCache {
mwp.crosspoint = new OsmNode(mwp.waypoint.ilon, mwp.waypoint.ilat); mwp.crosspoint = new OsmNode(mwp.waypoint.ilon, mwp.waypoint.ilat);
mwp.direct = true; mwp.direct = true;
} else { } else {
throw new IllegalArgumentException(mwp.name + "-position not mapped in existing datafile"); // do not break here throw new IllegalArgumentException(mwp.name + "-position not mapped in existing datafile");
return false;
} }
} }
if (unmatchedWaypoints.size() > 1 && i == unmatchedWaypoints.size() - 1 && unmatchedWaypoints.get(i - 1).direct) { if (unmatchedWaypoints.size() > 1 && i == unmatchedWaypoints.size() - 1 && unmatchedWaypoints.get(i - 1).direct) {
@ -313,17 +315,18 @@ public final class NodesCache {
mwp.direct = true; mwp.direct = true;
} }
} }
return true;
} }
private void preloadPosition(OsmNode n, int d) { private void preloadPosition(OsmNode n, int d, int scale) {
first_file_access_failed = false; first_file_access_failed = false;
first_file_access_name = null; first_file_access_name = null;
loadSegmentFor(n.ilon, n.ilat); loadSegmentFor(n.ilon, n.ilat);
if (first_file_access_failed) { if (first_file_access_failed) {
throw new IllegalArgumentException("datafile " + first_file_access_name + " not found"); throw new IllegalArgumentException("datafile " + first_file_access_name + " not found");
} }
for (int idxLat = -1; idxLat <= 1; idxLat++) for (int idxLat = -scale; idxLat <= scale; idxLat++)
for (int idxLon = -1; idxLon <= 1; idxLon++) { for (int idxLon = -scale; idxLon <= scale; idxLon++) {
if (idxLon != 0 || idxLat != 0) { if (idxLon != 0 || idxLat != 0) {
loadSegmentFor(n.ilon + d * idxLon, n.ilat + d * idxLat); loadSegmentFor(n.ilon + d * idxLon, n.ilat + d * idxLat);
} }

View File

@ -29,6 +29,8 @@ public final class WaypointMatcherImpl implements WaypointMatcher {
private int lonLast; private int lonLast;
private int latLast; private int latLast;
boolean useAsStartWay = true; boolean useAsStartWay = true;
public boolean useDynamicRange;
private int maxWptIdx;
private Comparator<MatchedWaypoint> comparator; private Comparator<MatchedWaypoint> comparator;
@ -36,6 +38,11 @@ public final class WaypointMatcherImpl implements WaypointMatcher {
this.waypoints = waypoints; this.waypoints = waypoints;
this.islandPairs = islandPairs; this.islandPairs = islandPairs;
MatchedWaypoint last = null; MatchedWaypoint last = null;
this.useDynamicRange = maxDistance < 0;
if (maxDistance < 0.) {
maxDistance *= -1;
}
for (MatchedWaypoint mwp : waypoints) { for (MatchedWaypoint mwp : waypoints) {
mwp.radius = maxDistance; mwp.radius = maxDistance;
if (last != null && mwp.directionToNext == -1) { if (last != null && mwp.directionToNext == -1) {
@ -50,6 +57,7 @@ public final class WaypointMatcherImpl implements WaypointMatcher {
} else { } else {
last.directionToNext = CheapAngleMeter.getDirection(last.waypoint.ilon, last.waypoint.ilat, waypoints.get(lastidx).waypoint.ilon, waypoints.get(lastidx).waypoint.ilat); last.directionToNext = CheapAngleMeter.getDirection(last.waypoint.ilon, last.waypoint.ilat, waypoints.get(lastidx).waypoint.ilon, waypoints.get(lastidx).waypoint.ilat);
} }
maxWptIdx = waypoints.size() - 1;
// sort result list // sort result list
comparator = new Comparator<>() { comparator = new Comparator<>() {
@ -105,7 +113,7 @@ public final class WaypointMatcherImpl implements WaypointMatcher {
double r22 = x2 * x2 + y2 * y2; double r22 = x2 * x2 + y2 * y2;
double radius = Math.abs(r12 < r22 ? y1 * dx - x1 * dy : y2 * dx - x2 * dy) / d; double radius = Math.abs(r12 < r22 ? y1 * dx - x1 * dy : y2 * dx - x2 * dy) / d;
if (radius <= mwp.radius) { if (radius <= mwp.radius || (this.useDynamicRange && (i == 0 || i == maxWptIdx))) {
double s1 = x1 * dx + y1 * dy; double s1 = x1 * dx + y1 * dy;
double s2 = x2 * dx + y2 * dy; double s2 = x2 * dx + y2 * dy;