playback_pygame.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import pygame
  2. import sys
  3. import pandas as pd
  4. import os
  5. import numpy as np
  6. # 初始化Pygame
  7. pygame.init()
  8. # 设置常量
  9. WIDTH, HEIGHT = 1200, 1000
  10. BACKGROUND_COLOR = (255, 255, 255)
  11. LEFT_LANE_COLOR = (0, 128, 255) # 左车道线颜色
  12. RIGHT_LANE_COLOR = (255, 0, 0) # 右车道线颜色
  13. CENTER_LINE_COLOR = (0, 255, 0) # 中心线颜色
  14. VEHICLE_COLOR_DEFAULT = (0, 0, 255) # 默认车辆颜色
  15. X_OFFSET = 258109.4239876
  16. Y_OFFSET = 4149969.964821
  17. # 创建颜色映射
  18. vehicle_colors = {
  19. 1: (255, 0, 0), # 红色
  20. }
  21. # 提供一些额外的颜色
  22. additional_colors = [
  23. (0, 0, 255), # 蓝色
  24. (0, 255, 0), # 绿色
  25. (255, 165, 0), # 橙色
  26. (128, 0, 128), # 紫色
  27. (255, 255, 0), # 黄色
  28. ]
  29. # 创建窗口
  30. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  31. pygame.display.set_caption("Lane Visualization")
  32. font = pygame.font.Font(None, 24) # 默认字体
  33. # 读取数据
  34. csv_data_path = "./"
  35. csv_path = os.path.join(csv_data_path, 'LaneMap.csv')
  36. df = pd.read_csv(csv_path)
  37. car_csv_path = os.path.join(csv_data_path, 'ObjState.csv')
  38. car_df = pd.read_csv(car_csv_path)
  39. # 提取车道线数据
  40. left_lane_x = df['lane_left_x'].values
  41. left_lane_y = df['lane_left_y'].values
  42. right_lane_x = df['lane_right_x'].values
  43. right_lane_y = df['lane_right_y'].values
  44. center_line_x = df['centerLine_x'].values
  45. center_line_y = df['centerLine_y'].values
  46. # 提取车辆数据,将车辆信息按 playerId 分组
  47. car_ids = car_df['playerId'].unique()
  48. car_data = {player_id: car_df[car_df['playerId'] == player_id].reset_index(drop=True) for player_id in car_ids}
  49. # 为未分配颜色的车辆分配颜色
  50. for idx, player_id in enumerate(car_ids):
  51. print(player_id)
  52. if player_id not in vehicle_colors:
  53. vehicle_colors[player_id] = additional_colors[idx % len(additional_colors)]
  54. # 计算坐标范围
  55. min_x = min(left_lane_x.min(), right_lane_x.min(), center_line_x.min())
  56. max_x = max(left_lane_x.max(), right_lane_x.max(), center_line_x.max())
  57. min_y = min(left_lane_y.min(), right_lane_y.min(), center_line_y.min())
  58. max_y = max(left_lane_y.max(), right_lane_y.max(), center_line_y.max())
  59. # 计算缩放比例
  60. scale_x = WIDTH / (max_x - min_x)
  61. scale_y = HEIGHT / (max_y - min_y)
  62. scale = min(scale_x, scale_y)
  63. # 计算偏移量
  64. offset_x = -min_x * scale
  65. offset_y = -min_y * scale
  66. # 主循环
  67. car_indices = {player_id: 0 for player_id in car_ids} # 保存每辆车的当前索引
  68. while True:
  69. for event in pygame.event.get():
  70. if event.type == pygame.QUIT:
  71. pygame.quit()
  72. sys.exit()
  73. # 清屏
  74. screen.fill(BACKGROUND_COLOR)
  75. # 绘制车道线
  76. for i in range(len(left_lane_x) - 1):
  77. start_pos = (left_lane_x[i] * scale + offset_x, HEIGHT - (left_lane_y[i] * scale + offset_y))
  78. end_pos = (left_lane_x[i + 1] * scale + offset_x, HEIGHT - (left_lane_y[i + 1] * scale + offset_y))
  79. pygame.draw.line(screen, LEFT_LANE_COLOR, start_pos, end_pos, 2)
  80. for i in range(len(right_lane_x) - 1):
  81. start_pos = (right_lane_x[i] * scale + offset_x, HEIGHT - (right_lane_y[i] * scale + offset_y))
  82. end_pos = (right_lane_x[i + 1] * scale + offset_x, HEIGHT - (right_lane_y[i + 1] * scale + offset_y))
  83. pygame.draw.line(screen, RIGHT_LANE_COLOR, start_pos, end_pos, 2)
  84. for i in range(len(center_line_x) - 1):
  85. start_pos = (center_line_x[i] * scale + offset_x, HEIGHT - (center_line_y[i] * scale + offset_y))
  86. end_pos = (center_line_x[i + 1] * scale + offset_x, HEIGHT - (center_line_y[i + 1] * scale + offset_y))
  87. pygame.draw.line(screen, CENTER_LINE_COLOR, start_pos, end_pos, 2)
  88. # 绘制车辆
  89. for player_id, car_df in car_data.items():
  90. car_index = car_indices[player_id]
  91. if car_index < len(car_df):
  92. posX = (car_df['posX'][car_index] + X_OFFSET)* scale + offset_x
  93. posY = HEIGHT - ((car_df['posY'][car_index] + Y_OFFSET) * scale + offset_y) # 反转Y坐标
  94. posH = car_df['posH'][car_index] # 车辆方向(弧度)
  95. # 计算矩形的四个角点
  96. # vehicle_length = car_df['dimX'][car_index] * scale # 车辆长度,以像素表示
  97. # vehicle_width = car_df['dimY'][car_index] * scale # 车辆宽度,以像素表示
  98. vehicle_length = 5.0 * scale # 车辆长度,以像素表示
  99. vehicle_width = 2.0 * scale # 车辆宽度,以像素表示
  100. corners = np.array([
  101. [-vehicle_length / 2, -vehicle_width / 2],
  102. [ vehicle_length / 2, -vehicle_width / 2],
  103. [ vehicle_length / 2, vehicle_width / 2],
  104. [-vehicle_length / 2, vehicle_width / 2]
  105. ])
  106. # 旋转矩形
  107. rotation_matrix = np.array([
  108. [np.cos(posH), -np.sin(posH)],
  109. [np.sin(posH), np.cos(posH)]
  110. ])
  111. rotated_corners = corners.dot(rotation_matrix) + np.array([posX, posY])
  112. # 获取车辆颜色
  113. vehicle_color = vehicle_colors[player_id]
  114. # 创建矩形对象并绘制
  115. pygame.draw.polygon(screen, vehicle_color, rotated_corners)
  116. # 更新车辆的索引
  117. car_indices[player_id] += 1
  118. # 如果车的索引超过数据长度,重置为0
  119. if car_indices[player_id] >= len(car_df):
  120. car_indices[player_id] = 0
  121. # 更新显示
  122. pygame.display.flip()
  123. # 控制帧率
  124. pygame.time.Clock().tick(60) # 每秒60帧