2 from __future__ import print_function
5 from numpy import cos, sin
8 from moedebug import dbg
11 from math import atan2
15 def augment(v): return np.append(v, 1)
16 def augment0(v): return np.append(v, 0)
17 def unaugment(v): return v[0:3]
19 findcurve_subproc = None
21 class HelixishCurve():
27 dp = unit_v(cp[1]-cp[0])
28 dq = unit_v(cp[3]-cp[2])
30 dbg('HelixishCurve __init__', cp)
33 # - solve in the plane containing dP and dQ
34 # - total distance normal to that plane gives mu
35 # - now resulting curve is not parallel to dP at P
36 # nor dQ at Q, so tilt it
37 # - [[ pick as the hinge point the half of the curve
38 # with the larger s or t ]] not yet implemented
39 # - increase the other distance {t,s} by a bodge factor
40 # approx distance between {Q,P} and {Q,P}' due to hinging
41 # but minimum is 10% of (wlog) {s,t} [[ not quite like this ]]
43 dPQplane_normal = np.cross(dp, dq)
44 if (np.linalg.norm(dPQplane_normal) < 1E6):
45 dPQplane_normal += [0, 0, 1E5]
46 dPQplane_normal = unit_v(dPQplane_normal)
48 dPQplane_basis = np.column_stack((np.cross(dp, dPQplane_normal),
52 dPQplane_basis = np.vstack((dPQplane_basis, [0,0,0,1]))
53 dPQplane_into = np.linalg.inv(dPQplane_basis)
55 dp_plane = unaugment(dPQplane_into * augment0(dp))
56 dq_plane = unaugment(dPQplane_into * augment0(dq))
57 q_plane = unaugment(dPQplane_into * augment(q))
58 dist_pq_plane = np.linalg.norm(q_plane)
60 # two circular arcs of equal maximum possible radius
61 # algorithm courtesy of Simon Tatham (`Railway problem',
62 # pers.comm. to ijackson@chiark 23.1.2004)
63 railway_angleoffset = atan2(*q_plane[0:2])
64 railway_theta = tau/4 - railway_angleoffset
65 railway_phi = atan2(*dq_plane[0:2]) - railway_angleoffset
66 railway_cos_theta = cos(railway_theta)
67 railway_cos_phi = cos(railway_phi)
68 if railway_cos_theta**2 + railway_cos_phi**2 > 1E6:
69 railway_roots = np.roots([
70 2 * (1 + cos(railway_theta - railway_phi)),
71 2 * (railway_cos_theta - railway_cos_phi),
74 for railway_r in railway_roots:
75 def railway_CPQ(pq, dpq):
77 return pq + railway_r * [-dpq[1], dpq[0]]
79 railway_CP = railway_CPQ([0,0,0], dp_plane)
80 railway_QP = railway_CPQ(q_plane[0:2], -dq_plane)
81 railway_midpt = 0.5 * (railway_CP + railway_QP)
84 def railway_ST(C, start, end):
86 delta = atan2(*(end - C)[0:2]) - atan2(start - C)[0:2]
89 try_s = railway_ST(railway_CP, [0,0], midpt)
90 try_t = railway_ST(railway_CP, midpt, q_plane)
91 try_st = try_s + try_t
92 if best_st is None or try_st < best_st:
97 start_mu = q_plane[2] / (start_s + start_t)
99 else: # twoarcs algorithm is not well defined
101 start_s = dist_pq_plane * .65
102 start_t = dist_pq_plane * .35
105 bodge = max( q_plane[2] * mu,
106 (start_s + start_t) * 0.1 )
107 start_s += 0.5 * bodge
108 start_t += 0.5 * bodge
113 tilt_basis = np.array([
115 0, cos(tilt), -sin(tilt), 0,
116 0, sin(tilt), cos(tilt), 0,
119 findcurve_basis = dPQplane_basis * tilt_basis
120 findcurve_into = np.linalg.inv(findcurve_basis)
122 q_findcurve = unaugment(findcurve_into, augment(q))
123 dq_findcurve = unaugment(findcurve_into, augment0(dq))
125 findcurve_target = np.concatenate(q_findcurve, dq_findcurve)
126 findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
127 start_mu, start_gamma, start_kappa)
129 findcurve_epsilon = dist_pq_plane * 0.01
131 if findcurve_subproc is None:
132 findcurve_subproc = subprocess.Popen(
135 stdin=subprocess.PIPE,
136 stdout=subprocess.PIPE,
139 restore_signals=True,
140 universal_newlines=True,
143 findcurve_input = np.hstack((findcurve_target,
145 [findcurve_epsilon]))
146 dbg('RUNNING FINDCURVE', *findcurve_input)
147 print(findcurve_subproc.stdin, *findcurve_input)
148 findcurve_subproc.stdin.flush()
151 l = findcurve_subproc.stdout.readline()
157 hc.findcurve_result = l[0:5]
158 hc.func = symbolic.get_python(something)
159 hc.threshold = l[0]**2
160 hc.total_dist = hc.threshold + l[1]**2
162 def point_at_t(hc, normalised_parameter):
163 dist = normalised_parameter * hc.total_dist
164 ours = [p for p in findcurve_result]
165 if dist <= hc.threshold:
169 ours[1] = sqrt(dist - hc.threshold)
170 return hc.func(*ours)