Skip to content

Commit 9f740ed

Browse files
authored
Add length and cumulative lengths to RoadMapElement (#425)
1 parent aeba458 commit 9f740ed

13 files changed

Lines changed: 21 additions & 34 deletions

pufferlib/ocean/drive/datatypes.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ struct RoadMapElement {
291291
int num_exits;
292292
int *exit_lanes;
293293
float speed_limit;
294+
float length;
295+
float *cum_lengths;
294296
};
295297

296298
struct TrafficControlElement {
@@ -313,7 +315,6 @@ typedef struct {
313315
struct LaneGraph {
314316
int n_lanes;
315317
int *lane_ids;
316-
float *lane_lengths;
317318
float *distances; // n_lanes * n_lanes row-major
318319
};
319320

@@ -339,6 +340,7 @@ void free_road_element(struct RoadMapElement *element) {
339340
free(element->headings);
340341
free(element->entry_lanes);
341342
free(element->exit_lanes);
343+
free(element->cum_lengths);
342344
}
343345

344346
void free_traffic_element(struct TrafficControlElement *element) {
@@ -348,6 +350,5 @@ void free_traffic_element(struct TrafficControlElement *element) {
348350

349351
void free_lane_graph(struct LaneGraph *graph) {
350352
free(graph->lane_ids);
351-
free(graph->lane_lengths);
352353
free(graph->distances);
353354
}

pufferlib/ocean/drive/drive.h

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
13681373
static 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

14311416
static float compute_lane_end_distance_sq(RoadMapElement *lane, float origin_x, float origin_y) {
4.47 KB
Binary file not shown.
2.53 KB
Binary file not shown.
11.7 KB
Binary file not shown.
Binary file not shown.
17.2 KB
Binary file not shown.
15.7 KB
Binary file not shown.
7.76 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)