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