def plot_spline(rw, tck, rgba): '''Plot the spline on RisWidget Parameters: ------------ rw: RisWidget Object Reference to the RisWidget object you want to display the spline on tck: parametric spline tuple Spline tuple to plot rgba: 3-d tuple of ints RGBA values to color the spline Returns: ----------- display_path: QGraphicsPathItem Reference to the display item displaying the spline ''' bezier_elements = interpolate.spline_to_bezier(tck) path = Qt.QPainterPath() path.moveTo(*bezier_elements[0][0]) for (sx, sy), (c1x, c1y), (c2x, c2y), (ex, ey) in bezier_elements: path.cubicTo(c1x, c1y, c2x, c2y, ex, ey) display_path = Qt.QGraphicsPathItem(path, parent=rw.image_scene.layer_stack_item) pen = Qt.QPen(Qt.QColor(*rgba, 100)) pen.setWidth(2) pen.setCosmetic(True) display_path.setPen(pen) return display_path
def path(pts, smooth=True, filter=False): global pen #print resolution if draw: if filter: fpts = [] fpts.append(pts[0]) for pt in pts[1:]: if delta(fpts[-1],pt) > filter: fpts.append(pt) if (pt != fpts[-1]).all(): fpts.append(pt) pts = np.array(fpts) if smooth and len(pts) > 3: tck = spline.fit_spline(pts,smoothing=0.0) beziers = spline.spline_to_bezier(tck) bezier = beziers.pop(0) gcode = "G0 X%.3F Y%.3F F%.0f" % (bezier[0, 0] + OFFSET[0], bezier[0, 1] + OFFSET[1], MOVESPEED) gwrite(gcode) gwrite(pendown) if bezier.shape[0] == 4: gcode = "G5 I%.3F J%.3F P%.3F Q%.3F X%.3F Y%.3F F%.0f" % (bezier[1, 0] - bezier[0, 0] + OFFSET[0], bezier[1, 1] - bezier[0, 1] + OFFSET[1],\ bezier[2, 0] - bezier[3, 0], bezier[2, 1] - bezier[3, 1],\ bezier[3, 0] + OFFSET[0], bezier[3, 1] + OFFSET[1], DRAWSPEED) elif bezier.shape[0] == 3: gcode = "G5 I%.3F J%.3F P%.3F Q%.3F X%.3F Y%.3F F%.0f" % (bezier[1, 0] - bezier[0, 0] + OFFSET[0], bezier[1, 1] - bezier[0, 1] + OFFSET[1], \ bezier[1, 0] - bezier[2, 0], bezier[1, 1] - bezier[2, 1], \ bezier[2, 0] + OFFSET[0], bezier[2, 1] + OFFSET[1], DRAWSPEED) else: gcode = "G1 X%.3F Y%.3F F%.0f" % (bezier[1,0] + OFFSET[0], bezier[1,1] + OFFSET[1], DRAWSPEED) gwrite(gcode) for bezier in beziers: if bezier.shape[0] == 4: gcode = "G5 I%.3F J%.3F P%.3F Q%.3F X%.3F Y%.3F F%.0f" % (bezier[1, 0] - bezier[0, 0] + OFFSET[0], bezier[1, 1] - bezier[0, 1] + OFFSET[1], bezier[2, 0] - bezier[3, 0], bezier[2, 1] - bezier[3, 1], \ bezier[3, 0] + OFFSET[0], bezier[3, 1] + OFFSET[1], DRAWSPEED) else: gcode = "G5 I%.3F J%.3F P%.3F Q%.3F X%.3F Y%.3F F%.0f" % (bezier[1, 0] - bezier[0, 0] + OFFSET[0], bezier[1, 1] - bezier[0, 1] + OFFSET[1], \ bezier[1, 0] - bezier[2, 0], bezier[1, 1] - bezier[2, 1], \ bezier[2, 0] + OFFSET[0], bezier[2, 1] + OFFSET[1], DRAWSPEED) gwrite(gcode) else: if delta(pts[0], pts[1]) > 1: print "Large Gap ",pts[0],pts[1] ptx = pts[:,0] pty = pts[:,1] gcode = "G0 X%.3F Y%.3F F%.0f" % (ptx[0] + OFFSET[0], pty[0] + OFFSET[1], MOVESPEED) gwrite(gcode) gwrite(pendown) gcode = "G1 X%.3F Y%.3F F%.0f" % (ptx[1] + OFFSET[0], pty[1] + OFFSET[1], DRAWSPEED) gwrite(gcode) gwrite(penup) #gwrite("G4 P1000") #draw.flush() else: print "Nothing open to draw onto"
def _update_path(self): self.path = Qt.QPainterPath() tck = self._tck if tck is not None: bezier_elements = interpolate.spline_to_bezier(tck) self.path.moveTo(*bezier_elements[0][0]) for (sx, sy), (c1x, c1y), (c2x, c2y), (ex, ey) in bezier_elements: self.path.cubicTo(c1x, c1y, c2x, c2y, ex, ey) self.setPath(self.path)
def plot_spline(rw, points, smoothing, rgba): tck = interpolate.fit_spline(points, smoothing=smoothing) bezier_elements = interpolate.spline_to_bezier(tck) path = Qt.QPainterPath() path.moveTo(*bezier_elements[0][0]) for (sx, sy), (c1x, c1y), (c2x, c2y), (ex, ey) in bezier_elements: path.cubicTo(c1x, c1y, c2x, c2y, ex, ey) display_path = Qt.QGraphicsPathItem(path, parent=rw.image_scene.layer_stack_item) pen = Qt.QPen(Qt.QColor(*rgba)) pen.setWidth(2) pen.setCosmetic(True) display_path.setPen(pen) return tck, display_path
def _generate_path_from_positions(self, positions): if len(positions) < 4: return super()._generate_path_from_positions(positions) points = [(pos.x(), pos.y()) for pos in positions] self.tck = interpolate.fit_spline(points, smoothing=self._smoothing * len(points)) bezier_elements = interpolate.spline_to_bezier(self.tck) path = Qt.QPainterPath() path.moveTo(*bezier_elements[0][0]) for (sx, sy), (c1x, c1y), (c2x, c2y), (ex, ey) in bezier_elements: path.cubicTo(c1x, c1y, c2x, c2y, ex, ey) return path
def path(pts, smooth=True, filter=False): global pen #print resolution if draw: if filter: fpts = [] fpts.append(pts[0]) for pt in pts[1:]: if delta(fpts[-1], pt) > filter: fpts.append(pt) if (pt != fpts[-1]).all(): fpts.append(pt) pts = np.array(fpts) gcode = "G0 X%.3f Y%.3f F%.0f" % (pts[0, 0], pts[0, 1], MOVESPEED) gwrite(gcode) gwrite(pendown) if smooth and len(pts) > 2: tck = spline.fit_spline(pts, smoothing=0.0) beziers = spline.spline_to_bezier(tck) for bezier in beziers: # bez = map(tuple,beziers.pop(0)) cubicB = CubicBezier(bezier[0], bezier[1], bezier[2], bezier[3]) arcs = cubicB.biarc_approximation() for arc in arcs: if isinstance(arc, Line): gcode = "G1 X%.3f Y%.3f F%.0f" % (arc.p2.x, arc.p2.y, DRAWSPEED) elif isinstance(arc, Arc): arcv = arc.center - arc.p1 if arc.is_clockwise(): gcode = "G2 X%.3f Y%.3f I%.3f J%.3f F%.0f" % ( arc.p2.x, arc.p2.y, arcv.x, arcv.y, DRAWSPEED) else: gcode = "G3 X%.3f Y%.3f I%.3f J%.3f F%.0f" % ( arc.p2.x, arc.p2.y, arcv.x, arcv.y, DRAWSPEED) gwrite(gcode) else: for pt in pts[1:]: gcode = "G1 X%.3f Y%.3f F%.0f" % (pt[0], pt[1], DRAWSPEED) gwrite(gcode) gwrite(penup) #gwrite("G4 P1000") #draw.flush() else: print "Nothing open to draw onto"