aboutsummaryrefslogtreecommitdiff
path: root/official_climb/matcher.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-08-21 15:30:51 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-08-21 15:30:51 +0200
commit5e3d39cda5250cb55bc8324bcf96b0ab4b36c47f (patch)
treef92fe6f938866688b994c99670e1e27863579255 /official_climb/matcher.go
parent0e27097e3d706e0e5bd761bf35fe301fd119d2a9 (diff)
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
Diffstat (limited to 'official_climb/matcher.go')
-rw-r--r--official_climb/matcher.go66
1 files changed, 23 insertions, 43 deletions
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
}