def fit_cubic(z0, z1, arclen, th_fn, fast, aabmin=0, aabmax=1.): chord = hypot(z1[0] - z0[0], z1[1] - z0[1]) if arclen < 1.000001 * chord: return [z0, z1], 0 th0 = th_fn(0) th1 = th_fn(arclen) imax = 4 jmax = 10 if fast: imax = 1 jmax = 0 for i in range(imax): for j in range(jmax + 1): if jmax == 0: aab = 0.5 * (aabmin + aabmax) else: aab = aabmin + (aabmax - aabmin) * j / jmax if fast == 2: bz = fit_cubic_superfast(z0, z1, arclen, th0, th1, aab) else: bz = tocubic.fit_cubic_arclen(z0, z1, arclen, th0, th1, aab) score = tocubic.measure_bz_rk4(bz, arclen, th_fn) print('% aab =', aab, 'score =', score) sys.stdout.flush() if j == 0 or score < best_score: best_score = score best_aab = aab best_bz = bz daab = .06 * (aabmax - aabmin) aabmin = max(0, best_aab - daab) aabmax = min(1, best_aab + daab) print('%--- best_aab =', best_aab) return best_bz, best_score
def fit_cubic(z0, z1, arclen, th_fn, fast, aabmin = 0, aabmax = 1.): chord = hypot(z1[0] - z0[0], z1[1] - z0[1]) if (arclen < 1.000001 * chord): return [z0, z1], 0 th0 = th_fn(0) th1 = th_fn(arclen) imax = 4 jmax = 10 if fast: imax = 1 jmax = 0 for i in range(imax): for j in range(jmax + 1): if jmax == 0: aab = 0.5 * (aabmin + aabmax) else: aab = aabmin + (aabmax - aabmin) * j / jmax if fast == 2: bz = fit_cubic_superfast(z0, z1, arclen, th0, th1, aab) else: bz = tocubic.fit_cubic_arclen(z0, z1, arclen, th0, th1, aab) score = tocubic.measure_bz_rk4(bz, arclen, th_fn) print '% aab =', aab, 'score =', score sys.stdout.flush() if j == 0 or score < best_score: best_score = score best_aab = aab best_bz = bz daab = .06 * (aabmax - aabmin) aabmin = max(0, best_aab - daab) aabmax = min(1, best_aab + daab) print '%--- best_aab =', best_aab return best_bz, best_score
def measure_bz(curve, s0, s1, bz): bz_arclen = tocubic.bz_arclength_rk4(bz) if bz_arclen == 0: return 1e9 arclen_scale = (s1 - s0) / bz_arclen def th_fn(s): return curve.th(s0 + arclen_scale * s, s == 0) return tocubic.measure_bz_rk4(bz, bz_arclen, th_fn)