chiark / gitweb /
curveopt: fix handling of empty lines from findcurve
[moebius3.git] / curveopt.py
index 5ebe1661c5f9f35c06dbde6fa5c584a4ce8f9e81..00019762d8f20a01b1c5b93378fa544c8641c16f 100644 (file)
@@ -17,63 +17,79 @@ from math import atan2, atan, sqrt
 
 import symbolic
 
-findcurve_subprocs = { }
-
 class OptimisedCurve():
+  counter = 0
+
+  def _dbg(oc, s):
+    dbg('OC#%04d %s' % (oc._counter, s))
+
   def __init__(oc, cp, nt):
+    oc._counter = OptimisedCurve.counter
+    OptimisedCurve.counter += 1
+    oc._dbg('cp= ' + ' '.join(map(vec2dbg, cp)))
+
     db = DiscreteBezier(cp, nt, bezier_constructor=BezierSegment)
 
     fc_input = map(db.point_at_it, range(0, nt+1))
-    dbg(repr(fc_input))
+    oc._dbg(repr(fc_input))
 
     for end in (False,True):
       ei = nt if end else 0
       fi = nt-1 if end else 1
-      cp0i = 0 if end else 3
-      cp1i = 1 if end else 2
+      cp0i = 3 if end else 0
+      cp1i = 2 if end else 1
       e = np.array(cp[cp0i])
       ef_dirn = unit_v(cp[cp1i] - cp[cp0i])
       ef_len = np.linalg.norm(np.array(fc_input[fi]) - np.array(fc_input[ei]))
       f = e + ef_dirn * ef_len
+      oc._dbg(repr((end, e,f, ef_dirn, ef_len)))
       fc_input[ei] = e
       fc_input[fi] = f
 
+    oc._dbg(repr(fc_input))
+
     findcurve_epsilon = 0.01
 
-    try:
-      subproc = findcurve_subprocs[nt]
-    except KeyError:
-      cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
-      dbg('STARTING FINDCURVE %s' % cl)
-      subproc = subprocess.Popen(
-        cl,
-        bufsize=1,
-        stdin=subprocess.PIPE,
-        stdout=subprocess.PIPE,
-        stderr=None,
-        close_fds=False,
-        # restore_signals=True, // want python2 compat, nnng
-        universal_newlines=True,
-      )
-      findcurve_subprocs[nt] = subproc
-
-    dbg('RUNNING FINDCURVE')
+    cl = ['./findcurve', '%d' % (nt+1), '%.18g' % findcurve_epsilon]
+    oc._dbg('STARTING FINDCURVE %s' % cl)
+    subproc = subprocess.Popen(
+      cl,
+      bufsize=1,
+      stdin=subprocess.PIPE,
+      stdout=subprocess.PIPE,
+      stderr=None,
+      close_fds=False,
+      # restore_signals=True, // want python2 compat, nnng
+      universal_newlines=True,
+    )
+
+    oc._dbg('RUNNING FINDCURVE')
 
     fc_input = np.hstack(fc_input)
     s = ' '.join(map(str, fc_input))
 
-    dbg(('>> %s' % s))
+    oc._dbg(('>> %s' % s))
 
     print(s, file=subproc.stdin)
     subproc.stdin.flush()
 
+    oc.subproc = subproc
+    oc.nt = nt
+
+  def _await_subproc(oc):
+    subproc = oc.subproc
+    if subproc is None: return
+
+    oc._dbg('(awaiting)')
     commentary = ''
 
     while True:
       l = subproc.stdout.readline()
+      if not l:
+        oc._dbg('findcurve EOF')
+        vdbg().crashing('findcurve EOF')
       l = l.rstrip()
-      dbg('<< ', l)
-      if not l: vdbg().crashing('findcurve EOF')
+      oc._dbg('<< ' + l)
       if not l.startswith('['):
         commentary += ' '
         commentary += l
@@ -82,19 +98,24 @@ class OptimisedCurve():
       l = eval(l)
       if not l: break
 
-      dbg('[%s] %s' % (l, commentary))
+      oc._dbg('[%s] %s' % (l, commentary))
       commentary = ''
 
       findcurve_result = l
 
-    oc.nt = nt
+    subproc.stdin.close()
+    subproc.wait()
+    assert(subproc.returncode == 0)
+    oc.subproc = None
+
     oc._result = np.reshape(findcurve_result, (-1,3), 'C')
-    dbg(repr(oc._result))
+    oc._dbg(repr(oc._result))
 
-    vdbg().curve( oc.point_at_t )
+    #vdbg().curve( oc.point_at_t )
 
   def point_at_it(oc, it):
-    dbg(repr((it,)))
+    oc._await_subproc()
+    oc._dbg(repr((it,)))
     return oc._result[it]
 
   def point_at_t(oc, t):