| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
from math import * |
| 9 |
|
| 10 |
import pcorn |
| 11 |
|
| 12 |
def pt_wsum(points, wts): |
| 13 |
x, y = 0, 0 |
| 14 |
for i in range(len(points)): |
| 15 |
x += points[i][0] * wts[i] |
| 16 |
y += points[i][1] * wts[i] |
| 17 |
return x, y |
| 18 |
|
| 19 |
|
| 20 |
def bz_eval(bz, t): |
| 21 |
degree = len(bz) - 1 |
| 22 |
mt = 1 - t |
| 23 |
if degree == 3: |
| 24 |
return pt_wsum(bz, [mt * mt * mt, 3 * mt * mt * t, 3 * mt * t * t, t * t * t]) |
| 25 |
elif degree == 2: |
| 26 |
return pt_wsum(bz, [mt * mt, 2 * mt * t, t * t]) |
| 27 |
elif degree == 1: |
| 28 |
return pt_wsum(bz, [mt, t]) |
| 29 |
|
| 30 |
def bz_deriv(bz): |
| 31 |
degree = len(bz) - 1 |
| 32 |
return [(degree * (bz[i + 1][0] - bz[i][0]), degree * (bz[i + 1][1] - bz[i][1])) for i in range(degree)] |
| 33 |
|
| 34 |
def bz_arclength(bz, n = 10): |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
wtarr = (3./8, 7./6, 23./24) |
| 41 |
dt = 1./n |
| 42 |
s = 0 |
| 43 |
dbz = bz_deriv(bz) |
| 44 |
for i in range(0, n + 1): |
| 45 |
if i < 3: |
| 46 |
wt = wtarr[i] |
| 47 |
elif i > n - 3: |
| 48 |
wt = wtarr[n - i] |
| 49 |
else: |
| 50 |
wt = 1. |
| 51 |
dx, dy = bz_eval(dbz, i * dt) |
| 52 |
ds = hypot(dx, dy) |
| 53 |
s += wt * ds |
| 54 |
return s * dt |
| 55 |
|
| 56 |
|
| 57 |
def rk4(y, dydx, x, h, derivs): |
| 58 |
hh = h * .5 |
| 59 |
h6 = h * (1./6) |
| 60 |
xh = x + hh |
| 61 |
yt = [] |
| 62 |
for i in range(len(y)): |
| 63 |
yt.append(y[i] + hh * dydx[i]) |
| 64 |
dyt = derivs(xh, yt) |
| 65 |
for i in range(len(y)): |
| 66 |
yt[i] = y[i] + hh * dyt[i] |
| 67 |
dym = derivs(xh, yt) |
| 68 |
for i in range(len(y)): |
| 69 |
yt[i] = y[i] + h * dym[i] |
| 70 |
dym[i] += dyt[i] |
| 71 |
dyt = derivs(x + h, yt) |
| 72 |
for i in range(len(y)): |
| 73 |
y[i] += h6 * (dydx[i] + dyt[i] + 2 * dym[i]) |
| 74 |
|
| 75 |
def bz_arclength_rk4(bz, n = 10): |
| 76 |
dbz = bz_deriv(bz) |
| 77 |
def arclength_deriv(x, ys): |
| 78 |
dx, dy = bz_eval(dbz, x) |
| 79 |
return [hypot(dx, dy)] |
| 80 |
dt = 1./n |
| 81 |
t = 0 |
| 82 |
ys = [0] |
| 83 |
for i in range(n): |
| 84 |
dydx = arclength_deriv(t, ys) |
| 85 |
rk4(ys, dydx, t, dt, arclength_deriv) |
| 86 |
t += dt |
| 87 |
return ys[0] |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
def fit_cubic_arclen(z0, z1, arclen, th0, th1, aab): |
| 94 |
chord = hypot(z1[0] - z0[0], z1[1] - z0[1]) |
| 95 |
cth0, sth0 = cos(th0), sin(th0) |
| 96 |
cth1, sth1 = -cos(th1), -sin(th1) |
| 97 |
armlen = .66667 * chord |
| 98 |
darmlen = 1e-6 * armlen |
| 99 |
for i in range(10): |
| 100 |
a = armlen * aab |
| 101 |
b = armlen - a |
| 102 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 103 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 104 |
actual_s = bz_arclength_rk4(bz) |
| 105 |
if (abs(arclen - actual_s) < 1e-12): |
| 106 |
break |
| 107 |
a = (armlen + darmlen) * aab |
| 108 |
b = (armlen + darmlen) - a |
| 109 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 110 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 111 |
actual_s2 = bz_arclength_rk4(bz) |
| 112 |
ds = (actual_s2 - actual_s) / darmlen |
| 113 |
|
| 114 |
if ds == 0: |
| 115 |
break |
| 116 |
armlen += (arclen - actual_s) / ds |
| 117 |
a = armlen * aab |
| 118 |
b = armlen - a |
| 119 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 120 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 121 |
return bz |
| 122 |
|
| 123 |
def mod_2pi(th): |
| 124 |
u = th / (2 * pi) |
| 125 |
return 2 * pi * (u - floor(u + 0.5)) |
| 126 |
|
| 127 |
def measure_bz(bz, arclen, th_fn, n = 1000): |
| 128 |
dt = 1./n |
| 129 |
dbz = bz_deriv(bz) |
| 130 |
s = 0 |
| 131 |
score = 0 |
| 132 |
for i in range(n): |
| 133 |
dx, dy = bz_eval(dbz, (i + .5) * dt) |
| 134 |
ds = dt * hypot(dx, dy) |
| 135 |
s += ds |
| 136 |
score += ds * (mod_2pi(atan2(dy, dx) - th_fn(s)) ** 2) |
| 137 |
return score |
| 138 |
|
| 139 |
def measure_bz_rk4(bz, arclen, th_fn, n = 10): |
| 140 |
dbz = bz_deriv(bz) |
| 141 |
def measure_derivs(x, ys): |
| 142 |
dx, dy = bz_eval(dbz, x) |
| 143 |
ds = hypot(dx, dy) |
| 144 |
s = ys[0] |
| 145 |
dscore = ds * (mod_2pi(atan2(dy, dx) - th_fn(s)) ** 2) |
| 146 |
return [ds, dscore] |
| 147 |
dt = 1./n |
| 148 |
t = 0 |
| 149 |
ys = [0, 0] |
| 150 |
for i in range(n): |
| 151 |
dydx = measure_derivs(t, ys) |
| 152 |
rk4(ys, dydx, t, dt, measure_derivs) |
| 153 |
t += dt |
| 154 |
return ys[1] |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
def fit_cubic(z0, z1, arclen, th_fn, fast = 1): |
| 161 |
chord = hypot(z1[0] - z0[0], z1[1] - z0[1]) |
| 162 |
if (arclen < 1.000001 * chord): |
| 163 |
return [z0, z1], 0 |
| 164 |
th0 = th_fn(0) |
| 165 |
th1 = th_fn(arclen) |
| 166 |
imax = 4 |
| 167 |
jmax = 10 |
| 168 |
aabmin = 0 |
| 169 |
aabmax = 1. |
| 170 |
if fast: |
| 171 |
imax = 1 |
| 172 |
jmax = 0 |
| 173 |
for i in range(imax): |
| 174 |
for j in range(jmax + 1): |
| 175 |
if jmax == 0: |
| 176 |
aab = 0.5 * (aabmin + aabmax) |
| 177 |
else: |
| 178 |
aab = aabmin + (aabmax - aabmin) * j / jmax |
| 179 |
bz = fit_cubic_arclen(z0, z1, arclen, th0, th1, aab) |
| 180 |
score = measure_bz_rk4(bz, arclen, th_fn) |
| 181 |
print '% aab =', aab, 'score =', score |
| 182 |
sys.stdout.flush() |
| 183 |
if j == 0 or score < best_score: |
| 184 |
best_score = score |
| 185 |
best_aab = aab |
| 186 |
best_bz = bz |
| 187 |
daab = .06 * (aabmax - aabmin) |
| 188 |
aabmin = max(0, best_aab - daab) |
| 189 |
aabmax = min(1, best_aab + daab) |
| 190 |
print '%--- best_aab =', best_aab |
| 191 |
return best_bz, best_score |
| 192 |
|
| 193 |
def plot_prolog(): |
| 194 |
print '%!PS' |
| 195 |
print '/m { moveto } bind def' |
| 196 |
print '/l { lineto } bind def' |
| 197 |
print '/c { curveto } bind def' |
| 198 |
print '/z { closepath } bind def' |
| 199 |
|
| 200 |
def plot_bz(bz, z0, scale, do_moveto = True): |
| 201 |
x0, y0 = z0 |
| 202 |
if do_moveto: |
| 203 |
print bz[0][0] * scale + x0, bz[0][1] * scale + y0, 'm' |
| 204 |
if len(bz) == 4: |
| 205 |
x1, y1 = bz[1][0] * scale + x0, bz[1][1] * scale + y0 |
| 206 |
x2, y2 = bz[2][0] * scale + x0, bz[2][1] * scale + y0 |
| 207 |
x3, y3 = bz[3][0] * scale + x0, bz[3][1] * scale + y0 |
| 208 |
print x1, y1, x2, y2, x3, y3, 'c' |
| 209 |
elif len(bz) == 2: |
| 210 |
print bz[1][0] * scale + x0, bz[1][1] * scale + y0, 'l' |
| 211 |
|
| 212 |
def test_bz_arclength(): |
| 213 |
bz = [(0, 0), (.5, 0), (1, 0.5), (1, 1)] |
| 214 |
ans = bz_arclength_rk4(bz, 2048) |
| 215 |
last = 1 |
| 216 |
lastrk = 1 |
| 217 |
for i in range(3, 11): |
| 218 |
n = 1 << i |
| 219 |
err = bz_arclength(bz, n) - ans |
| 220 |
err_rk = bz_arclength_rk4(bz, n) - ans |
| 221 |
print n, err, last / err, err_rk, lastrk / err_rk |
| 222 |
last = err |
| 223 |
lastrk = err_rk |
| 224 |
|
| 225 |
def test_fit_cubic_arclen(): |
| 226 |
th = pi / 4 |
| 227 |
arclen = th / sin(th) |
| 228 |
bz = fit_cubic_arclen((0, 0), (1, 0), arclen, th, th, .5) |
| 229 |
print '%', bz |
| 230 |
plot_bz(bz, (100, 400), 500) |
| 231 |
print 'stroke' |
| 232 |
print 'showpage' |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
import cornu |
| 237 |
|
| 238 |
def cornu_to_cubic(t0, t1): |
| 239 |
def th_fn(s): |
| 240 |
return (s + t0) ** 2 |
| 241 |
y0, x0 = cornu.eval_cornu(t0) |
| 242 |
y1, x1 = cornu.eval_cornu(t1) |
| 243 |
bz, score = fit_cubic((x0, y0), (x1, y1), t1 - t0, th_fn, 0) |
| 244 |
return bz, score |
| 245 |
|
| 246 |
def test_draw_cornu(): |
| 247 |
plot_prolog() |
| 248 |
thresh = 1e-6 |
| 249 |
print '/ss 1.5 def' |
| 250 |
print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def' |
| 251 |
s0 = 0 |
| 252 |
imax = 200 |
| 253 |
x0, y0, scale = 36, 100, 500 |
| 254 |
bzs = [] |
| 255 |
for i in range(1, imax): |
| 256 |
s = sqrt(i * .1) |
| 257 |
bz, score = cornu_to_cubic(s0, s) |
| 258 |
if score > (s - s0) * thresh or i == imax - 1: |
| 259 |
plot_bz(bz, (x0, y0), scale, s0 == 0) |
| 260 |
bzs.append(bz) |
| 261 |
s0 = s |
| 262 |
print 'stroke' |
| 263 |
for i in range(len(bzs)): |
| 264 |
bz = bzs[i] |
| 265 |
bx0, by0 = x0 + bz[0][0] * scale, y0 + bz[0][1] * scale |
| 266 |
bx1, by1 = x0 + bz[1][0] * scale, y0 + bz[1][1] * scale |
| 267 |
bx2, by2 = x0 + bz[2][0] * scale, y0 + bz[2][1] * scale |
| 268 |
bx3, by3 = x0 + bz[3][0] * scale, y0 + bz[3][1] * scale |
| 269 |
print 'gsave 0 0 1 setrgbcolor .5 setlinewidth' |
| 270 |
print bx0, by0, 'moveto', bx1, by1, 'lineto stroke' |
| 271 |
print bx2, by2, 'moveto', bx3, by3, 'lineto stroke' |
| 272 |
print 'grestore' |
| 273 |
print 'gsave', bx0, by0, 'translate circle fill grestore' |
| 274 |
print 'gsave', bx1, by1, 'translate .5 dup scale circle fill grestore' |
| 275 |
print 'gsave', bx2, by2, 'translate .5 dup scale circle fill grestore' |
| 276 |
print 'gsave', bx3, by3, 'translate circle fill grestore' |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
def pcorn_segment_to_bzs_optim_inner(curve, s0, s1, thresh, nmax = None): |
| 281 |
result = [] |
| 282 |
if s0 == s1: return [], 0 |
| 283 |
while s0 < s1: |
| 284 |
def th_fn_inner(s): |
| 285 |
if s > s1: s = s1 |
| 286 |
return curve.th(s0 + s, s == 0) |
| 287 |
z0 = curve.xy(s0) |
| 288 |
z1 = curve.xy(s1) |
| 289 |
bz, score = fit_cubic(z0, z1, s1 - s0, th_fn_inner, 0) |
| 290 |
if score < thresh or nmax != None and len(result) == nmax - 1: |
| 291 |
result.append(bz) |
| 292 |
break |
| 293 |
r = s1 |
| 294 |
l = s0 + .001 * (s1 - s0) |
| 295 |
for i in range(10): |
| 296 |
smid = 0.5 * (l + r) |
| 297 |
zmid = curve.xy(smid) |
| 298 |
bz, score = fit_cubic(z0, zmid, smid - s0, th_fn_inner, 0) |
| 299 |
if score > thresh: |
| 300 |
r = smid |
| 301 |
else: |
| 302 |
l = smid |
| 303 |
print '% s0=', s0, 'smid=', smid, 'actual score =', score |
| 304 |
result.append(bz) |
| 305 |
s0 = smid |
| 306 |
print '% last actual score=', score |
| 307 |
return result, score |
| 308 |
|
| 309 |
def pcorn_segment_to_bzs_optim(curve, s0, s1, thresh, optim): |
| 310 |
result, score = pcorn_segment_to_bzs_optim_inner(curve, s0, s1, thresh) |
| 311 |
bresult, bscore = result, score |
| 312 |
if len(result) > 1 and optim > 2: |
| 313 |
nmax = len(result) |
| 314 |
gamma = 1./6 |
| 315 |
l = score |
| 316 |
r = thresh |
| 317 |
for i in range(5): |
| 318 |
tmid = (0.5 * (l ** gamma + r ** gamma)) ** (1/gamma) |
| 319 |
result, score = pcorn_segment_to_bzs_optim_inner(curve, s0, s1, tmid, nmax) |
| 320 |
if score < tmid: |
| 321 |
l = max(l, score) |
| 322 |
r = tmid |
| 323 |
else: |
| 324 |
l = tmid |
| 325 |
r = min(r, score) |
| 326 |
if max(score, tmid) < bscore: |
| 327 |
bresult, bscore = result, max(score, tmid) |
| 328 |
return result |
| 329 |
|
| 330 |
def pcorn_segment_to_bzs(curve, s0, s1, optim = 0, thresh = 1e-3): |
| 331 |
if optim >= 2: |
| 332 |
return pcorn_segment_to_bzs_optim(curve, s0, s1, thresh, optim) |
| 333 |
z0 = curve.xy(s0) |
| 334 |
z1 = curve.xy(s1) |
| 335 |
fast = (optim == 0) |
| 336 |
def th_fn(s): |
| 337 |
return curve.th(s0 + s, s == 0) |
| 338 |
bz, score = fit_cubic(z0, z1, s1 - s0, th_fn, fast) |
| 339 |
if score < thresh: |
| 340 |
return [bz] |
| 341 |
else: |
| 342 |
smid = 0.5 * (s0 + s1) |
| 343 |
result = pcorn_segment_to_bzs(curve, s0, smid, optim, thresh) |
| 344 |
result.extend(pcorn_segment_to_bzs(curve, smid, s1, optim, thresh)) |
| 345 |
return result |
| 346 |
|
| 347 |
def pcorn_curve_to_bzs(curve, optim = 3, thresh = 1e-3): |
| 348 |
result = [] |
| 349 |
extrema = curve.find_extrema() |
| 350 |
extrema.extend(curve.find_breaks()) |
| 351 |
extrema.sort() |
| 352 |
print '%', extrema |
| 353 |
for i in range(len(extrema)): |
| 354 |
s0 = extrema[i] |
| 355 |
if i == len(extrema) - 1: |
| 356 |
s1 = extrema[0] + curve.arclen |
| 357 |
else: |
| 358 |
s1 = extrema[i + 1] |
| 359 |
result.extend(pcorn_segment_to_bzs(curve, s0, s1, optim, thresh)) |
| 360 |
return result |
| 361 |
|
| 362 |
import struct |
| 363 |
|
| 364 |
def fit_cubic_arclen_forplot(z0, z1, arclen, th0, th1, aab): |
| 365 |
chord = hypot(z1[0] - z0[0], z1[1] - z0[1]) |
| 366 |
cth0, sth0 = cos(th0), sin(th0) |
| 367 |
cth1, sth1 = -cos(th1), -sin(th1) |
| 368 |
armlen = .66667 * chord |
| 369 |
darmlen = 1e-6 * armlen |
| 370 |
for i in range(10): |
| 371 |
a = armlen * aab |
| 372 |
b = armlen - a |
| 373 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 374 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 375 |
actual_s = bz_arclength_rk4(bz) |
| 376 |
if (abs(arclen - actual_s) < 1e-12): |
| 377 |
break |
| 378 |
a = (armlen + darmlen) * aab |
| 379 |
b = (armlen + darmlen) - a |
| 380 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 381 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 382 |
actual_s2 = bz_arclength_rk4(bz) |
| 383 |
ds = (actual_s2 - actual_s) / darmlen |
| 384 |
|
| 385 |
armlen += (arclen - actual_s) / ds |
| 386 |
a = armlen * aab |
| 387 |
b = armlen - a |
| 388 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 389 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 390 |
return bz, a, b |
| 391 |
|
| 392 |
def plot_errors_2d(t0, t1, as_ppm): |
| 393 |
xs = 1024 |
| 394 |
ys = 1024 |
| 395 |
if as_ppm: |
| 396 |
print 'P6' |
| 397 |
print xs, ys |
| 398 |
print 255 |
| 399 |
def th_fn(s): |
| 400 |
return (s + t0) ** 2 |
| 401 |
y0, x0 = cornu.eval_cornu(t0) |
| 402 |
y1, x1 = cornu.eval_cornu(t1) |
| 403 |
z0 = (x0, y0) |
| 404 |
z1 = (x1, y1) |
| 405 |
chord = hypot(y1 - y0, x1 - x0) |
| 406 |
arclen = t1 - t0 |
| 407 |
th0 = th_fn(0) |
| 408 |
th1 = th_fn(arclen) |
| 409 |
cth0, sth0 = cos(th0), sin(th0) |
| 410 |
cth1, sth1 = -cos(th1), -sin(th1) |
| 411 |
|
| 412 |
for y in range(ys): |
| 413 |
b = .8 * chord * (ys - y - 1) / ys |
| 414 |
for x in range(xs): |
| 415 |
a = .8 * chord * x / xs |
| 416 |
bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a), |
| 417 |
(z1[0] + cth1 * b, z1[1] + sth1 * b), z1] |
| 418 |
s_bz = bz_arclength(bz, 10) |
| 419 |
def th_fn_scaled(s): |
| 420 |
return (s * arclen / s_bz + t0) ** 2 |
| 421 |
score = measure_bz_rk4(bz, arclen, th_fn_scaled, 10) |
| 422 |
if as_ppm: |
| 423 |
ls = -log(score) |
| 424 |
color_th = ls |
| 425 |
darkstep = 0 |
| 426 |
if s_bz > arclen: |
| 427 |
g0 = 128 - darkstep |
| 428 |
else: |
| 429 |
g0 = 128 + darkstep |
| 430 |
sc = 127 - darkstep |
| 431 |
rr = g0 + sc * cos(color_th) |
| 432 |
gg = g0 + sc * cos(color_th + 2 * pi / 3) |
| 433 |
bb = g0 + sc * cos(color_th - 2 * pi / 3) |
| 434 |
sys.stdout.write(struct.pack('3B', rr, gg, bb)) |
| 435 |
else: |
| 436 |
print a, b, score |
| 437 |
if not as_ppm: |
| 438 |
print |
| 439 |
|
| 440 |
def plot_arclen(t0, t1): |
| 441 |
def th_fn(s): |
| 442 |
return (s + t0) ** 2 |
| 443 |
y0, x0 = cornu.eval_cornu(t0) |
| 444 |
y1, x1 = cornu.eval_cornu(t1) |
| 445 |
z0 = (x0, y0) |
| 446 |
z1 = (x1, y1) |
| 447 |
chord = hypot(y1 - y0, x1 - x0) |
| 448 |
arclen = t1 - t0 |
| 449 |
th0 = th_fn(0) |
| 450 |
th1 = th_fn(arclen) |
| 451 |
for i in range(101): |
| 452 |
aab = i * .01 |
| 453 |
bz, a, b = fit_cubic_arclen_forplot(z0, z1, arclen, th0, th1, aab) |
| 454 |
print a, b |
| 455 |
|
| 456 |
if __name__ == '__main__': |
| 457 |
|
| 458 |
test_draw_cornu() |
| 459 |
|
| 460 |
|
| 461 |
|