Пример #1
0
def create_sidebar(cr, width, height, sim_state, id_vehicle, id_episode,  # @UnusedVariable
                   timestamp, observations_values,
                   commands_values, commands_source,
                   bg_color=None,
                   show_observations=True,
                   show_commands=True,
                   show_annotations=True):

    if len(commands_values.shape) == 1:
        commands_values = np.array([commands_values.tolist()])

    commands_rgb = posneg(commands_values,
                          max_value=(+1),  # not sure +1 
                          nan_color=[1, 1, 1])

    observations_rgb = scale(reshape2d(observations_values), min_value=0,
                         nan_color=[1, 1, 1])

    import cairo
    if bg_color is not None:
        cr.rectangle(0, 0, width, height)
        cr.set_source_rgb(bg_color[0], bg_color[1], bg_color[2])
        cr.fill()

    fo = cairo.FontOptions()  # @UndefinedVariable
    fo.set_hint_style(cairo.HINT_STYLE_FULL)  # @UndefinedVariable
    fo.set_antialias(cairo.ANTIALIAS_GRAY)  # @UndefinedVariable
    cr.set_font_options(fo)

    # M = width / 20.0
    M = width / 15.0

    legend_font_size = M * 0.75
    details_font_size = M

    label_font = 'Mono'
    legend_font = 'Serif'

    cr.set_source_rgb(0, 0, 0)

    padding_fraction = 0.1
    padding = width * padding_fraction
    nvalues = 128
    bar_width = 0.4 * width
    bar_ratio = 0.15
    bar_height = bar_width * bar_ratio
    spacer = 0.05 * width

    values = np.linspace(-1, +1, nvalues)
    values = np.vstack([values] * 1)
    colorbar_posneg = posneg(values)
    values = np.linspace(-1, +1, nvalues)
    values = np.vstack([values] * 1)
    colorbar_scale = scale(values)

    cr.translate(0, 2 * M)

    if show_observations:
        with cairo_transform(cr, t=[width / 2, 0]):
            cr.select_font_face(label_font)
            cr.set_font_size(M)
            cairo_text_align(cr, 'observations', halign='center')

        cr.translate(0, M * 0.8)

        with cairo_transform(cr, t=[padding, 0]):
            data_width = width - 2 * padding
            # Don't draw grid if there are many pixels
            if max(observations_rgb.shape[0], observations_rgb.shape[1]) > 15:
                grid_color = None
            else:
                grid_color = [1, .9, .9]

            last_height = cairo_pixels(cr, observations_rgb, width=data_width,
                                       # Force square
                                       height=data_width,
                                       grid_color=grid_color)

        cr.translate(0, last_height)

        cr.translate(0, spacer)

        with cairo_transform(cr, t=[width / 2, 0]):
            with cairo_transform(cr, t=[-bar_width / 2, 0]):
                last_height = cairo_pixels(cr, colorbar_scale,
                             bar_width, height=bar_height,
                              grid_color=None)

            cr.set_font_size(legend_font_size)
            cr.select_font_face(legend_font)

            with cairo_transform(cr, t=[0, bar_height / 2]):
                with cairo_transform(cr, t=[-bar_width / 2 - M / 2, 0]):
                    cairo_text_align(cr, '0', 'right', 'middle')
                with cairo_transform(cr, t=[+bar_width / 2 + M / 2, 0]):
                    cairo_text_align(cr, '1', 'left', 'middle')

        cr.translate(0, last_height + spacer * 3)

    if show_commands:
        with cairo_transform(cr, t=[width / 2, 0]):
            cr.select_font_face(label_font)
            cr.set_font_size(M)
            cairo_text_align(cr, 'commands', halign='center')
        cr.translate(0, M * 0.8)

        padding = padding * 2
        with cairo_transform(cr, t=[padding, 0]):
            data_width = width - 2 * padding
            last_height = cairo_pixels(cr, commands_rgb, data_width)
        cr.translate(0, last_height)

        cr.translate(0, spacer)

        with cairo_transform(cr, t=[width / 2, 0]):
            with cairo_transform(cr, t=[-bar_width / 2, 0]):
                last_height = cairo_pixels(cr, colorbar_posneg,
                             bar_width, height=bar_width * bar_ratio,
                              grid_color=None)

            cr.set_font_size(legend_font_size)
            cr.select_font_face(legend_font)

            with cairo_transform(cr, t=[0, bar_height / 2]):
                with cairo_transform(cr, t=[-bar_width / 2 - M / 2, 0]):
                    cairo_text_align(cr, '-1', 'right', 'middle')
                with cairo_transform(cr, t=[+bar_width / 2 + M / 2, 0]):
                    cairo_text_align(cr, '+1', 'left', 'middle')

        cr.translate(0, last_height + spacer * 2)

    if show_annotations:
        cr.translate(width / 10, 0)
        strings = ['vehicle: %s' % id_vehicle,
                   '  agent: %s' % commands_source,
                   'episode: %s' % id_episode,
                   '   time: %6.2f' % timestamp,
                   ]
        cr.select_font_face('Mono')

        max_len = max(len(x) for x in strings)
        padding = 5
        font_size = 1.6 * width / (max_len + padding)
        cr.set_font_size(font_size)
        line = details_font_size * 1.2
        for s in strings:
            with cairo_save(cr):
                cr.show_text(s)
                cr.stroke()
            cr.translate(0, line)
Пример #2
0
    def draw_everything(self, cr):

        boot_obs = self.input.boot_obs

        if 'id_episode' in boot_obs:
            id_episode = boot_obs['id_episode'].item()
        else:
            id_episode = ''
            
        id_vehicle = boot_obs['id_robot'].item()
        
        if 'extra' in boot_obs:
            extra = boot_obs['extra'].item()
        else:
            extra = {}

        def extra_draw_world(cr):
            if 'servonav' in extra:
                plot_servonave(cr, extra['servonav'])

            if 'servoing_poses' in extra:
                plot_servoing_poses(cr, extra['servoing_poses'])

        plotting_params = self.config.plotting_params

        plotting_params['extra_draw_world'] = extra_draw_world

        sidebar_params = self.config.sidebar_params

        # todo: check
        sim_state = extra['robot_state']

        observations_values = boot_obs['observations']

        commands = boot_obs['commands']

        commands_source = boot_obs['commands_source'].item()
        timestamp = boot_obs['time_from_episode_start'].item()

        with cairo_save(cr):
            if self.config.display_sidebar:
                padding = 0.03 * self.config.width
                map_width = self.config.width - 2 * padding
                map_height = self.config.height - 2 * padding
                cr.translate(padding, padding)
            else:
                map_width = self.config.width
                map_height = self.config.height

            with cairo_save(cr):
                cr.rectangle(0, 0, map_width, map_height)
                cr.clip()

                # TODO: implement trace

                vehicles_cairo_display_all(cr,
                                       map_width,
                                       map_height,
                                       sim_state,
                                       **plotting_params)

            if self.config.display_sidebar:
                cr.set_line_width(1)
                cr.set_source_rgb(0, 0, 0)
                cr.rectangle(0, 0, map_width, map_height)
                cr.stroke()

        if self.config.display_sidebar:
            with cairo_transform(cr, t=[self.config.width, 0]):
                create_sidebar(cr, width=self.config.sidebar_width,
                               height=self.config.height,
                               sim_state=sim_state,
                               id_vehicle=id_vehicle,
                               id_episode=id_episode,
                               timestamp=timestamp,
                               observations_values=observations_values,
                               commands_values=commands,
                               commands_source=commands_source,
                               **sidebar_params)