2 from __future__ import print_function
5 from numpy import cos, sin
10 from moedebug import *
13 from math import atan2, atan, sqrt
17 findcurve_subproc = None
19 class HelixishCurve():
25 dp = unit_v(cp[1]-cp[0])
26 dq = unit_v(cp[3]-cp[2])
28 dbg('HelixishCurve __init__', cp)
35 # - solve in the plane containing dP and dQ
36 # - total distance normal to that plane gives mu
37 # - now resulting curve is not parallel to dP at P
38 # nor dQ at Q, so tilt it
39 # - [[ pick as the hinge point the half of the curve
40 # with the larger s or t ]] not yet implemented
41 # - increase the other distance {t,s} by a bodge factor
42 # approx distance between {Q,P} and {Q,P}' due to hinging
43 # but minimum is 10% of (wlog) {s,t} [[ not quite like this ]]
45 dPQplane_normal = np.cross(dp, dq)
47 if np.linalg.norm(dPQplane_normal) < 1E-6:
48 dbg('dPQplane_normal small')
49 dPQplane_normal = np.cross([1,0,0], dp)
50 if np.linalg.norm(dPQplane_normal) < 1E-6:
51 dbg('dPQplane_normal small again')
52 dPQplane_normal = np.cross([0,1,0], dp)
54 dPQplane_normal = unit_v(dPQplane_normal)
56 vdbg().arrow([0,0,0], dPQplane_normal, color=(1,1,0))
58 dPQplane_basis = np.column_stack((np.cross(dp, dPQplane_normal),
63 dPQplane_basis = np.vstack((dPQplane_basis, [0,0,0,1]))
66 vdbg().basis(dPQplane_basis)
68 dPQplane_into = np.linalg.inv(dPQplane_basis)
71 p_plane_check = augmatmultiply(dPQplane_into, p)
72 dp_plane = augmatmultiply(dPQplane_into, dp, augwith=0)
73 dq_plane = augmatmultiply(dPQplane_into, dq, augwith=0)
74 q_plane = augmatmultiply(dPQplane_into, q)
75 dist_pq_plane = np.linalg.norm(q_plane)
77 vdbg_plane = MatrixVisdebug(vdbg(), dPQplane_basis)
79 dbg('plane p', p_plane_check, 'dp', dp_plane, 'dq', dq_plane,
80 'q', q_plane, 'dist_pq_plane', dist_pq_plane)
81 vdbg_plane.arrow(p_plane_check, dp_plane)
82 vdbg_plane.arrow(q_plane, dq_plane)
84 railway_inplane_basis_x = np.hstack((q_plane[0:2], [0]))
85 railway_inplane_basis = np.column_stack((
86 railway_inplane_basis_x,
87 -np.cross([0,0,1], railway_inplane_basis_x),
91 #dbg('railway_inplane_basis\n', railway_inplane_basis)
92 railway_inplane_basis = np.vstack((railway_inplane_basis,
94 dbg('railway_inplane_basis\n', railway_inplane_basis)
95 railway_basis = matmatmultiply(dPQplane_basis, railway_inplane_basis)
96 dbg('railway_basis\n', railway_basis)
97 vdbg().basis(railway_basis, hue=(1,0,1))
98 vdbg_railway = MatrixVisdebug(vdbg(), railway_basis)
100 # two circular arcs of equal maximum possible radius
101 # algorithm courtesy of Simon Tatham (`Railway problem',
102 # pers.comm. to ijackson@chiark 23.1.2004)
103 railway_angleoffset = atan2(*q_plane[0:2])
104 # these two angles are unconventional: clockwise from north
105 railway_theta = tau/4 - (atan2(*dp_plane[0:2]) - railway_angleoffset)
106 railway_phi = tau/4 - (atan2(*-dq_plane[0:2]) - railway_angleoffset)
107 railway_cos_theta = cos(railway_theta)
108 railway_cos_phi = cos(railway_phi)
110 dbg('railway:', railway_theta, railway_phi, railway_angleoffset)
112 def vdbg_railway_angle(start, angle, **kw):
113 vdbg_railway.arrow(start, [sin(angle), cos(angle), 0], **kw)
114 vdbg_railway_angle([0, 0, 0.1], railway_theta, color=(1, 0.5, 0))
115 vdbg_railway_angle([1, 0, 0.1], railway_phi, color=(1, 0.5, 0))
117 if railway_cos_theta**2 + railway_cos_phi**2 > 1E-6:
118 railway_polynomial = [
119 2 * (1 + cos(railway_theta - railway_phi)),
120 2 * (railway_cos_theta - railway_cos_phi),
123 railway_roots = np.roots(railway_polynomial)
124 dbg('railway poly, roots:', railway_polynomial, railway_roots)
125 for railway_r in railway_roots:
126 dbg(' twoarcs root r=',railway_r)
128 def railway_CPQ(pq, dpq, railway_r):
129 CPQ = pq + railway_r * np.array([-dpq[1], dpq[0]])
130 dbg('railway_CPQ', railway_r, pq, dpq, CPQ)
131 vdbg_plane.circle( np.hstack((CPQ, [0])),
136 railway_CP = railway_CPQ([0,0], dp_plane, railway_r)
137 railway_QP = railway_CPQ(q_plane[0:2], -dq_plane, railway_r)
138 railway_midpt = 0.5 * (railway_CP + railway_QP)
141 def railway_ST(C, start, end, railway_r):
142 delta = atan2(*(end - C)[0:2]) - atan2(*(start - C)[0:2])
143 s = delta * railway_r
144 dbg('railway_ST', C, start, end, railway_r, s)
147 try_s = railway_ST(railway_CP, [0,0], railway_midpt, railway_r)
148 try_t = railway_ST(railway_CP, railway_midpt, q_plane[0:2], railway_r)
149 dbg('try_s, _t', try_s, try_t)
150 if try_s < 0 or try_t < 0:
153 try_st = try_s + try_t
154 if best_st is None or try_st < best_st:
155 start_la = 1/railway_r
159 start_mu = q_plane[2] / (start_s + start_t)
162 else: # twoarcs algorithm is not well defined
165 start_s = dist_pq_plane * .65
166 start_t = dist_pq_plane * .35
169 bodge = max( q_plane[2] * start_mu,
170 (start_s + start_t) * 0.1 )
171 start_s += 0.5 * bodge
172 start_t += 0.5 * bodge
176 tilt = atan(start_mu)
177 tilt_basis = np.array([
179 [ 0, cos(tilt), sin(tilt), 0 ],
180 [ 0, -sin(tilt), cos(tilt), 0 ],
183 findcurve_basis = matmatmultiply(dPQplane_basis, tilt_basis)
184 findcurve_into = np.linalg.inv(findcurve_basis)
186 for ax in range(0,3):
187 vdbg().arrow(findcurve_basis[0:3,3], findcurve_basis[0:3,ax])
189 q_findcurve = augmatmultiply(findcurve_into, q)
190 dq_findcurve = augmatmultiply(findcurve_into, dq, augwith=0)
192 findcurve_target = np.hstack((q_findcurve, dq_findcurve))
193 findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
194 start_mu, start_gamma, start_kappa)
196 findcurve_epsilon = dist_pq_plane * 0.01
198 global findcurve_subproc
199 if findcurve_subproc is None:
200 dbg('STARTING FINDCURVE')
201 findcurve_subproc = subprocess.Popen(
204 stdin=subprocess.PIPE,
205 stdout=subprocess.PIPE,
208 # restore_signals=True, // want python2 compat, nnng
209 universal_newlines=True,
212 findcurve_input = np.hstack((findcurve_target,
214 [findcurve_epsilon]))
216 def dbg_fmt_params(fcp):
217 return (('s=%10.7f t=%10.7f sh=%10.7f'
218 +' st=%10.7f la=%10.7f mu=%10.7f ga=%10.7f ka=%10.7f')
220 (( fcp[0]**2, fcp[1]**2 ) + tuple(fcp)))
222 #dbg('>> ' + ' '.join(map(str,findcurve_input)))
224 dbg(('RUNNING FINDCURVE' +
226 ' target Q=[%10.7f %10.7f %10.7f] dQ=[%10.7f %10.7f %10.7f]')
228 tuple(findcurve_input[0:6]))
229 dbg(('%s initial') % dbg_fmt_params(findcurve_input[6:12]))
231 print(*findcurve_input, file=findcurve_subproc.stdin)
232 findcurve_subproc.stdin.flush()
234 hc.func = symbolic.get_python()
238 l = findcurve_subproc.stdout.readline()
241 if not l: vdbg().crashing('findcurve EOF')
242 if not l.startswith('['):
250 dbg(('%s Q=[%10.7f %10.7f %10.7f] dQ=[%10.7f %10.7f %10.7f]%s')
252 (( dbg_fmt_params(l[0:6]), ) + tuple(l[6:12]) + (commentary,) ))
255 hc.findcurve_result = l[0:6]
256 hc.threshold = l[0]**2
257 hc.total_dist = hc.threshold + l[1]**2
258 #vdbg().curve( hc.point_at_t )
260 def point_at_t(hc, normalised_parameter):
261 dist = normalised_parameter * hc.total_dist
262 ours = list(hc.findcurve_result)
263 if dist <= hc.threshold:
267 ours[1] = sqrt(dist - hc.threshold)
268 asmat = hc.func(*ours)