|
@@ -4,7 +4,7 @@ from typing import List, Dict, Tuple
|
|
|
import numpy as np
|
|
|
import pandas as pd
|
|
|
|
|
|
-
|
|
|
+
|
|
|
def dict2json(data_dict: Dict, file_path: str) -> None:
|
|
|
"""
|
|
|
将字典转换为JSON格式并保存到文件中。
|
|
@@ -41,24 +41,24 @@ def get_interpolation(x: float, point1: Tuple[float, float], point2: Tuple[float
|
|
|
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
-def get_frame_with_time(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
|
|
|
+def get_frame_with_time_fast(time_to_frame, target_time):
|
|
|
"""
|
|
|
- 将两个DataFrame按照时间列进行合并,并返回结果。
|
|
|
-
|
|
|
- 参数:
|
|
|
- df1: 包含start_time和end_time的DataFrame。
|
|
|
- df2: 包含simTime和simFrame的DataFrame。
|
|
|
-
|
|
|
- 返回:
|
|
|
- 合并后的DataFrame。
|
|
|
+ 使用字典快速查找最接近的帧号(假设时间戳是精确匹配的)
|
|
|
"""
|
|
|
- df1_start = df1.merge(df2[["simTime", "simFrame"]], left_on="start_time", right_on="simTime")
|
|
|
- df1_start = df1_start[["start_time", "simFrame"]].rename(columns={"simFrame": "start_frame"})
|
|
|
+ # 如果目标时间存在精确匹配
|
|
|
+ if target_time in time_to_frame:
|
|
|
+ return time_to_frame[target_time]
|
|
|
+
|
|
|
+ # 否则找最接近的时间(如果无法保证精确匹配)
|
|
|
+ closest_time = min(time_to_frame.keys(), key=lambda t: abs(t - target_time))
|
|
|
+ return time_to_frame[closest_time]
|
|
|
|
|
|
- df1_end = df1.merge(df2[["simTime", "simFrame"]], left_on="end_time", right_on="simTime")
|
|
|
- df1_end = df1_end[["end_time", "simFrame"]].rename(columns={"simFrame": "end_frame"})
|
|
|
|
|
|
- return pd.concat([df1_start, df1_end], axis=1)
|
|
|
+# 辅助函数
|
|
|
+def get_frame_with_time(time_list, frame_list, target_time):
|
|
|
+ time_array = np.array(time_list)
|
|
|
+ idx = np.argmin(np.abs(time_array - target_time))
|
|
|
+ return frame_list[idx]
|
|
|
|
|
|
|
|
|
class PolynomialCurvatureFitting:
|
|
@@ -117,7 +117,8 @@ class PolynomialCurvatureFitting:
|
|
|
distances = np.sqrt((x1 - window_means[:, 0]) ** 2 + (y1 - window_means[:, 1]) ** 2)
|
|
|
return np.argmin(distances)
|
|
|
|
|
|
- def find_projection(self, x_point: float, y_point: float, polynomial: np.poly1d, x_data_range: Tuple[float, float], search_step: float = 0.0001) -> Tuple[float, float, float]:
|
|
|
+ def find_projection(self, x_point: float, y_point: float, polynomial: np.poly1d, x_data_range: Tuple[float, float],
|
|
|
+ search_step: float = 0.0001) -> Tuple[float, float, float]:
|
|
|
"""
|
|
|
找到目标点在多项式曲线上的投影点。
|
|
|
|
|
@@ -154,7 +155,8 @@ class PolynomialCurvatureFitting:
|
|
|
x_window = self.x_data[best_start:best_start + window_size]
|
|
|
y_window = self.y_data[best_start:best_start + window_size]
|
|
|
coefficients, polynomial = self.polynomial_fit(x_window, y_window)
|
|
|
- proj_x, proj_y, min_distance = self.find_projection(point[0], point[1], polynomial, (min(x_window), max(x_window)))
|
|
|
+ proj_x, proj_y, min_distance = self.find_projection(point[0], point[1], polynomial,
|
|
|
+ (min(x_window), max(x_window)))
|
|
|
curvature_value = self.curvature(coefficients, proj_x)
|
|
|
second_derivative_coefficients = np.polyder(np.polyder(coefficients))
|
|
|
curvature_change_value = np.polyval(second_derivative_coefficients, proj_x)
|
|
@@ -169,7 +171,6 @@ class PolynomialCurvatureFitting:
|
|
|
|
|
|
return results
|
|
|
|
|
|
-
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
data_path = "/home/kevin/kevin/zhaoyuan/zhaoyuan/data/raw/data/LaneMap.csv"
|