Debian Codegraph @

third_party/spiro/curves/poly3.py

raw symbols SCIP
1 # Numerical techniques for solving 3rd order polynomial spline systems
2
3 # The standard representation is the vector of derivatives at s=0,
4 # with -.5 <= s <= 5.
5 #
6 # Thus, \kappa(s) = k0 + k1 s + 1/2 k2 s^2 + 1/6 k3 s^3
7
8 from math import *
9
10 def eval_cubic(a, b, c, d, x):
11 return ((d * x + c) * x + b) * x + a
12
13 # integrate over s = [0, 1]
14 def int_3spiro_poly(ks, n):
15 x, y = 0, 0
16 th = 0
17 ds = 1.0 / n
18 th1, th2, th3, th4 = ks[0], .5 * ks[1], (1./6) * ks[2], (1./24) * ks[3]
19 k0, k1, k2, k3 = ks[0] * ds, ks[1] * ds, ks[2] * ds, ks[3] * ds
20 s = 0
21 result = [(x, y)]
22 for i in range(n):
23 sm = s + 0.5 * ds
24 th = sm * eval_cubic(th1, th2, th3, th4, sm)
25 cth = cos(th)
26 sth = sin(th)
27
28 km0 = ((1./6 * k3 * sm + .5 * k2) * sm + k1) * sm + k0
29 km1 = ((.5 * k3 * sm + k2) * sm + k1) * ds
30 km2 = (k3 * sm + k2) * ds * ds
31 km3 = k3 * ds * ds * ds
32 #print km0, km1, km2, km3
33 u = 1 - km0 * km0 / 24
34 v = km1 / 24
35
36 u = 1 - km0 * km0 / 24 + (km0 ** 4 - 4 * km0 * km2 - 3 * km1 * km1) / 1920
37 v = km1 / 24 + (km3 - 6 * km0 * km0 * km1) / 1920
38
39 x += cth * u - sth * v
40 y += cth * v + sth * u
41 result.append((ds * x, ds * y))
42
43 s += ds
44
45 return result
46
47 def integ_chord(k, n = 64):
48 ks = (k[0] * .5, k[1] * .25, k[2] * .125, k[3] * .0625)
49 xp, yp = int_3spiro_poly(ks, n)[-1]
50 ks = (k[0] * -.5, k[1] * .25, k[2] * -.125, k[3] * .0625)
51 xm, ym = int_3spiro_poly(ks, n)[-1]
52 dx, dy = .5 * (xp + xm), .5 * (yp + ym)
53 return hypot(dx, dy), atan2(dy, dx)
54
55 # Return th0, th1, k0, k1 for given params
56 def calc_thk(ks):
57 chord, ch_th = integ_chord(ks)
58 th0 = ch_th - (-.5 * ks[0] + .125 * ks[1] - 1./48 * ks[2] + 1./384 * ks[3])
59 th1 = (.5 * ks[0] + .125 * ks[1] + 1./48 * ks[2] + 1./384 * ks[3]) - ch_th
60 k0 = chord * (ks[0] - .5 * ks[1] + .125 * ks[2] - 1./48 * ks[3])
61 k1 = chord * (ks[0] + .5 * ks[1] + .125 * ks[2] + 1./48 * ks[3])
62 #print '%', (-.5 * ks[0] + .125 * ks[1] - 1./48 * ks[2] + 1./384 * ks[3]), (.5 * ks[0] + .125 * ks[1] + 1./48 * ks[2] + 1./384 * ks[3]), ch_th
63 return th0, th1, k0, k1
64
65 def calc_k1k2(ks):
66 chord, ch_th = integ_chord(ks)
67 k1l = chord * chord * (ks[1] - .5 * ks[2] + .125 * ks[3])
68 k1r = chord * chord * (ks[1] + .5 * ks[2] + .125 * ks[3])
69 k2l = chord * chord * chord * (ks[2] - .5 * ks[3])
70 k2r = chord * chord * chord * (ks[2] + .5 * ks[3])
71 return k1l, k1r, k2l, k2r
72
73 def plot(ks):
74 ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
75 pside = int_3spiro_poly(ksp, 64)
76 ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
77 mside = int_3spiro_poly(ksm, 64)
78 mside.reverse()
79 for i in range(len(mside)):
80 mside[i] = (-mside[i][0], -mside[i][1])
81 pts = mside + pside[1:]
82 cmd = "moveto"
83 for j in range(len(pts)):
84 x, y = pts[j]
85 print 306 + 300 * x, 400 + 300 * y, cmd
86 cmd = "lineto"
87 print "stroke"
88 x, y = pts[0]
89 print 306 + 300 * x, 400 + 300 * y, "moveto"
90 x, y = pts[-1]
91 print 306 + 300 * x, 400 + 300 * y, "lineto .5 setlinewidth stroke"
92 print "showpage"
93
94 def solve_3spiro(th0, th1, k0, k1):
95 ks = [0, 0, 0, 0]
96 for i in range(5):
97 th0_a, th1_a, k0_a, k1_a = calc_thk(ks)
98 dth0 = th0 - th0_a
99 dth1 = th1 - th1_a
100 dk0 = k0 - k0_a
101 dk1 = k1 - k1_a
102 ks[0] += (dth0 + dth1) * 1.5 + (dk0 + dk1) * -.25
103 ks[1] += (dth1 - dth0) * 15 + (dk0 - dk1) * 1.5
104 ks[2] += (dth0 + dth1) * -12 + (dk0 + dk1) * 6
105 ks[3] += (dth0 - dth1) * 360 + (dk1 - dk0) * 60
106 #print '% ks =', ks
107 return ks
108
109 def iter_spline(pts, ths, ks):
110 pass
111
112 def solve_vee():
113 kss = []
114 for i in range(10):
115 kss.append([0, 0, 0, 0])
116 thl = [0] * len(kss)
117 thr = [0] * len(kss)
118 k0l = [0] * len(kss)
119 k0r = [0] * len(kss)
120 k1l = [0] * len(kss)
121 k1r = [0] * len(kss)
122 k2l = [0] * len(kss)
123 k2r = [0] * len(kss)
124 for i in range(10):
125 for j in range(len(kss)):
126 thl[j], thr[j], k0l[j], k0r[j] = calc_thk(kss[j])
127 k0l[j], k1r[j], k2l[j], k2r[j] = calc_k1k2(kss[j])
128 for j in range(len(kss) - 1):
129 dth = thl[j + 1] + thr[j]
130 if j == 5: dth += .1
131 dk0 = k0l[j + 1] - k0r[j]
132 dk1 = k1l[j + 1] - k1r[j]
133 dk2 = k2l[j + 1] - k2r[j]
134
135
136 if __name__ == '__main__':
137 k0 = pi * 3
138 ks = [0, k0, -2 * k0, 0]
139 ks = [0, 0, 0, 0.01]
140 #plot(ks)
141 thk = calc_thk(ks)
142 print '%', thk
143
144 ks = solve_3spiro(0, 0, 0, 0.001)
145 print '% thk =', calc_thk(ks)
146 #plot(ks)
147 print '%', ks
148 print calc_k1k2(ks)