Exemplo n.º 1
0
    def touch_to_pattern(self, touch):
        """ Find the related pattern from the touch and return it

        :param touch: Kivy touch event with fiducial

        Touch with pattern fiducial

        >>> import mock
        >>> thing = type("Thing", (TUIODragDropMixin, Widget), {})()
        >>> thing.touch_to_pattern(mock.Mock(fid=2, angle=0))
        array([[ True, False],
               [ True,  True]], dtype=bool)
        >>> thing.touch_to_pattern(mock.Mock(fid=2, angle=3))
        array([[ True,  True],
               [False,  True]], dtype=bool)

        Touch with non-pattern fiducial

        >>> event = mock.Mock(fid=101)
        >>> event.__repr__ = lambda *a: "Mock event"
        >>> thing.touch_to_pattern(event)
        Traceback (most recent call last):
        UnknownFiducialError: Mock event
        """

        fid_type, pattern = FIDUCIALS.get(touch.fid, (None, None))
        if fid_type != Types.PATTERN:
            raise UnknownFiducialError(touch)
        pattern = np.array(pattern)
        # Use the (radian) angle to find out optimum rotation. This uses the
        # index of self.rotation_array, eg. array[2] == np.pi (180deg); to
        # rotate 180deg, rot90 needs to be called with argument 2.
        rotations = np.abs(self.rotation_array - touch.angle).argmin()
        return np.rot90(pattern, rotations)
Exemplo n.º 2
0
    def touch_to_pattern(self, touch):
        """ Find the related pattern from the touch and return it

        :param touch: Kivy touch event with fiducial

        Touch with pattern fiducial

        >>> import mock
        >>> thing = type("Thing", (TUIODragDropMixin, Widget), {})()
        >>> thing.touch_to_pattern(mock.Mock(fid=2, angle=0))
        array([[ True, False],
               [ True,  True]], dtype=bool)
        >>> thing.touch_to_pattern(mock.Mock(fid=2, angle=3))
        array([[ True,  True],
               [False,  True]], dtype=bool)

        Touch with non-pattern fiducial

        >>> event = mock.Mock(fid=101)
        >>> event.__repr__ = lambda *a: "Mock event"
        >>> thing.touch_to_pattern(event)
        Traceback (most recent call last):
        UnknownFiducialError: Mock event
        """

        fid_type, pattern = FIDUCIALS.get(touch.fid, (None, None))
        if fid_type != Types.PATTERN:
            raise UnknownFiducialError(touch)
        pattern = np.array(pattern)
        # Use the (radian) angle to find out optimum rotation. This uses the
        # index of self.rotation_array, eg. array[2] == np.pi (180deg); to
        # rotate 180deg, rot90 needs to be called with argument 2.
        rotations = np.abs(self.rotation_array - touch.angle).argmin()
        return np.rot90(pattern, rotations)
Exemplo n.º 3
0
    def on_touch_down(self, touch):
        """ TUIO touch down event

        No fiducial

        >>> import mock
        >>> logging.root._log = mock.Mock()
        >>> thing = type("Thing", (TUIODragDropMixin, Widget), {"cell_coordinates": lambda s, a: a})()
        >>> thing.on_touch_down(object())

        Event fiducial

        >>> events.CustomEvent.dispatch = mock.Mock()
        >>> thing.on_touch_down(mock.Mock(fid=101, pos=(0, 0), angle=0))
        False
        >>> events.ConfirmEventWhite.dispatch.call_count
        1

        Pattern fiducial

        >>> thing.on_touch_down(mock.Mock(id=100, fid=2, pos=(0, 0), angle=0))
        >>> thing.pattern_locations
        {100: (0, 0, 2, 2)}

        Unknown fiducial

        >>> thing.on_touch_down(mock.Mock(fid=234, pos=(0, 0), angle=0))
        False
        >>> logging.root._log.call_count
        1
        >>> logging.root._log.call_args
        call(30, 'Unrecognised fiducial 234 on down', ())
        """

        # Fire custom events
        fid_type, data = FIDUCIALS.get(touch.fid, (None, None))
        if fid_type == Types.EVENT_DISPATCHER:
            Event = getattr(events, data)
            Event(touch).dispatch(self)
            return False

        # Set pattern location data
        try:
            pattern = self.touch_to_pattern(touch)
        except UnknownFiducialError:
            logging.warning("Unrecognised fiducial {} on down".format(
                touch.fid))
            return False
        if self.collide_point(*touch.pos):
            self.pattern_locations[touch.id] = \
                self.cell_coordinates(touch.pos) + pattern.shape
        else:
            self.pattern_locations[touch.id] = (None, None, None, None)
Exemplo n.º 4
0
    def on_touch_down(self, touch):
        """ TUIO touch down event

        No fiducial

        >>> import mock
        >>> logging.root._log = mock.Mock()
        >>> thing = type("Thing", (TUIODragDropMixin, Widget), {"cell_coordinates": lambda s, a: a})()
        >>> thing.on_touch_down(object())

        Event fiducial

        >>> events.CustomEvent.dispatch = mock.Mock()
        >>> thing.on_touch_down(mock.Mock(fid=101, pos=(0, 0), angle=0))
        False
        >>> events.ConfirmEventWhite.dispatch.call_count
        1

        Pattern fiducial

        >>> thing.on_touch_down(mock.Mock(id=100, fid=2, pos=(0, 0), angle=0))
        >>> thing.pattern_locations
        {100: (0, 0, 2, 2)}

        Unknown fiducial

        >>> thing.on_touch_down(mock.Mock(fid=234, pos=(0, 0), angle=0))
        False
        >>> logging.root._log.call_count
        1
        >>> logging.root._log.call_args
        call(30, 'Unrecognised fiducial 234 on down', ())
        """

        # Fire custom events
        fid_type, data = FIDUCIALS.get(touch.fid, (None, None))
        if fid_type == Types.EVENT_DISPATCHER:
            Event = getattr(events, data)
            Event(touch).dispatch(self)
            return False

        # Set pattern location data
        try:
            pattern = self.touch_to_pattern(touch)
        except UnknownFiducialError:
            logging.warning("Unrecognised fiducial {} on down".format(touch.fid))
            return False
        if self.collide_point(*touch.pos):
            self.pattern_locations[touch.id] = \
                self.cell_coordinates(touch.pos) + pattern.shape
        else:
            self.pattern_locations[touch.id] = (None, None, None, None)