@@ -1200,12 +1200,23 @@ int load_map_binary(const char *filename, Drive *drive) {
12001200 fclose (file );
12011201 return -1 ;
12021202 }
1203+ if (fread (& road -> length , sizeof (float ), 1 , file ) != 1 ) {
1204+ fclose (file );
1205+ return -1 ;
1206+ }
1207+ road -> cum_lengths = (float * ) malloc (slen * sizeof (float ));
1208+ if ((size_t ) slen > 0 && fread (road -> cum_lengths , sizeof (float ), slen , file ) != (size_t ) slen ) {
1209+ fclose (file );
1210+ return -1 ;
1211+ }
12031212 } else {
12041213 road -> num_entries = 0 ;
12051214 road -> num_exits = 0 ;
12061215 road -> entry_lanes = NULL ;
12071216 road -> exit_lanes = NULL ;
12081217 road -> speed_limit = 0.0f ;
1218+ road -> length = 0.0f ;
1219+ road -> cum_lengths = NULL ;
12091220 }
12101221 }
12111222
@@ -1286,19 +1297,13 @@ int load_map_binary(const char *filename, Drive *drive) {
12861297 }
12871298 drive -> lane_graph .n_lanes = n_lanes_graph ;
12881299 drive -> lane_graph .lane_ids = NULL ;
1289- drive -> lane_graph .lane_lengths = NULL ;
12901300 drive -> lane_graph .distances = NULL ;
12911301 if (n_lanes_graph > 0 ) {
12921302 drive -> lane_graph .lane_ids = (int * ) malloc (n_lanes_graph * sizeof (int ));
12931303 if (fread (drive -> lane_graph .lane_ids , sizeof (int ), n_lanes_graph , file ) != (size_t ) n_lanes_graph ) {
12941304 fclose (file );
12951305 return -1 ;
12961306 }
1297- drive -> lane_graph .lane_lengths = (float * ) malloc (n_lanes_graph * sizeof (float ));
1298- if (fread (drive -> lane_graph .lane_lengths , sizeof (float ), n_lanes_graph , file ) != (size_t ) n_lanes_graph ) {
1299- fclose (file );
1300- return -1 ;
1301- }
13021307 drive -> lane_graph .distances = (float * ) malloc (n_lanes_graph * n_lanes_graph * sizeof (float ));
13031308 if (fread (drive -> lane_graph .distances , sizeof (float ), n_lanes_graph * n_lanes_graph , file )
13041309 != (size_t ) (n_lanes_graph * n_lanes_graph )) {
@@ -1366,13 +1371,7 @@ int load_map_binary(const char *filename, Drive *drive) {
13661371
13671372// Compute the length of a lane
13681373static float compute_lane_length (RoadMapElement * lane ) {
1369- float length = 0.0f ;
1370- for (int i = 1 ; i < lane -> segment_length ; i ++ ) {
1371- float dx = lane -> x [i ] - lane -> x [i - 1 ];
1372- float dy = lane -> y [i ] - lane -> y [i - 1 ];
1373- length += sqrtf (dx * dx + dy * dy );
1374- }
1375- return length ;
1374+ return lane -> length ;
13761375}
13771376
13781377// Compute the remaining distance on a lane from a given position to the end of the lane
@@ -1409,23 +1408,9 @@ static float compute_remaining_lane_distance(RoadMapElement *lane, float pos_x,
14091408 }
14101409 }
14111410
1412- // Compute remaining distance from closest point to end of lane
1413- float remaining = 0.0f ;
1414-
1415- // Partial distance in current segment (from t to end of segment)
1416- float dx = lane -> x [closest_seg + 1 ] - lane -> x [closest_seg ];
1417- float dy = lane -> y [closest_seg + 1 ] - lane -> y [closest_seg ];
1418- float seg_len = sqrtf (dx * dx + dy * dy );
1419- remaining += (1.0f - closest_t ) * seg_len ;
1420-
1421- // Full distance of remaining segments
1422- for (int i = closest_seg + 1 ; i < lane -> segment_length - 1 ; i ++ ) {
1423- dx = lane -> x [i + 1 ] - lane -> x [i ];
1424- dy = lane -> y [i + 1 ] - lane -> y [i ];
1425- remaining += sqrtf (dx * dx + dy * dy );
1426- }
1427-
1428- return remaining ;
1411+ float progress = lane -> cum_lengths [closest_seg ]
1412+ + closest_t * (lane -> cum_lengths [closest_seg + 1 ] - lane -> cum_lengths [closest_seg ]);
1413+ return fmaxf (0.0f , lane -> length - progress );
14291414}
14301415
14311416static float compute_lane_end_distance_sq (RoadMapElement * lane , float origin_x , float origin_y ) {
0 commit comments