Example #1
0
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
Example #2
0
    def _render_(self, gc):
        with gc:
            self.indicator.render(gc)

            for pi in self.points:
                pi.render(gc)

            gc.set_stroke_color((0, 0, 1))

            pts = [pi.get_xy() for pi in self.points]
            if len(pts) == 2:
                x0, y0 = pts[0][0], pts[0][1]
                x1, y1 = pts[1][0], pts[1][1]

                x, y = min(x0, x1), min(y0, y1)
                w, h = abs(x0 - x1), abs(y0 - y1)

                gc.rect(x, y, w, h)

            else:
                if len(pts) > 2 and self.use_convex_hull:
                    pts = convex_hull(pts)

                if pts is not None and len(pts) > 2:
                    gc.set_stroke_color(self.default_color)
                    gc.move_to(pts[0][0], pts[0][1])
                    for pi in pts[1:]:
                        gc.line_to(pi[0], pi[1])
                    gc.line_to(pts[0][0], pts[0][1])

            gc.stroke_path()
Example #3
0
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
Example #4
0
    def _render(self, gc):
        with gc:
            self.indicator.render(gc)

            for pi in self.points:
                pi.render(gc)

            gc.set_stroke_color((0, 0, 1))

            pts = [pi.get_xy() for pi in self.points]
            if len(pts) == 2:
                x0, y0 = pts[0][0], pts[0][1]
                x1, y1 = pts[1][0], pts[1][1]

                x, y = min(x0, x1), min(y0, y1)
                w, h = abs(x0 - x1), abs(y0 - y1)

                gc.rect(x, y, w, h)

            else:
                if len(pts) > 2 and self.use_convex_hull:
                    from pychron.core.geometry.convex_hull import convex_hull
                    pts = convex_hull(pts)

                if pts is not None and len(pts) > 2:
                    gc.set_stroke_color(self.default_color)
                    gc.move_to(pts[0][0], pts[0][1])
                    for pi in pts[1:]:
                        gc.line_to(pi[0], pi[1])
                    gc.line_to(pts[0][0], pts[0][1])

            gc.stroke_path()
Example #5
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')
Example #6
0
    npoints, lens = raster(poly,
                     step=750,
                     offset=-500,
                     use_convex_hull=use_convex_hull, find_min=True)

    from pychron.graph.graph import Graph
    g = Graph(window_height=700)
    g.plotcontainer.padding = 5
    g.new_plot(padding=[60, 30, 30, 50],
               bounds=[400, 400],
               resizable='h',
               xtitle='X (microns)',
               ytitle='Y (microns)')

    if use_convex_hull:
        poly = convex_hull(poly)
        xs, ys = poly.T
        cx, cy = xs.mean(), ys.mean()
        P = poly.T
        xs = np.hstack((xs, xs[0]))
        ys = np.hstack((ys, ys[0]))
    else:
        xs, ys = poly.T
        xs = np.hstack((xs, xs[0]))
        ys = np.hstack((ys, ys[0]))

    cx, cy = xs.mean(), ys.mean()

    # plot original
    g.new_series(xs, ys)
    g.set_x_limits(min(xs), max(xs), pad='0.1')
Example #7
0
    polyinset.append(getinsetpoint(poly[0], poly[1], poly[2], offset))
    return polyinset


if __name__ == '__main__':
    from pylab import plot, show
    pts = [(2, 7), (4, 12), (8, 15), (16, 9), (11, 5), (8, 7), (5, 5)]
    pts = [(0, 0), (10, 0), (10, 10), (5, 20), (0, 10)]
    n = 100
    pts = [(x * n, y * n) for x, y in pts]
    #    pts = sort_clockwise(pts, pts)
    ppts = array(pts + pts[:1])
    xs, ys = ppts.T
    plot(xs, ys, 'red')

    pts = sort_clockwise(pts, pts)
    pts = convex_hull(pts)

    for i in range(20):
        #        print  i * 0.25
        opts = polygon_offset(pts, -(i + 1) * 25)
        #    #    opts = offsetpolygon(pts, -0.25)
        #    #    print opts
        opts = array(opts)
        #    #    print opts
        xs, ys, zs = opts.T
        plot(xs, ys, 'b')

    show()
#============= EOF =============    ================================
Example #8
0
    #    xs, ys = poly.T
    #    cx, cy = xs.mean(), ys.mean()
    #    poly = rotate_poly(poly.T, 45, loc=(cx, cy))
    #    poly = poly.T

    use_convex_hull = False
    npoints, lens = raster(poly, step=750, offset=-500, use_convex_hull=use_convex_hull, find_min=True)

    from pychron.graph.graph import Graph

    g = Graph(window_height=700)
    g.plotcontainer.padding = 5
    g.new_plot(padding=[60, 30, 30, 50], bounds=[400, 400], resizable="h", xtitle="X (microns)", ytitle="Y (microns)")

    if use_convex_hull:
        poly = convex_hull(poly)
        xs, ys = poly.T
        cx, cy = xs.mean(), ys.mean()
        P = poly.T
        xs = np.hstack((xs, xs[0]))
        ys = np.hstack((ys, ys[0]))
    else:
        xs, ys = poly.T
        xs = np.hstack((xs, xs[0]))
        ys = np.hstack((ys, ys[0]))

    cx, cy = xs.mean(), ys.mean()

    # plot original
    g.new_series(xs, ys)
    g.set_x_limits(min(xs), max(xs), pad="0.1")
Example #9
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')
Example #10
0
    return polyinset


if __name__ == '__main__':
    from pylab import plot, show

    pts = [(2, 7), (4, 12), (8, 15), (16, 9), (11, 5), (8, 7), (5, 5)]
    pts = [(0, 0), (10, 0), (10, 10), (5, 20), (0, 10)]
    n = 100
    pts = [(x * n, y * n) for x, y in pts]
    #    pts = sort_clockwise(pts, pts)
    ppts = array(pts + pts[:1])
    xs, ys = ppts.T
    plot(xs, ys, 'red')

    pts = sort_clockwise(pts, pts)
    pts = convex_hull(pts)

    for i in range(20):
        #        print  i * 0.25
        opts = polygon_offset(pts, -(i + 1) * 25)
        #    #    opts = offsetpolygon(pts, -0.25)
        #    #    print opts
        opts = array(opts)
        #    #    print opts
        xs, ys, zs = opts.T
        plot(xs, ys, 'b')

    show()
# ============= EOF =============    ================================