From: EMC C. s. <cv...@cv...> - 2006-08-29 22:47:32
|
Modified file emc2/src/emc/usr_intf/axis/scripts/image-to-gcode.py Full file: <http://cvs.linuxcnc.org/cvs/emc2/src/emc/usr_intf/axis/scripts/image-to-gcode.py?rev=1.9> Difference: <http://cvs.linuxcnc.org/cvs/emc2/src/emc/usr_intf/axis/scripts/image-to-gcode.py.diff?r1=1.8;r2=1.9> Branch: HEAD Log: revision 1.9 date: 2006/08/29 22:47:29; author: jepler; state: Exp; lines: +110 -88 use a Converer class instead of having a zillion function arguments everywhere --- image-to-gcode.py 2006/08/28 19:44:02 1.8 +++ image-to-gcode.py 2006/08/29 22:47:29 1.9 @@ -163,97 +163,118 @@ unitcodes = ['G20', 'G21'] convert_makers = [ Convert_Scan_Increasing, Convert_Scan_Decreasing, Convert_Scan_Alternating, Convert_Scan_Upmill, Convert_Scan_Downmill ] -def convert(image, units, tool_shape, pixelsize, pixelstep, safetyheight, tolerance, feed, convert_rows, convert_cols, cols_first_flag): - w, h = image.shape - tw, th = tool_shape.shape - h1 = h - th - w1 = w - tw - tool_shape = tool_shape * pixelsize * tw / 2. +class Converter: + def __init__(self, + image, units, tool_shape, pixelsize, pixelstep, safetyheight, \ + tolerance, feed, convert_rows, convert_cols, cols_first_flag): + self.image = image + self.units = units + self.tool = tool_shape + self.pixelsize = pixelsize + self.pixelstep = pixelstep + self.safetyheight = safetyheight + self.tolerance = tolerance + self.feed = feed + self.convert_rows = convert_rows + self.convert_cols = convert_cols + self.cols_first_flag = cols_first_flag + + self.cache = {} + + w, h = self.w, self.h = image.shape + ts = self.ts = tool_shape.shape[0] + + self.h1 = h - ts + self.w1 = w - ts + + self.tool_shape = tool_shape * self.pixelsize * ts / 2; - g = Gcode(safetyheight=safetyheight, tolerance=tolerance) - g.begin() - g.continuous() - g.write(units) - g.safety() - g.set_feed(feed) - if convert_cols and cols_first_flag: - mill_cols(g, image, tool_shape, pixelsize, pixelstep, safetyheight, tolerance, feed, convert_cols, True) - if convert_rows: g.safety() - if convert_rows: - mill_rows(g, image, tool_shape, pixelsize, pixelstep, safetyheight, tolerance, feed, convert_rows, not cols_first_flag) - if convert_cols and not cols_first_flag: - if convert_rows: g.safety() - mill_cols(g, image, tool_shape, pixelsize, pixelstep, safetyheight, tolerance, feed, convert_cols, False) - g.end() + def convert(self): + self.g = g = Gcode(safetyheight=self.safetyheight, + tolerance=self.tolerance) + g.begin() + g.continuous() + g.write(self.units) + g.safety() + g.set_feed(self.feed) + + if self.convert_cols and self.cols_first_flag: + self.mill_cols(self.convert_cols, True) + if self.convert_rows: g.safety() + if self.convert_rows: + self.mill_rows(self.convert_rows, not self.cols_first_flag) + if self.convert_cols and not self.cols_first_flag: + if self.convert_rows: g.safety() + self.mill_cols(self.convert_cols, not self.convert_rows) + g.end() -cache = {} -def get_z(image, tool, x, y): - try: - return cache[x,y] - except KeyError: - tt = tool.shape[0] - m1 = image[y:y+tt, x:x+tt] - cache[x,y] = d = (m1 - tool).max() - return d + def get_z(self, x, y): + try: + return self.cache[x,y] + except KeyError: + m1 = self.image[y:y+self.ts, x:x+self.ts] + self.cache[x,y] = d = (m1 - self.tool).max() + return d -def get_dz_dy(image, tool, x, y, pixelsize): - y1 = max(0, y-1) - y2 = min(image.shape[0]-1, y+1) - dy = pixelsize * (y2-y1) - return (get_z(image, tool, x, y2) - get_z(image, tool, x, y1)) / dy + def get_dz_dy(self, x, y): + y1 = max(0, y-1) + y2 = min(self.image.shape[0]-1, y+1) |