| 1 |
|
| 2 |
|
| 3 |
from math import * |
| 4 |
|
| 5 |
import clothoid |
| 6 |
import pcorn |
| 7 |
import tocubic |
| 8 |
|
| 9 |
import offset |
| 10 |
|
| 11 |
def read_bz(f): |
| 12 |
result = [] |
| 13 |
for l in f.xreadlines(): |
| 14 |
s = l.split() |
| 15 |
if len(s) > 0: |
| 16 |
cmd = s[-1] |
| 17 |
|
| 18 |
if cmd == 'm': |
| 19 |
sp = [] |
| 20 |
result.append(sp) |
| 21 |
curpt = [float(x) for x in s[0:2]] |
| 22 |
startpt = curpt |
| 23 |
elif cmd == 'l': |
| 24 |
newpt = [float(x) for x in s[0:2]] |
| 25 |
sp.append((curpt, newpt)) |
| 26 |
curpt = newpt |
| 27 |
elif cmd == 'c': |
| 28 |
c1 = [float(x) for x in s[0:2]] |
| 29 |
c2 = [float(x) for x in s[2:4]] |
| 30 |
newpt = [float(x) for x in s[4:6]] |
| 31 |
sp.append((curpt, c1, c2, newpt)) |
| 32 |
curpt = newpt |
| 33 |
return result |
| 34 |
|
| 35 |
def plot_bzs(bzs, z0, scale, fancy = False): |
| 36 |
for sp in bzs: |
| 37 |
for i in range(len(sp)): |
| 38 |
bz = sp[i] |
| 39 |
tocubic.plot_bz(bz, z0, scale, i == 0) |
| 40 |
print 'stroke' |
| 41 |
if fancy: |
| 42 |
for i in range(len(sp)): |
| 43 |
bz = sp[i] |
| 44 |
|
| 45 |
x0, y0 = z0[0] + scale * bz[0][0], z0[1] + scale * bz[0][1] |
| 46 |
print 'gsave', x0, y0, 'translate circle fill grestore' |
| 47 |
if len(bz) == 4: |
| 48 |
x1, y1 = z0[0] + scale * bz[1][0], z0[1] + scale * bz[1][1] |
| 49 |
x2, y2 = z0[0] + scale * bz[2][0], z0[1] + scale * bz[2][1] |
| 50 |
x3, y3 = z0[0] + scale * bz[3][0], z0[1] + scale * bz[3][1] |
| 51 |
print 'gsave 0.5 setlinewidth', x0, y0, 'moveto' |
| 52 |
print x1, y1, 'lineto stroke' |
| 53 |
print x2, y2, 'moveto' |
| 54 |
print x3, y3, 'lineto stroke grestore' |
| 55 |
print 'gsave', x1, y1, 'translate 0.75 dup scale circle fill grestore' |
| 56 |
print 'gsave', x2, y2, 'translate 0.75 dup scale circle fill grestore' |
| 57 |
print 'gsave', x3, y3, 'translate 0.75 dup scale circle fill grestore' |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
def measure_bz_cloth(seg, bz, n = 100): |
| 62 |
bz_arclen = tocubic.bz_arclength_rk4(bz) |
| 63 |
arclen_ratio = seg.arclen / bz_arclen |
| 64 |
dbz = tocubic.bz_deriv(bz) |
| 65 |
|
| 66 |
def measure_derivs(x, ys): |
| 67 |
dx, dy = tocubic.bz_eval(dbz, x) |
| 68 |
ds = hypot(dx, dy) |
| 69 |
s = ys[0] * arclen_ratio |
| 70 |
dscore = ds * (tocubic.mod_2pi(atan2(dy, dx) - seg.th(s)) ** 2) |
| 71 |
|
| 72 |
return [ds, dscore] |
| 73 |
dt = 1./n |
| 74 |
t = 0 |
| 75 |
ys = [0, 0] |
| 76 |
for i in range(n): |
| 77 |
dydx = measure_derivs(t, ys) |
| 78 |
tocubic.rk4(ys, dydx, t, dt, measure_derivs) |
| 79 |
t += dt |
| 80 |
return ys[1] |
| 81 |
|
| 82 |
def cubic_bz_to_pcorn(bz, thresh): |
| 83 |
dx = bz[3][0] - bz[0][0] |
| 84 |
dy = bz[3][1] - bz[0][1] |
| 85 |
dx1 = bz[1][0] - bz[0][0] |
| 86 |
dy1 = bz[1][1] - bz[0][1] |
| 87 |
dx2 = bz[3][0] - bz[2][0] |
| 88 |
dy2 = bz[3][1] - bz[2][1] |
| 89 |
chth = atan2(dy, dx) |
| 90 |
th0 = tocubic.mod_2pi(chth - atan2(dy1, dx1)) |
| 91 |
th1 = tocubic.mod_2pi(atan2(dy2, dx2) - chth) |
| 92 |
seg = pcorn.Segment(bz[0], bz[3], th0, th1) |
| 93 |
err = measure_bz_cloth(seg, bz) |
| 94 |
if err < thresh: |
| 95 |
return [seg] |
| 96 |
else: |
| 97 |
|
| 98 |
x01, y01 = 0.5 * (bz[0][0] + bz[1][0]), 0.5 * (bz[0][1] + bz[1][1]) |
| 99 |
x12, y12 = 0.5 * (bz[1][0] + bz[2][0]), 0.5 * (bz[1][1] + bz[2][1]) |
| 100 |
x23, y23 = 0.5 * (bz[2][0] + bz[3][0]), 0.5 * (bz[2][1] + bz[3][1]) |
| 101 |
xl2, yl2 = 0.5 * (x01 + x12), 0.5 * (y01 + y12) |
| 102 |
xr1, yr1 = 0.5 * (x12 + x23), 0.5 * (y12 + y23) |
| 103 |
xm, ym = 0.5 * (xl2 + xr1), 0.5 * (yl2 + yr1) |
| 104 |
bzl = [bz[0], (x01, y01), (xl2, yl2), (xm, ym)] |
| 105 |
bzr = [(xm, ym), (xr1, yr1), (x23, y23), bz[3]] |
| 106 |
segs = cubic_bz_to_pcorn(bzl, 0.5 * thresh) |
| 107 |
segs.extend(cubic_bz_to_pcorn(bzr, 0.5 * thresh)) |
| 108 |
return segs |
| 109 |
|
| 110 |
def bzs_to_pcorn(bzs, thresh = 1e-9): |
| 111 |
result = [] |
| 112 |
for sp in bzs: |
| 113 |
rsp = [] |
| 114 |
for bz in sp: |
| 115 |
if len(bz) == 2: |
| 116 |
dx = bz[1][0] - bz[0][0] |
| 117 |
dy = bz[1][1] - bz[0][1] |
| 118 |
th = atan2(dy, dx) |
| 119 |
rsp.append(pcorn.Segment(bz[0], bz[1], 0, 0)) |
| 120 |
else: |
| 121 |
rsp.extend(cubic_bz_to_pcorn(bz, thresh)) |
| 122 |
result.append(rsp) |
| 123 |
return result |
| 124 |
|
| 125 |
def plot_segs(segs): |
| 126 |
for i in range(len(segs)): |
| 127 |
seg = segs[i] |
| 128 |
if i == 0: |
| 129 |
print seg.z0[0], seg.z0[1], 'moveto' |
| 130 |
print seg.z1[0], seg.z1[1], 'lineto' |
| 131 |
print 'stroke' |
| 132 |
for i in range(len(segs)): |
| 133 |
seg = segs[i] |
| 134 |
if i == 0: |
| 135 |
print 'gsave', seg.z0[0], seg.z0[1], 'translate circle fill grestore' |
| 136 |
print 'gsave', seg.z1[0], seg.z1[1], 'translate circle fill grestore' |
| 137 |
|
| 138 |
import sys |
| 139 |
|
| 140 |
def test_to_pcorn(): |
| 141 |
C1 = 0.55228 |
| 142 |
bz = [(100, 100), (100 + 400 * C1, 100), (500, 500 - 400 * C1), (500, 500)] |
| 143 |
for i in range(0, 13): |
| 144 |
thresh = .1 ** i |
| 145 |
segs = cubic_bz_to_pcorn(bz, thresh) |
| 146 |
plot_segs(segs) |
| 147 |
print >> sys.stderr, thresh, len(segs) |
| 148 |
print '0 20 translate' |
| 149 |
|
| 150 |
if __name__ == '__main__': |
| 151 |
f = file(sys.argv[1]) |
| 152 |
bzs = read_bz(f) |
| 153 |
rsps = bzs_to_pcorn(bzs, 1) |
| 154 |
|
| 155 |
tocubic.plot_prolog() |
| 156 |
print 'grestore' |
| 157 |
print '1 -1 scale 0 -720 translate' |
| 158 |
print '/ss 1.5 def' |
| 159 |
print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def' |
| 160 |
tot = 0 |
| 161 |
for segs in rsps: |
| 162 |
curve = pcorn.Curve(segs) |
| 163 |
|
| 164 |
print '%', curve.arclen |
| 165 |
print '%', curve.sstarts |
| 166 |
if 0: |
| 167 |
print 'gsave 1 0 0 setrgbcolor' |
| 168 |
cmd = 'moveto' |
| 169 |
for i in range(100): |
| 170 |
s = i * .01 * curve.arclen |
| 171 |
x, y = curve.xy(s) |
| 172 |
th = curve.th(s) |
| 173 |
sth = 5 * sin(th) |
| 174 |
cth = 5 * cos(th) |
| 175 |
print x, y, cmd |
| 176 |
cmd = 'lineto' |
| 177 |
print 'closepath stroke grestore' |
| 178 |
for i in range(100): |
| 179 |
s = i * .01 * curve.arclen |
| 180 |
x, y = curve.xy(s) |
| 181 |
th = curve.th(s) |
| 182 |
sth = 5 * sin(th) |
| 183 |
cth = 5 * cos(th) |
| 184 |
if 0: |
| 185 |
print x - cth, y - sth, 'moveto' |
| 186 |
print x + cth, y + sth, 'lineto stroke' |
| 187 |
if 1: |
| 188 |
for s in curve.find_breaks(): |
| 189 |
print 'gsave 0 1 0 setrgbcolor' |
| 190 |
x, y = curve.xy(s) |
| 191 |
print x, y, 'translate 2 dup scale circle fill' |
| 192 |
print 'grestore' |
| 193 |
|
| 194 |
|
| 195 |
print 'gsave 0 0 0 setrgbcolor' |
| 196 |
optim = 3 |
| 197 |
thresh = 1e-2 |
| 198 |
new_bzs = tocubic.pcorn_curve_to_bzs(curve, optim, thresh) |
| 199 |
tot += len(new_bzs) |
| 200 |
plot_bzs([new_bzs], (0, 0), 1, True) |
| 201 |
print 'grestore' |
| 202 |
print 'grestore' |
| 203 |
print '/Helvetica 12 selectfont' |
| 204 |
print '36 720 moveto (thresh=%g optim=%d) show' % (thresh, optim) |
| 205 |
print '( tot segs=%d) show' % tot |
| 206 |
print 'showpage' |
| 207 |
|
| 208 |
|