2 from __future__ import print_function
5 from numpy import cos, sin
10 from moedebug import *
13 from math import atan2, atan, sqrt
17 def augment(v, augwith=1): return np.append(v, 1)
18 def augment0(v): return augment(v, 0)
19 def unaugment(v): return v[0:3]
21 def matmultiply(mat,vect):
23 # we would prefer to write mat @ vect
24 # but that doesn't work in Python 2
25 return np.array((vect * np.matrix(mat).T))[0,:]
27 def matmatmultiply(mat1,mat2):
29 # we would prefer to write mat1 @ mat2
30 # but that doesn't work in Python 2
31 return np.array((np.matrix(mat1) * np.matrix(mat2)))
33 def augmatmultiply(mat,unaugvect, augwith=1):
34 return unaugment(matmultiply(mat, augment(unaugvect, augwith)))
36 findcurve_subproc = None
38 class HelixishCurve():
44 dp = unit_v(cp[1]-cp[0])
45 dq = unit_v(cp[3]-cp[2])
47 dbg('HelixishCurve __init__', cp)
50 # - solve in the plane containing dP and dQ
51 # - total distance normal to that plane gives mu
52 # - now resulting curve is not parallel to dP at P
53 # nor dQ at Q, so tilt it
54 # - [[ pick as the hinge point the half of the curve
55 # with the larger s or t ]] not yet implemented
56 # - increase the other distance {t,s} by a bodge factor
57 # approx distance between {Q,P} and {Q,P}' due to hinging
58 # but minimum is 10% of (wlog) {s,t} [[ not quite like this ]]
60 dPQplane_normal = np.cross(dp, dq)
61 if (np.linalg.norm(dPQplane_normal) < 1E6):
62 dPQplane_normal += [0, 0, 1E5]
63 dPQplane_normal = unit_v(dPQplane_normal)
65 dPQplane_basis = np.column_stack((np.cross(dp, dPQplane_normal),
70 dPQplane_basis = np.vstack((dPQplane_basis, [0,0,0,1]))
72 dPQplane_into = np.linalg.inv(dPQplane_basis)
74 dp_plane = augmatmultiply(dPQplane_into, dp, augwith=0)
75 dq_plane = augmatmultiply(dPQplane_into, dq, augwith=0)
76 q_plane = augmatmultiply(dPQplane_into, q)
77 dist_pq_plane = np.linalg.norm(q_plane)
79 # two circular arcs of equal maximum possible radius
80 # algorithm courtesy of Simon Tatham (`Railway problem',
81 # pers.comm. to ijackson@chiark 23.1.2004)
82 railway_angleoffset = atan2(*q_plane[0:2])
83 railway_theta = tau/4 - railway_angleoffset
84 railway_phi = atan2(*dq_plane[0:2]) - railway_angleoffset
85 railway_cos_theta = cos(railway_theta)
86 railway_cos_phi = cos(railway_phi)
87 if railway_cos_theta**2 + railway_cos_phi**2 > 1E6:
88 railway_roots = np.roots([
89 2 * (1 + cos(railway_theta - railway_phi)),
90 2 * (railway_cos_theta - railway_cos_phi),
93 for railway_r in railway_roots:
94 def railway_CPQ(pq, dpq, railway_r):
95 return pq + railway_r * [-dpq[1], dpq[0]]
97 railway_CP = railway_CPQ([0,0,0], dp_plane, railway_r)
98 railway_QP = railway_CPQ(q_plane[0:2], -dq_plane, railway_r)
99 railway_midpt = 0.5 * (railway_CP + railway_QP)
102 def railway_ST(C, start, end, railway_r):
103 delta = atan2(*(end - C)[0:2]) - atan2(start - C)[0:2]
104 s = delta * railway_r
106 try_s = railway_ST(railway_CP, [0,0], midpt, railway_r)
107 try_t = railway_ST(railway_CP, midpt, q_plane, railway_r)
108 try_st = try_s + try_t
109 if best_st is None or try_st < best_st:
114 start_mu = q_plane[2] / (start_s + start_t)
117 else: # twoarcs algorithm is not well defined
120 start_s = dist_pq_plane * .65
121 start_t = dist_pq_plane * .35
124 bodge = max( q_plane[2] * start_mu,
125 (start_s + start_t) * 0.1 )
126 start_s += 0.5 * bodge
127 start_t += 0.5 * bodge
131 tilt = atan(start_mu)
132 tilt_basis = np.array([
134 [ 0, cos(tilt), -sin(tilt), 0 ],
135 [ 0, sin(tilt), cos(tilt), 0 ],
138 findcurve_basis = matmatmultiply(dPQplane_basis, tilt_basis)
139 findcurve_into = np.linalg.inv(findcurve_basis)
141 q_findcurve = augmatmultiply(findcurve_into, q)
142 dq_findcurve = augmatmultiply(findcurve_into, dq, augwith=0)
144 findcurve_target = np.hstack((q_findcurve, dq_findcurve))
145 findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
146 start_mu, start_gamma, start_kappa)
148 findcurve_epsilon = dist_pq_plane * 0.01
150 global findcurve_subproc
151 if findcurve_subproc is None:
152 dbg('STARTING FINDCURVE')
153 findcurve_subproc = subprocess.Popen(
156 stdin=subprocess.PIPE,
157 stdout=subprocess.PIPE,
160 # restore_signals=True, // want python2 compat, nnng
161 universal_newlines=True,
164 findcurve_input = np.hstack((findcurve_target,
166 [findcurve_epsilon]))
168 dbg(('RUNNING FINDCURVE ' +
169 ' target Q=[%5.2f %5.2f %5.2f] dQ=[%5.2f %5.2f %5.2f]')
171 tuple(findcurve_input[0:6]))
172 dbg(('s=%5.2f t=%5.2f la=%5.2f mu=%5.2f ga=%5.2f ka=%5.2f initial')
174 (( findcurve_input[6]**2, findcurve_input[7]**2 ) +
175 tuple(findcurve_input[8:12])))
177 print(*findcurve_input, file=findcurve_subproc.stdin)
178 findcurve_subproc.stdin.flush()
180 hc.func = symbolic.get_python()
183 l = findcurve_subproc.stdout.readline()
186 if not l: vdbg().crashing('findcurve EOF')
190 dbg(('s=%5.2f t=%5.2f la=%5.2f mu=%5.2f ga=%5.2f ka=%5.2f' +
191 ' Q=[%5.2f %5.2f %5.2f] dQ=[%5.2f %5.2f %5.2f]')
193 (( l[0]**2, l[1]**2 ) + tuple(l[2:12])))
195 hc.findcurve_result = l[0:6]
196 hc.threshold = l[0]**2
197 hc.total_dist = hc.threshold + l[1]**2
198 vdbg().curve( hc.point_at_t )
200 def point_at_t(hc, normalised_parameter):
201 dist = normalised_parameter * hc.total_dist
202 ours = list(hc.findcurve_result)
203 if dist <= hc.threshold:
207 ours[1] = sqrt(dist - hc.threshold)
208 asmat = hc.func(*ours)