1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package ride
import (
"fmt"
"log/slog"
"math"
"slices"
"time"
"github.com/jftuga/geodist"
"github.com/martinlehoux/kagamigo/kcore"
)
const ClimbDistanceMinimum = 500
type Climb struct {
ride Ride
rideStart int
rideEnd int
Name string
}
func (climb Climb) Duration() time.Duration {
return climb.ride.Timestamp(climb.rideEnd).Sub(climb.ride.Timestamp(climb.rideStart))
}
func (climb Climb) Speed() float64 {
return (climb.EndDistanceM() - climb.StartDistanceM()) / climb.Duration().Seconds()
}
func (climb Climb) StartIndex() int { return climb.rideStart }
func (climb Climb) EndIndex() int { return climb.rideEnd }
func (climb Climb) StartDistanceM() float64 {
return climb.ride.DistanceM(climb.rideStart)
}
func (climb Climb) EndDistanceM() float64 {
return climb.ride.DistanceM(climb.rideEnd)
}
func (climb Climb) StartCoord() geodist.Coord {
return climb.ride.Coord(climb.rideStart)
}
func (climb Climb) EndCoord() geodist.Coord {
return climb.ride.Coord(climb.rideEnd)
}
func (climb Climb) PointCoord(index int) geodist.Coord {
kcore.Assert(index >= climb.rideStart && index <= climb.rideEnd, "point is outside climb")
return climb.ride.Coord(index)
}
// TopIndex returns the highest point inside the climb, used as the crest anchor.
func (climb Climb) TopIndex() int {
top := climb.rideStart
for i := climb.rideStart; i <= climb.rideEnd; i++ {
if climb.ride.ElevationM(i) > climb.ride.ElevationM(top) {
top = i
}
}
return top
}
func (climb Climb) TopDistanceM() float64 {
return climb.ride.DistanceM(climb.TopIndex())
}
func (climb Climb) TopElevationM() float64 {
return climb.ride.ElevationM(climb.TopIndex())
}
func (climb Climb) TopCoord() geodist.Coord {
return climb.ride.Coord(climb.TopIndex())
}
func (climb Climb) String() string {
startDistance := climb.StartDistanceM()
endDistance := climb.EndDistanceM()
cotacol := climb.DifficultyScore()
body := fmt.Sprintf("%.1fkm-%.1fkm: %.1fkm at %.1f%% (%d pts - %s)", startDistance/1000, endDistance/1000, (endDistance-startDistance)/1000, Slope(climb.ride, climb.rideStart, climb.rideEnd)*100, int(cotacol), Category(cotacol))
if climb.Name == "" {
return body
}
return climb.Name + ": " + body
}
func (climb Climb) DifficultyScore() float64 {
return difficultyScore(climb.ride, climb.rideStart, climb.rideEnd)
}
func Slope(r Ride, start, end int) float64 {
return (r.ElevationM(end) - r.ElevationM(start)) / (r.DistanceM(end) - r.DistanceM(start))
}
func climbDetectionScore(r Ride, start, end int) float64 {
kcore.Assert(end > start, "no points for climb detection")
distance := r.DistanceM(end) - r.DistanceM(start)
if distance == 0 {
return 0
}
dElevation := r.ElevationM(end) - r.ElevationM(start)
return math.Abs(dElevation) * dElevation / distance * 100.0 * 100.0 / 1000.0
}
func Category(cotacol float64) string {
switch {
case cotacol < 35:
return "NO"
case cotacol < 80:
return "Cat 4"
case cotacol < 180:
return "Cat 3"
case cotacol < 250:
return "Cat 2"
case cotacol < 600:
return "Cat 1"
default:
return "HC"
}
}
func bestClimbBetween(r Ride, start, end int) Climb {
kcore.Assert(end > start, "empty points")
bestDetectionScore := climbDetectionScore(r, start, end)
bestStart := start
for i := start; i < end; i++ {
detectionScore := climbDetectionScore(r, i, end)
if detectionScore > bestDetectionScore {
bestStart = i
bestDetectionScore = detectionScore
}
}
bestEnd := end
for i := end; i > bestStart; i-- {
detectionScore := climbDetectionScore(r, bestStart, i)
if detectionScore > bestDetectionScore {
bestEnd = i
bestDetectionScore = detectionScore
}
}
for i := bestStart; i < bestEnd; i++ {
detectionScore := climbDetectionScore(r, i, bestEnd)
if detectionScore > bestDetectionScore {
bestStart = i
bestDetectionScore = detectionScore
}
}
kcore.Assert(bestStart < bestEnd, "empty climb")
return Climb{ride: r, rideStart: bestStart, rideEnd: bestEnd}
}
func climbsBetween(r Ride, start, end int) []Climb {
climbs := []Climb{}
if r.DistanceM(end)-r.DistanceM(start) < ClimbDistanceMinimum {
return climbs
}
slog.Debug("Searching climbs between", slog.Int("start", int(r.DistanceM(start))), slog.Int("end", int(r.DistanceM(end))))
highest := start
for i := start; i <= end; i++ {
if r.ElevationM(i) > r.ElevationM(highest) {
highest = i
}
}
// TODO: Use descent to reduce recursion
if r.DistanceM(highest)-r.DistanceM(start) < ClimbDistanceMinimum {
return climbsBetween(r, start+1, end)
}
climb := bestClimbBetween(r, start, highest)
if climbDetectionScore(r, climb.rideStart, climb.rideEnd) >= 35 && climb.EndDistanceM()-climb.StartDistanceM() >= ClimbDistanceMinimum {
slog.Debug("Found climb between", slog.Int("start", int(climb.StartDistanceM())), slog.Int("end", int(climb.EndDistanceM())))
climbs = append(climbs, climb)
}
climbs = append(climbs, climbsBetween(r, start, climb.rideStart)...)
climbs = append(climbs, climbsBetween(r, climb.rideEnd, end)...)
return climbs
}
func (ride *Ride) AllClimbs() []Climb {
climbs := climbsBetween(*ride, 0, ride.Len()-1)
slices.SortFunc(climbs, climbCmpStart)
return climbs
}
func climbCmpStart(a, b Climb) int { return a.rideStart - b.rideStart }
|