Debian Codegraph @

third_party/spiro/curves/plot_solve_clothoid.py

raw symbols SCIP
1 import clothoid
2 from math import *
3
4 print '%!PS-Adobe'
5
6 def integ_spiro(k0, k1, k2, k3, n = 4):
7 th1 = k0
8 th2 = .5 * k1
9 th3 = (1./6) * k2
10 th4 = (1./24) * k3
11 ds = 1. / n
12 ds2 = ds * ds
13 ds3 = ds2 * ds
14 s = .5 * ds - .5
15
16 k0 *= ds
17 k1 *= ds
18 k2 *= ds
19 k3 *= ds
20
21 x = 0
22 y = 0
23
24 for i in range(n):
25 if n == 1:
26 km0 = k0
27 km1 = k1 * ds
28 km2 = k2 * ds2
29 else:
30 km0 = (((1./6) * k3 * s + .5 * k2) * s + k1) * s + k0
31 km1 = ((.5 * k3 * s + k2) * s + k1) * ds
32 km2 = (k3 * s + k2) * ds2
33 km3 = k3 * ds3
34
35 t1_1 = km0
36 t1_2 = .5 * km1
37 t1_3 = (1./6) * km2
38 t1_4 = (1./24) * km3
39 t2_2 = t1_1 * t1_1
40 t2_3 = 2 * (t1_1 * t1_2)
41 t2_4 = 2 * (t1_1 * t1_3) + t1_2 * t1_2
42 t2_5 = 2 * (t1_1 * t1_4 + t1_2 * t1_3)
43 t2_6 = 2 * (t1_2 * t1_4) + t1_3 * t1_3
44 t2_7 = 2 * (t1_3 * t1_4)
45 t2_8 = t1_4 * t1_4
46 t3_4 = t2_2 * t1_2 + t2_3 * t1_1
47 t3_6 = t2_2 * t1_4 + t2_3 * t1_3 + t2_4 * t1_2 + t2_5 * t1_1
48 t3_8 = t2_4 * t1_4 + t2_5 * t1_3 + t2_6 * t1_2 + t2_7 * t1_1
49 t3_10 = t2_6 * t1_4 + t2_7 * t1_3 + t2_8 * t1_2
50 t4_4 = t2_2 * t2_2
51 t4_5 = 2 * (t2_2 * t2_3)
52 t4_6 = 2 * (t2_2 * t2_4) + t2_3 * t2_3
53 t4_7 = 2 * (t2_2 * t2_5 + t2_3 * t2_4)
54 t4_8 = 2 * (t2_2 * t2_6 + t2_3 * t2_5) + t2_4 * t2_4
55 t4_9 = 2 * (t2_2 * t2_7 + t2_3 * t2_6 + t2_4 * t2_5)
56 t4_10 = 2 * (t2_2 * t2_8 + t2_3 * t2_7 + t2_4 * t2_6) + t2_5 * t2_5
57 t5_6 = t4_4 * t1_2 + t4_5 * t1_1
58 t5_8 = t4_4 * t1_4 + t4_5 * t1_3 + t4_6 * t1_2 + t4_7 * t1_1
59 t5_10 = t4_6 * t1_4 + t4_7 * t1_3 + t4_8 * t1_2 + t4_9 * t1_1
60 t6_6 = t4_4 * t2_2
61 t6_7 = t4_4 * t2_3 + t4_5 * t2_2
62 t6_8 = t4_4 * t2_4 + t4_5 * t2_3 + t4_6 * t2_2
63 t6_9 = t4_4 * t2_5 + t4_5 * t2_4 + t4_6 * t2_3 + t4_7 * t2_2
64 t6_10 = t4_4 * t2_6 + t4_5 * t2_5 + t4_6 * t2_4 + t4_7 * t2_3 + t4_8 * t2_2
65 t7_8 = t6_6 * t1_2 + t6_7 * t1_1
66 t7_10 = t6_6 * t1_4 + t6_7 * t1_3 + t6_8 * t1_2 + t6_9 * t1_1
67 t8_8 = t6_6 * t2_2
68 t8_9 = t6_6 * t2_3 + t6_7 * t2_2
69 t8_10 = t6_6 * t2_4 + t6_7 * t2_3 + t6_8 * t2_2
70 t9_10 = t8_8 * t1_2 + t8_9 * t1_1
71 t10_10 = t8_8 * t2_2
72 u = 1
73 u -= (1./24) * t2_2 + (1./160) * t2_4 + (1./896) * t2_6 + (1./4608) * t2_8
74 u += (1./1920) * t4_4 + (1./10752) * t4_6 + (1./55296) * t4_8 + (1./270336) * t4_10
75 u -= (1./322560) * t6_6 + (1./1658880) * t6_8 + (1./8110080) * t6_10
76 u += (1./92897280) * t8_8 + (1./454164480) * t8_10
77 u -= 2.4464949595157930e-11 * t10_10
78 v = (1./12) * t1_2 + (1./80) * t1_4
79 v -= (1./480) * t3_4 + (1./2688) * t3_6 + (1./13824) * t3_8 + (1./67584) * t3_10
80 v += (1./53760) * t5_6 + (1./276480) * t5_8 + (1./1351680) * t5_10
81 v -= (1./11612160) * t7_8 + (1./56770560) * t7_10
82 v += 2.4464949595157932e-10 * t9_10
83 if n == 1:
84 x = u
85 y = v
86 else:
87 th = (((th4 * s + th3) * s + th2) * s + th1) * s
88 cth = cos(th)
89 sth = sin(th)
90
91 x += cth * u - sth * v
92 y += cth * v + sth * u
93 s += ds
94 return [x * ds, y * ds]
95
96 count_iter = 0
97
98 # Given th0 and th1 at endpoints (measured from chord), return k0
99 # and k1 such that the clothoid k(s) = k0 + k1 s, evaluated from
100 # s = -.5 to .5, has the tangents given
101 def solve_clothoid(th0, th1, verbose = False):
102 global count_iter
103
104 k1_old = 0
105 e_old = th1 - th0
106 k0 = th0 + th1
107 k1 = 6 * (1 - ((.5 / pi) * k0) ** 3) * e_old
108
109 # secant method
110 for i in range(10):
111 x, y = integ_spiro(k0, k1, 0, 0)
112 e = (th1 - th0) + 2 * atan2(y, x) - .25 * k1
113 count_iter += 1
114 if verbose:
115 print k0, k1, e
116 if abs(e) < 1e-9: break
117 k1_old, e_old, k1 = k1, e, k1 + (k1_old - k1) * e / (e - e_old)
118
119 return k0, k1
120
121 def plot_by_thp():
122 count = 0
123 for i in range(11):
124 thp = i * .1
125 print .5 + .05 * i, .5, .5, 'setrgbcolor'
126 print '.75 setlinewidth'
127 cmd = 'moveto'
128 for j in range(-40, 41):
129 thm = j * .02
130 k0, k1 = solve_clothoid(thp - thm, thp + thm, True)
131 count += 1
132 k1 = min(40, max(-40, k1))
133 print 306 + 75 * thm, 396 - 10 * k1, cmd
134 cmd = 'lineto'
135 print 'stroke'
136 print '% count_iter = ', count_iter, 'for', count
137
138 def plot_by_thm():
139 print '.75 setlinewidth'
140 print 36, 396 - 350, 'moveto'
141 print 0, 700, 'rlineto stroke'
142 for i in range(-10, 10):
143 if i == 0: wid = 636
144 else: wid = 5
145 print 36, 396 - 10 * i, 'moveto', wid, '0 rlineto stroke'
146 cmd = 'moveto'
147 thm = -.1
148 for i in range(41):
149 thp = i * .1
150 k0, k1 = solve_clothoid(thp - thm, thp + thm)
151 print 36 + 150 * thp, 396 - 100 * k1, cmd
152 cmd = 'lineto'
153 print 'stroke'
154 print '0 0 1 setrgbcolor'
155 cmd = 'moveto'
156 for i in range(41):
157 thp = i * .1
158 k1 = 12 * thm * cos(.5 * thp)
159 k1 = 12 * thm * (1 - (thp / pi) ** 3)
160 print 36 + 150 * thp, 396 - 100 * k1, cmd
161 cmd = 'lineto'
162 print 'stroke'
163
164 plot_by_thp()