123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import pygame
- import sys
- import pandas as pd
- import os
- import numpy as np
- pygame.init()
- WIDTH, HEIGHT = 1200, 1000
- BACKGROUND_COLOR = (255, 255, 255)
- LEFT_LANE_COLOR = (0, 128, 255)
- RIGHT_LANE_COLOR = (255, 0, 0)
- CENTER_LINE_COLOR = (0, 255, 0)
- VEHICLE_COLOR_DEFAULT = (0, 0, 255)
- X_OFFSET = 258109.4239876
- Y_OFFSET = 4149969.964821
- vehicle_colors = {
- 1: (255, 0, 0),
- }
- additional_colors = [
- (0, 0, 255),
- (0, 255, 0),
- (255, 165, 0),
- (128, 0, 128),
- (255, 255, 0),
- ]
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Lane Visualization")
- font = pygame.font.Font(None, 24)
- csv_data_path = "./"
- csv_path = os.path.join(csv_data_path, 'LaneMap.csv')
- df = pd.read_csv(csv_path)
- car_csv_path = os.path.join(csv_data_path, 'ObjState.csv')
- car_df = pd.read_csv(car_csv_path)
- left_lane_x = df['lane_left_x'].values
- left_lane_y = df['lane_left_y'].values
- right_lane_x = df['lane_right_x'].values
- right_lane_y = df['lane_right_y'].values
- center_line_x = df['centerLine_x'].values
- center_line_y = df['centerLine_y'].values
- car_ids = car_df['playerId'].unique()
- car_data = {player_id: car_df[car_df['playerId'] == player_id].reset_index(drop=True) for player_id in car_ids}
- for idx, player_id in enumerate(car_ids):
- print(player_id)
- if player_id not in vehicle_colors:
- vehicle_colors[player_id] = additional_colors[idx % len(additional_colors)]
- min_x = min(left_lane_x.min(), right_lane_x.min(), center_line_x.min())
- max_x = max(left_lane_x.max(), right_lane_x.max(), center_line_x.max())
- min_y = min(left_lane_y.min(), right_lane_y.min(), center_line_y.min())
- max_y = max(left_lane_y.max(), right_lane_y.max(), center_line_y.max())
- scale_x = WIDTH / (max_x - min_x)
- scale_y = HEIGHT / (max_y - min_y)
- scale = min(scale_x, scale_y)
- offset_x = -min_x * scale
- offset_y = -min_y * scale
- car_indices = {player_id: 0 for player_id in car_ids}
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
-
- screen.fill(BACKGROUND_COLOR)
-
- for i in range(len(left_lane_x) - 1):
- start_pos = (left_lane_x[i] * scale + offset_x, HEIGHT - (left_lane_y[i] * scale + offset_y))
- end_pos = (left_lane_x[i + 1] * scale + offset_x, HEIGHT - (left_lane_y[i + 1] * scale + offset_y))
- pygame.draw.line(screen, LEFT_LANE_COLOR, start_pos, end_pos, 2)
- for i in range(len(right_lane_x) - 1):
- start_pos = (right_lane_x[i] * scale + offset_x, HEIGHT - (right_lane_y[i] * scale + offset_y))
- end_pos = (right_lane_x[i + 1] * scale + offset_x, HEIGHT - (right_lane_y[i + 1] * scale + offset_y))
- pygame.draw.line(screen, RIGHT_LANE_COLOR, start_pos, end_pos, 2)
- for i in range(len(center_line_x) - 1):
- start_pos = (center_line_x[i] * scale + offset_x, HEIGHT - (center_line_y[i] * scale + offset_y))
- end_pos = (center_line_x[i + 1] * scale + offset_x, HEIGHT - (center_line_y[i + 1] * scale + offset_y))
- pygame.draw.line(screen, CENTER_LINE_COLOR, start_pos, end_pos, 2)
-
- for player_id, car_df in car_data.items():
- car_index = car_indices[player_id]
- if car_index < len(car_df):
- posX = (car_df['posX'][car_index] + X_OFFSET)* scale + offset_x
- posY = HEIGHT - ((car_df['posY'][car_index] + Y_OFFSET) * scale + offset_y)
- posH = car_df['posH'][car_index]
-
-
-
- vehicle_length = 5.0 * scale
- vehicle_width = 2.0 * scale
- corners = np.array([
- [-vehicle_length / 2, -vehicle_width / 2],
- [ vehicle_length / 2, -vehicle_width / 2],
- [ vehicle_length / 2, vehicle_width / 2],
- [-vehicle_length / 2, vehicle_width / 2]
- ])
-
- rotation_matrix = np.array([
- [np.cos(posH), -np.sin(posH)],
- [np.sin(posH), np.cos(posH)]
- ])
- rotated_corners = corners.dot(rotation_matrix) + np.array([posX, posY])
-
- vehicle_color = vehicle_colors[player_id]
-
- pygame.draw.polygon(screen, vehicle_color, rotated_corners)
-
- car_indices[player_id] += 1
-
- if car_indices[player_id] >= len(car_df):
- car_indices[player_id] = 0
-
- pygame.display.flip()
-
- pygame.time.Clock().tick(60)
|