123 unsigned long pos = 0;
124 std::vector<CachedSimpleWaypoint> cached_waypoints;
125 std::unordered_map<uint64_t, uint32_t> id2index;
129 memcpy(&total, &content[pos],
sizeof(total));
130 pos +=
sizeof(total);
133 for (uint32_t i=0; i < total; i++) {
135 cached_wp.
Read(content, pos);
136 cached_waypoints.push_back(cached_wp);
150 auto cached_wp = cached_waypoints.at(i);
152 std::vector<SimpleWaypointPtr> next_waypoints;
153 for (
auto id : cached_wp.next_waypoints) {
156 std::vector<SimpleWaypointPtr> previous_waypoints;
157 for (
auto id : cached_wp.previous_waypoints) {
160 wp->SetNextWaypoint(next_waypoints);
161 wp->SetPreviousWaypoint(previous_waypoints);
162 if (cached_wp.next_left_waypoint > 0) {
163 wp->SetLeftWaypoint(
dense_topology.at(id2index.at(cached_wp.next_left_waypoint)));
165 if (cached_wp.next_right_waypoint > 0) {
166 wp->SetRightWaypoint(
dense_topology.at(id2index.at(cached_wp.next_right_waypoint)));
179 assert(
_world_map !=
nullptr &&
"No map reference found.");
180 auto waypoint_topology =
_world_map->GetTopology();
183 std::unordered_map<int64_t, std::pair<std::set<crd::RoadId>, std::set<crd::RoadId>>> std_road_connectivity;
184 std::unordered_map<crd::RoadId, bool> is_real_junction;
186 for (
auto &connection : waypoint_topology) {
187 auto &waypoint = connection.first;
188 auto &successor = connection.second;
193 if (waypoint_segment_id == successor_segment_id){
198 using SegIdVectorPair = std::pair<std::vector<SegmentId>, std::vector<SegmentId>>;
199 SegIdVectorPair &connection_first = segment_topology[waypoint_segment_id];
200 SegIdVectorPair &connection_second = segment_topology[successor_segment_id];
201 connection_first.second.push_back(successor_segment_id);
202 connection_second.first.push_back(waypoint_segment_id);
205 bool waypoint_is_junction = waypoint->IsJunction();
206 bool successor_is_junction = successor->IsJunction();
207 if (waypoint_is_junction && !successor_is_junction) {
209 int64_t std_road_id =
static_cast<int64_t
>(successor->GetRoadId());
210 std_road_id = (successor->GetLaneId() < 0) ? -1 * std_road_id : std_road_id;
212 std::set<crd::RoadId> &in_paths = std_road_connectivity[std_road_id].first;
213 in_paths.insert(path_id);
215 if (in_paths.size() >= 2) {
216 for (
auto &in_path_id: in_paths) {
217 is_real_junction[in_path_id] =
true;
223 if (!waypoint_is_junction && successor_is_junction) {
225 int64_t std_road_id =
static_cast<int64_t
>(waypoint->GetRoadId());
226 std_road_id = (waypoint->GetLaneId() < 0) ? -1 * std_road_id : std_road_id;
228 std::set<crd::RoadId> &out_paths = std_road_connectivity[std_road_id].second;
229 out_paths.insert(path_id);
231 if (out_paths.size() >= 2) {
232 for (
auto &out_path_id: out_paths) {
233 is_real_junction[out_path_id] =
true;
241 assert(
_world_map !=
nullptr &&
"No map reference found.");
242 auto raw_dense_topology =
_world_map->GenerateWaypoints(MAP_RESOLUTION);
243 for (
auto &waypoint_ptr: raw_dense_topology) {
246 segment_map[
GetSegmentId(waypoint_ptr)].emplace_back(std::make_shared<SimpleWaypoint>(waypoint_ptr));
252 return cg::Math::DistanceSquared(l1, l2);
254 auto square = [](
float input) {
return std::pow(input, 2);};
256 return (swp1->GetWaypoint()->GetDistance() < swp2->GetWaypoint()->GetDistance());
259 return cg::Math::GetVectorAngle(l1, l2);
261 auto max = [](int16_t x, int16_t y) {
262 return x ^ ((x ^ y) & -(x < y));
266 for (
auto &segment: segment_map) {
267 auto &segment_waypoints = segment.second;
270 ++geodesic_grid_id_counter;
273 std::sort(segment_waypoints.begin(), segment_waypoints.end(), compare_s);
274 auto lane_id = segment_waypoints.front()->GetWaypoint()->GetLaneId();
276 std::reverse(segment_waypoints.begin(), segment_waypoints.end());
280 for (std::size_t i = 0; i < segment_waypoints.size() - 1; ++i) {
281 double distance = std::abs(segment_waypoints.at(i)->GetWaypoint()->GetDistance() - segment_waypoints.at(i+1)->GetWaypoint()->GetDistance());
282 double angle = wpt_angle(segment_waypoints.at(i)->GetTransform().GetForwardVector(), segment_waypoints.at(i+1)->GetTransform().GetForwardVector());
284 int16_t distance_splits =
static_cast<int16_t
>((distance*distance)/
MAX_WPT_DISTANCE);
285 auto max_splits = max(angle_splits, distance_splits);
286 if (max_splits >= 1) {
288 for (uint16_t j = 0; j < max_splits; ++j) {
289 auto next_waypoints = segment_waypoints.at(i)->GetWaypoint()->GetNext(distance/(max_splits+1));
290 if (next_waypoints.size() != 0) {
291 auto new_waypoint = next_waypoints.front();
293 segment_waypoints.insert(segment_waypoints.begin()+
static_cast<int64_t
>(i), std::make_shared<SimpleWaypoint>(new_waypoint));
303 cg::Location grid_edge_location = segment_waypoints.front()->GetLocation();
304 for (std::size_t i = 0; i < segment_waypoints.size() - 1; ++i) {
308 if (distance_squared(grid_edge_location, current_waypoint->GetLocation()) >
310 ++geodesic_grid_id_counter;
311 grid_edge_location = current_waypoint->GetLocation();
313 current_waypoint->SetGeodesicGridId(geodesic_grid_id_counter);
315 current_waypoint->SetNextWaypoint({next_waypoint});
316 next_waypoint->SetPreviousWaypoint({current_waypoint});
319 segment_waypoints.back()->SetGeodesicGridId(geodesic_grid_id_counter);
322 for (
auto swp: segment_waypoints) {
324 auto wpt = swp->GetWaypoint();
325 auto road_id = wpt->GetRoadId();
326 if (wpt->IsJunction() && !is_real_junction.count(road_id)) {
327 swp->SetIsJunction(
false);
329 swp->SetIsJunction(swp->GetWaypoint()->IsJunction());
339 for (
auto &segment : segment_map) {
341 auto &segment_waypoints = segment.second;
343 auto successors =
GetSuccessors(segment_id, segment_topology, segment_map);
344 auto predecessors =
GetPredecessors(segment_id, segment_topology, segment_map);
346 segment_waypoints.front()->SetPreviousWaypoint(predecessors);
347 segment_waypoints.back()->SetNextWaypoint(successors);
352 if (!swp->CheckJunction()) {
359 if (swp->GetNextWaypoint().empty()) {
360 auto neighbour = swp->GetRightWaypoint();
362 neighbour = swp->GetLeftWaypoint();
366 swp->SetNextWaypoint(neighbour->GetNextWaypoint());
367 for (
auto next_waypoint : neighbour->GetNextWaypoint()) {
368 next_waypoint->SetPreviousWaypoint({swp});
390 std::vector<SimpleWaypointPtr> next_waypoints = swp->GetNextWaypoint();
391 std::size_t next_swp_size = next_waypoints.size();
393 if (next_swp_size == 0) {
398 else if (next_swp_size > 1 || (!swp->CheckJunction() && next_waypoints.front()->CheckJunction())) {
401 bool found_landmark=
false;
402 if (next_swp_size <= 1) {
404 auto all_landmarks = swp->GetWaypoint()->GetAllLandmarksInDistance(15.0);
406 if (all_landmarks.empty()) {
410 for (
auto &landmark : all_landmarks) {
411 auto landmark_type = landmark->GetType();
412 if (landmark_type ==
"1000001" || landmark_type ==
"206" || landmark_type ==
"205") {
414 found_landmark=
true;
418 if (!found_landmark) {
426 if (found_landmark || next_swp_size > 1) {
428 for (
auto &next_swp : next_waypoints) {
429 std::vector<SimpleWaypointPtr> traversed_waypoints;
432 if (next_swp_size > 1) {
433 junction_end_waypoint = next_swp;
435 junction_end_waypoint = next_waypoints.front();
438 while (junction_end_waypoint->CheckJunction()){
439 traversed_waypoints.push_back(junction_end_waypoint);
440 std::vector<SimpleWaypointPtr> temp = junction_end_waypoint->GetNextWaypoint();
444 junction_end_waypoint = temp.front();
448 int16_t current_angle =
static_cast<int16_t
>(traversed_waypoints.front()->GetTransform().rotation.yaw);
449 int16_t junction_end_angle =
static_cast<int16_t
>(traversed_waypoints.back()->GetTransform().rotation.yaw);
450 int16_t diff_angle = (junction_end_angle - current_angle) % 360;
451 bool straight = (diff_angle < STRAIGHT_DEG && diff_angle > -
STRAIGHT_DEG) ||
454 bool right = (diff_angle >=
STRAIGHT_DEG && diff_angle <= 180) ||
455 (diff_angle <= -180 && diff_angle >= -360+
STRAIGHT_DEG);
457 auto assign_option = [](
RoadOption ro, std::vector<SimpleWaypointPtr> traversed_waypoints) {
458 for (
auto &twp : traversed_waypoints) {
459 twp->SetRoadOption(ro);
523 const WaypointPtr raw_waypoint = reference_waypoint->GetWaypoint();
524 const crd::element::LaneMarking::LaneChange lane_change = raw_waypoint->GetLaneChange();
530 case crd::element::LaneMarking::LaneChange::Left:
532 const WaypointPtr left_waypoint = raw_waypoint->GetLeft();
533 if (left_waypoint !=
nullptr &&
534 left_waypoint->GetType() == crd::Lane::LaneType::Driving &&
535 (left_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
538 reference_waypoint->SetLeftWaypoint(closest_simple_waypoint);
544 case crd::element::LaneMarking::LaneChange::Right:
546 const WaypointPtr right_waypoint = raw_waypoint->GetRight();
547 if(right_waypoint !=
nullptr &&
548 right_waypoint->GetType() == crd::Lane::LaneType::Driving &&
549 (right_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
552 reference_waypoint->SetRightWaypoint(closest_simple_waypoint);
558 case crd::element::LaneMarking::LaneChange::Both:
561 const WaypointPtr right_waypoint = raw_waypoint->GetRight();
562 if (right_waypoint !=
nullptr &&
563 right_waypoint->GetType() == crd::Lane::LaneType::Driving &&
564 (right_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
567 reference_waypoint->SetRightWaypoint(closest_simple_waypointR);
571 const WaypointPtr left_waypoint = raw_waypoint->GetLeft();
572 if (left_waypoint !=
nullptr &&
573 left_waypoint->GetType() == crd::Lane::LaneType::Driving &&
574 (left_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
577 reference_waypoint->SetLeftWaypoint(closest_simple_waypointL);