aboutsummaryrefslogtreecommitdiff
path: root/climb/route.go
blob: e341bea158131b8774d68669fad55e63e7f581b9 (plain) (blame)
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

package climb

import "github.com/jftuga/geodist"

type Route struct {
	Distances  []float64
	Elevations []float64
	Coords     []geodist.Coord
}

// SegmentSample is a recorded passage: the length and elevation gain ridden between two route points.
type SegmentSample struct {
	DistanceM      float64
	ElevationGainM float64
}

// Slice returns the sub-route covering the given point indices (inclusive). A route without coords
// (a resampled profile) keeps them empty.
func (route Route) Slice(start, end int) Route {
	sliced := Route{Distances: route.Distances[start : end+1], Elevations: route.Elevations[start : end+1]}
	if len(route.Coords) == len(route.Distances) {
		sliced.Coords = route.Coords[start : end+1]
	}
	return sliced
}

// Sample returns the recorded length and elevation gain of the route slice between start and end (inclusive), counting only upward steps in the gain.
func (route Route) Sample(start, end int) SegmentSample {
	sample := SegmentSample{DistanceM: route.Distances[end] - route.Distances[start]}
	for i := start + 1; i <= end; i++ {
		if diff := route.Elevations[i] - route.Elevations[i-1]; diff > 0 {
			sample.ElevationGainM += diff
		}
	}
	return sample
}