-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultibezier.py
562 lines (463 loc) · 14.8 KB
/
multibezier.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import math
import os
import numpy as np
import matplotlib.pyplot as plt
"""
Utility functions
-----------------
"""
def binomial(n, i):
if n >= 0 and i >= 0:
b = math.factorial(n) / (math.factorial(i) * math.factorial(n - i))
else:
b = 0
return b
def block_diag(A,B):
out = np.zeros((A.shape[0] + B.shape[0], A.shape[1] + B.shape[1]))
out[:A.shape[0], :A.shape[1]] = A
out[A.shape[0]:, A.shape[1]:] = B
return out
def cubic_bezier_basis(nbDim, nbSeg, edges_at_zero=False, C1=True):
"""
Computes the matrix phi whose columns are cubic Bernstein polynomials
Parameters
----------
nbDim : int
desired length of each basis function (number of rows of phi)
nbSeg : int
number of segments to concatenate
edges_at_zero : bool
if true, the extremities will be constrained to 0
C1 : bool
if true, the resulting curve will be C1 continuous
if false, the resulting curve will be C0 continuous
Returns
-------
phi : basis functions matrix
C : constraint matrix, useful to retrieve control points
"""
nbT = nbDim // nbSeg
nbFct = 4
t = np.linspace(0, 1 - 1 / nbT, nbT)
T0 = np.zeros((len(t), nbFct))
for n in range(nbFct):
T0[:, n] = np.power(t, n)
B0 = np.zeros((nbFct, nbFct))
for n in range(1, nbFct + 1):
for i in range(1, nbFct + 1):
tmp = (-1) ** (nbFct - i - n)
tmp *= -binomial(nbFct - 1, i - 1)
tmp *= binomial(nbFct - 1 - (i - 1), nbFct - 1 - (n - 1) - (i - 1))
B0[nbFct - i, n - 1] = tmp
T = np.kron(np.eye(nbSeg), T0)
B = np.kron(np.eye(nbSeg), B0)
if C1 is True: # Continuous derivative constraint
C0 = np.array([[1, 0, 0, -1], [0, 1, 1, 2]]).T
if nbFct >= 4:
C0 = block_diag(np.eye(nbFct-4),C0)
C = np.eye(2)
for n in range(nbSeg-1):
C = block_diag(C,C0)
C = block_diag(C,np.eye(nbFct-2))
if edges_at_zero is True:
# remove the 2 first and 2 last columns to force keypoints to 0
C = C[:,2:-2]
else: # C0 continuity constraint
C0 = np.array([[1], [1]])
C0 = block_diag(C0, np.eye(2))
C = np.eye(3)
for n in range(nbSeg-1):
C = block_diag(C,C0)
C = block_diag(C,np.eye(1))
if edges_at_zero is True:
# remove the first and last columns to force keypoints to 0
C = C[:,1:-1]
phi = T @ B @ C
return phi, C
# TODO: re-implement the C matrix computation for any nbSeg
def quad_bezier_basis(nbDim, nbSeg, edges_at_zero=False, C1=True):
"""
Computes the matrix phi whose columns are quadratic Bernstein polynomials
Parameters
----------
nbDim : int
desired length of each basis function (number of rows of phi)
nbSeg : int
number of segments to concatenate
edges_at_zero : bool
if true, the extremities will be constrained to 0
C1 : bool
if true, the resulting curve will be C1 continuous
if false, the resulting curve will be C0 continuous
Returns
-------
phi : basis functions matrix
C : constraint matrix, useful to retrieve control points
"""
nbT = nbDim // nbSeg
nbFct = 3
t = np.linspace(0, 1 - 1 / nbT, nbT)
T0 = np.zeros((len(t), nbFct))
for n in range(nbFct):
T0[:, n] = np.power(t, n)
B0 = np.zeros((nbFct, nbFct))
for n in range(1, nbFct + 1):
for i in range(1, nbFct + 1):
tmp = (-1) ** (nbFct - i - n)
tmp *= -binomial(nbFct - 1, i - 1)
tmp *= binomial(nbFct - 1 - (i - 1), nbFct - 1 - (n - 1) - (i - 1))
B0[nbFct - i, n - 1] = tmp
T = np.kron(np.eye(nbSeg), T0)
B = np.kron(np.eye(nbSeg), B0)
if C1 is True: # Continuous derivative constraint
# ! Only valid for nbSeg = 2
C = np.array([
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, -1, 2, 0, 0],
[0, -1/2, 1, 1/2, 0],
[0, -1/2, 1, 1/2, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]
])
if edges_at_zero is True:
# remove the 2 first and 2 last columns to force keypoints to 0
C = C[:,2:-2]
else: # C0 continuity constraint
# ! Only valid for nbSeg = 2
C0 = np.array([
[1, 0],
[1, 0]
])
C = np.eye(2)
for n in range(nbSeg-1):
C = block_diag(C,C0)
C = block_diag(C, np.eye(1))
C = block_diag(C,np.eye(1))
if edges_at_zero is True:
# remove the first and last columns to force keypoints to 0
C = C[:,1:-1]
phi = T @ B @ C
return phi, C
def subdivide_2D(x):
axis0_center = x.shape[0] // 2
axis1_center = x.shape[1] // 2
subdivisions = [
x[:axis0_center, :axis1_center],
x[:axis0_center, axis1_center:],
x[axis0_center:, :axis1_center],
x[axis0_center:, axis1_center:]
]
return subdivisions
def subdivide_3D(x):
axis0_center = x.shape[0] // 2
axis1_center = x.shape[1] // 2
axis2_center = x.shape[2] // 2
subdivisions = [
x[:axis0_center, :axis1_center, :axis2_center],
x[:axis0_center, :axis1_center, axis2_center:],
x[:axis0_center, axis1_center:, :axis2_center],
x[:axis0_center, axis1_center:, axis2_center:],
x[axis0_center:, :axis1_center, :axis2_center],
x[axis0_center:, :axis1_center, axis2_center:],
x[axis0_center:, axis1_center:, :axis2_center],
x[axis0_center:, axis1_center:, axis2_center:],
]
return subdivisions
"""
Plotting functions
------------------
"""
def plot_1D(Psi, weights, x0, subdivisions=[], layers_indices = [], title=""):
"""
Plot each reconstruction layer (contribution of each iteration)
"""
fig, axs = plt.subplots(len(layers_indices)+1, 1, sharex=True)
if len(layers_indices) > 0:
ax = axs[-1]
fig.set_size_inches(8,2*(len(layers_indices)+1))
for l, layer in enumerate(layers_indices):
x_layer = Psi[:, layer]@weights[layer]
axs[l].plot(x_layer)
axs[l].set_title(f"Layer {l+1}")
for s, segment in enumerate(subdivisions):
axs[l].axvline(segment[0], c='black', alpha=0.5)
else:
ax = axs
fig.set_size_inches(8, 6)
ax.plot(Psi@weights, label="reconstruction")
ax.plot(x, label="original")
for s, segment in enumerate(subdivisions):
ax.axvline(segment[0], c='black', alpha=0.5)
ax.legend()
ax.set_xlabel("Samples")
ax.set_ylabel("Amplitude")
ax.set_title(title)
fig.tight_layout()
return
def plot_2D(x, in_shape, subdivisions=[], title=""):
T1, T2 = np.meshgrid(
np.linspace(0, 1, in_shape[0]),
np.linspace(0, 1, in_shape[1])
)
plt.figure(figsize=(8, 8))
# Reconstructed surface
plt.axis('off')
# ax2.plot_surface(T1, T2, np.reshape(xb, (nbDim, nbDim)) - np.max(xb), cmap='viridis', edgecolor='k')
plt.contour(T1, T2, np.reshape(x, in_shape), levels=np.arange(0, 1, 0.02), linewidths=2)
msh = plt.contour(T1, T2, np.reshape(x, in_shape), levels=[0], linewidths=4, colors='b')
plt.axis('tight')
plt.axis('equal')
plt.title(title)
# Subdivisions
nbDim = in_shape[0] # assuming square input
for segment in subdivisions:
x = (segment[0,0] % nbDim)/nbDim
y = (segment[0,0] // nbDim)/nbDim
width = segment.shape[0]/nbDim
height = segment.shape[1]/nbDim
p = plt.gca().add_patch(plt.Rectangle((x,y), width, height, fill=False))
return
"""
Fitting algorithm
-----------------
"""
def adaptive_fit(x, in_shape, nbOut, nbFct, iterations=1, threshold=0.0, C1=True):
"""
Computes an approximation of a given signal using a superposition of concatenated
Bézier curves of different resolutions by recursive subdivisions of the signal
Parameters
----------
x : signal to fit (can be 1D, 2D or 3D)
in_shape : tuple with the size of each input dimension
nbOut: number of output dimensions
nbFct: degree + 1 of polynomials used for the fitting
iterations: number of subdivisions to perform
threshold: max RMSE of each segment for the subdivision criterion
C1 : bool, whether the concatenated polynomials should be C1 continuous
Returns
-------
x = Psi @ weights
Psi : basis functions matrix
weights : weights of each basis polynomial
"""
if nbFct == 4: # cubic
nbSeg = 2
suffix='cubic_'
elif nbFct == 3:
nbSeg = 3
suffix='quad_'
if C1 is True:
suffix += 'C1'
else:
suffix += 'C0'
print(f"Fitting a {in_shape} -> ({nbOut}) signal with {suffix} curves")
# Make directories to store plots and 3D approximations
os.makedirs("figures", exist_ok=True)
os.makedirs("3D_rec", exist_ok=True)
residual = np.copy(x)
Psi = np.array([]).reshape(x.size, 0)
weights = np.array([])
subdivisions = [np.arange(0, x.size).reshape(in_shape)] # indices
layers_indices = [] # to keep track of each layer contribution
for i in range(0, iterations):
new_subdivisions = []
for region in subdivisions:
residual_error = np.linalg.norm(residual[region]) # Frobenius norm
if residual_error > threshold:
# Build a 1D basis
if nbFct == 3:
phi_local,_ = quad_bezier_basis(
region.shape[0],
nbSeg,
edges_at_zero = (i>0),
C1=C1)
elif nbFct == 4:
phi_local,_ = cubic_bezier_basis(
region.shape[0],
nbSeg,
edges_at_zero = (i>0),
C1=C1)
# Extend 1D basis to multiple dimensions
Psi_local = np.zeros(
(x.size, phi_local.shape[1]**len(in_shape))
)
psi_tmp = np.copy(phi_local)
for _ in range(len(in_shape)-1):
psi_tmp = np.kron(psi_tmp, phi_local)
Psi_local[region.flatten()] = np.kron(psi_tmp, np.eye(nbOut))
# Augment previous basis with new detail functions
Psi = np.hstack((Psi, Psi_local))
if len(in_shape) == 1:
new_subdivisions.extend(np.array_split(region, nbSeg))
elif len(in_shape) == 2:
new_subdivisions.extend(subdivide_2D(region))
elif len(in_shape) == 3:
new_subdivisions.extend(subdivide_3D(region))
else:
new_subdivisions.append(region)
# Recompute coefficients
weights = np.linalg.pinv(Psi)@x
residual = x - Psi@weights
total_RMSE = np.linalg.norm(residual)
subdivisions = new_subdivisions
if i > 0:
layers_indices.append(
np.arange(layers_indices[i-1][-1]+1, len(weights))
)
else:
layers_indices = [np.arange(len(weights))]
# Visualization
title = f"{i+1} iterations, threshold={threshold:.0e}, RMSE={total_RMSE:.2e}, {len(weights)} weights, {suffix}"
if len(in_shape) == 1:
plot_1D(Psi, weights, x0, subdivisions, [], title)
filename = "figures/1D_" + suffix + f"_iter{i+1}"
plt.savefig(filename)
plt.show()
plt.close()
elif len(in_shape) == 2:
plot_2D(Psi@weights, in_shape, subdivisions, title)
filename = "figures/2D_" + suffix + f"_iter{i+1}"
plt.savefig(filename)
plt.show()
plt.close()
elif len(in_shape) == 3:
data = {
'nbDim': nbDim,
'x': np.arange(x.size).reshape((in_shape)),
'y': Psi@weights,
}
label = 'C1' if C1 is True else 'C0'
np.save(f"3D_rec/{label}_iter{i+1}.npy", data)
print(f"{i+1} iterations, {len(subdivisions)} subdivisions, RMSE={total_RMSE:.2e}, {len(weights)} weights")
return Psi, weights
"""
1D test
-------
"""
"""
For quadratic curves, use a SDF with 3^n samples
so that the region can be split into 3 segments each iteration
"""
data = np.load('sdf_obj02_81.npy',allow_pickle=True).item() # reference
nbDim = data['nbDim']
t12 = data['x']
x0 = np.copy(data['y'])
nbIn = t12.shape[0]
in_shape = tuple([nbDim for i in range(nbIn)])
nbOut = 1
# Slice of the SDF as a 1D test signal
x = x0[round(60/128*nbDim)::nbDim]
# Using quadratic C1 continuous curves
Psi, w = adaptive_fit(
x,
tuple([nbDim]),
nbOut,
nbFct=3,
iterations=4,
threshold=1e-4,
C1=True
)
# Using quadratic C0 continuous curves
Psi, w = adaptive_fit(
x,
tuple([nbDim]),
nbOut,
nbFct=3,
iterations=4,
threshold=1e-4,
C1=False
)
"""
For cubic curves, use a SDF with 2^n samples
so that the region can be split into 2 segments each iteration
"""
data = np.load('sdf_obj02.npy',allow_pickle=True).item() # reference
nbDim = data['nbDim']
t12 = data['x']
x0 = np.copy(data['y'])
nbIn = t12.shape[0]
in_shape = tuple([nbDim for i in range(nbIn)])
nbOut = 1
# Slice of the SDF as a 1D test signal
x = x0[round(60/128*nbDim)::nbDim]
# Using cubic C1 continuous curves
Psi, w = adaptive_fit(
x,
tuple([nbDim]),
nbOut,
nbFct=4,
iterations=5,
threshold=1e-4,
C1=True
)
# Using cubic C0 continuous curves
Psi, w = adaptive_fit(
x,
tuple([nbDim]),
nbOut,
nbFct=4,
iterations=5,
threshold=1e-4,
C1=False
)
"""
2D test
-------
"""
plot_2D(x0, in_shape, title=f"Original SDF with dimensions {in_shape}")
plt.savefig("figures/2D_original")
plt.close()
# Using cubic C1 continuous curves
Psi, w = adaptive_fit(
x0,
in_shape,
nbOut,
nbFct=4,
iterations=5,
threshold=1e-3,
C1=True
)
# Using cubic C0 continuous curves
Psi, w = adaptive_fit(
x0,
in_shape,
nbOut,
nbFct=4,
iterations=5,
threshold=1e-3,
C1=False
)
"""
3D SDF test (slow)
------------------
Each iteration's reconstruction is stored in another file for external visualization
"""
data = np.load('../robotics-codes-from-scratch-master/data/sdf3D01.npy',allow_pickle=True).item()
nbDim = data['nbDim']
t12 = data['x'] # 3D
y = data['y']
nbIn = t12.shape[0]
in_shape = tuple([nbDim for i in range(nbIn)])
nbOut = 1
x0 = np.copy(y)
Psi, weights = adaptive_fit(
x0.flatten(),
in_shape,
nbOut,
nbFct=4,
iterations=5,
threshold=1e-2,
C1=True
)
Psi, weights = adaptive_fit(
x0.flatten(),
in_shape,
nbOut,
nbFct=4,
iterations=4,
threshold=1e-2,
C1=False
)