-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex01_optimize_curve.py
203 lines (164 loc) · 5.68 KB
/
ex01_optimize_curve.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
'''
- Apply optimization procedure to 3D curves
- Plots 2D projects of data and the fit. For 3D visualizations, I recommend to use `vedo` (`vtkplotter`) package
- Try to apply optimization tools to fit 3d curve to projections (simple, e.g. a line)
- Line example:
- works good, when M=30 (and tol at least 10 ** -3); If M=20, then points visibly oscillate around the line
- Good, even when initial guess is very far off
- Helix - OK
- Anisotropic helix ( a/b =27):
- Not good precision with N,M = (30,15) points; (30,30)
- Didn't finish in reasonable time with 40 points
- Better initial conditions: fits xz (smaller projection) good, but in general fails
- Anisotropic helix (a/b = 9): initial conditions - Helix + a lot of noise
- Works almost fine; probably just need more control points
- (60,60) points -> fits almost perfectly
- Helix - different projections: x = cos(s)
- Perfect fit, (30,30)
'''
import time, datetime
import curve3d
import numpy as np
import scipy.optimize as opt
from scipy.interpolate import pchip_interpolate
import random
from curve3d import calc_slist, calc_length
import matplotlib.pyplot as plt
## Define _penalty
def projection_penalty(xx, yy, xx0, yy0): # standard deviation
'''
Assume that xx0, yy0 - are equidistantly spaced in terms of arc length
'''
slist_norm = np.linspace(0, 1, len(xx0), endpoint=True)
slist = calc_slist(xx, yy)
xx_interp = pchip_interpolate(slist / slist[-1], xx, slist_norm)
yy_interp = pchip_interpolate(slist / slist[-1], yy, slist_norm)
return (np.sum((xx_interp - xx0) ** 2 + (yy_interp - yy0) ** 2) / (len(xx0) - 1)) ** (1 / 2)
def f(params, r0, xx0, yy0, xx20, zz0): # Minimization target
'''
:param params: [ds, theta_1, psi_1,..]
:param ds: float
:return:
'''
if len(params) % 2 == 1:
ds = params[0]
thetas = params[1::2]
psis = params[2::2]
else:
raise ValueError
rs = curve3d.build_curve(r0, ds, thetas, psis) # reconstructed curve
xx, yy, zz = rs.T
xy_penalty = projection_penalty(xx, yy, xx0, yy0)
xz_penalty = projection_penalty(xx, zz, xx20, zz0)
# angle_penalty =
# MAYBE: normalize by projection length?
penalty = xy_penalty + xz_penalty
return penalty
N = 40 # Number of data points
M = 40 # Number of fit points
### Define projections
## Line
ss0 = np.linspace(0, 1, N, endpoint=True)
xx0 = 10 * ss0
yy0 = 5 * ss0
xx20 = 10 * ss0
zz0 = 0 * ss0
r0 = (0, 0, 0)
# # Spiral
ss0 = np.linspace(0, 1, N, endpoint=True)
xx0 = 10 * ss0
yy0 = np.sin(10 * ss0)
xx20 = 10 * ss0
zz0 = np.cos(10 * ss0)
r0 = (0, 0, 1)
## Example 5.1 - Spiral: different projection
ss0 = np.linspace(0, 1, N, endpoint=True)
xx0 = np.cos(10 * ss0)
yy0 = np.sin(10 * ss0)
xx20 = np.cos(10 * ss0)
zz0 = 10 * ss0
r0 = (1, 0, 0)
## Example 6.1 - Elleptic spiral # L = 57.8840
# ss0 = sp.linspace(0, 1, N, endpoint=True)
# xx0 = 10 * ss0
# yy0 = sp.sin(10 * ss0)
# xx20 = 10 * ss0
# zz0 = 9 * sp.cos(10 * ss0)
# r0 = (0, 0, 9)
## Example 6.2 - Elleptic spiral # L = 167.4400
# ss0 = sp.linspace(0, 1, N, endpoint=True)
# xx0 = 10 * ss0
# yy0 = sp.sin(10 * ss0)
# xx20 = 10 * ss0
# zz0 = 27 * sp.cos(10 * ss0)
# r0 = (0, 0, 27)
# Make the points equidistantly spaced in terms of projections arc length
slist = calc_slist(xx0, yy0)
slist2 = calc_slist(xx20, zz0)
slist_norm = np.linspace(0, 1, N, endpoint=True)
# Interpolate to the equidistant spaicing in terms of projections arc length
xx1 = pchip_interpolate(slist / slist[-1], xx0, slist_norm)
yy1 = pchip_interpolate(slist / slist[-1], yy0, slist_norm)
xx21 = pchip_interpolate(slist2 / slist2[-1], xx20, slist_norm)
zz1 = pchip_interpolate(slist2 / slist2[-1], zz0, slist_norm)
# ## Test penalty
# # OK: = 0 on the same curve (line)
# # OK: = 0 on the same curve, but different parameterization
# # OK: offshift y -> only changed y penalty
# ss = sp.linspace(0, 1, 2 *N, endpoint=True) ** 2
# xx = 10 * ss
# yy = 5 * ss + 1
# zz = 30 * ss
#
# xy_penalty = projection_penalty(xx, yy, xx0, yy0)
# xz_penalty = projection_penalty(xx, zz, xx20, zz0)
#
# print(xy_penalty, xz_penalty)
## Minimize
random.seed(11)
k = 0.5
L_true = calc_length(xx0, yy0, zz0)
## Initialize with a line + noise
# ds0 = L_true / M * (1 + k * 2 * (random.random() - 0.5))
# params0 = [ds0]
# for n in range(M):
# params0.append(sp.arctan(1 / 2) + k * 2 * (random.random() - 0.5)) # theta
# params0.append(0 + k * 2 * (random.random() - 0.5)) # psi
## Initialize with initial curve + noise
rr0 = np.array((xx0, yy0, zz0)).T
_, ds0, thetas0, psis0 = curve3d.reconstruct_angles(rr0, M - 1)
params0 = [ds0 * (1 + k * 2 * (random.random() - 0.5))]
for theta, psi in zip(thetas0, psis0):
params0.append(theta + k * 2 * (random.random() - 0.5)) # theta
params0.append(psi + k * 2 * (random.random() - 0.5)) # psi
rr_init = curve3d.build_curve(r0, ds0, params0[1::2], params0[2::2])
xx_init, yy_init, zz_init = rr_init.T
print("Initial parameters:")
print(params0)
method = None # "Powell" #
start = time.time()
print("Optimization in progress...")
res = opt.minimize(f, params0, args=(r0, xx1, yy1, xx21, zz1), tol=10 ** -4, options={'disp': True}, method=method)
time_spent = time.time() - start
print("Done. Time spent:", datetime.timedelta(seconds=time_spent))
ds = res.x[0]
thetas = res.x[1::2]
psis = res.x[2::2]
rr = curve3d.build_curve(r0, ds, thetas, psis)
xx, yy, zz = rr.T
print(ds)
print(thetas)
print(psis)
# Show projections
plt.gca().set_aspect(1, 'datalim')
plt.xlabel('x')
plt.ylabel('y')
plt.plot(xx0, yy0, c='black', lw=1)
plt.plot(xx, yy, 'o', c='orange', markersize=2)
plt.show()
plt.gca().set_aspect(1, 'datalim')
plt.xlabel('x2')
plt.ylabel('z')
plt.plot(xx20, zz0, c='black', lw=1)
plt.plot(xx, zz, 'o', c='orange', markersize=2)
plt.show()