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])
31 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]))
63 dPQplane_into = np.linalg.inv(dPQplane_basis)
66 p_plane_check = augmatmultiply(dPQplane_into, p)
67 dp_plane = augmatmultiply(dPQplane_into, dp, augwith=0)
68 dq_plane = augmatmultiply(dPQplane_into, dq, augwith=0)
69 q_plane = augmatmultiply(dPQplane_into, q)
70 dist_pq_plane = np.linalg.norm(q_plane)
72 dbg('plane:', p_plane_check, dp_plane, dq_plane, q_plane)
74 # two circular arcs of equal maximum possible radius
75 # algorithm courtesy of Simon Tatham (`Railway problem',
76 # pers.comm. to ijackson@chiark 23.1.2004)
77 railway_angleoffset = atan2(*q_plane[0:2])
78 railway_theta = tau/4 - railway_angleoffset
79 railway_phi = atan2(*dq_plane[0:2]) - railway_angleoffset
80 railway_cos_theta = cos(railway_theta)
81 railway_cos_phi = cos(railway_phi)
82 if railway_cos_theta**2 + railway_cos_phi**2 > 1E6:
83 railway_roots = np.roots([
84 2 * (1 + cos(railway_theta - railway_phi)),
85 2 * (railway_cos_theta - railway_cos_phi),
88 for railway_r in railway_roots:
89 def railway_CPQ(pq, dpq, railway_r):
90 return pq + railway_r * [-dpq[1], dpq[0]]
92 railway_CP = railway_CPQ([0,0,0], dp_plane, railway_r)
93 railway_QP = railway_CPQ(q_plane[0:2], -dq_plane, railway_r)
94 railway_midpt = 0.5 * (railway_CP + railway_QP)
97 def railway_ST(C, start, end, railway_r):
98 delta = atan2(*(end - C)[0:2]) - atan2(start - C)[0:2]
101 try_s = railway_ST(railway_CP, [0,0], midpt, railway_r)
102 try_t = railway_ST(railway_CP, midpt, q_plane, railway_r)
103 try_st = try_s + try_t
104 if best_st is None or try_st < best_st:
109 start_mu = q_plane[2] / (start_s + start_t)
112 else: # twoarcs algorithm is not well defined
115 start_s = dist_pq_plane * .65
116 start_t = dist_pq_plane * .35
119 bodge = max( q_plane[2] * start_mu,
120 (start_s + start_t) * 0.1 )
121 start_s += 0.5 * bodge
122 start_t += 0.5 * bodge
126 tilt = atan(start_mu)
127 tilt_basis = np.array([
129 [ 0, cos(tilt), sin(tilt), 0 ],
130 [ 0, -sin(tilt), cos(tilt), 0 ],
133 findcurve_basis = matmatmultiply(dPQplane_basis, tilt_basis)
134 findcurve_into = np.linalg.inv(findcurve_basis)
136 q_findcurve = augmatmultiply(findcurve_into, q)
137 dq_findcurve = augmatmultiply(findcurve_into, dq, augwith=0)
139 findcurve_target = np.hstack((q_findcurve, dq_findcurve))
140 findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
141 start_mu, start_gamma, start_kappa)
143 findcurve_epsilon = dist_pq_plane * 0.01
145 global findcurve_subproc
146 if findcurve_subproc is None:
147 dbg('STARTING FINDCURVE')
148 findcurve_subproc = subprocess.Popen(
151 stdin=subprocess.PIPE,
152 stdout=subprocess.PIPE,
155 # restore_signals=True, // want python2 compat, nnng
156 universal_newlines=True,
159 findcurve_input = np.hstack((findcurve_target,
161 [findcurve_epsilon]))
163 def dbg_fmt_params(fcp):
164 return (('s=%10.7f t=%10.7f sh=%10.7f'
165 +' st=%10.7f la=%10.7f mu=%10.7f ga=%10.7f ka=%10.7f')
167 (( fcp[0]**2, fcp[1]**2 ) + tuple(fcp)))
169 #dbg('>> ' + ' '.join(map(str,findcurve_input)))
171 dbg(('RUNNING FINDCURVE' +
173 ' target Q=[%10.7f %10.7f %10.7f] dQ=[%10.7f %10.7f %10.7f]')
175 tuple(findcurve_input[0:6]))
176 dbg(('%s initial') % dbg_fmt_params(findcurve_input[6:12]))
178 print(*findcurve_input, file=findcurve_subproc.stdin)
179 findcurve_subproc.stdin.flush()
181 hc.func = symbolic.get_python()
185 l = findcurve_subproc.stdout.readline()
188 if not l: vdbg().crashing('findcurve EOF')
189 if not l.startswith('['):
197 dbg(('%s Q=[%10.7f %10.7f %10.7f] dQ=[%10.7f %10.7f %10.7f]%s')
199 (( dbg_fmt_params(l[0:6]), ) + tuple(l[6:12]) + (commentary,) ))
202 hc.findcurve_result = l[0:6]
203 hc.threshold = l[0]**2
204 hc.total_dist = hc.threshold + l[1]**2
205 #vdbg().curve( hc.point_at_t )
207 def point_at_t(hc, normalised_parameter):
208 dist = normalised_parameter * hc.total_dist
209 ours = list(hc.findcurve_result)
210 if dist <= hc.threshold:
214 ours[1] = sqrt(dist - hc.threshold)
215 asmat = hc.func(*ours)