/
roadway_generation.jl
233 lines (206 loc) · 10.1 KB
/
roadway_generation.jl
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
gen_straight_curve(A::VecE2{T}, B::VecE2{T}, nsamples::Integer) where T<:Real
Returns a `Curve` corresponding to a straight line between A and B. `nsamples` indicates the
number of points to place between A and B, if set to two, the curve will only contains A and B.
"""
function gen_straight_curve(A::VecE2{T}, B::VecE2{T}, nsamples::Integer) where T<:Real
θ = atan(B-A)
δ = norm(B-A)/(nsamples-1)
s = 0.0
curve = Array{CurvePt{T}}(undef, nsamples)
for i in 1 : nsamples
t = (i-1)/(nsamples-1)
P = lerp(A,B,t)
curve[i] = CurvePt(VecSE2(P.x,P.y,θ), s, 0.0)
s += δ
end
return curve
end
"""
gen_straight_segment(seg_id::Integer, nlanes::Integer, length::Float64=1000.0;
Generate a straight `RoadSegment` with `nlanes` number of lanes of length `length`.
"""
function gen_straight_segment(seg_id::Integer, nlanes::Integer, length::Float64=1000.0;
origin::VecSE2{Float64} = VecSE2(0.0,0.0,0.0),
lane_width::Float64=DEFAULT_LANE_WIDTH, # [m]
lane_widths::Vector{Float64} = fill(lane_width, nlanes),
boundary_leftmost::LaneBoundary=LaneBoundary(:solid, :white),
boundary_rightmost::LaneBoundary=LaneBoundary(:solid, :white),
boundary_middle::LaneBoundary=LaneBoundary(:broken, :white),
)
seg = RoadSegment(seg_id, Array{Lane{Float64}}(undef, nlanes))
y = -lane_widths[1]/2
for i in 1 : nlanes
y += lane_widths[i]/2
seg.lanes[i] = Lane(LaneTag(seg_id,i), [CurvePt(body2inertial(VecSE2( 0.0,y,0.0), origin), 0.0),
CurvePt(body2inertial(VecSE2(length,y,0.0), origin), length)],
width=lane_widths[i],
boundary_left=(i == nlanes ? boundary_leftmost : boundary_middle),
boundary_right=(i == 1 ? boundary_rightmost : boundary_middle)
)
y += lane_widths[i]/2
end
return seg
end
"""
quadratic bezier lerp
"""
Vec.lerp(A::VecE2{T}, B::VecE2{T}, C::VecE2{T}, t::T) where T<:Real = (1-t)^2*A + 2*(1-t)*t*B + t^2*B
"""
cubic bezier lerp
"""
Vec.lerp(A::VecE2{T}, B::VecE2{T}, C::VecE2{T}, D::VecE2{T}, t::T) where T<:Real = (1-t)^3*A + 3*(1-t)^2*t*B + 3*(1-t)*t^2*C + t^3*D
"""
gen_bezier_curve(A::VecSE2{T}, B::VecSE2{T}, rA::T, rB::T, nsamples::Int) where T <: Real
Generate a Bezier curve going from A to B with radii specified by rA and rB. It uses cubic
interpolation. `nsamples` specifies the number of point along the curve between A and B. The more
points, the more accurate the approximation is.
This is useful to generate arcs.
"""
function gen_bezier_curve(A::VecSE2{T}, B::VecSE2{T}, rA::T, rB::T, nsamples::Int) where T <: Real
a = convert(VecE2, A)
d = convert(VecE2, B)
b = a + polar( rA, A.θ)
c = d + polar(-rB, B.θ)
s = 0.0
curve = Array{CurvePt{T}}(undef, nsamples)
for i in 1 : nsamples
t = (i-1)/(nsamples-1)
P = lerp(a,b,c,d,t)
P′ = 3*(1-t)^2*(b-a) + 6*(1-t)*t*(c-b) + 3*t^2*(d-c)
P′′ = 6*(1-t)*(c-2b+a) + 6t*(d-2*c+b)
θ = atan(P′)
κ = (P′.x*P′′.y - P′.y*P′′.x)/(P′.x^2 + P′.y^2)^1.5 # signed curvature
if i > 1
s += norm(P - convert(VecE2, curve[i-1].pos)) # approximation, but should be good for many samples
end
curve[i] = CurvePt(VecSE2(P.x,P.y,θ), s, κ)
end
return curve
end
"""
gen_straight_roadway(nlanes::Int, length::Float64)
Generate a roadway with a single straight segment whose rightmost lane center starts at starts at (0,0),
and proceeds in the positive x direction.
"""
function gen_straight_roadway(nlanes::Int, length::Float64=1000.0;
origin::VecSE2{Float64} = VecSE2(0.0,0.0,0.0),
lane_width::Float64=DEFAULT_LANE_WIDTH, # [m]
lane_widths::Vector{Float64} = fill(lane_width, nlanes),
boundary_leftmost::LaneBoundary=LaneBoundary(:solid, :white),
boundary_rightmost::LaneBoundary=LaneBoundary(:solid, :white),
boundary_middle::LaneBoundary=LaneBoundary(:broken, :white),
)
retval = Roadway()
push!(retval.segments, gen_straight_segment(1, nlanes, length,
origin=origin, lane_widths=lane_widths,
boundary_leftmost=boundary_leftmost,
boundary_rightmost=boundary_rightmost,
boundary_middle=boundary_middle))
retval
end
"""
gen_stadium_roadway(nlanes::Int; length::Float64=100.0; width::Float64=10.0; radius::Float64=25.0)
Generate a roadway that is a rectangular racetrack with rounded corners.
length = length of the x-dim straight section for the innermost (leftmost) lane [m]
width = length of the y-dim straight section for the innermost (leftmost) lane [m]
radius = turn radius [m]
______________________
/ \\
| |
| |
\\______________________/
"""
function gen_stadium_roadway(nlanes::Int;
length::Float64=100.0,
width::Float64=10.0,
radius::Float64=25.0,
ncurvepts_per_turn::Int=25, # includes start and end
lane_width::Float64=DEFAULT_LANE_WIDTH, # [m]
boundary_leftmost::LaneBoundary=LaneBoundary(:solid, :white),
boundary_rightmost::LaneBoundary=LaneBoundary(:solid, :white),
boundary_middle::LaneBoundary=LaneBoundary(:broken, :white),
)
ncurvepts_per_turn ≥ 2 || error("must have at least 2 pts per turn")
A = VecE2(length, radius)
B = VecE2(length, width + radius)
C = VecE2(0.0, width + radius)
D = VecE2(0.0, radius)
seg1 = RoadSegment(1, Array{Lane{Float64}}(undef, nlanes))
seg2 = RoadSegment(2, Array{Lane{Float64}}(undef, nlanes))
seg3 = RoadSegment(3, Array{Lane{Float64}}(undef, nlanes))
seg4 = RoadSegment(4, Array{Lane{Float64}}(undef, nlanes))
seg5 = RoadSegment(5, Array{Lane{Float64}}(undef, nlanes))
seg6 = RoadSegment(6, Array{Lane{Float64}}(undef, nlanes))
for i in 1 : nlanes
curvepts1 = Array{CurvePt{Float64}}(undef, ncurvepts_per_turn)
curvepts2 = Array{CurvePt{Float64}}(undef, ncurvepts_per_turn)
curvepts3 = Array{CurvePt{Float64}}(undef, 2)
curvepts4 = Array{CurvePt{Float64}}(undef, ncurvepts_per_turn)
curvepts5 = Array{CurvePt{Float64}}(undef, ncurvepts_per_turn)
curvepts6 = Array{CurvePt{Float64}}(undef, 2)
r = radius + lane_width*(i-1)
for j in 1:ncurvepts_per_turn
t = (j-1)/(ncurvepts_per_turn-1) # ∈ [0,1]
s = r*π/2*t
curvepts1[j] = CurvePt(VecSE2(A + polar(r, lerp(-π/2, 0.0, t)), lerp(0.0,π/2,t)), s)
curvepts2[j] = CurvePt(VecSE2(B + polar(r, lerp( 0.0, π/2, t)), lerp(π/2,π, t)), s)
curvepts4[j] = CurvePt(VecSE2(C + polar(r, lerp( π/2, π, t)), lerp(π, 3π/2,t)), s)
curvepts5[j] = CurvePt(VecSE2(D + polar(r, lerp( π, 3π/2, t)), lerp(3π/2,2π,t)), s)
end
# fill in straight segments
curvepts3[1] = CurvePt(curvepts2[end].pos, 0.0)
curvepts3[2] = CurvePt(curvepts4[1].pos, length)
curvepts6[1] = CurvePt(curvepts5[end].pos, 0.0)
curvepts6[2] = CurvePt(curvepts1[1].pos, length)
laneindex = nlanes-i+1
tag1 = LaneTag(1,laneindex)
tag2 = LaneTag(2,laneindex)
tag3 = LaneTag(3,laneindex)
tag4 = LaneTag(4,laneindex)
tag5 = LaneTag(5,laneindex)
tag6 = LaneTag(6,laneindex)
boundary_left = (laneindex == nlanes ? boundary_leftmost : boundary_middle)
boundary_right = (laneindex == 1 ? boundary_rightmost : boundary_middle)
curveind_lo = CurveIndex(1,0.0)
curveind_hi = CurveIndex(ncurvepts_per_turn,1.0)
seg1.lanes[laneindex] = Lane(tag1, curvepts1, width=lane_width,
boundary_left=boundary_left, boundary_right=boundary_right,
next = RoadIndex(curveind_lo, tag2),
prev = RoadIndex(curveind_hi, tag6),
)
seg2.lanes[laneindex] = Lane(tag2, curvepts2, width=lane_width,
boundary_left=boundary_left, boundary_right=boundary_right,
next = RoadIndex(curveind_lo, tag3),
prev = RoadIndex(CurveIndex(ncurvepts_per_turn - 1,1.0), tag1),
)
seg3.lanes[laneindex] = Lane(tag3, curvepts3, width=lane_width,
boundary_left=boundary_left, boundary_right=boundary_right,
next = RoadIndex(curveind_lo, tag4),
prev = RoadIndex(curveind_hi, tag2),
)
seg4.lanes[laneindex] = Lane(tag4, curvepts4, width=lane_width,
boundary_left=boundary_left, boundary_right=boundary_right,
next = RoadIndex(curveind_lo, tag5),
prev = RoadIndex(curveind_hi, tag3),
)
seg5.lanes[laneindex] = Lane(tag5, curvepts5, width=lane_width,
boundary_left=boundary_left, boundary_right=boundary_right,
next = RoadIndex(curveind_lo, tag6),
prev = RoadIndex(CurveIndex(ncurvepts_per_turn - 1,1.0), tag4),
)
seg6.lanes[laneindex] = Lane(tag6, curvepts6, width=lane_width,
boundary_left=boundary_left, boundary_right=boundary_right,
next = RoadIndex(curveind_lo, tag1),
prev = RoadIndex(curveind_hi, tag5),
)
end
retval = Roadway()
push!(retval.segments, seg1)
push!(retval.segments, seg2)
push!(retval.segments, seg3)
push!(retval.segments, seg4)
push!(retval.segments, seg5)
push!(retval.segments, seg6)
retval
end