Esempio n. 1
0
def flatten_beziers(svg_d):
    sp = parse_path(svg_d)
    spn = SVGPath()

    for seg in sp:
        if isinstance(seg, Line):
            spn.append(seg)
        elif isinstance(seg, CubicBezier):
            B = [seg.bpoints()]
            foo = np.dot(B, CUBIC_TO_POLY_SAMPLE)
            spn.extend([Line(x, y) for x, y in zip(foo[0, :-1], foo[0, 1:])])
        else:
            raise RuntimeError(f"unsupported {seg}")

    return spn.d()
Esempio n. 2
0
def order_paths(paths):
    total_length = sum([path.length() for path in paths])
    samples_per_path = [
        round(args.N_points * path.length() / total_length) for path in paths
    ]

    # Order the shapes in each path in paths
    for i, path in enumerate(paths):
        tmp = Path(path.pop(0))
        while len(path) > 1:
            curr_end = tmp[-1].end
            next_start = np.argmin(
                np.abs([next_path.start - curr_end for next_path in path]))
            tmp.append(path.pop(next_start))
        tmp.append(path[0])
        paths[i] = tmp

    # Sample points along the paths
    shape = [[
        path.point(i / samples_in_path) for i in range(samples_in_path)
        if samples_in_path > 0
    ] for path, samples_in_path in zip(paths, samples_per_path)]

    # Order the paths
    tmp = shape.pop(0)
    while len(shape) > 0:
        curr_end = tmp[-1]
        next_start = np.argmin(np.abs([path[0] - curr_end for path in shape]))
        tmp.extend(shape.pop(next_start))

    # Center and normalize the points
    shape = np.conjugate(tmp)
    shape -= np.mean(shape)
    shape /= np.max(np.abs(shape))

    return shape