From 51613971c1e25dedbc808c66b79d15111d250160 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 18 Aug 2026 11:24:34 +0200 Subject: fix: match official climbs outside detected bounds --- official_climb/matcher.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'official_climb/matcher.go') diff --git a/official_climb/matcher.go b/official_climb/matcher.go index 59c3e0e..e7b51de 100644 --- a/official_climb/matcher.go +++ b/official_climb/matcher.go @@ -7,7 +7,7 @@ import ( "github.com/martinlehoux/biking_home/ride" ) -func MatchClimb(climb ride.Climb, officialClimbs []OfficialClimb, policy MatchPolicy) (OfficialClimb, bool) { +func MatchClimb(route ride.Ride, climb ride.Climb, officialClimbs []OfficialClimb, policy MatchPolicy) (OfficialClimb, bool) { if policy.Validate() != nil { return OfficialClimb{}, false } @@ -15,14 +15,17 @@ func MatchClimb(climb ride.Climb, officialClimbs []OfficialClimb, policy MatchPo var best OfficialClimb found := false for _, official := range officialClimbs { - startIndex, startDistance := nearestClimbPoint(climb, official.StartCoord) - endIndex, endDistance := nearestClimbPoint(climb, official.EndCoord) + startIndex, startDistance := nearestRoutePoint(route, official.StartCoord) + endIndex, endDistance := nearestRoutePoint(route, official.EndCoord) if startDistance > policy.EndpointRadiusM || endDistance > policy.EndpointRadiusM { continue } if startIndex >= endIndex { continue } + if endIndex < climb.StartIndex() || startIndex > climb.EndIndex() { + continue + } totalDistance := startDistance + endDistance if totalDistance < bestDistance || (totalDistance == bestDistance && (!found || official.ID < best.ID)) { best = official @@ -33,11 +36,11 @@ func MatchClimb(climb ride.Climb, officialClimbs []OfficialClimb, policy MatchPo return best, found } -func nearestClimbPoint(climb ride.Climb, target geodist.Coord) (int, float64) { - bestIndex := climb.StartIndex() +func nearestRoutePoint(route ride.Ride, target geodist.Coord) (int, float64) { + bestIndex := 0 bestDistance := math.Inf(1) - for index := climb.StartIndex(); index <= climb.EndIndex(); index++ { - distance := distanceM(climb.PointCoord(index), target) + for index := 0; index < route.Len(); index++ { + distance := distanceM(route.Coord(index), target) if distance < bestDistance { bestIndex = index bestDistance = distance -- cgit v1.2.3