2 from __future__ import print_function
5 from numpy import cos, sin
12 from moedebug import *
15 from schequeue import ScheduledTask
17 from math import atan2, atan, sqrt
21 class OptimisedCurve():
25 dbg('%s %s' % (oc._desc, s))
27 def __init__(oc, cp, nt):
28 oc._desc = 'OC#%04d' % OptimisedCurve.counter
29 OptimisedCurve.counter += 1
31 oc._dbg('cp= ' + ' '.join(map(vec2dbg, cp)))
33 db = DiscreteBezier(cp, nt, bezier_constructor=BezierSegment)
35 fc_input = map(db.point_at_it, range(0, nt+1))
36 oc._dbg(repr(fc_input))
38 for end in (False,True):
40 fi = nt-1 if end else 1
41 cp0i = 3 if end else 0
42 cp1i = 2 if end else 1
43 e = np.array(cp[cp0i])
44 ef_dirn = unit_v(cp[cp1i] - cp[cp0i])
45 ef_len = np.linalg.norm(np.array(fc_input[fi]) - np.array(fc_input[ei]))
46 f = e + ef_dirn * ef_len
47 oc._dbg(repr((end, e,f, ef_dirn, ef_len)))
51 oc._dbg(repr(fc_input))
53 findcurve_epsilon = 0.01
55 oc.sched = ScheduledTask(oc._await_subproc, oc._desc)
58 cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
59 oc._dbg('STARTING FINDCURVE %s' % cl)
60 subproc = subprocess.Popen(
63 stdin=subprocess.PIPE,
64 stdout=subprocess.PIPE,
67 # restore_signals=True, // want python2 compat, nnng
68 universal_newlines=True,
71 oc._dbg('RUNNING FINDCURVE')
73 fc_input = np.hstack(fc_input)
74 s = ' '.join(map(str, fc_input))
76 oc._dbg(('>> %s' % s))
78 print(s, file=subproc.stdin)
87 def _await_subproc(oc):
89 if subproc is None: return
95 l = subproc.stdout.readline()
97 oc._dbg('findcurve EOF')
98 vdbg().crashing('findcurve EOF')
101 if not l.startswith('['):
109 oc._dbg('[%s] %s' % (l, commentary))
114 subproc.stdin.close()
116 assert(subproc.returncode == 0)
121 oc._result = np.reshape(findcurve_result, (-1,3), 'C')
122 oc._dbg(repr(oc._result))
124 #vdbg().curve( oc.point_at_t )
126 def point_at_it(oc, it):
129 return oc._result[it]
131 def point_at_t(oc, t):
133 it0 = int(math.floor(itd))
134 it1 = int(math.ceil(itd))
135 p0 = oc.point_at_it(it0)
136 p1 = oc.point_at_it(it1)
137 return p0 + (p1-p0) * (itd-it0)