aboutsummaryrefslogtreecommitdiff
path: root/official_climb/matcher.go
blob: faf51a7881c13f3cadb8b2c4829d89a2e459dffb (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

package official_climb

import (
	"math"

	"github.com/jftuga/geodist"
	"github.com/martinlehoux/biking_home/geo"
)

// 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
}

// 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
	}
	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
}