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 dPQplane_basis = np.column_stack((np.cross(dp, dPQplane_normal),
61 dPQplane_basis = np.vstack((dPQplane_basis, [0,0,0,1]))
65 vdbg().arrow(dPQplane_basis[0:3,3], dPQplane_basis[0:3,ax],
66 colour= [ c * (1.0 - ax*0.2) for c in (1,1,1) ])
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_plane_check, dp_plane, dq_plane, q_plane)
80 vdbg_plane.arrow(p_plane_check, dp_plane)
81 vdbg_plane.arrow(q_plane, dq_plane)
83 # two circular arcs of equal maximum possible radius
84 # algorithm courtesy of Simon Tatham (`Railway problem',
85 # pers.comm. to ijackson@chiark 23.1.2004)
86 railway_angleoffset = atan2(*q_plane[0:2])
87 railway_theta = atan2(*dp_plane[0:2]) - railway_angleoffset
88 railway_phi = atan2(*dq_plane[0:2]) - railway_angleoffset
89 railway_cos_theta = cos(railway_theta)
90 railway_cos_phi = cos(railway_phi)
91 dbg('railway:', railway_theta, railway_phi, railway_angleoffset)
92 if railway_cos_theta**2 + railway_cos_phi**2 > 1E-6:
93 railway_roots = np.roots([
94 2 * (1 + cos(railway_theta - railway_phi)),
95 2 * (railway_cos_theta - railway_cos_phi),
98 for railway_r in railway_roots:
99 dbg(' twoarcs root r=',railway_r)
101 def railway_CPQ(pq, dpq, railway_r):
102 CPQ = pq + railway_r * np.array([-dpq[1], dpq[0]])
103 dbg('railway_CPQ', railway_r, pq, dpq, CPQ)
106 railway_CP = railway_CPQ([0,0], dp_plane, railway_r)
107 railway_QP = railway_CPQ(q_plane[0:2], -dq_plane, railway_r)
108 railway_midpt = 0.5 * (railway_CP + railway_QP)
111 def railway_ST(C, start, end, railway_r):
112 delta = atan2(*(end - C)[0:2]) - atan2(*(start - C)[0:2])
113 s = delta * railway_r
114 dbg('railway_ST', C, start, end, railway_r, s)
117 try_s = railway_ST(railway_CP, [0,0], railway_midpt, railway_r)
118 try_t = railway_ST(railway_CP, railway_midpt, q_plane[0:2], railway_r)
119 dbg('try_s, _t', try_s, try_t)
120 if try_s < 0 or try_t < 0:
123 try_st = try_s + try_t
124 if best_st is None or try_st < best_st:
125 start_la = 1/railway_r
129 start_mu = q_plane[2] / (start_s + start_t)
132 else: # twoarcs algorithm is not well defined
135 start_s = dist_pq_plane * .65
136 start_t = dist_pq_plane * .35
139 bodge = max( q_plane[2] * start_mu,
140 (start_s + start_t) * 0.1 )
141 start_s += 0.5 * bodge
142 start_t += 0.5 * bodge
146 tilt = atan(start_mu)
147 tilt_basis = np.array([
149 [ 0, cos(tilt), sin(tilt), 0 ],
150 [ 0, -sin(tilt), cos(tilt), 0 ],
153 findcurve_basis = matmatmultiply(dPQplane_basis, tilt_basis)
154 findcurve_into = np.linalg.inv(findcurve_basis)
156 for ax in range(0,3):
157 vdbg().arrow(findcurve_basis[0:3,3], findcurve_basis[0:3,ax])
159 q_findcurve = augmatmultiply(findcurve_into, q)
160 dq_findcurve = augmatmultiply(findcurve_into, dq, augwith=0)
162 findcurve_target = np.hstack((q_findcurve, dq_findcurve))
163 findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
164 start_mu, start_gamma, start_kappa)
166 findcurve_epsilon = dist_pq_plane * 0.01
168 global findcurve_subproc
169 if findcurve_subproc is None:
170 dbg('STARTING FINDCURVE')
171 findcurve_subproc = subprocess.Popen(
174 stdin=subprocess.PIPE,
175 stdout=subprocess.PIPE,
178 # restore_signals=True, // want python2 compat, nnng
179 universal_newlines=True,
182 findcurve_input = np.hstack((findcurve_target,
184 [findcurve_epsilon]))
186 def dbg_fmt_params(fcp):
187 return (('s=%10.7f t=%10.7f sh=%10.7f'
188 +' st=%10.7f la=%10.7f mu=%10.7f ga=%10.7f ka=%10.7f')
190 (( fcp[0]**2, fcp[1]**2 ) + tuple(fcp)))
192 #dbg('>> ' + ' '.join(map(str,findcurve_input)))
194 dbg(('RUNNING FINDCURVE' +
196 ' target Q=[%10.7f %10.7f %10.7f] dQ=[%10.7f %10.7f %10.7f]')
198 tuple(findcurve_input[0:6]))
199 dbg(('%s initial') % dbg_fmt_params(findcurve_input[6:12]))
201 print(*findcurve_input, file=findcurve_subproc.stdin)
202 findcurve_subproc.stdin.flush()
204 hc.func = symbolic.get_python()
208 l = findcurve_subproc.stdout.readline()
211 if not l: vdbg().crashing('findcurve EOF')
212 if not l.startswith('['):
220 dbg(('%s Q=[%10.7f %10.7f %10.7f] dQ=[%10.7f %10.7f %10.7f]%s')
222 (( dbg_fmt_params(l[0:6]), ) + tuple(l[6:12]) + (commentary,) ))
225 hc.findcurve_result = l[0:6]
226 hc.threshold = l[0]**2
227 hc.total_dist = hc.threshold + l[1]**2
228 #vdbg().curve( hc.point_at_t )
230 def point_at_t(hc, normalised_parameter):
231 dist = normalised_parameter * hc.total_dist
232 ours = list(hc.findcurve_result)
233 if dist <= hc.threshold:
237 ours[1] = sqrt(dist - hc.threshold)
238 asmat = hc.func(*ours)