def raster(poly, use_convex_hull=False, offset=0, step=1, find_min=False, theta=None): poly = np.array(poly) poly = sort_clockwise(poly, poly) if use_convex_hull: poly = convex_hull(poly) poly = np.array(poly) if offset: opoly = polygon_offset(poly, offset) opoly = np.array(opoly, dtype=int) opoly = opoly[:, (0, 1)] else: opoly = poly lens = [] rtheta = 0 if find_min: lines, rtheta, lens = find_minimum_orientation(opoly, step) else: lines = make_raster_polygon(opoly, step) if theta is not None: cx, cy = get_center(opoly) P_prime = rotate_poly(opoly.T, theta, loc=(cx, cy)) lines = make_raster_polygon(P_prime.T, step) lines = rotate_lines(lines, theta, cx, cy) return lines, lens, rtheta
def _replot_polygon(self, poly): pts = [(pi.x, pi.y) for pi in poly.points] pts = sort_clockwise(pts, pts) # pts = pts + pts[:1] pts = array(pts) scale = 1000 pts *= scale use_convex_hull = False find_min = poly.find_min npoints, lens, theta = raster( pts, step=poly.scan_size, offset=-poly.offset if poly.use_outline else 0, use_convex_hull=use_convex_hull, find_min=find_min, theta=poly.theta) poly._set_theta(theta) # poly.trait_set(theta=theta) g = self.graph g.clear() g.plotcontainer.padding = 5 g.new_plot( padding=[60, 30, 30, 50], # bounds=[400, 400], # resizable='h', xtitle='X (microns)', ytitle='Y (microns)') g.new_plot( padding=[50, 30, 30, 30], xtitle='Theta (degrees)', ytitle='Num. Scan Lines', # bounds=[400, 100], # resizable='h' ) if find_min: ts, ls = zip(*lens) g.new_series(ts, ls, plotid=1) # plot original pts = vstack((pts, pts[:1])) xs, ys = pts.T self.graph.new_series(xs, ys, plotid=0) self.graph.set_x_limits(min(xs), max(xs), pad='0.1') self.graph.set_y_limits(min(ys), max(ys), pad='0.1') for ps in npoints: for i in range(0, len(ps), 2): p1, p2 = ps[i], ps[i + 1] g.new_series((p1[0], p2[0]), (p1[1], p2[1]), color='black', plotid=0) if poly.use_outline: from pychron.core.geometry.polygon_offset import polygon_offset opoly = polygon_offset(pts, -1 * poly.offset) opoly = array(opoly) xs, ys, _ = opoly.T self.graph.new_series(xs, ys, plotid=0)
def _replot_polygon(self, poly): pts = [(pi.x, pi.y) for pi in poly.points] pts = sort_clockwise(pts, pts) # pts = pts + pts[:1] pts = array(pts) scale = 1000 pts *= scale use_convex_hull = False find_min = poly.find_min npoints, lens, theta = raster(pts, step=poly.scan_size, offset=-poly.offset if poly.use_outline else 0, use_convex_hull=use_convex_hull, find_min=find_min, theta=poly.theta ) poly._set_theta(theta) # poly.trait_set(theta=theta) g = self.graph g.clear() g.plotcontainer.padding = 5 g.new_plot(padding=[60, 30, 30, 50], # bounds=[400, 400], # resizable='h', xtitle='X (microns)', ytitle='Y (microns)') g.new_plot(padding=[50, 30, 30, 30], xtitle='Theta (degrees)', ytitle='Num. Scan Lines', # bounds=[400, 100], # resizable='h' ) if find_min: ts, ls = zip(*lens) g.new_series(ts, ls, plotid=1) # plot original pts = vstack((pts, pts[:1])) xs, ys = pts.T self.graph.new_series(xs, ys, plotid=0) self.graph.set_x_limits(min(xs), max(xs), pad='0.1') self.graph.set_y_limits(min(ys), max(ys), pad='0.1') for ps in npoints: for i in range(0, len(ps), 2): p1, p2 = ps[i], ps[i + 1] g.new_series((p1[0], p2[0]), (p1[1], p2[1]), color='black', plotid=0) if poly.use_outline: from pychron.core.geometry.polygon_offset import polygon_offset opoly = polygon_offset(pts, -1 * poly.offset) opoly = array(opoly) xs, ys, _ = opoly.T self.graph.new_series(xs, ys, plotid=0)
def _move_polygon(self, pts, velocity=5, offset=50, use_outline=True, find_min=False, scan_size=None, use_move=True, use_convex_hull=True, motors=None, verbose=True, start_callback=None, end_callback=None): """ motors is a dict of motor_name:value pairs """ if pts is None: return if not isinstance(pts, list): velocity = pts.velocity use_convex_hull = pts.use_convex_hull if scan_size is None: scan_size = pts.scan_size use_outline = pts.use_outline offset = pts.offset find_min = pts.find_min pts = [dict(xy=(pi.x, pi.y), z=pi.z, ) for pi in pts.points] # set motors if motors is not None: for k, v in motors.values(): ''' motor will not set if it has been locked using set_motor_lock or remotely using SetMotorLock ''' if use_move: self.parent.set_motor(k, v, block=True) xy = [pi['xy'] for pi in pts] n = 1000 if scan_size is None: scan_size = n / 2 # convert points to um pts = array(xy) pts *= n pts = asarray(pts, dtype=int) ''' sort clockwise ensures consistent offset behavior a polygon gain have a inner or outer sense depending on order of vertices always use sort_clockwise prior to any polygon manipulation ''' pts = sort_clockwise(pts, pts) sc = self.stage_controller sc.set_program_mode('absolute') # do smooth transitions between points sc.set_smooth_transitions(True) if use_convex_hull: pts = convex_hull(pts) if use_outline: # calculate new polygon offset_pts = polygon_offset(pts, -offset) offset_pts = array(offset_pts, dtype=int) # polygon offset used 3D vectors. # trim to only x,y pts = offset_pts[:, (0, 1)] # trace perimeter if use_move: p0 = xy[0] self.linear_move(p0[0], p0[1], mode='absolute', block=True) sc.timer = sc.timer_factory() if start_callback is not None: start_callback() # buf=[] for pi in xy[1:]: self.linear_move(pi[0], pi[1], velocity=velocity, mode='absolute', set_stage=False) # finish at first point self.linear_move(p0[0], p0[1], velocity=velocity, mode='absolute', set_stage=False) sc.block() self.info('polygon perimeter trace complete') ''' have the oppurtunity here to turn off laser and change parameters i.e mask ''' if use_move: # calculate and step thru scan lines self._raster(pts, velocity, step=scan_size, scale=n, find_min=find_min, start_callback=start_callback, end_callback=end_callback, verbose=verbose) sc.set_program_mode('relative') if end_callback is not None: end_callback() self.info('polygon raster complete')
cx, cy = xs.mean(), ys.mean() # plot original g.new_series(xs, ys) g.set_x_limits(min(xs), max(xs), pad='0.1') g.set_y_limits(min(ys), max(ys), pad='0.1') for ps in npoints: for i in range(0, len(ps), 2): p1, p2 = ps[i], ps[i + 1] g.new_series((p1[0], p2[0]), (p1[1], p2[1]), color='black') # plot offset polygon # poly = sort_clockwise(poly, poly) opoly = polygon_offset(poly, -500) if use_convex_hull: opoly = convex_hull(opoly) xs, ys, _ = opoly.T xs = np.hstack((xs, xs[0])) ys = np.hstack((ys, ys[0])) else: opoly = np.array(opoly, dtype=int) xs, ys, _ = opoly.T # opoly = opoly[:, (0, 1)] # rpoly = rotate_poly(opoly.T, 145, loc=(cx, cy)) # xs, ys = rpoly[0], rpoly[1] g.new_series(xs, ys) g.new_plot(padding=[50, 30, 30, 30],
cx, cy = xs.mean(), ys.mean() # plot original g.new_series(xs, ys) g.set_x_limits(min(xs), max(xs), pad="0.1") g.set_y_limits(min(ys), max(ys), pad="0.1") for ps in npoints: for i in range(0, len(ps), 2): p1, p2 = ps[i], ps[i + 1] g.new_series((p1[0], p2[0]), (p1[1], p2[1]), color="black") # plot offset polygon # poly = sort_clockwise(poly, poly) opoly = polygon_offset(poly, -500) if use_convex_hull: opoly = convex_hull(opoly) xs, ys, _ = opoly.T xs = np.hstack((xs, xs[0])) ys = np.hstack((ys, ys[0])) else: opoly = np.array(opoly, dtype=int) xs, ys, _ = opoly.T # opoly = opoly[:, (0, 1)] # rpoly = rotate_poly(opoly.T, 145, loc=(cx, cy)) # xs, ys = rpoly[0], rpoly[1] g.new_series(xs, ys) g.new_plot(