-
Notifications
You must be signed in to change notification settings - Fork 8
/
Bezier.py
353 lines (304 loc) · 14.1 KB
/
Bezier.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import random
import matplotlib.pyplot as plt
import math
from scipy.optimize import minimize, rosen, rosen_der
import random
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def random(self, min= 0, max= 1):
self.x = random.uniform(min,max)
self.y = random.uniform(min,max)
#
#=============Cubic Bezier curve====================
#
class CubicBezier(object):
def __init__(self, p0x= 0, p0y= 0, p1x= 0, p1y= 0, p2x= 0, p2y= 0, p3x= 0, p3y= 0):
self.p0 = Point(p0x, p0y)
self.p1 = Point(p1x, p1y)
self.p2 = Point(p2x, p2y)
self.p3 = Point(p3x, p3y)
self.obstacles = []
def random(self, min= 0, max= 1):
'Create a random cubic Bezier curve within [min, max] limits. Default [0,1].'
self.p0.random(min, max)
self.p1.random(min, max)
self.p2.random(min, max)
self.p3.random(min, max)
def max_k(self, granuality=100):
'Calculate maximal curvature of the cubic Bezier curve.'
k = 0
for t in range(0, granuality):
t = t / granuality
x_d = 3 * ((1 - t) ** 2) * (self.p1.x - self.p0.x) + 6 * (1 - t) * t * (self.p2.x - self.p1.x) + 3 * (t ** 2) * (
self.p3.x - self.p2.x)
y_d = 3 * ((1 - t) ** 2) * (self.p1.y - self.p0.y) + 6 * (1 - t) * t * (self.p2.y - self.p1.y) + 3 * (t ** 2) * (
self.p3.y - self.p2.y)
x_dd = 6 * (1 - t) * (self.p2.x - 2 * self.p1.x + self.p0.x) + 6 * t * (self.p3.x - 2 * self.p2.x + self.p1.x)
y_dd = 6 * (1 - t) * (self.p2.y - 2 * self.p1.y + self.p0.y) + 6 * t * (self.p3.y - 2 * self.p2.y + self.p1.y)
k = max(k,abs(x_d*y_dd - y_d*x_dd)/math.pow(x_d**2 + y_d**2, 3/2))
return k
def calc_curve(self, granuality=100):
'Calculate the cubic Bezier curve with the given granuality.'
B_x = []
B_y = []
for t in range(0, granuality):
t = t / granuality
x = ((1 - t) ** 3) * self.p0.x + 3 * ((1 - t) ** 2) * t * self.p1.x + 3 * (1 - t) * (t ** 2) * self.p2.x\
+ (t ** 3) * self.p3.x
y = ((1 - t) ** 3) * self.p0.y + 3 * ((1 - t) ** 2) * t * self.p1.y + 3 * (1 - t) * (t ** 2) * self.p2.y\
+ (t ** 3) * self.p3.y
B_x.append(x)
B_y.append(y)
return [B_x, B_y]
def plot(self, granuality=100):
'Plot the cubic Bezier curve.'
B = self.calc_curve(granuality)
plt.plot(B[0], B[1])
plt.scatter([self.p0.x,self.p1.x,self.p2.x,self.p3.x], [self.p0.y,self.p1.y,self.p2.y,self.p3.y])
for i in range(len(self.obstacles)):
plt.gcf().gca().add_artist(plt.Circle((self.obstacles[i][0].x, self.obstacles[i][0].y), self.obstacles[i][1], color='r'))
plt.axis('equal')
plt.show()
def arc_len(self, granuality=1000):
'Calculate the arc-length of the cubic Bezier curve.'
B = self.calc_curve(granuality=granuality)
a_l = 0
for i in range(1,len(B[0])):
a_l += math.sqrt((B[0][i]-B[0][i-1])**2 + (B[1][i]-B[1][i-1])**2)
return a_l
def optimize_k(self, granuality= 100, obs= True):
'Optimize the cubic Bezier curve to minimize the curvature. By setting obs=False, ignore the obstacles.'
x0 = [0.0, 0.0, 0.0, 0.0]
res = minimize(self.optimizer_k, x0, args= (granuality, obs), method='Nelder-Mead', tol=1e-7)
self.p1.x = self.p1.x + res.x[0]
self.p1.y = self.p1.y + res.x[1]
self.p2.x = self.p2.x + res.x[2]
self.p2.y = self.p2.y + res.x[3]
def optimizer_k(self,x, *args):
'Curvature optimizer function.'
granuality = args[0]
obs = args[1]
o = CubicBezier()
o.p0 = self.p0
o.p1.x = self.p1.x+x[0]
o.p1.y = self.p1.y+x[1]
o.p2.x = self.p2.x + x[2]
o.p2.y = self.p2.y + x[3]
o.p3 = self.p3
penalty = 0
if obs:
B = o.calc_curve(granuality)
for i in range(len(B[0])):
for j in range(len(self.obstacles)):
d = math.sqrt((B[0][i] - self.obstacles[j][0].x)**2 + (B[1][i] - self.obstacles[j][0].y)**2)
if d<self.obstacles[j][1]:
penalty += (self.obstacles[j][1]-d)*100
return o.max_k(granuality) + penalty
def optimize_l(self, granuality= 100, obs= True):
'Optimize the cubic Bezier curve to minimize the arc-length. By setting obs=False, ignore the obstacles.'
x0 = [0.0, 0.0, 0.0, 0.0]
res = minimize(self.optimizer_l, x0, args=(granuality, obs), method='Nelder-Mead', tol=1e-7)
self.p1.x = self.p1.x + res.x[0]
self.p1.y = self.p1.y + res.x[1]
self.p2.x = self.p2.x + res.x[2]
self.p2.y = self.p2.y + res.x[3]
def optimizer_l(self,x, *args):
'Arc-length optimizer function.'
granuality = args[0]
obs = args[1]
o = CubicBezier()
o.p0 = self.p0
o.p1.x = self.p1.x+x[0]
o.p1.y = self.p1.y+x[1]
o.p2.x = self.p2.x + x[2]
o.p2.y = self.p2.y + x[3]
o.p3 = self.p3
penalty = 0
if obs:
B = o.calc_curve(granuality)
for i in range(len(B[0])):
for j in range(len(self.obstacles)):
d = math.sqrt((B[0][i] - self.obstacles[j][0].x)**2 + (B[1][i] - self.obstacles[j][0].y)**2)
if d<self.obstacles[j][1]:
penalty += (self.obstacles[j][1]-d)*100
return o.arc_len(granuality) + penalty
def optimize(self, granuality=100, obs=True, l_multiplier=0.5, k_multiplier=0.5):
"""
Optimize the cubic Bezier curve to simultaniously minimize the arc-lenght and the curvature.
Setting obs=False ignores the obstacles. l_multiplier and k_multiplier multiplies
the outputs of their respective optimizer functions.
"""
x0 = [0.0, 0.0, 0.0, 0.0]
res = minimize(self.optimizer, x0, args=(granuality, obs, l_multiplier, k_multiplier), method='Nelder-Mead', tol=1e-7)
self.p1.x = self.p1.x + res.x[0]
self.p1.y = self.p1.y + res.x[1]
self.p2.x = self.p2.x + res.x[2]
self.p2.y = self.p2.y + res.x[3]
def optimizer(self,x,*args):
'Optimizer function of the arc-length and curvature simultanious optimization.'
granuality = args[0]
obs = args[1]
l_multiplier = args[2]
k_multiplier = args[3]
return self.optimizer_l(x, granuality, obs) * l_multiplier + self.optimizer_k(x, granuality, obs) * k_multiplier
def add_obstacle(self, x=0, y=0, radius=0):
'Add an obstacle to the cubic Bezier curve.'
self.obstacles.append([Point(x,y), radius])
def add_random_obstacle(self, min_x= 1, max_x= 0, min_y=1, max_y=0, min_radius=0.3, max_radius = 0.0):
"""Add a random obstacle to the cubic Bezier curve. The obstacle will not cover the p0 and p3 points
of the Bezier curve.
"""
radius = random.uniform(min_radius,max_radius)
d = 0
x = 0
y = 0
while d<radius:
x = random.uniform(min_x,max_x)
y = random.uniform(min_y,max_y)
d1 = math.sqrt((x - self.p0.x)**2 + (y - self.p0.y)**2)
d2 = math.sqrt((x - self.p3.x) ** 2 + (y - self.p3.y) ** 2)
d = min(d1,d2)
self.obstacles.append([Point(x, y), radius])
def clear(self):
'Re-initialize the curve.'
self.__init__()
#
#=============Quadratic Bezier curve====================
#
class QuadBezier(object):
def __init__(self, p0x= 0, p0y= 0, p1x= 0, p1y= 0, p2x= 0, p2y= 0):
self.p0 = Point(p0x, p0y)
self.p1 = Point(p1x, p1y)
self.p2 = Point(p2x, p2y)
self.obstacles = []
def random(self,min= 0, max= 1):
'Create a random quadratic Bezier curve within [min, max] limits. Default [0,1].'
self.p0.random(min, max)
self.p1.random(min, max)
self.p2.random(min, max)
def max_k(self, granuality=100):
'Calculate maximal curvature of the quadratic Bezier curve.'
k = 0
for t in range(0, granuality):
t = t / granuality
x_d = 2 * (t - 1)*(self.p1.x - self.p0.x) + 2 * t * (self.p2.x - self.p1.x)
y_d = 2 * (t - 1)*(self.p1.y - self.p0.y) + 2 * t * (self.p2.y - self.p1.y)
x_dd = 2 * (self.p2.x - 2 * self.p1.x + self.p0.x)
y_dd = 2 * (self.p2.y - 2 * self.p1.y + self.p0.y)
k = max(k,abs(x_d*y_dd - y_d*x_dd)/math.pow(x_d**2 + y_d**2, 3/2))
return k
def calc_curve(self, granuality=100):
'Calculate the quadratic Bezier curve with the given granuality.'
B_x = []
B_y = []
for t in range(0, granuality):
t = t / granuality
x = self.p1.x + (1 - t)**2 * (self.p0.x-self.p1.x) + t**2 * (self.p2.x - self.p1.x)
y = self.p1.y + (1 - t)**2 * (self.p0.y-self.p1.y) + t**2 * (self.p2.y - self.p1.y)
B_x.append(x)
B_y.append(y)
return [B_x, B_y]
def plot(self, granuality=100):
'Plot the quadratic Bezier curve.'
B = self.calc_curve(granuality)
plt.plot(B[0], B[1])
plt.scatter([self.p0.x,self.p1.x,self.p2.x], [self.p0.y,self.p1.y,self.p2.y])
for i in range(len(self.obstacles)):
plt.gcf().gca().add_artist(plt.Circle((self.obstacles[i][0].x, self.obstacles[i][0].y), self.obstacles[i][1], color='r'))
plt.axis('equal')
plt.show()
def arc_len(self, granuality=1000):
'Calculate the arc-length of the quadratic Bezier curve.'
B = self.calc_curve(granuality=granuality)
a_l = 0
for i in range(1,len(B[0])):
a_l += math.sqrt((B[0][i]-B[0][i-1])**2 + (B[1][i]-B[1][i-1])**2)
return a_l
def optimize_k(self, granuality= 100, obs= True):
'Optimize the quadratic Bezier curve to minimize the curvature. By setting obs=False, ignore the obstacles.'
x0 = [0.0, 0.0]
res = minimize(self.optimizer_k, x0, args= (granuality, obs), method='Nelder-Mead', tol=1e-7)
self.p1.x = self.p1.x + res.x[0]
self.p1.y = self.p1.y + res.x[1]
def optimizer_k(self,x, *args):
'Curvature optimizer function.'
granuality = args[0]
obs = args[1]
o = QuadBezier()
o.p0 = self.p0
o.p1.x = self.p1.x+x[0]
o.p1.y = self.p1.y+x[1]
o.p2 = self.p2
penalty = 0
if obs:
B = o.calc_curve(granuality)
for i in range(len(B[0])):
for j in range(len(self.obstacles)):
d = math.sqrt((B[0][i] - self.obstacles[j][0].x)**2 + (B[1][i] - self.obstacles[j][0].y)**2)
if d<self.obstacles[j][1]:
penalty += (self.obstacles[j][1]-d)*100
return o.max_k(granuality) + penalty
def optimize_l(self, granuality= 100, obs= True):
'Optimize the quadratic Bezier curve to minimize the arc-length. By setting obs=False, ignore the obstacles.'
x0 = [0.0, 0.0]
res = minimize(self.optimizer_l, x0, args=(granuality, obs), method='Nelder-Mead', tol=1e-7)
self.p1.x = self.p1.x + res.x[0]
self.p1.y = self.p1.y + res.x[1]
def optimizer_l(self,x, *args):
'Arc-length optimizer function.'
granuality = args[0]
obs = args[1]
o = QuadBezier()
o.p0 = self.p0
o.p1.x = self.p1.x+x[0]
o.p1.y = self.p1.y+x[1]
o.p2 = self.p2
penalty = 0
if obs:
B = o.calc_curve(granuality)
for i in range(len(B[0])):
for j in range(len(self.obstacles)):
d = math.sqrt((B[0][i] - self.obstacles[j][0].x)**2 + (B[1][i] - self.obstacles[j][0].y)**2)
if d<self.obstacles[j][1]:
penalty += (self.obstacles[j][1]-d)*100
return o.arc_len(granuality) + penalty
def optimize(self, granuality=100, obs=True, l_multiplier=0.5, k_multiplier=0.5):
"""
Optimize the quadratic Bezier curve to simultaniously minimize the arc-lenght and the curvature.
Setting obs=False ignores the obstacles. l_multiplier and k_multiplier multiplies
the outputs of their respective optimizer functions.
"""
x0 = [0.0, 0.0]
res = minimize(self.optimizer, x0, args=(granuality, obs, l_multiplier, k_multiplier), method='Nelder-Mead', tol=1e-7)
self.p1.x = self.p1.x + res.x[0]
self.p1.y = self.p1.y + res.x[1]
def optimizer(self,x,*args):
'Optimizer function of the arc-length and curvature simultanious optimization.'
granuality = args[0]
obs = args[1]
l_multiplier = args[2]
k_multiplier = args[3]
return self.optimizer_l(x, granuality, obs) * l_multiplier + self.optimizer_k(x, granuality, obs) * k_multiplier
def add_obstacle(self, x=0, y=0, radius=0):
'Add an obstacle to the quadratic Bezier curve.'
self.obstacles.append([Point(x,y), radius])
def add_random_obstacle(self, min_x= 1, max_x= 0, min_y=1, max_y=0, min_radius=0.3, max_radius = 0.0):
"""Add a random obstacle to the quadratic Bezier curve. The obstacle will not cover the p0 and p2 points
of the Bezier curve.
"""
radius = random.uniform(min_radius,max_radius)
d = 0
x = 0
y = 0
while d<radius:
x = random.uniform(min_x,max_x)
y = random.uniform(min_y,max_y)
d1 = math.sqrt((x - self.p0.x)**2 + (y - self.p0.y)**2)
d2 = math.sqrt((x - self.p2.x) ** 2 + (y - self.p2.y) ** 2)
d = min(d1,d2)
self.obstacles.append([Point(x, y), radius])
def clear(self):
'Re-initialize the curve.'
self.__init__()