def pocket_model(polygons, first_offset, second_offset=None, interior=True, N_offsets=1): _log.info("number of polygons: %d" % len(polygons)) _log.info("offset distance: %f" % first_offset) if second_offset is None: second_offset = first_offset radius = _polygon_model_radius(polygons) _log.info("Radius: %f" % radius) bin_size = int(math.ceil(math.sqrt(sum([len(poly.get_points()) for poly in polygons])))) _log.info("bin_size: %f" % bin_size) dia = openvoronoi.VoronoiDiagram(radius, bin_size) point_set, line_set = _polygons_to_line_set(polygons) _add_connected_point_set_to_diagram(point_set, line_set, dia) _filter_diagram(dia, interior) # choose either interior or exterior to be pocketed _log.info("diagram complete") _log.info("diagram check: %s" % str(dia.check())) offset_dia = openvoronoi.Offset(dia.getGraph()) _log.info("offset diagram created") offset_loops = [] if N_offsets == 1: # one single offset _log.info("generating one offset") offset_loops = offset_dia.offset(first_offset) elif N_offsets == -1: # "complete" offsetting of pocket interior _log.info("generating complete interior offsets") offset_loops = [] current_offset = first_offset offset_loops.extend(offset_dia.offset(current_offset)) N = 0 _log.info(" Offset %d at distance: %f" % (N_offsets, current_offset)) while True: N = N + 1 current_offset = current_offset + second_offset current_loops = offset_dia.offset(current_offset) if len(current_loops) > 0: _log.info(" Offset %d at distance: %f" % (N, current_offset)) offset_loops.extend(current_loops) else: break # if offset returns no loops then we are done else: # a specified (>= 2) number of offsets _log.info("generating %d offsets" % N_offsets) offset_loops = [] current_offset = first_offset while N_offsets > 0: current_loops = offset_dia.offset(current_offset) if len(current_loops) > 0: offset_loops.extend(current_loops) N_offsets = N_offsets - 1 current_offset = current_offset + second_offset _log.info("got %d loops from openvoronoi" % len(offset_loops)) return offset_loops
def pocket_model(polygons, offset): _log.info("number of polygons: %d", len(polygons)) _log.info("offset distance: %f", offset) maxx = max([poly.maxx for poly in polygons]) maxy = max([poly.maxy for poly in polygons]) minx = min([poly.minx for poly in polygons]) miny = min([poly.miny for poly in polygons]) radius = math.sqrt((maxx - minx)**2 + (maxy - miny)**2) / 1.8 _log.info("Radius: %f", radius) bin_size = int( math.ceil(math.sqrt(sum([len(poly.get_points()) for poly in polygons])))) _log.info("bin_size: %f", bin_size) dia = openvoronoi.VoronoiDiagram(radius, bin_size) point_set, line_set = _polygons_to_line_set(polygons) _add_connected_point_set_to_diagram(point_set, line_set, dia) _log.info("diagram complete") _log.info("diagram check: %s", str(dia.check())) offset_dia = openvoronoi.Offset(dia.getGraph()) _log.info("offset diagram created") offset_loops = offset_dia.offset(offset) _log.info("got %d loops from openvoronoi", len(offset_loops)) return _offset_loops_to_polygons(offset_loops)
# 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: offset2vtk.drawOffsets2(myscreen, ofs) oftext = ovdvtk.Text() oftext.SetPos((50, 100))
# 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) ngc_writer.preamble() for ofs in ofs_list: printOffsets(ofs)