class ContourCutter(pycam.PathProcessors.BasePathProcessor): def __init__(self, reverse=False): super(ContourCutter, self).__init__() self.curr_path = None self.scanline = None self.polygon_extractor = None self.points = [] self.reverse = reverse self.__forward = Point(1, 1, 0) def append(self, point): # Sort the points in positive x/y direction - otherwise the # PolygonExtractor breaks. if self.points and (point.sub(self.points[0]).dot(self.__forward) < 0): self.points.insert(0, point) else: self.points.append(point) def new_direction(self, direction): if self.polygon_extractor == None: self.polygon_extractor = PolygonExtractor(PolygonExtractor.CONTOUR) self.polygon_extractor.new_direction(direction) def end_direction(self): self.polygon_extractor.end_direction() def new_scanline(self): self.polygon_extractor.new_scanline() self.points = [] def end_scanline(self): for i in range(1, len(self.points) - 1): self.polygon_extractor.append(self.points[i]) self.polygon_extractor.end_scanline() def finish(self): self.polygon_extractor.finish() if self.polygon_extractor.merge_path_list: paths = self.polygon_extractor.merge_path_list elif self.polygon_extractor.hor_path_list: paths = self.polygon_extractor.hor_path_list else: paths = self.polygon_extractor.ver_path_list if paths: for path in paths: path.append(path.points[0]) simplify_toolpath(path) if paths: if self.reverse: paths.reverse() self.paths.extend(paths) self.sort_layered() self.polygon_extractor = None
def new_direction(self, direction): if self.polygon_extractor == None: self.polygon_extractor = PolygonExtractor(PolygonExtractor.CONTOUR) self.polygon_extractor.new_direction(direction)
class PolygonCutter(pycam.PathProcessors.BasePathProcessor): def __init__(self, reverse=False): super().__init__() self.curr_path = None self.scanline = None self.polygon_extractor = PolygonExtractor(PolygonExtractor.MONOTONE) self.reverse = reverse def append(self, point): self.polygon_extractor.append(point) def new_direction(self, direction): self.polygon_extractor.new_direction(direction) def end_direction(self): self.polygon_extractor.end_direction() def new_scanline(self): self.polygon_extractor.new_scanline() def end_scanline(self): self.polygon_extractor.end_scanline() def finish(self): self.polygon_extractor.finish() paths = [] source_paths = [] if self.polygon_extractor.hor_path_list: source_paths.extend(self.polygon_extractor.hor_path_list) if self.polygon_extractor.ver_path_list: source_paths.extend(self.polygon_extractor.ver_path_list) for path in source_paths: points = path.points for i in range((len(points) + 1) // 2): new_path = Path() if i % 2 == 0: new_path.append(points[i]) new_path.append(points[-i-1]) else: new_path.append(points[-i-1]) new_path.append(points[i]) paths.append(new_path) if paths: for path in paths: simplify_toolpath(path) if self.reverse: path.reverse() self.paths.extend(paths) self.sort_layered()
def __init__(self, reverse=False): super().__init__() self.curr_path = None self.scanline = None self.polygon_extractor = PolygonExtractor(PolygonExtractor.MONOTONE) self.reverse = reverse
def __init__(self, reverse=False): super(PolygonCutter, self).__init__() self.curr_path = None self.scanline = None self.polygon_extractor = PolygonExtractor(PolygonExtractor.MONOTONE) self.reverse = reverse
class PolygonCutter(pycam.PathProcessors.BasePathProcessor): def __init__(self, reverse=False): super(PolygonCutter, self).__init__() self.curr_path = None self.scanline = None self.polygon_extractor = PolygonExtractor(PolygonExtractor.MONOTONE) self.reverse = reverse def append(self, point): self.polygon_extractor.append(point) def new_direction(self, direction): self.polygon_extractor.new_direction(direction) def end_direction(self): self.polygon_extractor.end_direction() def new_scanline(self): self.polygon_extractor.new_scanline() def end_scanline(self): self.polygon_extractor.end_scanline() def finish(self): self.polygon_extractor.finish() paths = [] source_paths = [] if self.polygon_extractor.hor_path_list: source_paths.extend(self.polygon_extractor.hor_path_list) if self.polygon_extractor.ver_path_list: source_paths.extend(self.polygon_extractor.ver_path_list) for path in source_paths: points = path.points for i in range(0, (len(points)+1)/2): new_path = Path() if i % 2 == 0: new_path.append(points[i]) new_path.append(points[-i-1]) else: new_path.append(points[-i-1]) new_path.append(points[i]) paths.append(new_path) if paths: for path in paths: simplify_toolpath(path) if self.reverse: path.reverse() self.paths.extend(paths) self.sort_layered()
def new_direction(self, direction): if self.polygon_extractor is None: self.polygon_extractor = PolygonExtractor(PolygonExtractor.CONTOUR) self.polygon_extractor.new_direction(direction)
class ContourCutter(pycam.PathProcessors.BasePathProcessor): def __init__(self): super().__init__() self.curr_path = None self.scanline = None self.polygon_extractor = None self.points = [] self.__forward = (1, 1, 0) def append(self, point): # Sort the points in positive x/y direction - otherwise the # PolygonExtractor breaks. if self.points and (pdot(psub(point, self.points[0]), self.__forward) < 0): self.points.insert(0, point) else: self.points.append(point) def new_direction(self, direction): if self.polygon_extractor is None: self.polygon_extractor = PolygonExtractor(PolygonExtractor.CONTOUR) self.polygon_extractor.new_direction(direction) def end_direction(self): self.polygon_extractor.end_direction() def new_scanline(self): self.polygon_extractor.new_scanline() self.points = [] def end_scanline(self): for i in range(1, len(self.points) - 1): self.polygon_extractor.append(self.points[i]) self.polygon_extractor.end_scanline() def finish(self): self.polygon_extractor.finish() if self.polygon_extractor.merge_path_list: paths = self.polygon_extractor.merge_path_list elif self.polygon_extractor.hor_path_list: paths = self.polygon_extractor.hor_path_list else: paths = self.polygon_extractor.ver_path_list if paths: for path in paths: path.append(path.points[0]) simplify_toolpath(path) if paths: self.paths.extend(paths) self.sort_layered() self.polygon_extractor = None
def test_image(image, image2=None): pe = PolygonExtractor(policy=PolygonExtractor.CONTOUR) h = len(image) w = len(image[0]) for dir in [1, 0, 1]: if dir == 1 and image2: image = image2 pe.new_direction(dir) imax = 0 jmax = 0 if dir == 0: imax = w jmax = h else: imax = h jmax = w for j in range(0, jmax): pe.new_scanline() x = 0 y = 0 if (dir == 0): x = 0 y = j else: x = j y = 0 c = image[y][x] for i in range(0, imax): if dir == 0: x = i else: y = i if image[y][x] != c: if dir == 0: pe.append(Point(x - 0.5, y, 0)) else: pe.append(Point(x, y - 0.5, 0)) c = image[y][x] pe.end_scanline() pe.end_direction() pe.finish() # w = int(w*1.5) # h = int(h*2) fg = 0xae bg = 0xfe for dir in [0, 1, 2]: screen = [] for i in range(0, h): screen.append([]) for j in range(0, w): screen[i].append([bg, bg, bg]) path_list = None if dir == 0: path_list = pe.hor_path_list elif dir == 1: path_list = pe.ver_path_list else: path_list = pe.merge_path_list if not path_list: continue for curr_path in path_list: for point in curr_path.points: x = (int)(point.x) y = (int)(point.y) screen[y][x][0] = r(curr_path.id) screen[y][x][1] = g(curr_path.id) screen[y][x][2] = b(curr_path.id) if dir == 0: for i in range(x + 1, w): if screen[y][i][0] == bg: screen[y][i][0] = fg screen[y][i][1] = fg screen[y][i][2] = fg elif screen[y][i][0] == fg: screen[y][i][0] = bg screen[y][i][1] = bg screen[y][i][2] = bg else: for i in range(y + 1, h): if screen[i][x][0] == bg: screen[i][x][0] = fg screen[i][x][1] = fg screen[i][x][2] = fg elif screen[i][x][0] == fg: screen[i][x][0] = bg screen[i][x][1] = bg screen[i][x][2] = bg l = "+" for i in range(0, w): l += "-" l += "+" print(l) for j in range(0, h): l = "|" for i in range(0, w): if (screen[j][i][0] == bg): l += " " elif (screen[j][i][0] == fg): l += "." else: l += "*" l += "|" print(l) l = "+" for i in range(0, w): l += "-" l += "+" print(l) filename = None if dir == 0: filename = "polygon_h.svg" elif dir == 1: filename = "polygon_v.svg" else: filename = "polygon.svg" f = open(filename, "w") f.write("<?xml version='1.0'?>\n") f.write("<svg") f.write(" xmlns='http://www.w3.org/2000/svg'") f.write(" width='%f'" % (10 + (w) * 30)) f.write(" height='%f'" % (10 + (h) * 30)) f.write(">\n") f.write("<g transform='translate(10, 10)'>\n") f.write("<g transform='scale(20)'>\n") for winding in range(0, 10): f.write("<g ") if (pe.policy == PolygonExtractor.CONTOUR) and (winding & 1) == 0: f.write(" fill='#ffffff'") else: f.write(" fill='#e0e0e0'") f.write(" stroke-width='0.1'") f.write(">\n") s = "" for path in path_list: if path.winding != winding: continue f.write("<path") f.write(" stroke='#%02x%02x%02x'" % (r(path.id), g(path.id), b(path.id))) f.write(" d='") first = True for point in path.points: x = point.x y = point.y s += ( "<text x='%g' y='%g' font-size='0.3' fill='#000000'>%d</text>\n" % (x, y, point.id)) if first: first = False f.write("M ") else: f.write(" L ") f.write("%g %g" % (x, y)) f.write(" z'/>\n") f.write(s) f.write("</g>\n") f.write("<g font-size='0.3' fill='#000000' opacity='0.5'>\n") for j in range(h): for i in range(w): c = image[j][i] if c != ' ': f.write("<text x='%g' y='%g'>%c</text>\n" % (i * 1.0, j * 1.0, c)) f.write("</g>\n") f.write("</g>\n") f.write("</g>\n") f.write("</svg>\n")