Example #1
0
def plot_path(segs, nodes, tol = 1.0, show_cpts = False):
    if show_cpts:
        cpts = []
    j = 0
    cmd = 'moveto'
    for i in range(len(segs)):
        i1 = (i + 1) % len(nodes)
        n0 = nodes[i]
        n1 = nodes[i1]
        x0, y0, t0 = n0.x, n0.y, n0.ty
        x1, y1, t1 = n1.x, n1.y, n1.ty
        ks = segs[i].ks
        abs_ks = abs(ks[0]) + abs(ks[1] / 2) + abs(ks[2] / 8) + abs(ks[3] / 48)
        n_subdiv = int(ceil(.001 + tol * abs_ks))
        n_subhalf = (n_subdiv + 1) / 2
        if n_subdiv > 1:
            n_subdiv = n_subhalf * 2
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, n_subhalf)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, n_subhalf)
        mside.reverse()
        for j in range(len(mside)):
            mside[j] = (-mside[j][0], -mside[j][1])
        if n_subdiv > 1:
            pts = mside + pside[1:]
        else:
            pts = mside[:1] + pside[1:]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        rot = chord_th - atan2(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0])
        scale = chord_len / hypot(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0])
        u, v = scale * cos(rot), scale * sin(rot)
        xt = x0 - u * pts[0][0] + v * pts[0][1]
        yt = y0 - u * pts[0][1] - v * pts[0][0]
        s = -.5
        for x, y in pts:
            xp, yp = xt + u * x - v * y, yt + u * y + v * x
            thp = (((ks[3]/24 * s + ks[2]/6) * s + ks[1] / 2) * s + ks[0]) * s + rot
            up, vp = scale / (1.5 * n_subdiv) * cos(thp), scale / (1.5 * n_subdiv) * sin(thp)
            if s == -.5:
                if cmd == 'moveto':
                    print xp, yp, 'moveto'
                    cmd = 'curveto'
            else:
                if show_cpts:
                    cpts.append((xlast + ulast, ylast + vlast))
                    cpts.append((xp - up, yp - vp))
                print xlast + ulast, ylast + vlast, xp - up, yp - vp, xp, yp, 'curveto'
            xlast, ylast, ulast, vlast = xp, yp, up, vp
            s += 1. / n_subdiv
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print 'stroke'
    if show_cpts:
        for x, y in cpts:
            print 'gsave 0 0 1 setrgbcolor', x, y, 'translate circle fill grestore'
Example #2
0
def make_error_vec(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    n = len(path)
    v = zeros(n * 2, Float)
    j = 0
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th), k0 * chord_len,
                                k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        l = chord_len / hypot(pside[-1][0] + mside[-1][0],
                              pside[-1][1] + mside[-1][1])
        k1l = (ks[1] - .5 * ks[2] + .125 * ks[3]) / (l * l)
        k1r = (ks[1] + .5 * ks[2] + .125 * ks[3]) / (l * l)
        k2l = (ks[2] - .5 * ks[3]) / (l * l * l)
        k2r = (ks[2] + .5 * ks[3]) / (l * l * l)

        if t0 == 'o' or t0 == '[' or t0 == 'v':
            v[2 * j] -= k1l
            v[2 * j + 1] -= k2l
        elif t0 == 'c':
            v[2 * j + 1] += k2l

        if t1 == 'o' or t1 == ']' or t1 == 'v':
            v[2 * j1] += k1r
            v[2 * j1 + 1] += k2r
        elif t1 == 'c':
            v[2 * j1] += k2r

        print "% left k'", k1l, "k''", k2l, "right k'", k1r, "k''", k2r
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print '% error vector:'
    for i in range(n):
        print '%  ', v[2 * i], v[2 * i + 1]
    return v
Example #3
0
def make_error_vec(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    n = len(path)
    v = zeros(n * 2, Float)
    j = 0
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th),
                                k0 * chord_len, k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        l = chord_len / hypot(pside[-1][0] + mside[-1][0], pside[-1][1] + mside[-1][1])
        k1l = (ks[1] - .5 * ks[2] + .125 * ks[3]) / (l * l)
        k1r = (ks[1] + .5 * ks[2] + .125 * ks[3]) / (l * l)
        k2l = (ks[2] - .5 * ks[3]) / (l * l * l)
        k2r = (ks[2] + .5 * ks[3]) / (l * l * l)

        if t0 == 'o' or t0 == '[' or t0 == 'v':
            v[2 * j] -= k1l
            v[2 * j + 1] -= k2l
        elif t0 == 'c':
            v[2 * j + 1] += k2l

        if t1 == 'o' or t1 == ']' or t1 == 'v':
            v[2 * j1] += k1r
            v[2 * j1 + 1] += k2r
        elif t1 == 'c':
            v[2 * j1] += k2r

        print "% left k'", k1l, "k''", k2l, "right k'", k1r, "k''", k2r
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print '% error vector:'
    for i in range(n):
        print '%  ', v[2 * i], v[2 * i + 1]
    return v
Example #4
0
def plot_path(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    j = 0
    cmd = 'moveto'
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th), k0 * chord_len,
                                k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        mside.reverse()
        for i in range(len(mside)):
            mside[i] = (-mside[i][0], -mside[i][1])
        pts = mside + pside[1:]
        rot = chord_th - atan2(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0])
        scale = hypot(x1 - x0, y1 - y0) / hypot(pts[-1][1] - pts[0][1],
                                                pts[-1][0] - pts[0][0])
        u, v = scale * cos(rot), scale * sin(rot)
        xt = x0 - u * pts[0][0] + v * pts[0][1]
        yt = y0 - u * pts[0][1] - v * pts[0][0]
        for x, y in pts:
            print xt + u * x - v * y, yt + u * y + v * x, cmd
            cmd = 'lineto'
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print 'stroke'
    if 0:
        j = 0
        for i in range(len(path)):
            x0, y0, t0 = path[i]
            print 'gsave', x0, y0, 'translate'
            print ' ', th[j] * 180 / pi, 'rotate'
            print '  -50 0 moveto 50 0 lineto stroke'
            print 'grestore'
            j += 1
Example #5
0
def plot_path(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    j = 0
    cmd = 'moveto'
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th),
                                k0 * chord_len, k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        mside.reverse()
        for i in range(len(mside)):
            mside[i] = (-mside[i][0], -mside[i][1])
        pts = mside + pside[1:]
        rot = chord_th - atan2(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0])
        scale = hypot(x1 - x0, y1 - y0) / hypot(pts[-1][1] - pts[0][1], pts[-1][0] - pts[0][0])
        u, v = scale * cos(rot), scale * sin(rot)
        xt = x0 - u * pts[0][0] + v * pts[0][1]
        yt = y0 - u * pts[0][1] - v * pts[0][0]
        for x, y in pts:
            print xt + u * x - v * y, yt + u * y + v * x, cmd
            cmd = 'lineto'
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print 'stroke'
    if 0:
        j = 0
        for i in range(len(path)):
            x0, y0, t0 = path[i]
            print 'gsave', x0, y0, 'translate'
            print ' ', th[j] * 180 / pi, 'rotate'
            print '  -50 0 moveto 50 0 lineto stroke'
            print 'grestore'
            j += 1
Example #6
0
def plot_ks(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    j = 0
    cmd = 'moveto'
    xo = 36
    yo = 550
    xscale = .5
    yscale = 2000
    x = xo
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th), k0 * chord_len,
                                k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        print '%', x0, y0, k0, k1
        print "% ks = ", ks
        l = 2 * chord_len / hypot(pside[-1][0] + mside[-1][0],
                                  pside[-1][1] + mside[-1][1])
        for i in range(100):
            s = .01 * i - 0.5
            kp = poly3.eval_cubic(ks[0], ks[1], .5 * ks[2], 1. / 6 * ks[3], s)
            kp /= l
            print x + xscale * l * (s + .5), yo + yscale * kp, cmd
            cmd = 'lineto'
        x += xscale * l
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print 'stroke'
    print xo, yo, 'moveto', x, yo, 'lineto stroke'
Example #7
0
def make_matrix(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    n = len(path)
    m = zeros((n * 2, n * 2), Float)
    j = 0
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th), k0 * chord_len,
                                k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        l = chord_len / hypot(pside[-1][0] + mside[-1][0],
                              pside[-1][1] + mside[-1][1])

        if t0 == 'o' or t0 == '[' or t0 == 'v':
            add_k1l(m, 2 * j, j, j1, l, -1)
            add_k2l(m, 2 * j + 1, j, j1, l, -1)
        elif t0 == 'c':
            add_k2l(m, 2 * j + 1, j, j1, l, 1)

        if t1 == 'o' or t1 == ']' or t1 == 'v':
            add_k1r(m, 2 * j1, j, j1, l, 1)
            add_k2r(m, 2 * j1 + 1, j, j1, l, 1)
        elif t1 == 'c':
            add_k2r(m, 2 * j1, j, j1, l, 1)

        if t1 == 'v':
            j += 2
        else:
            j += 1
    return m
Example #8
0
def plot_ks(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    j = 0
    cmd = 'moveto'
    xo = 36
    yo = 550
    xscale = .5
    yscale = 2000
    x = xo
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th),
                                k0 * chord_len, k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        print '%', x0, y0, k0, k1
        print "% ks = ", ks
        l = 2 * chord_len / hypot(pside[-1][0] + mside[-1][0], pside[-1][1] + mside[-1][1])
        for i in range(100):
            s = .01 * i - 0.5
            kp = poly3.eval_cubic(ks[0], ks[1], .5 * ks[2], 1./6 * ks[3], s)
            kp /= l
            print x + xscale * l * (s + .5), yo + yscale * kp, cmd
            cmd = 'lineto'
        x += xscale * l
        if t1 == 'v':
            j += 2
        else:
            j += 1
    print 'stroke'
    print xo, yo, 'moveto', x, yo, 'lineto stroke'
Example #9
0
def make_matrix(path, th, k):
    if path[0][2] == '{': closed = 0
    else: closed = 1
    n = len(path)
    m = zeros((n * 2, n * 2), Float)
    j = 0
    for i in range(len(path) + closed - 1):
        i1 = (i + 1) % len(path)
        x0, y0, t0 = path[i]
        x1, y1, t1 = path[i1]
        j1 = (j + 1) % len(th)
        th0 = th[j]
        k0 = k[j]
        th1 = th[j1]
        k1 = k[j1]
        chord_th = atan2(y1 - y0, x1 - x0)
        chord_len = hypot(y1 - y0, x1 - x0)
        ks = poly3.solve_3spiro(mod_2pi(chord_th - th0),
                                mod_2pi(th1 - chord_th),
                                k0 * chord_len, k1 * chord_len)
        ksp = (ks[0] * .5, ks[1] * .25, ks[2] * .125, ks[3] * .0625)
        pside = poly3.int_3spiro_poly(ksp, 64)
        ksm = (ks[0] * -.5, ks[1] * .25, ks[2] * -.125, ks[3] * .0625)
        mside = poly3.int_3spiro_poly(ksm, 64)
        l = chord_len / hypot(pside[-1][0] + mside[-1][0], pside[-1][1] + mside[-1][1])

        if t0 == 'o' or t0 == '[' or t0 == 'v':
            add_k1l(m, 2 * j, j, j1, l, -1)
            add_k2l(m, 2 * j + 1, j, j1, l, -1)
        elif t0 == 'c':
            add_k2l(m, 2 * j + 1, j, j1, l, 1)

        if t1 == 'o' or t1 == ']' or t1 == 'v':
            add_k1r(m, 2 * j1, j, j1, l, 1)
            add_k2r(m, 2 * j1 + 1, j, j1, l, 1)
        elif t1 == 'c':
            add_k2r(m, 2 * j1, j, j1, l, 1)

        if t1 == 'v':
            j += 2
        else:
            j += 1
    return m