debug_passage_region.py 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/env python

###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################

import itertools
20 21
import sys

22
import matplotlib.pyplot as plt
23 24

import common.proto_utils as proto_utils
25
import debug_topo
26 27 28
from modules.routing.proto.routing_pb2 import RoutingResponse
from modules.routing.proto.topo_graph_pb2 import Graph

29 30 31 32 33 34 35 36 37

color_iter = itertools.cycle(
    ['navy', 'c', 'cornflowerblue', 'gold', 'darkorange'])
g_central_curve_dict = {}
g_center_point_dict = {}


def get_center_of_passage_region(region):
    """Get center of passage region center curve"""
38
    center_points = [g_center_point_dict[seg.id] for seg in region.segment]
39 40 41 42 43 44 45 46 47 48
    return center_points[len(center_points) // 2]


def plot_region(region, color):
    "Plot passage region"
    for seg in region.segment:
        center_pt = debug_topo.plot_central_curve_with_s_range(
            g_central_curve_dict[seg.id], seg.start_s, seg.end_s, color=color)
        debug_topo.draw_id(center_pt, seg.id, 'r')
        g_center_point_dict[seg.id] = center_pt
49 50
        print('Plot lane id: %s, start s: %f, end s: %f' % (seg.id, seg.start_s,
                                                            seg.end_s))
51 52 53


def plot_lane_change(lane_change, passage_regions):
A
Aaron Xiao 已提交
54
    """Plot lane change information"""
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    st_idx = lane_change.start_passage_region_index
    ed_idx = lane_change.end_passage_region_index
    from_pt = get_center_of_passage_region(passage_regions[st_idx])
    to_pt = get_center_of_passage_region(passage_regions[ed_idx])
    plt.gca().annotate(
        "",
        xy=(to_pt[0], to_pt[1]),
        xytext=(from_pt[0], from_pt[1]),
        arrowprops=dict(
            facecolor='blue', edgecolor='none', alpha=0.7, shrink=0.05))


def plot_road(road):
    """Plot road"""
    for region in road.passage_region:
        plot_region(region, 'green')
    for lane_change in road.lane_change_info:
        plot_lane_change(lane_change, road.passage_region)


def plot_junction(junction):
    """Plot junction"""
    plot_region(junction.passage_region, 'red')


def plot_result(routing_result, central_curve_dict):
    """Plot routing result"""
    plt.close()
    plt.figure()
    for way in routing_result.route:
        if way.HasField("road_info"):
            plot_road(way.road_info)
        else:
            plot_junction(way.junction_info)

    plt.gca().set_aspect(1)
    plt.title('Passage region')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()

    plt.draw()


def print_help():
    """Print help information.

    Print help information of usage.

    Args:

    """
107 108
    print('usage:')
    print('     python debug_topo.py file_path, then', end=' ')
109 110 111 112 113 114 115 116 117 118 119
    print_help_command()


def print_help_command():
    """Print command help information.

    Print help information of command.

    Args:

    """
120 121 122
    print('type in command: [q] [r]')
    print('         q               exit')
    print('         p               plot passage region')
123 124 125 126 127 128


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print_help()
        sys.exit(0)
129
    print('Please wait for loading data...')
130

131 132
    topo_graph_file = sys.argv[1]
    graph = proto_utils.get_pb_from_bin_file(topo_graph_file, Graph())
133
    g_central_curve_dict = {nd.lane_id: nd.central_curve for nd in graph.node}
134 135 136 137

    plt.ion()
    while 1:
        print_help_command()
138
        print('cmd>', end=' ')
139 140 141 142 143 144
        instruction = raw_input()
        argv = instruction.strip(' ').split(' ')
        if len(argv) == 1:
            if argv[0] == 'q':
                sys.exit(0)
            elif argv[0] == 'p':
145 146 147
                routing_result_file = sys.argv[2]
                result = proto_utils.get_pb_from_bin_file(routing_result_file,
                                                          RoutingResponse())
148 149
                plot_result(result, g_central_curve_dict)
            else:
150
                print('[ERROR] wrong command')
151 152 153
            continue

        else:
154
            print('[ERROR] wrong arguments')
155
            continue