mirror of
https://github.com/Dasemu/NavFrame.git
synced 2026-11-26 15:26:05 +00:00
feat: route GPX deviations back to the nearest point ahead
After a sustained deviation from an imported route, a road leg from the current position is prepended to the rest of the route, starting at the nearest point ahead of the progress made (never behind). While off the route, passing within 100 m of a later point counts as reaching it. The imported geometry itself is never replaced. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
31eea182b9
commit
997816f2e4
6 files changed
+156
-14
No files matched your search
@@ -279,6 +279,53 @@ private fun wrappedLongitude(value: Double): Double = ((value + 180) % 360 + 360
|
||||
const val IMPORTED_APPROACH_MIN_METERS = 150.0
|
||||
fun needsApproach(position: GeoPoint, route: RouteResult): Boolean =
|
||||
route.geometry.isNotEmpty() && earthDistance(position, route.geometry.first()) > IMPORTED_APPROACH_MIN_METERS
|
||||
/** Length of [route]'s geometry in metres, as measured by guidance. */
|
||||
fun routeLength(route: RouteResult): Double = route.geometry.zipWithNext().sumOf { (a, b) -> earthDistance(a, b) }
|
||||
|
||||
/**
|
||||
* Off an imported route, passing within [radiusMeters] of a later point counts as having reached it:
|
||||
* returns the furthest such distance along [route] in the [windowMeters] after [fromMeters], or null.
|
||||
*/
|
||||
fun passedAlong(route: RouteResult, position: GeoPoint, fromMeters: Double, radiusMeters: Double = 100.0, windowMeters: Double = 3_000.0): Double? {
|
||||
var along = 0.0
|
||||
var passed: Double? = null
|
||||
route.geometry.forEachIndexed { i, point ->
|
||||
if (i > 0) along += earthDistance(route.geometry[i - 1], point)
|
||||
if (along > fromMeters + windowMeters) return passed
|
||||
if (along >= fromMeters && earthDistance(position, point) <= radiusMeters) passed = along
|
||||
}
|
||||
return passed
|
||||
}
|
||||
|
||||
/**
|
||||
* The rest of [route] from its point nearest to [position] among those in the [windowMeters] after
|
||||
* [fromMeters] (never behind it), with maneuvers rebased and distance/duration prorated. Null when
|
||||
* only the final point is left.
|
||||
*/
|
||||
fun rejoinRemainder(route: RouteResult, position: GeoPoint, fromMeters: Double, windowMeters: Double = 10_000.0): RouteResult? {
|
||||
val cumulative = DoubleArray(route.geometry.size)
|
||||
for (i in 1 until route.geometry.size) cumulative[i] = cumulative[i - 1] + earthDistance(route.geometry[i - 1], route.geometry[i])
|
||||
val total = cumulative.last()
|
||||
val start = cumulative.indexOfFirst { it >= fromMeters }.takeIf { it >= 0 } ?: return null
|
||||
var best = start
|
||||
var bestDistance = Double.POSITIVE_INFINITY
|
||||
for (i in start until route.geometry.size) {
|
||||
if (cumulative[i] > fromMeters + windowMeters) break
|
||||
val distance = earthDistance(position, route.geometry[i])
|
||||
if (distance < bestDistance) { best = i; bestDistance = distance }
|
||||
}
|
||||
if (best >= route.geometry.lastIndex) return null
|
||||
if (best == 0) return route
|
||||
val share = if (total > 0) (total - cumulative[best]) / total else 0.0
|
||||
return route.copy(
|
||||
geometry = route.geometry.drop(best),
|
||||
maneuvers = route.maneuvers.filter { (it.beginShapeIndex ?: -1) >= best }.map {
|
||||
it.copy(beginShapeIndex = it.beginShapeIndex?.minus(best), endShapeIndex = it.endShapeIndex?.minus(best))
|
||||
},
|
||||
distanceMeters = (route.distanceMeters * share).roundToInt().coerceAtLeast(1),
|
||||
durationSeconds = (route.durationSeconds * share).roundToInt(),
|
||||
)
|
||||
}
|
||||
private fun earthDistance(a: GeoPoint, b: GeoPoint): Double {
|
||||
val lat1 = Math.toRadians(a.latitude); val lat2 = Math.toRadians(b.latitude)
|
||||
val dlat = lat2 - lat1; val dlon = Math.toRadians(wrappedLongitude(b.longitude - a.longitude))
|
||||
|
||||
@@ -198,7 +198,8 @@ fun mergeRouteLegs(parts: List<RouteResult>): RouteResult {
|
||||
*/
|
||||
fun prependApproach(approach: RouteResult, route: RouteResult): RouteResult {
|
||||
val merged = mergeRouteLegs(listOf(approach, route))
|
||||
return if (route.imported && route.maneuvers.isEmpty()) merged.copy(maneuvers = emptyList(), durationSeconds = 0) else merged
|
||||
// Raw traces carry no travel time; road-routed imports keep it even if no maneuver is left ahead.
|
||||
return if (route.imported && route.durationSeconds == 0) merged.copy(maneuvers = emptyList(), durationSeconds = 0) else merged
|
||||
}
|
||||
|
||||
private fun validatePoint(point: GeoPoint) {
|
||||
|
||||
@@ -207,5 +207,28 @@ class ValhallaRouteTest {
|
||||
assertEquals(trace.destination, merged.destination)
|
||||
assertEquals(0, merged.durationSeconds) // A trace never shows an ETA.
|
||||
}
|
||||
|
||||
@Test fun `rejoining an imported route never goes back and counts points passed nearby`() {
|
||||
val points = (0..10).map { GeoPoint(43.0 + it * 0.001, -5.0) } // About 111 m apart.
|
||||
val route = RouteResult(points, listOf(
|
||||
Maneuver("right", 100, type = 10, beginShapeIndex = 3, endShapeIndex = 4),
|
||||
Maneuver("left", 100, type = 15, beginShapeIndex = 7, endShapeIndex = 8),
|
||||
), 1_110, 100, points.last(), imported = true)
|
||||
val step = routeLength(route) / 10
|
||||
// Off to the side of point 2, but progress already reached point 5.
|
||||
val ahead = rejoinRemainder(route, GeoPoint(43.002, -4.998), 5 * step - 1)!!
|
||||
assertEquals(points[5], ahead.geometry.first())
|
||||
assertEquals(listOf(15), ahead.maneuvers.map { it.type })
|
||||
assertEquals(2, ahead.maneuvers.single().beginShapeIndex)
|
||||
assertEquals(555, ahead.distanceMeters)
|
||||
assertEquals(50, ahead.durationSeconds)
|
||||
// Nearest point ahead, not the next one.
|
||||
assertEquals(points[8], rejoinRemainder(route, GeoPoint(43.008, -4.999), 0.0)!!.geometry.first())
|
||||
assertNull(rejoinRemainder(route, points.last(), 0.0))
|
||||
// Riding past point 4 within 100 m counts; a point beyond the window does not.
|
||||
assertEquals(4 * step, passedAlong(route, GeoPoint(43.004, -4.9995), 0.0)!!, 1e-6)
|
||||
assertNull(passedAlong(route, GeoPoint(43.004, -4.99), 0.0))
|
||||
assertNull(passedAlong(route, points[9], 0.0, windowMeters = 500.0))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user