def new_direction(self, direction): self.current_dir = direction self.all_path_list = [] self.curr_path_list = [] self.prev_line = [] self.curr_line = [] if DEBUG_POLYGONEXTRACTOR3: self.svg = SVGExporter("test-%d.svg" % direction) if direction == 0: self.svg.fill("red") else: self.svg.fill("blue") if (self.policy == PolygonExtractor.CONTOUR) and (direction == 1) \ and self.hor_path_list: self.last_x = -INFINITE self.delta_x = 0 self.convert_hor_path_list() if DEBUG_POLYGONEXTRACTOR3: self.cont = SVGExporter("test-2.svg") self.cont.fill("green")
def end_direction(self): if self.policy == PolygonExtractor.CONTOUR and self.hor_path_list: self.process_virtual_hor_scanline(INFINITE) self.new_scanline() self.end_scanline() if DEBUG_POLYGONEXTRACTOR3: self.svg.close() if self.policy == PolygonExtractor.CONTOUR and self.hor_path_list: for path in self.all_path_list: if DEBUG_POLYGONEXTRACTOR2: print("points=", path.points) i = 0 while i < len(path.points) - 1: if path.points[i][0] > path.points[i + 1][0]: if DEBUG_POLYGONEXTRACTOR2: print("drop point %d:" % path.points[i].id) path.points = path.points[:i] + path.points[i + 1:] if i > 0: i -= 1 else: i += 1 if DEBUG_POLYGONEXTRACTOR: print("%d paths" % len(self.all_path_list)) for path in self.all_path_list: print("%d:" % path.id), print("%d ->" % path.top_join.id) for point in path.points: print("%d(%g,%g)" % (point.id, point[0], point[1]), ) print("->%d" % path.bot_join.id) path_list = [] while len(self.all_path_list) > 0: p0 = self.all_path_list[0] for path in self.all_path_list: if path.id < p0.id: p0 = path if DEBUG_POLYGONEXTRACTOR: print("linking %d" % p0.id) self.all_path_list.remove(p0) p1 = p0.bot_join while True: if DEBUG_POLYGONEXTRACTOR: print("splice %d into %d" % (p1.id, p0.id)) self.all_path_list.remove(p1) p1.reverse() p0.points += p1.points if p1.top_join == p0: break p2 = p1.top_join if DEBUG_POLYGONEXTRACTOR: print("splicing %d into %d" % (p2.id, p0.id)) self.all_path_list.remove(p2) p0.points += p2.points p1 = p2.bot_join path_list.append(p0) if DEBUG_POLYGONEXTRACTOR: print("%d paths" % len(path_list)) for path in path_list: print("path %d(w=%d): " % (path.id, path.winding)), for point in path.points: print("%d(%g,%g)" % (point.id, point[0], point[1])), print() if self.current_dir == 0: self.hor_path_list = path_list elif self.current_dir == 1: if (self.policy == PolygonExtractor.CONTOUR ) and self.hor_path_list and path_list: self.merge_path_list = path_list else: self.ver_path_list = path_list if DEBUG_POLYGONEXTRACTOR3: from pycam.Exporters.SVGExporter import SVGExporter self.svg = SVGExporter("test-3.svg") for path in path_list: prev = None if len(path.points) <= 1: continue for p in path.points: if p.dir == 0: self.svg.fill("red") else: self.svg.fill("blue") self.svg.AddDot(p[0], p[1]) self.svg.AddText(p[0], p[1], str(p.id)) if prev: self.svg.AddLine(p[0], p[1], prev[0], prev[1]) prev = p p = path.points[0] self.svg.AddLine(p[0], p[1], prev[0], prev[1]) self.svg.close() self.cont.close()