From 5e3d39cda5250cb55bc8324bcf96b0ab4b36c47f Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Fri, 21 Aug 2026 15:30:51 +0200 Subject: feat: bound climb detection by matched official climbs Detected climbs no longer absorb an official climb's range: official climbs matched to the route by coordinates claim their stored start/end range, and climb detection now runs only in the free regions around them. On ride 263 a single absorbed Cat 2 ascent is split so creating a first-half official climb lets the second half be detected on its own. - ride: add Range, freeRegions, and DetectClimbs(claimed); exclude claimed ranges so a climb never overlaps one - official_climb: MatchClimb now bounds-matches purely by coordinates without needing a detected climb; extract geo.Nearest shared helper - rideanalysis: Analyze builds claimed climbs from matched official climbs and detects only in free regions; render official vs unmatched climbs separately --- official_climb/matcher.go | 66 +++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 43 deletions(-) (limited to 'official_climb/matcher.go') diff --git a/official_climb/matcher.go b/official_climb/matcher.go index e7b51de..faf51a7 100644 --- a/official_climb/matcher.go +++ b/official_climb/matcher.go @@ -4,52 +4,32 @@ import ( "math" "github.com/jftuga/geodist" - "github.com/martinlehoux/biking_home/ride" + "github.com/martinlehoux/biking_home/geo" ) -func MatchClimb(route ride.Ride, climb ride.Climb, officialClimbs []OfficialClimb, policy MatchPolicy) (OfficialClimb, bool) { - if policy.Validate() != nil { - return OfficialClimb{}, false - } - bestDistance := math.Inf(1) - var best OfficialClimb - found := false - for _, official := range officialClimbs { - 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 - bestDistance = totalDistance - found = true - } - } - return best, found +// MatchedClimb is an official climb that lies on a route, together with the +// route point indexes of its start and end. It lets the climb detector know +// which segment of the route is already claimed by an official climb. +type MatchedClimb struct { + OfficialClimb + StartIndex int + EndIndex int } -func nearestRoutePoint(route ride.Ride, target geodist.Coord) (int, float64) { - bestIndex := 0 - bestDistance := math.Inf(1) - for index := 0; index < route.Len(); index++ { - distance := distanceM(route.Coord(index), target) - if distance < bestDistance { - bestIndex = index - bestDistance = distance - } +// MatchClimb matches an official climb against a route purely by +// coordinates: both the start and end coordinates must be within radiusM of a +// route point, and the matched start point must precede the matched end point. +func MatchClimb(route []geodist.Coord, official OfficialClimb, radiusM float64) (MatchedClimb, bool) { + if math.IsNaN(radiusM) || math.IsInf(radiusM, 0) || radiusM <= 0 { + return MatchedClimb{}, false } - return bestIndex, bestDistance -} - -func distanceM(a, b geodist.Coord) float64 { - _, distanceKm := geodist.HaversineDistance(a, b) - return distanceKm * 1000 + startIndex, startDistance := geo.Nearest(route, official.StartCoord) + endIndex, endDistance := geo.Nearest(route, official.EndCoord) + if startDistance > radiusM || endDistance > radiusM { + return MatchedClimb{}, false + } + if startIndex >= endIndex { + return MatchedClimb{}, false + } + return MatchedClimb{OfficialClimb: official, StartIndex: startIndex, EndIndex: endIndex}, true } -- cgit v1.3