| 1 |
import sys |
| 2 |
from math import * |
| 3 |
|
| 4 |
from Numeric import * |
| 5 |
import LinearAlgebra as la |
| 6 |
|
| 7 |
import poly3 |
| 8 |
|
| 9 |
n = 15 |
| 10 |
m = zeros(((n + 1) * 4, (n + 1) * 4), Float) |
| 11 |
for i in range(n): |
| 12 |
m[4 * i + 2][4 * i + 0] = .5 |
| 13 |
m[4 * i + 2][4 * i + 1] = -1./12 |
| 14 |
m[4 * i + 2][4 * i + 2] = 1./48 |
| 15 |
m[4 * i + 2][4 * i + 3] = -1./480 |
| 16 |
m[4 * i + 2][4 * i + 4] = .5 |
| 17 |
m[4 * i + 2][4 * i + 5] = 1./12 |
| 18 |
m[4 * i + 2][4 * i + 6] = 1./48 |
| 19 |
m[4 * i + 2][4 * i + 7] = 1./480 |
| 20 |
|
| 21 |
m[4 * i + 3][4 * i + 0] = 1 |
| 22 |
m[4 * i + 3][4 * i + 1] = .5 |
| 23 |
m[4 * i + 3][4 * i + 2] = .125 |
| 24 |
m[4 * i + 3][4 * i + 3] = 1./48 |
| 25 |
m[4 * i + 3][4 * i + 4] = -1 |
| 26 |
m[4 * i + 3][4 * i + 5] = .5 |
| 27 |
m[4 * i + 3][4 * i + 6] = -.125 |
| 28 |
m[4 * i + 3][4 * i + 7] = 1./48 |
| 29 |
|
| 30 |
m[4 * i + 4][4 * i + 0] = 0 |
| 31 |
m[4 * i + 4][4 * i + 1] = 1 |
| 32 |
m[4 * i + 4][4 * i + 2] = .5 |
| 33 |
m[4 * i + 4][4 * i + 3] = .125 |
| 34 |
m[4 * i + 4][4 * i + 4] = 0 |
| 35 |
m[4 * i + 4][4 * i + 5] = -1 |
| 36 |
m[4 * i + 4][4 * i + 6] = .5 |
| 37 |
m[4 * i + 4][4 * i + 7] = -.125 |
| 38 |
|
| 39 |
m[4 * i + 5][4 * i + 0] = 0 |
| 40 |
m[4 * i + 5][4 * i + 1] = 0 |
| 41 |
m[4 * i + 5][4 * i + 2] = 1 |
| 42 |
m[4 * i + 5][4 * i + 3] = .5 |
| 43 |
m[4 * i + 5][4 * i + 4] = 0 |
| 44 |
m[4 * i + 5][4 * i + 5] = 0 |
| 45 |
m[4 * i + 5][4 * i + 6] = -1 |
| 46 |
m[4 * i + 5][4 * i + 7] = .5 |
| 47 |
|
| 48 |
m[n * 4 + 2][2] = 1 |
| 49 |
m[n * 4 + 3][3] = 1 |
| 50 |
|
| 51 |
m[0][n * 4 + 2] = 1 |
| 52 |
m[1][n * 4 + 3] = 1 |
| 53 |
|
| 54 |
def printarr(m): |
| 55 |
for j in range(n * 4 + 4): |
| 56 |
for i in range(n * 4 + 4): |
| 57 |
print '%6.1f' % m[j][i], |
| 58 |
print '' |
| 59 |
|
| 60 |
sys.output_line_width = 160 |
| 61 |
|
| 62 |
mi = la.inverse(m) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
def fit_arc(x, y): |
| 68 |
th = atan2(y - 2 * x * y, y * y + x - x * x) |
| 69 |
return th |
| 70 |
|
| 71 |
def mod_2pi(th): |
| 72 |
u = th / (2 * pi) |
| 73 |
return 2 * pi * (u - floor(u + 0.5)) |
| 74 |
|
| 75 |
def plot_path(path, th, k): |
| 76 |
if path[0][2] == '{': closed = 0 |
| 77 |
else: closed = 1 |
| 78 |
j = 0 |
| 79 |
cmd = 'moveto' |
| 80 |
for i in range(len(path) + closed - 1): |
| 81 |
i1 = (i + 1) % len(path) |
| 82 |
x0, y0, t0 = path[i] |
| 83 |
x1, y1, t1 = path[i1] |
| 84 |
j1 = (j + 1) % len(th) |
| 85 |
th0 = th[j] |
| 86 |
k0 = k[j] |
| 87 |
th1 = th[j1] |
| 88 |
k1 = k[j1] |
| 89 |
chord_th = atan2(y1 - y0, x1 - x0) |
| 90 |
chord_len = hypot(y1 - y0, x1 - x0) |
| 91 |
ks = poly3.solve_3spiro(mod_2pi(chord_th - th0), |
| 92 |
mod_2pi(th1 - chord_th), |
| 93 |
k0 * chord_len, k1 * chord_len) |
| 94 |
ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625) |
| 95 |
pside = poly3.int_3spiro_poly(ksp, 64) |
| 96 |
ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625) |
| 97 |
mside = poly3.int_3spiro_poly(ksm, 64) |
| 98 |
mside.reverse() |
| 99 |
for i in range(len(mside)): |
| 100 |
mside[i] = (-mside[i][0], -mside[i][1]) |
| 101 |
pts = mside + pside[1:] |
| 102 |
rot = chord_th - atan2(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0]) |
| 103 |
scale = hypot(x1 - x0, y1 - y0) / hypot(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0]) |
| 104 |
u, v = scale * cos(rot), scale * sin(rot) |
| 105 |
xt = x0 - u * pts[0][0] + v * pts[0][1] |
| 106 |
yt = y0 - u * pts[0][1] - v * pts[0][0] |
| 107 |
for x, y in pts: |
| 108 |
print xt + u * x - v * y, yt + u * y + v * x, cmd |
| 109 |
cmd = 'lineto' |
| 110 |
if t1 == 'v': |
| 111 |
j += 2 |
| 112 |
else: |
| 113 |
j += 1 |
| 114 |
print 'stroke' |
| 115 |
if 0: |
| 116 |
j = 0 |
| 117 |
for i in range(len(path)): |
| 118 |
x0, y0, t0 = path[i] |
| 119 |
print 'gsave', x0, y0, 'translate' |
| 120 |
print ' ', th[j] * 180 / pi, 'rotate' |
| 121 |
print ' -50 0 moveto 50 0 lineto stroke' |
| 122 |
print 'grestore' |
| 123 |
j += 1 |
| 124 |
|
| 125 |
def plot_ks(path, th, k): |
| 126 |
if path[0][2] == '{': closed = 0 |
| 127 |
else: closed = 1 |
| 128 |
j = 0 |
| 129 |
cmd = 'moveto' |
| 130 |
xo = 36 |
| 131 |
yo = 550 |
| 132 |
xscale = .5 |
| 133 |
yscale = 2000 |
| 134 |
x = xo |
| 135 |
for i in range(len(path) + closed - 1): |
| 136 |
i1 = (i + 1) % len(path) |
| 137 |
x0, y0, t0 = path[i] |
| 138 |
x1, y1, t1 = path[i1] |
| 139 |
j1 = (j + 1) % len(th) |
| 140 |
th0 = th[j] |
| 141 |
k0 = k[j] |
| 142 |
th1 = th[j1] |
| 143 |
k1 = k[j1] |
| 144 |
chord_th = atan2(y1 - y0, x1 - x0) |
| 145 |
chord_len = hypot(y1 - y0, x1 - x0) |
| 146 |
ks = poly3.solve_3spiro(mod_2pi(chord_th - th0), |
| 147 |
mod_2pi(th1 - chord_th), |
| 148 |
k0 * chord_len, k1 * chord_len) |
| 149 |
ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625) |
| 150 |
pside = poly3.int_3spiro_poly(ksp, 64) |
| 151 |
ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625) |
| 152 |
mside = poly3.int_3spiro_poly(ksm, 64) |
| 153 |
print '%', x0, y0, k0, k1 |
| 154 |
print "% ks = ", ks |
| 155 |
l = 2 * chord_len / hypot(pside[-1][0] + mside[-1][0], pside[-1][1] + mside[-1][1]) |
| 156 |
for i in range(100): |
| 157 |
s = .01 * i - 0.5 |
| 158 |
kp = poly3.eval_cubic(ks[0], ks[1], .5 * ks[2], 1./6 * ks[3], s) |
| 159 |
kp /= l |
| 160 |
print x + xscale * l * (s + .5), yo + yscale * kp, cmd |
| 161 |
cmd = 'lineto' |
| 162 |
x += xscale * l |
| 163 |
if t1 == 'v': |
| 164 |
j += 2 |
| 165 |
else: |
| 166 |
j += 1 |
| 167 |
print 'stroke' |
| 168 |
print xo, yo, 'moveto', x, yo, 'lineto stroke' |
| 169 |
|
| 170 |
def make_error_vec(path, th, k): |
| 171 |
if path[0][2] == '{': closed = 0 |
| 172 |
else: closed = 1 |
| 173 |
n = len(path) |
| 174 |
v = zeros(n * 2, Float) |
| 175 |
j = 0 |
| 176 |
for i in range(len(path) + closed - 1): |
| 177 |
i1 = (i + 1) % len(path) |
| 178 |
x0, y0, t0 = path[i] |
| 179 |
x1, y1, t1 = path[i1] |
| 180 |
j1 = (j + 1) % len(th) |
| 181 |
th0 = th[j] |
| 182 |
k0 = k[j] |
| 183 |
th1 = th[j1] |
| 184 |
k1 = k[j1] |
| 185 |
chord_th = atan2(y1 - y0, x1 - x0) |
| 186 |
chord_len = hypot(y1 - y0, x1 - x0) |
| 187 |
ks = poly3.solve_3spiro(mod_2pi(chord_th - th0), |
| 188 |
mod_2pi(th1 - chord_th), |
| 189 |
k0 * chord_len, k1 * chord_len) |
| 190 |
ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625) |
| 191 |
pside = poly3.int_3spiro_poly(ksp, 64) |
| 192 |
ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625) |
| 193 |
mside = poly3.int_3spiro_poly(ksm, 64) |
| 194 |
l = chord_len / hypot(pside[-1][0] + mside[-1][0], pside[-1][1] + mside[-1][1]) |
| 195 |
k1l = (ks[1] - .5 * ks[2] + .125 * ks[3]) / (l * l) |
| 196 |
k1r = (ks[1] + .5 * ks[2] + .125 * ks[3]) / (l * l) |
| 197 |
k2l = (ks[2] - .5 * ks[3]) / (l * l * l) |
| 198 |
k2r = (ks[2] + .5 * ks[3]) / (l * l * l) |
| 199 |
|
| 200 |
if t0 == 'o' or t0 == '[' or t0 == 'v': |
| 201 |
v[2 * j] -= k1l |
| 202 |
v[2 * j + 1] -= k2l |
| 203 |
elif t0 == 'c': |
| 204 |
v[2 * j + 1] += k2l |
| 205 |
|
| 206 |
if t1 == 'o' or t1 == ']' or t1 == 'v': |
| 207 |
v[2 * j1] += k1r |
| 208 |
v[2 * j1 + 1] += k2r |
| 209 |
elif t1 == 'c': |
| 210 |
v[2 * j1] += k2r |
| 211 |
|
| 212 |
print "% left k'", k1l, "k''", k2l, "right k'", k1r, "k''", k2r |
| 213 |
if t1 == 'v': |
| 214 |
j += 2 |
| 215 |
else: |
| 216 |
j += 1 |
| 217 |
print '% error vector:' |
| 218 |
for i in range(n): |
| 219 |
print '% ', v[2 * i], v[2 * i + 1] |
| 220 |
return v |
| 221 |
|
| 222 |
def add_k1l(m, row, col, col1, l, s): |
| 223 |
l2 = l * l |
| 224 |
m[row][2 * col] += s * 36 / l2 |
| 225 |
m[row][2 * col + 1] -= s * 9 / l |
| 226 |
m[row][2 * col1] += s * 24 / l2 |
| 227 |
m[row][2 * col1 + 1] += s * 3 / l |
| 228 |
|
| 229 |
def add_k1r(m, row, col, col1, l, s): |
| 230 |
l2 = l * l |
| 231 |
m[row][2 * col] += s * 24 / l2 |
| 232 |
m[row][2 * col + 1] -= s * 3 / l |
| 233 |
m[row][2 * col1] += s * 36 / l2 |
| 234 |
m[row][2 * col1 + 1] += s * 9 / l |
| 235 |
|
| 236 |
def add_k2l(m, row, col, col1, l, s): |
| 237 |
l2 = l * l |
| 238 |
l3 = l2 * l |
| 239 |
m[row][2 * col] -= s * 192 / l3 |
| 240 |
m[row][2 * col + 1] += s * 36 / l2 |
| 241 |
m[row][2 * col1] -= s * 168 / l3 |
| 242 |
m[row][2 * col1 + 1] -= s * 24 / l2 |
| 243 |
|
| 244 |
def add_k2r(m, row, col, col1, l, s): |
| 245 |
l2 = l * l |
| 246 |
l3 = l2 * l |
| 247 |
m[row][2 * col] += s * 168 / l3 |
| 248 |
m[row][2 * col + 1] -= s * 24 / l2 |
| 249 |
m[row][2 * col1] += s * 192 / l3 |
| 250 |
m[row][2 * col1 + 1] += s * 36 / l2 |
| 251 |
|
| 252 |
def make_matrix(path, th, k): |
| 253 |
if path[0][2] == '{': closed = 0 |
| 254 |
else: closed = 1 |
| 255 |
n = len(path) |
| 256 |
m = zeros((n * 2, n * 2), Float) |
| 257 |
j = 0 |
| 258 |
for i in range(len(path) + closed - 1): |
| 259 |
i1 = (i + 1) % len(path) |
| 260 |
x0, y0, t0 = path[i] |
| 261 |
x1, y1, t1 = path[i1] |
| 262 |
j1 = (j + 1) % len(th) |
| 263 |
th0 = th[j] |
| 264 |
k0 = k[j] |
| 265 |
th1 = th[j1] |
| 266 |
k1 = k[j1] |
| 267 |
chord_th = atan2(y1 - y0, x1 - x0) |
| 268 |
chord_len = hypot(y1 - y0, x1 - x0) |
| 269 |
ks = poly3.solve_3spiro(mod_2pi(chord_th - th0), |
| 270 |
mod_2pi(th1 - chord_th), |
| 271 |
k0 * chord_len, k1 * chord_len) |
| 272 |
ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625) |
| 273 |
pside = poly3.int_3spiro_poly(ksp, 64) |
| 274 |
ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625) |
| 275 |
mside = poly3.int_3spiro_poly(ksm, 64) |
| 276 |
l = chord_len / hypot(pside[-1][0] + mside[-1][0], pside[-1][1] + mside[-1][1]) |
| 277 |
|
| 278 |
if t0 == 'o' or t0 == '[' or t0 == 'v': |
| 279 |
add_k1l(m, 2 * j, j, j1, l, -1) |
| 280 |
add_k2l(m, 2 * j + 1, j, j1, l, -1) |
| 281 |
elif t0 == 'c': |
| 282 |
add_k2l(m, 2 * j + 1, j, j1, l, 1) |
| 283 |
|
| 284 |
if t1 == 'o' or t1 == ']' or t1 == 'v': |
| 285 |
add_k1r(m, 2 * j1, j, j1, l, 1) |
| 286 |
add_k2r(m, 2 * j1 + 1, j, j1, l, 1) |
| 287 |
elif t1 == 'c': |
| 288 |
add_k2r(m, 2 * j1, j, j1, l, 1) |
| 289 |
|
| 290 |
if t1 == 'v': |
| 291 |
j += 2 |
| 292 |
else: |
| 293 |
j += 1 |
| 294 |
return m |
| 295 |
|
| 296 |
def solve(path): |
| 297 |
if path[0][2] == '{': closed = 0 |
| 298 |
else: closed = 1 |
| 299 |
dxs = [] |
| 300 |
dys = [] |
| 301 |
chords = [] |
| 302 |
for i in range(len(path) - 1): |
| 303 |
dxs.append(path[i + 1][0] - path[i][0]) |
| 304 |
dys.append(path[i + 1][1] - path[i][1]) |
| 305 |
chords.append(hypot(dxs[-1], dys[-1])) |
| 306 |
nominal_th = [] |
| 307 |
nominal_k = [] |
| 308 |
if not closed: |
| 309 |
nominal_th.append(atan2(dys[0], dxs[0])) |
| 310 |
nominal_k.append(0) |
| 311 |
for i in range(1 - closed, len(path) - 1 + closed): |
| 312 |
x0, y0, t0 = path[(i + len(path) - 1) % len(path)] |
| 313 |
x1, y1, t1 = path[i] |
| 314 |
x2, y2, t2 = path[(i + 1) % len(path)] |
| 315 |
dx = float(x2 - x0) |
| 316 |
dy = float(y2 - y0) |
| 317 |
ir2 = dx * dx + dy * dy |
| 318 |
x = ((x1 - x0) * dx + (y1 - y0) * dy) / ir2 |
| 319 |
y = ((y1 - y0) * dx - (x1 - x0) * dy) / ir2 |
| 320 |
th = fit_arc(x, y) + atan2(dy, dx) |
| 321 |
bend_angle = mod_2pi(atan2(y2 - y1, x2 - x1) - atan2(y1 - y0, x1 - x0)) |
| 322 |
k = 2 * bend_angle/(hypot(y2 - y1, x2 - x1) + hypot(y1 - y0, x1 - x0)) |
| 323 |
print '% bend angle', bend_angle, 'k', k |
| 324 |
if t1 == ']': |
| 325 |
th = atan2(y1 - y0, x1 - x0) |
| 326 |
k = 0 |
| 327 |
elif t1 == '[': |
| 328 |
th = atan2(y2 - y1, x2 - x1) |
| 329 |
k = 0 |
| 330 |
nominal_th.append(th) |
| 331 |
nominal_k.append(k) |
| 332 |
if not closed: |
| 333 |
nominal_th.append(atan2(dys[-1], dxs[-1])) |
| 334 |
nominal_k.append(0) |
| 335 |
print '%', nominal_th |
| 336 |
print '0 0 1 setrgbcolor .5 setlinewidth' |
| 337 |
plot_path(path, nominal_th, nominal_k) |
| 338 |
plot_ks(path, nominal_th, nominal_k) |
| 339 |
th = nominal_th[:] |
| 340 |
k = nominal_k[:] |
| 341 |
n = 8 |
| 342 |
for i in range(n): |
| 343 |
ev = make_error_vec(path, th, k) |
| 344 |
m = make_matrix(path, th, k) |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
v = dot(la.inverse(m), ev) |
| 349 |
|
| 350 |
for j in range(len(path)): |
| 351 |
th[j] += 1. * v[2 * j] |
| 352 |
k[j] -= 1. * .5 * v[2 * j + 1] |
| 353 |
if i == n - 1: |
| 354 |
print '0 0 0 setrgbcolor 1 setlinewidth' |
| 355 |
elif i == 0: |
| 356 |
print '1 0 0 setrgbcolor' |
| 357 |
elif i == 1: |
| 358 |
print '0 0.5 0 setrgbcolor' |
| 359 |
elif i == 2: |
| 360 |
print '0.3 0.3 0.3 setrgbcolor' |
| 361 |
plot_path(path, th, k) |
| 362 |
plot_ks(path, th, k) |
| 363 |
print '% th:', th |
| 364 |
print '% k:', k |
| 365 |
|
| 366 |
path = [(100, 100, 'o'), (200, 250, 'o'), (350, 225, 'o'), |
| 367 |
(450, 350, 'c'), (450, 200, 'o'), (300, 100, 'o')] |
| 368 |
if 0: |
| 369 |
path = [(100, 480, '['), (100, 300, ']'), (300, 100, 'o'), |
| 370 |
(500, 300, '['), (500, 480, ']'), (480, 500, '['), |
| 371 |
(120, 500, ']')] |
| 372 |
|
| 373 |
|
| 374 |
path = [(100, 480, ']'), (100, 120, '['), |
| 375 |
(120, 100, ']'), (480, 100, '['), |
| 376 |
(500, 120, ']'), (500, 480, '['), |
| 377 |
(480, 500, ']'), (120, 500, '[')] |
| 378 |
|
| 379 |
path = [(100, 120, '['), (120, 100, ']'), |
| 380 |
(140, 100, 'o'), (160, 100, 'o'), (180, 100, 'o'), (200, 100, 'o'), |
| 381 |
(250, 250, 'o'), |
| 382 |
(100, 200, 'o'), (100, 180, 'o'), (100, 160, 'o'), (100, 140, 'o')] |
| 383 |
|
| 384 |
path = [(100, 350, 'o'), (225, 350, 'o'), (350, 450, 'o'), |
| 385 |
(450, 400, 'o'), (315, 205, 'o'), (300, 200, 'o'), |
| 386 |
(285, 205, 'o')] |
| 387 |
|
| 388 |
if 0: |
| 389 |
path = [] |
| 390 |
path.append((350, 600, 'c')) |
| 391 |
path.append((50, 450, 'c')) |
| 392 |
for i in range(10): |
| 393 |
path.append((50 + i * 30, 300 - i * 5, 'c')) |
| 394 |
for i in range(11): |
| 395 |
path.append((350 + i * 30, 250 + i * 5, 'c')) |
| 396 |
path.append((650, 450, 'c')) |
| 397 |
|
| 398 |
solve(path) |
| 399 |
print 'showpage' |