chiark / gitweb /
helixish: use @ for matrix multiplication
[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 moedebug import dbg
9 from moenp import *
10
11 from math import atan2
12
13 import symbolic
14
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]
18
19 findcurve_subproc = None
20
21 class HelixishCurve():
22   def __init__(hc, cp):
23     symbolic.calculate()
24
25     p = cp[0]
26     q = cp[3]
27     dp = unit_v(cp[1]-cp[0])
28     dq = unit_v(cp[3]-cp[2])
29
30     dbg('HelixishCurve __init__', cp)
31
32     # the initial attempt
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 ]]
42
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)
47
48     dPQplane_basis = np.column_stack((np.cross(dp, dPQplane_normal),
49                                       dp,
50                                       dPQplane_normal,
51                                       p));
52     dPQplane_basis = np.vstack((dPQplane_basis, [0,0,0,1]))
53     dPQplane_into = np.linalg.inv(dPQplane_basis)
54
55     dp_plane = unaugment(dPQplane_into @ augment0(dp))
56     dq_plane = unaugment(dPQplane_into @ augment0(dq))
57     q_plane  = dPQplane_into @ augment(q)
58     q_plane  = unaugment(dPQplane_into @ augment(q))
59     dist_pq_plane = np.linalg.norm(q_plane)
60
61     # two circular arcs of equal maximum possible radius
62     # algorithm courtesy of Simon Tatham (`Railway problem',
63     # pers.comm. to ijackson@chiark 23.1.2004)
64     railway_angleoffset = atan2(*q_plane[0:2])
65     railway_theta =                      tau/4 - railway_angleoffset
66     railway_phi   = atan2(*dq_plane[0:2]) - railway_angleoffset
67     railway_cos_theta = cos(railway_theta)
68     railway_cos_phi   = cos(railway_phi)
69     if railway_cos_theta**2 + railway_cos_phi**2 > 1E6:
70       railway_roots = np.roots([
71         2 * (1 + cos(railway_theta - railway_phi)),
72         2 * (railway_cos_theta - railway_cos_phi),
73         -1
74         ])
75       for railway_r in railway_roots:
76         def railway_CPQ(pq, dpq):
77           nonlocal railway_r
78           return pq + railway_r * [-dpq[1], dpq[0]]
79
80         railway_CP = railway_CPQ([0,0,0],       dp_plane)
81         railway_QP = railway_CPQ(q_plane[0:2], -dq_plane)
82         railway_midpt = 0.5 * (railway_CP + railway_QP)
83
84         best_st = None
85         def railway_ST(C, start, end):
86           nonlocal railway_r
87           delta = atan2(*(end - C)[0:2]) - atan2(start - C)[0:2]
88           s = delta * railway_r
89
90         try_s = railway_ST(railway_CP, [0,0], midpt)
91         try_t = railway_ST(railway_CP, midpt, q_plane)
92         try_st = try_s + try_t
93         if best_st is None or try_st < best_st:
94           start_la = 1/r
95           start_s = try_s
96           start_t = try_t
97           best_st = try_st
98           start_mu = q_plane[2] / (start_s + start_t)
99
100     else: # twoarcs algorithm is not well defined
101       start_la = 0.1
102       start_s = dist_pq_plane * .65
103       start_t = dist_pq_plane * .35
104       start_mu = 0.05
105
106     bodge = max( q_plane[2] * mu,
107                  (start_s + start_t) * 0.1 )
108     start_s += 0.5 * bodge
109     start_t += 0.5 * bodge
110     start_kappa = 0
111     start_gamma = 1
112
113     tilt = atan(mu)
114     tilt_basis = np.array([
115       1,     0,           0,         0,
116       0,   cos(tilt), -sin(tilt),    0,
117       0,   sin(tilt),  cos(tilt),    0,
118       0,     0,           0,         1,
119     ])
120     findcurve_basis = dPQplane_basis * tilt_basis
121     findcurve_into = np.linalg.inv(findcurve_basis)
122
123     q_findcurve = unaugment(findcurve_into, augment(q))
124     dq_findcurve = unaugment(findcurve_into, augment0(dq))
125
126     findcurve_target = np.concatenate(q_findcurve, dq_findcurve)
127     findcurve_start = (sqrt(start_s), sqrt(start_t), start_la,
128                        start_mu, start_gamma, start_kappa)
129     
130     findcurve_epsilon = dist_pq_plane * 0.01
131
132     if findcurve_subproc is None:
133       findcurve_subproc = subprocess.Popen(
134         ['./findcurve'],
135         bufsize=1,
136         stdin=subprocess.PIPE,
137         stdout=subprocess.PIPE,
138         stderr=None,
139         close_fds=False,
140         restore_signals=True,
141         universal_newlines=True,
142       )
143
144     findcurve_input = np.hstack((findcurve_target,
145                                  findcurve_start,
146                                  [findcurve_epsilon]))
147     dbg('RUNNING FINDCURVE', *findcurve_input)
148     print(findcurve_subproc.stdin, *findcurve_input)
149     findcurve_subproc.stdin.flush()
150
151     while True:
152       l = findcurve_subproc.stdout.readline()
153       l = l.rstrip()
154       dbg('GOT ', l)
155       l = eval(l)
156       if l is None: break
157
158     hc.findcurve_result = l[0:5]
159     hc.func = symbolic.get_python(something)
160     hc.threshold = l[0]**2
161     hc.total_dist = hc.threshold + l[1]**2
162
163   def point_at_t(hc, normalised_parameter):
164     dist = normalised_parameter * hc.total_dist
165     ours = [p for p in findcurve_result]
166     if dist <= hc.threshold:
167       ours[0] = sqrt(dist)
168       ours[1] = 0
169     else:
170       ours[1] = sqrt(dist - hc.threshold)
171     return hc.func(*ours)