Example #1
0
def medial_axis(coords, far=500.0, v_flop=False, clean=False):

    segs = get_loops_from_coords(coords)
    if v_flop:
        reverse_segments(segs)

    n_bins = int(sqrt(len(coords)))  # approx. sqrt(nr of sites)
    vd = ovd.VoronoiDiagram(far, n_bins)
    # vd.debug_on()
    # vd.set_silent(True)  # suppress Warnings  # JvO: not implemented?

    insert_many_polygons(vd, segs)

    print("VD check: %s" % vd.check())

    pi = ovd.PolygonInterior(
        True)  # filter so that only polygon interior remains
    vd.filter_graph(pi)
    ma = ovd.MedialAxis()  # filter so that only medial axis remains
    vd.filter_graph(ma)

    maw = ovd.MedialAxisWalk(vd.getGraph())
    toolpath = maw.walk()

    # print_toolpath(toolpath, 1.0)  # write ngc to output.ngc

    return toolpath_to_v_coords(toolpath)
Example #2
0
def medial_axis(segs, clean=False):
    import sys

    # svg = "../samples/Hello_flattened2_rev.svg" if len(sys.argv) < 2 else sys.argv[1]
    # svgr = SvgReader(svg, error_threshold=.6)
    # svgr.parse()
    # svgr.centerPolys()

    # far = svgr.radius * 1.2
    far = 100.0
    n_bins = int(sqrt(1200))  # approx. sqrt(nr of sites)
    vd = ovd.VoronoiDiagram(far, n_bins)

    times = insert_many_polygons(vd, segs)
    print("all sites inserted: %d " % len(times))
    # print("all sites inserted.")
    print("VD check: %s" % vd.check())

    pi = ovd.PolygonInterior(True)
    vd.filter_graph(pi)
    ma = ovd.MedialAxis()
    vd.filter_graph(ma)

    printMedial(vd, 1)  # write ngc to output.ngc
    # boxsegs= [ [ [0.8,-0.8],[0.8,0.8], [-0.8,0.8], [-0.8,-0.8]   ] ]
    b = 0.6
    boxsegs = [[[-b, -b], [-b, b], [b, b], [b, -b]]]
    # for poly in boxsegs:
    # boxsegs = boxsegs.reverse()

    all_segs = boxsegs + segs + segs2 + segs3 + segs4 + segs5
    # all_segs=segs
    # all_segs=segs3 #+segs4
    # all_segs = segs3
    times = insert_many_polygons(vd, all_segs)
    vd.check()

    # ovd.PolygonInterior( vd.getGraph() , True )
    # ovd.MedialAxis( vd.getGraph() )
    pi_filt = ovd.PolygonInterior(False)
    vd.filter_graph(pi_filt)

    of = ovd.Offset(
        vd.getGraph())  # pass the created graph to the Offset class
    ofs_list = []
    t_before = time.time()
    for t in [0.01 * x for x in range(1, 30)]:
        ofs = of.offset(t)
        ofs_list.append(ofs)
    t_after = time.time()
    oftime = t_after - t_before

    print("offset done in %s" % oftime)

    for ofs in ofs_list:
Example #4
0
    # get segments from ttt
    # this is the input geometry to VoronoiDiagram. It could also come from a text-file
    # see the description of each function for details on the format
    segs = ttt_segments("LinuxCNC", 40000)
    segs = translate(segs, -0.06, 0.05)
    segs = modify_segments(segs)

    # build a VD from the input geometry
    times = insert_many_polygons(vd, segs)
    print "all sites inserted. "
    print "VD check: ", vd.check()  # sanity check

    # this filters the diagram so we are left with only the interior or the exterior
    # of the polygon. If the filtering is omitted we get offsets on both sides of the input geometry.
    # try True/False here and see what happens
    pi = ovd.PolygonInterior(False)
    vd.filter_graph(pi)

    # Create an Offset class, for offsetting.
    of = ovd.Offset(
        vd.getGraph())  # pass the created graph to the Offset class
    ofs_list = []
    t_before = time.time()
    for t in [0.002 * x for x in range(1, 20)]:
        ofs = of.offset(t)  # produce offsets at distance t
        ofs_list.append(ofs)
    t_after = time.time()
    oftime = t_after - t_before

    # offset output format
    # ofs will be a list of offset-loops.
Example #5
0
    #vd.debug_on()

    vd.addLineSite(id_list[3], id_list[4])
    vd.check()

    vd.addLineSite(id_list[4], id_list[0])
    vd.check()

    t_after = time.time()
    line_time = t_after - t_before
    if line_time < 1e-3:
        line_time = 1
    times.append(line_time)

    pi = ovd.PolygonInterior(True)
    vd.filter_graph(pi)
    ma = ovd.MedialAxis()
    vd.filter_graph(ma)

    mapocket = ovd.MedialAxisPocket(vd.getGraph())
    mapocket.setWidth(0.01)

    mapocket.run()
    mic_components = mapocket.get_mic_components()
    for mic_list in mic_components:
        for n in range(len(mic_list)):
            mic = mic_list[n]
            if n == 0:
                print "hello", mic[0], " r = ", mic[1]
                drawCircle(myscreen, mic[0], mic[1], ovdvtk.red)
    segs = ttt_segments("H", 30000)
    segs = translate(segs, -0.06, -0.05)
    segs = modify_segments(segs)

    # a box around the character
    box = 0.1
    p0 = [-box, -box]
    p1 = [box, -box]
    p2 = [box, box]
    p3 = [-box, box]
    seg0 = [p3, p2, p1, p0]
    segs.append(seg0)

    times = insert_many_polygons(vd, segs)

    pi = ovd.PolygonInterior(False)
    vd.filter_graph(pi)

    of = ovd.Offset(
        vd.getGraph())  # pass the created graph to the Offset class
    of.str()  # text output, for debug
    dists = [0.004 * x for x in range(1, 15)]
    ofs_loops = []
    ofsl = []
    for d in dists:
        d_offsets = of.offset(d)
        if len(d_offsets) != 0:
            ofs_loops.extend(
                d_offsets)  # compute offset at d, and add to loop-list
            ofsl.extend(of.offset_loop_list(d))
    #print ofsl
Example #7
0
    vod.drawVertexIndex = 0
    vod.drawGenerators = 0
    vod.offsetEdges = 0
    vod.drawNullEdges = 1
    vd.setEdgeOffset(0.001)

    all_segs = segs + segs2  #+segs3 +segs4+segs5
    #all_segs=segs
    #all_segs=segs3 #+segs4
    #all_segs = segs6
    #insert_many_polygons(vd,all_segs)
    times = insert_many_polygons(vd, all_segs)
    #vd.check()
    vod.setVDText2(times)

    ovd.PolygonInterior(vd.getGraph(), True)
    ovd.MedialAxis(vd.getGraph())
    vod.setAll()  # this draws the medial-axis interior to the letters

    vd.filterReset()
    # another vod to draw outside text:
    vod2 = ovdvtk.VD(myscreen,
                     vd,
                     float(scale),
                     textscale=0.01,
                     vertexradius=0.003)
    #vod.drawFarCircle()
    vod2.textScale = 0.0002
    vod2.vertexRadius = 0.0011
    vod2.drawVertices = 0
    vod2.drawVertexIndex = 0
    # count the number of segments
    num_segs = 0
    for s in segs:
        num_segs = num_segs + len(s)

    text_length = (extents.maxx - extents.minx) * scale * print_scale
    text_height = (extents.maxy - extents.miny) * scale * print_scale

    # print comments to g-code file
    print "( ttt2medial.py - experimental v-carving script )"
    print "( TTT++", ttt.version(), "                      )"
    print "( OpenVoronoi", ovd.version(), "                )"
    print "( number of polygons: ", len(segs), "                )"
    print "( number of segments: ", num_segs, "                )"
    print "( text length: ", text_length, " )"
    print "( text height: ", text_height, "                )"
    print "( VD built in %02.3f seconds   = %02f  us* n*log2{n} )" % (
        sum(times), 1e6 * float(sum(times)) /
        (float(num_segs) * float(math.log10(num_segs) / math.log10(2))))
    print "( VD check: ", vd.check(), "                              )"

    pi = ovd.PolygonInterior(
        True)  # filter so that only polygon interior remains
    vd.filter_graph(pi)
    ma = ovd.MedialAxis()  # filter so that only medial axis remains
    vd.filter_graph(ma)

    ngc_writer.preamble()
    printMedial(vd, print_scale)  # the actual cutting g-code
    ngc_writer.postamble()
Example #9
0
    vd.addLineSite(id_list[4], id_list[0])
    vd.check()

    t_after = time.time()
    line_time = t_after - t_before
    if line_time < 1e-3:
        line_time = 1
    times.append(line_time)

    #of = ovd.Offset( vd.getGraph() ) # pass the created graph to the Offset class
    #of.str()
    #ofs = of.offset(0.123)
    #print ofs
    #drawOffsets(myscreen, ofs)

    pi = ovd.PolygonInterior(vd.getGraph(), True)

    of = ovd.Offset(
        vd.getGraph())  # pass the created graph to the Offset class

    ofs = of.offset(0.123)
    #print ofs
    ovdvtk.drawOffsets(myscreen, ofs)

    #of.offset(0.125)

    vod.setVDText2(times)
    vod.setAll()
    print "PYTHON All DONE."
    myscreen.render()
    myscreen.iren.Start()
def _filter_diagram(dia, interior=True):
    polygon_filter = openvoronoi.PolygonInterior(interior)
    dia.filter_graph(polygon_filter)
    return
Example #11
0
    print "( TTT++", ttt.version(), " )"
    print "( OpenVoronoi", ovd.version(), " )"

    # get segments from ttt. NOTE: must set scale so all geometry fits within unit-circle!
    segs = ttt_segments(
        "LinuxCNC",
        20000)  # (text, scale) all coordinates are divided by scale
    segs = translate(segs, -0.06, 0.05)
    segs = modify_segments(segs)

    times = insert_many_polygons(vd, segs)  # insert segments into vd
    print "( all sites inserted in %.3f seconds )" % (sum(times))
    print "( VD check: ", vd.check(), " )"

    pi = ovd.PolygonInterior(True)  # polygon interior
    vd.filter_graph(pi)

    of = ovd.Offset(
        vd.getGraph())  # pass the filtered graph to the Offset class

    ofs_list = []
    t_before = time.time()
    for t in [0.003 * x for x in range(1, 20)
              ]:  # this defines the "step-over" and how many offsets we get
        ofs = of.offset(t)  # generate offset with offset-distance t
        ofs_list.append(ofs)
    t_after = time.time()
    oftime = t_after - t_before
    print "( offsets generated in %.3f seconds )" % (oftime)