Ejemplo n.º 1
0
def init():
    """Create global objects and show plot.
    """
    global c, ic
    c = Cube(3)
    ic = c.draw_interactive()
    plt.show(block=False)
Ejemplo n.º 2
0
    # These are displayed on the LED strip.
    strip_face_colors = ['#ffffff', '#ffff00',
                         '#0000ff', '#00cf00',
                         '#50002f', '#ff0000']

    # Cube dimension.
    N = 3
    
    try:
        # Establish an Arduino connection first.
        print 'Connecting to Arduino...'
        stream = serial.Serial(serial_device, 115200)
        time.sleep(2)

        # Start LED controller.
        print 'Starting controller'
        controller = CubeLedStripContoller(stream, led_to_sticker_mapping, strip_face_colors, debug=0)
    
        # Bring up the cube visualization. Add a call back that sends
        # the cube state to the Arduino.
        c = Cube(N, face_colors=display_face_colors + ['gray', 'none'])  # Unclear what those last two colors are used for.
        c.draw_interactive(callback=controller.send_cube_state)
        plt.show()

        # Visualization was exited, shut down LED strip and serial port.
        controller.turn_off()
    except (IOError, OSError):
        # Error or CTRL-C was pressed, shut down LED strip and serial port.
        controller.turn_off()
        sys.exit(141)
Ejemplo n.º 3
0
    def capture_face(self):
        if len(self.faces) < 6:
            face = []
            for row in self.current_face:
                face.append([])
                for cell in row:
                    face[-1].append(cell)

            next_face = self.POSSIBLE_FACES[len(self.faces)]
            
            if next_face == "B":
                # DONE!!!!!
                #face = face[::-1] # Hacer espejo vertical, no horizontal
                for i in xrange(len(face)):
                    face[i] = face[i][::-1]
            elif next_face == "R":
                face = face[::-1]
                face = zip(*face[::-1])
                # Girar 90 grados
                # face = face[::-1]
                # girar 90 grados, hacer espejo horizontal
            elif next_face == "L":
                # DONE!!!!
                for i in xrange(len(face)):
                    face[i] = face[i][::-1]
                face = zip(*face[::-1])
                
            elif next_face == "F":
                # DONE!!!!!
                face = face[::-1]
            elif next_face == "U":
                face = face[::-1]
                face = zip(*face[::-1])
                pass # Girar 270 grados
            elif next_face == "D":
                face = face[::-1]
                face = zip(*face[::-1])
                pass # Espejo y 90 grados
            
            self.faces[next_face] = face
            print face

        if len(self.faces) == 6:
            colors = []
            colors.append(self.map_face( self.faces["B"] ))
            colors.append(self.map_face( self.faces["F"] ))
            colors.append(self.map_face( self.faces["R"] ))
            colors.append(self.map_face( self.faces["L"] ))
            colors.append(self.map_face( self.faces["D"] ))
            colors.append(self.map_face( self.faces["U"] ))

            
            # colors = [[0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4]]
            

            colors_rgb = {}
            colors_rgb["B"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["B"], 3)))
            colors_rgb["F"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["F"], 3)))
            colors_rgb["R"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["R"], 3)))
            colors_rgb["L"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["L"], 3)))
            colors_rgb["U"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["D"], 1)))
            colors_rgb["D"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["U"], 3)))

            rubik = Rubik(colors_rgb)
            rubik.describe()



            c = Cube(self.dimensions, None, None, colors)
            c.draw_interactive()
            plt.show()
            
            solve(Rubik(colors_rgb))
    def capture_face(self):
        if len(self.faces) < 6:
            face = []
            for row in self.current_face:
                face.append([])
                for cell in row:
                    face[-1].append(cell)

            next_face = self.POSSIBLE_FACES[len(self.faces)]

            if next_face == "B":
                # DONE!!!!!
                #face = face[::-1] # Hacer espejo vertical, no horizontal
                for i in xrange(len(face)):
                    face[i] = face[i][::-1]
            elif next_face == "R":
                face = face[::-1]
                face = zip(*face[::-1])
                # Girar 90 grados
                # face = face[::-1]
                # girar 90 grados, hacer espejo horizontal
            elif next_face == "L":
                # DONE!!!!
                for i in xrange(len(face)):
                    face[i] = face[i][::-1]
                face = zip(*face[::-1])

            elif next_face == "F":
                # DONE!!!!!
                face = face[::-1]
            elif next_face == "U":
                face = face[::-1]
                face = zip(*face[::-1])
                pass  # Girar 270 grados
            elif next_face == "D":
                face = face[::-1]
                face = zip(*face[::-1])
                pass  # Espejo y 90 grados

            self.faces[next_face] = face
            print face

        if len(self.faces) == 6:
            colors = []
            colors.append(self.map_face(self.faces["B"]))
            colors.append(self.map_face(self.faces["F"]))
            colors.append(self.map_face(self.faces["R"]))
            colors.append(self.map_face(self.faces["L"]))
            colors.append(self.map_face(self.faces["D"]))
            colors.append(self.map_face(self.faces["U"]))

            # colors = [[0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4]]

            colors_rgb = {}
            colors_rgb["B"] = self.map_face_RGB(
                self.flip_matrix(self.rotate_matrix(self.faces["B"], 3)))
            colors_rgb["F"] = self.map_face_RGB(
                self.flip_matrix(self.rotate_matrix(self.faces["F"], 3)))
            colors_rgb["R"] = self.map_face_RGB(
                self.flip_matrix(self.rotate_matrix(self.faces["R"], 3)))
            colors_rgb["L"] = self.map_face_RGB(
                self.flip_matrix(self.rotate_matrix(self.faces["L"], 3)))
            colors_rgb["U"] = self.map_face_RGB(
                self.flip_matrix(self.rotate_matrix(self.faces["D"], 1)))
            colors_rgb["D"] = self.map_face_RGB(
                self.flip_matrix(self.rotate_matrix(self.faces["U"], 3)))

            rubik = Rubik(colors_rgb)
            rubik.describe()

            c = Cube(self.dimensions, None, None, colors)
            c.draw_interactive()
            plt.show()

            solve(Rubik(colors_rgb))
Ejemplo n.º 5
0
import sys

sys.path.append("lib/MagicCube/code")

from matplotlib.pyplot import show

from cube_interactive import Cube as UICube  # pylint: disable=import-error

### Load UI

c = UICube(N=3)
c.draw_interactive()
show()
Ejemplo n.º 6
0
    def __init__(self, cube=None,
                 interactive=True,
                 view=(0, 0, 10),
                 fig=None, rect=[0, 0.16, 1, 0.84],
                 max_ticks=100,
                 simulation_interval_msecs=500,
                 callback=None,
                 **kwargs):
        # Game of Life simulation controls.
        self.t = 0
        self._max_ticks = max_ticks
        self._simulation_interval_msecs = simulation_interval_msecs
        self._init_simulation()

        # Optional call-back that receives the cube state whenever it is updated.
        self.callback = callback
        if cube is None:
            self.cube = Cube(3)
        elif isinstance(cube, Cube):
            self.cube = cube
        else:
            self.cube = Cube(cube)

        self._view = view
        self._start_rot = Quaternion.from_v_theta((1, -1, 0),
                                                  - np.pi / 6)

        if fig is None:
            fig = plt.gcf()

        # disable default key press events
        callbacks = fig.canvas.callbacks.callbacks
        del callbacks['key_press_event']

        # add some defaults, and draw axes
        kwargs.update(dict(aspect=kwargs.get('aspect', 'equal'),
                           xlim=kwargs.get('xlim', (-2.0, 2.0)),
                           ylim=kwargs.get('ylim', (-2.0, 2.0)),
                           frameon=kwargs.get('frameon', False),
                           xticks=kwargs.get('xticks', []),
                           yticks=kwargs.get('yticks', [])))
        super(GameOfLifeGui, self).__init__(fig, rect, **kwargs)
        self.xaxis.set_major_formatter(plt.NullFormatter())
        self.yaxis.set_major_formatter(plt.NullFormatter())

        self._start_xlim = kwargs['xlim']
        self._start_ylim = kwargs['ylim']

        # Define movement for up/down arrows or up/down mouse movement
        self._ax_UD = (1, 0, 0)
        self._step_UD = 0.01

        # Define movement for left/right arrows or left/right mouse movement
        self._ax_LR = (0, -1, 0)
        self._step_LR = 0.01

        self._ax_LR_alt = (0, 0, 1)

        # Internal state variable
        self._active = False  # true when mouse is over axes
        self._button1 = False  # true when button 1 is pressed
        self._button2 = False  # true when button 2 is pressed
        self._event_xy = None  # store xy position of mouse event
        self._shift = False  # shift key pressed
        self._digit_flags = np.zeros(10, dtype=bool)  # digits 0-9 pressed

        self._current_rot = self._start_rot  # current rotation state
        self._face_polys = None
        self._sticker_polys = None

        self._draw_cube()
        self._execute_cube_callback()

        # connect some GUI events
        self.figure.canvas.mpl_connect('button_press_event',
                                       self._mouse_press)
        self.figure.canvas.mpl_connect('button_release_event',
                                       self._mouse_release)
        self.figure.canvas.mpl_connect('motion_notify_event',
                                       self._mouse_motion)
        self.figure.canvas.mpl_connect('key_press_event',
                                       self._key_press)
        self.figure.canvas.mpl_connect('key_release_event',
                                       self._key_release)
        self._simulation_id = None
        self.figure.canvas.mpl_connect('draw_event',
                                       self._start_simulation_timer)
        self._initialize_widgets()

        # write some instructions
        self.figure.text(0.05, 0.05,
                         "Mouse/arrow keys adjust view\n"
                         "Up/Down keys to adjust simulation speed",
                         size=10)
Ejemplo n.º 7
0
class GameOfLifeGui(plt.Axes):
    def __init__(self, cube=None,
                 interactive=True,
                 view=(0, 0, 10),
                 fig=None, rect=[0, 0.16, 1, 0.84],
                 max_ticks=100,
                 simulation_interval_msecs=500,
                 callback=None,
                 **kwargs):
        # Game of Life simulation controls.
        self.t = 0
        self._max_ticks = max_ticks
        self._simulation_interval_msecs = simulation_interval_msecs
        self._init_simulation()

        # Optional call-back that receives the cube state whenever it is updated.
        self.callback = callback
        if cube is None:
            self.cube = Cube(3)
        elif isinstance(cube, Cube):
            self.cube = cube
        else:
            self.cube = Cube(cube)

        self._view = view
        self._start_rot = Quaternion.from_v_theta((1, -1, 0),
                                                  - np.pi / 6)

        if fig is None:
            fig = plt.gcf()

        # disable default key press events
        callbacks = fig.canvas.callbacks.callbacks
        del callbacks['key_press_event']

        # add some defaults, and draw axes
        kwargs.update(dict(aspect=kwargs.get('aspect', 'equal'),
                           xlim=kwargs.get('xlim', (-2.0, 2.0)),
                           ylim=kwargs.get('ylim', (-2.0, 2.0)),
                           frameon=kwargs.get('frameon', False),
                           xticks=kwargs.get('xticks', []),
                           yticks=kwargs.get('yticks', [])))
        super(GameOfLifeGui, self).__init__(fig, rect, **kwargs)
        self.xaxis.set_major_formatter(plt.NullFormatter())
        self.yaxis.set_major_formatter(plt.NullFormatter())

        self._start_xlim = kwargs['xlim']
        self._start_ylim = kwargs['ylim']

        # Define movement for up/down arrows or up/down mouse movement
        self._ax_UD = (1, 0, 0)
        self._step_UD = 0.01

        # Define movement for left/right arrows or left/right mouse movement
        self._ax_LR = (0, -1, 0)
        self._step_LR = 0.01

        self._ax_LR_alt = (0, 0, 1)

        # Internal state variable
        self._active = False  # true when mouse is over axes
        self._button1 = False  # true when button 1 is pressed
        self._button2 = False  # true when button 2 is pressed
        self._event_xy = None  # store xy position of mouse event
        self._shift = False  # shift key pressed
        self._digit_flags = np.zeros(10, dtype=bool)  # digits 0-9 pressed

        self._current_rot = self._start_rot  # current rotation state
        self._face_polys = None
        self._sticker_polys = None

        self._draw_cube()
        self._execute_cube_callback()

        # connect some GUI events
        self.figure.canvas.mpl_connect('button_press_event',
                                       self._mouse_press)
        self.figure.canvas.mpl_connect('button_release_event',
                                       self._mouse_release)
        self.figure.canvas.mpl_connect('motion_notify_event',
                                       self._mouse_motion)
        self.figure.canvas.mpl_connect('key_press_event',
                                       self._key_press)
        self.figure.canvas.mpl_connect('key_release_event',
                                       self._key_release)
        self._simulation_id = None
        self.figure.canvas.mpl_connect('draw_event',
                                       self._start_simulation_timer)
        self._initialize_widgets()

        # write some instructions
        self.figure.text(0.05, 0.05,
                         "Mouse/arrow keys adjust view\n"
                         "Up/Down keys to adjust simulation speed",
                         size=10)

    def _initialize_widgets(self):
        self._ax_quit = self.figure.add_axes([0.8, 0.05, 0.15, 0.075])
        self._btn_quit = widgets.Button(self._ax_quit, 'Quit')
        self._btn_quit.on_clicked(self._quit)

    def _start_simulation_timer(self, event):
        # Create a new timer object. Set the interval to self._simulation_interval_msecs
        # milliseconds (1000 is default) and tell the timer what function should be called.
        if self._simulation_id is None:
            self._timer = fig.canvas.new_timer(interval=self._simulation_interval_msecs)
            self._timer.add_callback(self._run_simulation, self)
            self._timer.start()
            self._simulation_id = 1

    def _quit(self, *args):
        plt.close()

    def _project(self, pts):
        return project_points(pts, self._current_rot, self._view, [0, 1, 0])

    def _execute_cube_callback(self):
        if self.callback:
            self.callback(self.cube.color_id())

    def _draw_cube(self):                
        stickers = self._project(self.cube._stickers)[:, :, :2]
        faces = self._project(self.cube._faces)[:, :, :2]
        face_centroids = self._project(self.cube._face_centroids[:, :3])
        sticker_centroids = self._project(self.cube._sticker_centroids[:, :3])

        plastic_color = self.cube.plastic_color       
        colors = np.array([cell_color(self._game.live[u] if u in self._game.live else 0) for u in self._game.g.nodes()])
        #colors = np.array([cell_color(self._random_index[self._game.live[u]] if u in self._game.live else 0) for u in self._game.g.nodes()])
        # colors = np.array([cell_color(self.t, max_age=160) for u in xrange(54)])
        # colors = np.array(generate_colors(54))
        #print colors
        self.t += 1
        face_zorders = -face_centroids[:, 2]
        sticker_zorders = -sticker_centroids[:, 2]

        if self._face_polys is None:
            # initial call: create polygon objects and add to axes
            self._face_polys = []
            self._sticker_polys = []

            for i in xrange(len(colors)):
                fp = plt.Polygon(faces[i], facecolor=plastic_color,
                                 zorder=face_zorders[i])
                sp = plt.Polygon(stickers[i], facecolor=colors[i],
                                 zorder=sticker_zorders[i])

                self._face_polys.append(fp)
                self._sticker_polys.append(sp)
                self.add_patch(fp)
                self.add_patch(sp)
        else:
            # subsequent call: update the polygon objects
            for i in xrange(len(colors)):
                self._face_polys[i].set_xy(faces[i])
                self._face_polys[i].set_zorder(face_zorders[i])
                self._face_polys[i].set_facecolor(plastic_color)

                self._sticker_polys[i].set_xy(stickers[i])
                self._sticker_polys[i].set_zorder(sticker_zorders[i])
                self._sticker_polys[i].set_facecolor(colors[i])

        self.figure.canvas.draw()

    def rotate(self, rot):
        self._current_rot = self._current_rot * rot

    def rotate_face(self, face, turns=1, layer=0, steps=5, execute_call_back=True):
        if not np.allclose(turns, 0):
            for _ in xrange(steps):
                self.cube.rotate_face(face, turns * 1. / steps, layer=layer)
                self._draw_cube()
            if execute_call_back:
                self._execute_cube_callback()

    def _reset_view(self, *args):
        self.set_xlim(self._start_xlim)
        self.set_ylim(self._start_ylim)
        self._current_rot = self._start_rot
        self._draw_cube()

    def _randomize_cube(self, *args):
        # Reset the cube.
        self.set_xlim(self._start_xlim)
        self.set_ylim(self._start_ylim)
        self._current_rot = self._start_rot

        # Perform a sequence of random moves.
        layer = 0
        for _ in xrange(15):
            face = GameOfLifeGui.FACES[np.random.randint(2 * self.cube.N)]
            n = np.random.randint(4)
            self.rotate_face(face, n, layer, steps=3, execute_call_back=False)
        self._draw_cube()
        self._execute_cube_callback()

    def _key_press(self, event):
        """Handler for key press events"""
        if not event.key:
            return 
        if event.key == 'up':
            print 'up'
        elif event.key == 'down':
            print 'down'

    def _key_release(self, event):
        """Handler for key release event"""
        if event.key == None:
            return
        if event.key == 'shift':
            self._shift = False

    def _mouse_press(self, event):
        """Handler for mouse button press"""
        self._event_xy = (event.x, event.y)
        if event.button == 1:
            self._button1 = True
        elif event.button == 3:
            self._button2 = True

    def _mouse_release(self, event):
        """Handler for mouse button release"""
        self._event_xy = None
        if event.button == 1:
            self._button1 = False
        elif event.button == 3:
            self._button2 = False

    def _mouse_motion(self, event):
        """Handler for mouse motion"""
        if self._button1 or self._button2:
            dx = event.x - self._event_xy[0]
            dy = event.y - self._event_xy[1]
            self._event_xy = (event.x, event.y)

            if self._button1:
                if self._shift:
                    ax_LR = self._ax_LR_alt
                else:
                    ax_LR = self._ax_LR
                rot1 = Quaternion.from_v_theta(self._ax_UD,
                                               self._step_UD * dy)
                rot2 = Quaternion.from_v_theta(ax_LR,
                                               self._step_LR * dx)
                self.rotate(rot1 * rot2)

                self._draw_cube()

            if self._button2:
                factor = 1 - 0.003 * (dx + dy)
                xlim = self.get_xlim()
                ylim = self.get_ylim()
                self.set_xlim(factor * xlim[0], factor * xlim[1])
                self.set_ylim(factor * ylim[0], factor * ylim[1])

                self.figure.canvas.draw()

    def _init_simulation(self):
        # Restart a new Game of Life simulation.
        initial_population_size = random.randint(int(0.1 * g.number_of_nodes()), g.number_of_nodes())
        print 'Initial population size', initial_population_size
        initial_population = random.sample(g.nodes(), initial_population_size)
        self._game = GameOfLife(g, initial_population)
        self._tick = -1
        self._random_index = range(1, 384)
        random.shuffle(self._random_index)

    def _run_simulation(self, *args):
        # Restart simulation on extinction or after the max # of timesteps has been reached.
        # TODO(livne): restart when a cycle of a sufficiently small size is detected and repeated enough times.
        if self._tick == self._max_ticks or not self._game.live:
            self._init_simulation()
        else:
            self._game.tick()
        self._tick += 1
#        print 'Tick', self._tick, 'live cells', ' '.join(map(str, sorted(self._game.live.iteritems())))
        print 'Tick', self._tick, 'population', len(self._game.live), 'population age', Counter(self._game.live.itervalues())
        self._draw_cube()
        self._execute_cube_callback()