2 from __future__ import print_function
5 from numpy import cos, sin
12 from moedebug import *
16 from math import atan2, atan, sqrt
20 findcurve_subprocs = { }
22 class OptimisedCurve():
23 def __init__(oc, cp, nt):
24 db = DiscreteBezier(cp, nt, bezier_constructor=BezierSegment)
26 fc_input = map(db.point_at_it, range(0, nt+1))
29 for end in (False,True):
31 fi = nt-1 if end else 1
32 cp0i = 3 if end else 0
33 cp1i = 2 if end else 1
34 e = np.array(cp[cp0i])
35 ef_dirn = unit_v(cp[cp1i] - cp[cp0i])
36 ef_len = np.linalg.norm(np.array(fc_input[fi]) - np.array(fc_input[ei]))
37 f = e + ef_dirn * ef_len
38 dbg(repr((end, e,f, ef_dirn, ef_len)))
44 findcurve_epsilon = 0.01
47 subproc = findcurve_subprocs[nt]
49 cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
50 dbg('STARTING FINDCURVE %s' % cl)
51 subproc = subprocess.Popen(
54 stdin=subprocess.PIPE,
55 stdout=subprocess.PIPE,
58 # restore_signals=True, // want python2 compat, nnng
59 universal_newlines=True,
61 findcurve_subprocs[nt] = subproc
63 dbg('RUNNING FINDCURVE')
65 fc_input = np.hstack(fc_input)
66 s = ' '.join(map(str, fc_input))
70 print(s, file=subproc.stdin)
76 l = subproc.stdout.readline()
79 if not l: vdbg().crashing('findcurve EOF')
80 if not l.startswith('['):
88 dbg('[%s] %s' % (l, commentary))
94 oc._result = np.reshape(findcurve_result, (-1,3), 'C')
97 vdbg().curve( oc.point_at_t )
99 def point_at_it(oc, it):
101 return oc._result[it]
103 def point_at_t(oc, t):
105 it0 = int(math.floor(itd))
106 it1 = int(math.ceil(itd))
107 p0 = oc.point_at_it(it0)
108 p1 = oc.point_at_it(it1)
109 return p0 + (p1-p0) * (itd-it0)