chiark / gitweb /
helixish: wip
[moebius3.git] / helixish.py
1
2 from __future__ import print_function
3
4 import numpy as np
5 from numpy import cos, sin
6
7 import sys
8 from moebdebug import dbg
9
10 def augment(v): return np.append(v, 1)
11 def augment0(v): return np.append(v, 0)
12 def unaugment(v): return v[0:3]
13
14 findcurve_subproc = None
15
16 class HelixishCurve():
17   def __init__(hc, cp):
18     p = cp[0]
19     q = cp[3]
20     dp = unit_v(cp[1]-cp[0])
21     dq = unit_v(cp[3]-cp[2])
22
23     dbg('HelixishCurve __init__', cp)
24
25     # the initial attempt
26     #   - solve in the plane containing dP and dQ
27     #   - total distance normal to that plane gives mu
28     #   - now resulting curve is not parallel to dP at P
29     #     nor dQ at Q, so tilt it
30     #   - [[ pick as the hinge point the half of the curve
31     #     with the larger s or t ]] not yet implemented
32     #   - increase the other distance {t,s} by a bodge factor
33     #     approx distance between {Q,P} and {Q,P}' due to hinging
34     #     but minimum is 10% of (wlog) {s,t} [[ not quite like this ]]
35
36     dPQplane_normal = np.cross(dp, dq)
37     if (np.norm(dPQplane_normal) < 1E6):
38       dPQplane_normal += [0, 0, 1E5]
39     dPQplane_normal = unit_v(dPQplane_normal)
40
41     dPQplane_basis = np.column_stack(np.cross(dp, dPQplane_normal),
42                                      dp,
43                                      dPQplane_normal,
44                                      p);
45     dPQplane_basis = np.vstack(dPQplane_basis, [0,0,0,1])
46     dPQplane_into = np.linalg.inv(dPQplane_basis)
47
48     dp_plane = unaugment(dPQplane_into * augment0(dp))
49     dq_plane = unaugment(dPQplane_into * augment0(dq))
50     q_plane  = unaugment(dPQplane_into * augment(q))
51     dist_pq_plane = np.linalg.norm(q_plane)
52
53     # two circular arcs of equal maximum possible radius
54     # algorithm courtesy of Simon Tatham (`Railway problem',
55     # pers.comm. to ijackson@chiark 23.1.2004)
56     railway_angleoffset = atan2(*q_plane[0:1])
57     railway_theta =                      tau/4 - railway_angleoffset
58     railway_phi   = atan2(*dq_plane[0:1]) - railway_angleoffset
59     railway_cos_theta = cos(railway_theta)
60     railway_cos_phi   = cos(railway_phi)
61     if railway_cos_theta**2 + railway_cos_phi**2 > 1E6:
62       railway_roots = np.roots([
63         2 * (1 + cos(railway_theta - railway_phi)),
64         2 * (railway_cos_theta - railway_cos_phi),
65         -1
66         ])
67       for railway_r in railway_roots:
68         def railway_CPQ(pq, dpq):
69           nonlocal railway_r
70           return pq + railway_r * [-dpq[1], dpq[0]]
71
72         railway_CP = railway_CPQ([0,0,0],       dp_plane)
73         railway_QP = railway_CPQ(q_plane[0:2], -dq_plane)
74         railway_midpt = 0.5 * (railway_CP + railway_QP)
75
76         best_st = None
77         def railway_ST(C, start, end):
78           nonlocal railway_r
79           delta = atan2(*(end - C)[0:2]) - atan2(start - C)[0:2]
80           s = delta * railway_r
81
82         try_s = railway_ST(railway_CP, [0,0], midpt)
83         try_t = railway_ST(railway_CP, midpt, q_plane)
84         try_st = try_s + try_t
85         if best_st is None or try_st < best_st:
86           start_la = 1/r
87           start_s = try_s
88           start_t = try_t
89           best_st = try_st
90           start_mu = q_plane[2] / (start_s + start_t)
91
92     else: # twoarcs algorithm is not well defined
93       start_la = 0.1
94       start_s = dist_pq_plane * .65
95       start_t = dist_pq_plane * .35
96       start_mu = 0.05
97
98     bodge = max( q_plane[2] * mu,
99                  (start_s + start_t) * 0.1 )
100     start_s += 0.5 * bodge
101     start_t += 0.5 * bodge
102     start_kappa = 0
103     start_gamma = 1
104
105     tilt = atan(mu)
106     tilt_basis = np.array([
107       1,     0,           0,         0,
108       0,   cos(tilt), -sin(tilt),    0,
109       0,   sin(tilt),  cos(tilt),    0,
110       0,     0,           0,         1,
111     ])
112     findcurve_basis = dPQplane_basis * tilt_basis
113     findcurve_into = np.linalg.inv(findcurve_basis)
114
115     q_findcurve = unaugment(findcurve_into, augment(q))
116     dq_findcurve = unaugment(findcurve_into, augment0(dq))
117
118     findcurve_target = np.concatenate(q_findcurve, dq_findcurve)
119     findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
120                        start_mu, start_gamma, start_kappa)
121     
122     findcurve_epsilon = dist_pq_plane * 0.01
123
124     if findcurve_subproc is None:
125       findcurve_subproc = subprocess.Popen(
126         ['./findcurve'],
127         bufsize=1,
128         stdin=subprocess.PIPE,
129         stdout=subprocess.PIPE,
130         stderr=None,
131         close_fds=False,
132         restore_signals=True,
133         universal_newlines=True,
134       )
135
136     findcurve_input = np.hstack((findcurve_target,
137                                  findcurve_start,
138                                  [findcurve_epsilon])))
139     dbg('RUNNING FINDCURVE', *findcurve_input)
140     print(findcurve_subproc.stdin, *findcurve_input)
141     findcurve_subproc.stdin.flush()
142
143     while True:
144       l = findcurve_subproc.stdout.readline()
145       l = l.rstrip()
146       dbg('GOT ', l)
147       l = eval(l)
148       if l is None: break
149       findcurve_result = l[0:5]
150
151