2 from __future__ import print_function
5 from numpy import cos, sin
12 from moedebug import *
16 from math import atan2, atan, sqrt
20 class OptimisedCurve():
24 dbg('OC#%04d %s' % (oc._counter, s))
26 def __init__(oc, cp, nt):
27 oc._counter = OptimisedCurve.counter
28 OptimisedCurve.counter += 1
29 oc._dbg('cp= ' + ' '.join(map(vec2dbg, cp)))
31 db = DiscreteBezier(cp, nt, bezier_constructor=BezierSegment)
33 fc_input = map(db.point_at_it, range(0, nt+1))
34 oc._dbg(repr(fc_input))
36 for end in (False,True):
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)))
49 oc._dbg(repr(fc_input))
51 findcurve_epsilon = 0.01
53 cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
54 oc._dbg('STARTING FINDCURVE %s' % cl)
55 subproc = subprocess.Popen(
58 stdin=subprocess.PIPE,
59 stdout=subprocess.PIPE,
62 # restore_signals=True, // want python2 compat, nnng
63 universal_newlines=True,
66 oc._dbg('RUNNING FINDCURVE')
68 fc_input = np.hstack(fc_input)
69 s = ' '.join(map(str, fc_input))
71 oc._dbg(('>> %s' % s))
73 print(s, file=subproc.stdin)
79 def _await_subproc(oc):
81 if subproc is None: return
87 l = subproc.stdout.readline()
89 oc._dbg('findcurve EOF')
90 vdbg().crashing('findcurve EOF')
93 if not l.startswith('['):
101 oc._dbg('[%s] %s' % (l, commentary))
106 subproc.stdin.close()
108 assert(subproc.returncode == 0)
111 oc._result = np.reshape(findcurve_result, (-1,3), 'C')
112 oc._dbg(repr(oc._result))
114 #vdbg().curve( oc.point_at_t )
116 def point_at_it(oc, it):
119 return oc._result[it]
121 def point_at_t(oc, t):
123 it0 = int(math.floor(itd))
124 it1 = int(math.ceil(itd))
125 p0 = oc.point_at_it(it0)
126 p1 = oc.point_at_it(it1)
127 return p0 + (p1-p0) * (itd-it0)