Debian Codegraph @

third_party/spiro/curves/bezfigs.py

raw symbols SCIP
1 import sys
2 from math import *
3
4 import fromcubic
5 import tocubic
6
7 import cornu
8
9 def eps_prologue(x0, y0, x1, y1, draw_box = False):
10 print '%!PS-Adobe-3.0 EPSF'
11 print '%%BoundingBox:', x0, y0, x1, y1
12 print '%%EndComments'
13 print '%%EndProlog'
14 print '%%Page: 1 1'
15 if draw_box:
16 print x0, y0, 'moveto', x0, y1, 'lineto', x1, y1, 'lineto', x1, y0, 'lineto closepath stroke'
17
18 def eps_trailer():
19 print '%%EOF'
20
21 def fit_cubic_superfast(z0, z1, arclen, th0, th1, aab):
22 chord = hypot(z1[0] - z0[0], z1[1] - z0[1])
23 cth0, sth0 = cos(th0), sin(th0)
24 cth1, sth1 = -cos(th1), -sin(th1)
25 armlen = .66667 * arclen
26 a = armlen * aab
27 b = armlen - a
28 bz = [z0, (z0[0] + cth0 * a, z0[1] + sth0 * a),
29 (z1[0] + cth1 * b, z1[1] + sth1 * b), z1]
30 return bz
31
32 def fit_cubic(z0, z1, arclen, th_fn, fast, aabmin = 0, aabmax = 1.):
33 chord = hypot(z1[0] - z0[0], z1[1] - z0[1])
34 if (arclen < 1.000001 * chord):
35 return [z0, z1], 0
36 th0 = th_fn(0)
37 th1 = th_fn(arclen)
38 imax = 4
39 jmax = 10
40 if fast:
41 imax = 1
42 jmax = 0
43 for i in range(imax):
44 for j in range(jmax + 1):
45 if jmax == 0:
46 aab = 0.5 * (aabmin + aabmax)
47 else:
48 aab = aabmin + (aabmax - aabmin) * j / jmax
49 if fast == 2:
50 bz = fit_cubic_superfast(z0, z1, arclen, th0, th1, aab)
51 else:
52 bz = tocubic.fit_cubic_arclen(z0, z1, arclen, th0, th1, aab)
53 score = tocubic.measure_bz_rk4(bz, arclen, th_fn)
54 print '% aab =', aab, 'score =', score
55 sys.stdout.flush()
56 if j == 0 or score < best_score:
57 best_score = score
58 best_aab = aab
59 best_bz = bz
60 daab = .06 * (aabmax - aabmin)
61 aabmin = max(0, best_aab - daab)
62 aabmax = min(1, best_aab + daab)
63 print '%--- best_aab =', best_aab
64 return best_bz, best_score
65
66 def cornu_to_cubic(t0, t1, figno):
67 if figno == 1:
68 aabmin = 0
69 aabmax = 0.4
70 elif figno == 2:
71 aabmin = 0.5
72 aabmax = 1.
73 else:
74 aabmin = 0
75 aabmax = 1.
76 fast = 0
77 if figno == 3:
78 fast = 1
79 elif figno == 4:
80 fast = 2
81 def th_fn(s):
82 return (s + t0) ** 2
83 y0, x0 = cornu.eval_cornu(t0)
84 y1, x1 = cornu.eval_cornu(t1)
85 bz, score = fit_cubic((x0, y0), (x1, y1), t1 - t0, th_fn, fast, aabmin, aabmax)
86 return bz, score
87
88 def plot_k_of_bz(bz):
89 dbz = tocubic.bz_deriv(bz)
90 ddbz = tocubic.bz_deriv(dbz)
91 cmd = 'moveto'
92 ss = [0]
93 def arclength_deriv(x, ss):
94 dx, dy = tocubic.bz_eval(dbz, x)
95 return [hypot(dx, dy)]
96 dt = 0.01
97 t = 0
98 for i in range(101):
99 dx, dy = tocubic.bz_eval(dbz, t)
100 ddx, ddy = tocubic.bz_eval(ddbz, t)
101 k = (ddy * dx - dy * ddx) / (dx * dx + dy * dy) ** 1.5
102 print 100 + 500 * ss[0], 100 + 200 * k, cmd
103 cmd = 'lineto'
104
105 dsdx = arclength_deriv(t, ss)
106 tocubic.rk4(ss, dsdx, t, .01, arclength_deriv)
107 t += dt
108 print 'stroke'
109
110 def plot_k_nominal(s0, s1):
111 k0 = 2 * s0
112 k1 = 2 * s1
113 print 'gsave 0.5 setlinewidth'
114 print 100, 100 + 200 * k0, 'moveto'
115 print 100 + 500 * (s1 - s0), 100 + 200 * k1, 'lineto'
116 print 'stroke grestore'
117
118 def simple_bez():
119 eps_prologue(95, 126, 552, 508, 0)
120 tocubic.plot_prolog()
121 print '/ss 1.5 def'
122 print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def'
123 bz, score = cornu_to_cubic(.5, 1.1, 2)
124 fromcubic.plot_bzs([[bz]], (-400, 100), 1000, True)
125 print 'stroke'
126 print '/Times-Roman 12 selectfont'
127 print '95 130 moveto ((x0, y0)) show'
128 print '360 200 moveto ((x1, y1)) show'
129 print '480 340 moveto ((x2, y2)) show'
130 print '505 495 moveto ((x3, y3)) show'
131 print 'showpage'
132 eps_trailer()
133
134 def fast_bez(figno):
135 if figno == 3:
136 y1 = 520
137 else:
138 y1 = 550
139 eps_prologue(95, 140, 552, y1, 0)
140 tocubic.plot_prolog()
141 print '/ss 1.5 def'
142 print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def'
143 bz, score = cornu_to_cubic(.5, 1.1, figno)
144 fromcubic.plot_bzs([[bz]], (-400, 100), 1000, True)
145 print 'stroke'
146 plot_k_nominal(.5, 1.1)
147 plot_k_of_bz(bz)
148 print 'showpage'
149 eps_trailer()
150
151 def bezfig(s1):
152 eps_prologue(95, 38, 510, 550, 0)
153 #print '0.5 0.5 scale 500 100 translate'
154 tocubic.plot_prolog()
155 print '/ss 1.5 def'
156 print '/circle { ss 0 moveto currentpoint exch ss sub exch ss 0 360 arc } bind def'
157 bz, score = cornu_to_cubic(.5, 0.85, 1)
158 fromcubic.plot_bzs([[bz]], (-400, 0), 1000, True)
159 print 'stroke'
160 plot_k_nominal(.5, 0.85)
161 plot_k_of_bz(bz)
162 bz, score = cornu_to_cubic(.5, 0.85, 2)
163 fromcubic.plot_bzs([[bz]], (-400, 100), 1000, True)
164 print 'stroke'
165 print 'gsave 0 50 translate'
166 plot_k_nominal(.5, .85)
167 plot_k_of_bz(bz)
168 print 'grestore'
169 print 'showpage'
170
171 import sys
172
173 if __name__ == '__main__':
174 figno = int(sys.argv[1])
175 if figno == 0:
176 simple_bez()
177 elif figno == 1:
178 bezfig(1.0)
179 elif figno == 2:
180 bezfig(0.85)
181 else:
182 fast_bez(figno)
183 #fast_bez(4)