-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmath.go
More file actions
163 lines (141 loc) · 4.79 KB
/
math.go
File metadata and controls
163 lines (141 loc) · 4.79 KB
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
package main
import (
"fmt"
"math"
"github.com/pkg/errors"
m "pfeifer.dev/mapd/math"
ms "pfeifer.dev/mapd/settings"
)
func GetStateCurvatures(state *State) ([]m.Curvature, error) {
nodes := state.CurrentWay.Way.Nodes()
num_points := len(nodes)
all_nodes := [][]m.Position{nodes}
all_nodes_direction := []bool{state.CurrentWay.OnWay.IsForward}
all_nodes_is_merge_or_split := []bool{false}
lastWay := state.CurrentWay.Way
for _, nextWay := range state.NextWays {
nwNodes := nextWay.Way.Nodes()
if len(nwNodes) > 0 {
num_points += len(nwNodes) - 1
}
all_nodes = append(all_nodes, nwNodes)
all_nodes_direction = append(all_nodes_direction, nextWay.IsForward)
all_nodes_is_merge_or_split = append(all_nodes_is_merge_or_split, lastWay.Lanes() < nextWay.Way.Lanes() || (lastWay.Lanes() > nextWay.Way.Lanes() && !lastWay.OneWay() && nextWay.Way.OneWay()))
lastWay = nextWay.Way
}
positions := make([]m.Position, num_points)
merge_or_split_nodes := []int{}
all_nodes_idx := 0
nodes_idx := 0
for i := 0; i < num_points; i++ {
var index int
forward := all_nodes_direction[all_nodes_idx]
if forward {
index = nodes_idx
if all_nodes_idx > 0 {
index += 1
}
} else {
index = len(all_nodes[all_nodes_idx]) - nodes_idx - 1
if all_nodes_idx > 0 {
index -= 1
}
}
node := all_nodes[all_nodes_idx][index]
positions[i] = node
nodes_idx += 1
if nodes_idx == len(all_nodes[all_nodes_idx]) || (nodes_idx == len(all_nodes[all_nodes_idx])-1 && all_nodes_idx > 0) {
all_nodes_idx += 1
nodes_idx = 0
if all_nodes_idx < len(all_nodes_is_merge_or_split) && all_nodes_is_merge_or_split[all_nodes_idx] {
merge_or_split_nodes = append(merge_or_split_nodes, i)
}
}
}
curvatures, err := GetCurvatures(positions)
if err != nil {
return []m.Curvature{}, errors.Wrap(err, "could not get curvatures from points")
}
// set the merge nodes to be straight to help balance out issues with map representation
for _, merge_or_split_node := range merge_or_split_nodes {
if merge_or_split_node >= 2 {
curvatures[merge_or_split_node-2].Curvature = 0.0015
curvatures[merge_or_split_node-1].Curvature = 0.0015
}
// also include nodes within 15 meters
for i := merge_or_split_node - 3; i >= 0; i-- {
if positions[merge_or_split_node].DistanceTo(positions[i]) > 15 {
break
}
curvatures[i].Curvature = 0.0015
}
// also include forward nodes within 15 meters
for i := merge_or_split_node; i < len(curvatures); i++ {
if positions[merge_or_split_node].DistanceTo(positions[i]) > 15 {
break
}
curvatures[i].Curvature = 0.0015
}
}
average_curvatures, err := GetAverageCurvatures(curvatures)
if err != nil {
return []m.Curvature{}, errors.Wrap(err, "could not get average curvatures from curvatures")
}
return average_curvatures, nil
}
type Velocity struct {
Pos m.Position
Velocity float64
TriggerDistance float32
}
func GetTargetVelocities(curvatures []m.Curvature, previousTargets []Velocity) (velocities []Velocity) {
velocities = make([]Velocity, len(curvatures))
for i, curv := range curvatures {
if curv.Curvature == 0 {
continue
}
velocities[i].Velocity = math.Pow(float64(ms.Settings.MapCurveTargetLatA)/curv.Curvature, 1.0/2)
velocities[i].Pos = curv.Pos
for _, t := range previousTargets {
if velocities[i].Pos.Equals(t.Pos) {
velocities[i].TriggerDistance = t.TriggerDistance
}
}
}
return velocities
}
func GetAverageCurvatures(curvatures []m.Curvature) (average_curvatures []m.Curvature, err error) {
if len(curvatures) < 3 {
return []m.Curvature{}, errors.New("not enough curvatures to average")
}
average_curvatures = make([]m.Curvature, len(curvatures)-2)
for i := 0; i < len(curvatures)-2; i++ {
a := curvatures[i].Curvature
b := curvatures[i+1].Curvature
c := curvatures[i+2].Curvature
al := curvatures[i].ArcLength
bl := curvatures[i+1].ArcLength
cl := curvatures[i+2].ArcLength
if al+bl+cl == 0 {
average_curvatures[i] = curvatures[i+2]
continue
}
avg := m.Curvature{Pos: curvatures[i+1].Pos}
avg.Curvature = (a*al + b*bl + c*cl) / (al + bl + cl)
avg.ArcLength = (curvatures[i].ArcLength + curvatures[i+1].ArcLength + curvatures[i+2].ArcLength) / 3
avg.Angle = (curvatures[i].Angle + curvatures[i+1].Angle + curvatures[i+2].Angle) / 3
average_curvatures[i] = avg
}
return average_curvatures, nil
}
func GetCurvatures(positions []m.Position) (curvatures []m.Curvature, err error) {
if len(positions) < 3 {
return []m.Curvature{}, errors.New(fmt.Sprintf("not enough points to calculate curvatures. len(points): %d", len(positions)))
}
curvatures = make([]m.Curvature, len(positions)-2)
for i := 0; i < len(positions)-2; i++ {
curvature := m.CalculateCurvature(positions[i], positions[i+1], positions[i+2])
curvatures[i] = curvature
}
return curvatures, nil
}