chiark / gitweb /
simplex wip: use gsl_vector_get for X, for abandonment
[moebius3.git] / curveopt.py
1
2 from __future__ import print_function
3
4 import numpy as np
5 from numpy import cos, sin
6
7 import six
8 import sys
9 import subprocess
10 import math
11
12 from moedebug import *
13 from moenp import *
14 from moebez import *
15
16 from math import atan2, atan, sqrt
17
18 import symbolic
19
20 class OptimisedCurve():
21   counter = 0
22
23   def _dbg(oc, s):
24     dbg('OC#%04d %s' % (oc._counter, s))
25
26   def __init__(oc, cp, nt):
27     oc._counter = OptimisedCurve.counter
28     OptimisedCurve.counter += 1
29     oc._dbg('cp= ' + ' '.join(map(vec2dbg, cp)))
30
31     db = DiscreteBezier(cp, nt, bezier_constructor=BezierSegment)
32
33     fc_input = map(db.point_at_it, range(0, nt+1))
34     oc._dbg(repr(fc_input))
35
36     for end in (False,True):
37       ei = nt if end else 0
38       fi = nt-1 if end else 1
39       cp0i = 3 if end else 0
40       cp1i = 2 if end else 1
41       e = np.array(cp[cp0i])
42       ef_dirn = unit_v(cp[cp1i] - cp[cp0i])
43       ef_len = np.linalg.norm(np.array(fc_input[fi]) - np.array(fc_input[ei]))
44       f = e + ef_dirn * ef_len
45       oc._dbg(repr((end, e,f, ef_dirn, ef_len)))
46       fc_input[ei] = e
47       fc_input[fi] = f
48
49     oc._dbg(repr(fc_input))
50
51     findcurve_epsilon = 0.01
52
53     cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
54     oc._dbg('STARTING FINDCURVE %s' % cl)
55     subproc = subprocess.Popen(
56       cl,
57       bufsize=1,
58       stdin=subprocess.PIPE,
59       stdout=subprocess.PIPE,
60       stderr=None,
61       close_fds=False,
62       # restore_signals=True, // want python2 compat, nnng
63       universal_newlines=True,
64     )
65
66     oc._dbg('RUNNING FINDCURVE')
67
68     fc_input = np.hstack(fc_input)
69     s = ' '.join(map(str, fc_input))
70
71     oc._dbg(('>> %s' % s))
72
73     print(s, file=subproc.stdin)
74     subproc.stdin.flush()
75
76     oc.subproc = subproc
77     oc.nt = nt
78
79   def _await_subproc(oc):
80     subproc = oc.subproc
81     if subproc is None: return
82
83     oc._dbg('(awaiting)')
84     commentary = ''
85
86     while True:
87       l = subproc.stdout.readline()
88       l = l.rstrip()
89       oc._dbg('<< ' + l)
90       if not l: vdbg().crashing('findcurve EOF')
91       if not l.startswith('['):
92         commentary += ' '
93         commentary += l
94         continue
95
96       l = eval(l)
97       if not l: break
98
99       oc._dbg('[%s] %s' % (l, commentary))
100       commentary = ''
101
102       findcurve_result = l
103
104     subproc.stdin.close()
105     subproc.wait()
106     assert(subproc.returncode == 0)
107     oc.subproc = None
108
109     oc._result = np.reshape(findcurve_result, (-1,3), 'C')
110     oc._dbg(repr(oc._result))
111
112     #vdbg().curve( oc.point_at_t )
113
114   def point_at_it(oc, it):
115     oc._await_subproc()
116     oc._dbg(repr((it,)))
117     return oc._result[it]
118
119   def point_at_t(oc, t):
120     itd = t * oc.nt
121     it0 = int(math.floor(itd))
122     it1 = int(math.ceil(itd))
123     p0 = oc.point_at_it(it0)
124     p1 = oc.point_at_it(it1)
125     return p0 + (p1-p0) * (itd-it0)
126