From e5e7870eafbfc80f4668d2c0d0995149ddc2f60a Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sun, 23 Aug 2026 12:12:59 +0200 Subject: Match official climbs once per lap on looping rides Official climb matching now returns all non-overlapping occurrences per ride instead of only the globally nearest pair. Each lap is detected by clustering contiguous points within the endpoint radius and pairing each start cluster with the first end cluster inside its window, preserving direction. The ride analysis aggregates all matches and sorts claimed ranges before excluding them from auto-detection. Fixes the case where a looping ride only showed one instance of a saved climb while the user expected one per repetition. --- official_climb/matcher.go | 84 ++++++++++++++++++++++--- official_climb/official_climb_test.go | 111 ++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 8 deletions(-) (limited to 'official_climb') diff --git a/official_climb/matcher.go b/official_climb/matcher.go index faf51a7..00f7e9b 100644 --- a/official_climb/matcher.go +++ b/official_climb/matcher.go @@ -20,16 +20,84 @@ type MatchedClimb struct { // 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 { + matches := MatchClimbs(route, official, radiusM) + if len(matches) == 0 { return MatchedClimb{}, false } - startIndex, startDistance := geo.Nearest(route, official.StartCoord) - endIndex, endDistance := geo.Nearest(route, official.EndCoord) - if startDistance > radiusM || endDistance > radiusM { - return MatchedClimb{}, false + return matches[0], true +} + +// MatchClimbs returns all non-overlapping occurrences of an official climb on +// a route. Each occurrence is defined by a start and end point both within +// radiusM, with start strictly before end and direction preserved. Loops are +// handled by clustering contiguous approaches into single candidates and pairing +// each start cluster with the first end cluster inside its lap window. +func MatchClimbs(route []geodist.Coord, official OfficialClimb, radiusM float64) []MatchedClimb { + if math.IsNaN(radiusM) || math.IsInf(radiusM, 0) || radiusM <= 0 { + return nil } - if startIndex >= endIndex { - return MatchedClimb{}, false + if len(route) == 0 { + return nil + } + startClusters := clustersInRadius(route, official.StartCoord, radiusM) + endClusters := clustersInRadius(route, official.EndCoord, radiusM) + if len(startClusters) == 0 || len(endClusters) == 0 { + return nil + } + matches := make([]MatchedClimb, 0) + for i, start := range startClusters { + nextStartIdx := len(route) + if i+1 < len(startClusters) { + nextStartIdx = startClusters[i+1].Index + } + var matchedEnd *clusterCandidate + for j := range endClusters { + end := &endClusters[j] + if end.Index > start.Index && end.Index < nextStartIdx { + matchedEnd = end + break + } + } + if matchedEnd == nil { + continue + } + matches = append(matches, MatchedClimb{ + OfficialClimb: official, + StartIndex: start.Index, + EndIndex: matchedEnd.Index, + }) + } + return matches +} + +type clusterCandidate struct { + Index int + Distance float64 +} + +func clustersInRadius(route []geodist.Coord, target geodist.Coord, radiusM float64) []clusterCandidate { + candidates := make([]clusterCandidate, 0) + inCluster := false + bestIndex := 0 + bestDistance := 0.0 + for idx, coord := range route { + dist := geo.DistanceM(coord, target) + if dist <= radiusM { + if !inCluster { + inCluster = true + bestIndex = idx + bestDistance = dist + } else if dist < bestDistance { + bestIndex = idx + bestDistance = dist + } + } else if inCluster { + candidates = append(candidates, clusterCandidate{Index: bestIndex, Distance: bestDistance}) + inCluster = false + } + } + if inCluster { + candidates = append(candidates, clusterCandidate{Index: bestIndex, Distance: bestDistance}) } - return MatchedClimb{OfficialClimb: official, StartIndex: startIndex, EndIndex: endIndex}, true + return candidates } diff --git a/official_climb/official_climb_test.go b/official_climb/official_climb_test.go index 2114dd2..a0c8d65 100644 --- a/official_climb/official_climb_test.go +++ b/official_climb/official_climb_test.go @@ -138,3 +138,114 @@ func TestMatchClimbRejectsEndpointOutsideRadius(t *testing.T) { _, found := official_climb.MatchClimb(parsed.Coords, official, 10) assert.False(t, found) } + +func TestMatchClimbsMatchesTwoLaps(t *testing.T) { + coords := []geodist.Coord{ + {Lat: 43.10, Lon: 5.10}, + {Lat: 43.11, Lon: 5.11}, + {Lat: 43.12, Lon: 5.12}, + {Lat: 43.13, Lon: 5.13}, + {Lat: 43.10, Lon: 5.10}, + {Lat: 43.11, Lon: 5.11}, + {Lat: 43.12, Lon: 5.12}, + {Lat: 43.13, Lon: 5.13}, + } + parsed := ride.FromColumns( + []float64{0, 1000, 2000, 3000, 4000, 5000, 6000, 7000}, + []float64{100, 150, 200, 100, 100, 150, 200, 100}, + coords, + []time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0), time.Unix(240, 0), time.Unix(300, 0), time.Unix(360, 0), time.Unix(420, 0)}, + ) + official := official_climb.OfficialClimb{ + Name: "Loop", + StartCoord: geodist.Coord{Lat: 43.10, Lon: 5.10}, + EndCoord: geodist.Coord{Lat: 43.12, Lon: 5.12}, + } + matches := official_climb.MatchClimbs(parsed.Coords, official, official_climb.DefaultMatchRadiusM) + require.Len(t, matches, 2) + assert.Equal(t, 0, matches[0].StartIndex) + assert.Equal(t, 2, matches[0].EndIndex) + assert.Equal(t, 4, matches[1].StartIndex) + assert.Equal(t, 6, matches[1].EndIndex) + // Single-match helper still returns first occurrence for backward compatibility. + single, found := official_climb.MatchClimb(parsed.Coords, official, official_climb.DefaultMatchRadiusM) + require.True(t, found) + assert.Equal(t, 0, single.StartIndex) +} + +func TestMatchClimbsMatchesOnlyCompleteLaps(t *testing.T) { + coords := []geodist.Coord{ + {Lat: 43.10, Lon: 5.10}, + {Lat: 43.11, Lon: 5.11}, + {Lat: 43.13, Lon: 5.13}, + {Lat: 43.10, Lon: 5.10}, + {Lat: 43.11, Lon: 5.11}, + {Lat: 43.12, Lon: 5.12}, + } + parsed := ride.FromColumns( + []float64{0, 1000, 2000, 3000, 4000, 5000}, + []float64{100, 150, 100, 100, 150, 200}, + coords, + []time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0), time.Unix(240, 0), time.Unix(300, 0)}, + ) + official := official_climb.OfficialClimb{ + Name: "Partial", + StartCoord: geodist.Coord{Lat: 43.10, Lon: 5.10}, + EndCoord: geodist.Coord{Lat: 43.12, Lon: 5.12}, + } + matches := official_climb.MatchClimbs(parsed.Coords, official, official_climb.DefaultMatchRadiusM) + require.Len(t, matches, 1) + assert.Equal(t, 3, matches[0].StartIndex) + assert.Equal(t, 5, matches[0].EndIndex) +} + +func TestMatchClimbsPreservesDirectionAcrossLaps(t *testing.T) { + coords := []geodist.Coord{ + {Lat: 43.12, Lon: 5.12}, + {Lat: 43.11, Lon: 5.11}, + {Lat: 43.10, Lon: 5.10}, + } + parsed := ride.FromColumns( + []float64{0, 1000, 2000}, + []float64{200, 150, 100}, + coords, + []time.Time{time.Unix(0, 0), time.Unix(60, 0), time.Unix(120, 0)}, + ) + official := official_climb.OfficialClimb{ + Name: "Directional", + StartCoord: geodist.Coord{Lat: 43.10, Lon: 5.10}, + EndCoord: geodist.Coord{Lat: 43.12, Lon: 5.12}, + } + matches := official_climb.MatchClimbs(parsed.Coords, official, official_climb.DefaultMatchRadiusM) + assert.Empty(t, matches) +} + +func TestMatchClimbsClustersContiguousPoints(t *testing.T) { + coords := []geodist.Coord{ + {Lat: 43.1000, Lon: 5.1000}, + {Lat: 43.1001, Lon: 5.1001}, + {Lat: 43.1001, Lon: 5.1001}, + {Lat: 43.11, Lon: 5.11}, + {Lat: 43.12, Lon: 5.12}, + {Lat: 43.1000, Lon: 5.1000}, + {Lat: 43.1001, Lon: 5.1001}, + {Lat: 43.12, Lon: 5.12}, + } + parsed := ride.FromColumns( + []float64{0, 100, 200, 1000, 2000, 3000, 3100, 4000}, + []float64{100, 101, 102, 150, 200, 100, 101, 200}, + coords, + []time.Time{time.Unix(0, 0), time.Unix(10, 0), time.Unix(20, 0), time.Unix(60, 0), time.Unix(120, 0), time.Unix(180, 0), time.Unix(190, 0), time.Unix(240, 0)}, + ) + official := official_climb.OfficialClimb{ + Name: "Clustered", + StartCoord: geodist.Coord{Lat: 43.10, Lon: 5.10}, + EndCoord: geodist.Coord{Lat: 43.12, Lon: 5.12}, + } + matches := official_climb.MatchClimbs(parsed.Coords, official, official_climb.DefaultMatchRadiusM) + require.Len(t, matches, 2) + assert.Equal(t, 0, matches[0].StartIndex) + assert.Equal(t, 4, matches[0].EndIndex) + assert.Equal(t, 5, matches[1].StartIndex) + assert.Equal(t, 7, matches[1].EndIndex) +} -- cgit v1.3