113 unsigned long pos = 0;
114 std::vector<CachedSimpleWaypoint> cached_waypoints;
115 std::unordered_map<uint64_t, uint32_t> id2index;
119 memcpy(&total, &content[pos],
sizeof(total));
120 pos +=
sizeof(total);
123 for (uint32_t i=0; i < total; i++) {
125 cached_wp.
Read(content, pos);
126 cached_waypoints.push_back(cached_wp);
140 auto cached_wp = cached_waypoints.at(i);
142 std::vector<SimpleWaypointPtr> next_waypoints;
143 for (
auto id : cached_wp.next_waypoints) {
146 std::vector<SimpleWaypointPtr> previous_waypoints;
147 for (
auto id : cached_wp.previous_waypoints) {
150 wp->SetNextWaypoint(next_waypoints);
151 wp->SetPreviousWaypoint(previous_waypoints);
152 if (cached_wp.next_left_waypoint > 0) {
153 wp->SetLeftWaypoint(
dense_topology.at(id2index.at(cached_wp.next_left_waypoint)));
155 if (cached_wp.next_right_waypoint > 0) {
156 wp->SetRightWaypoint(
dense_topology.at(id2index.at(cached_wp.next_right_waypoint)));
169 assert(
_world_map !=
nullptr &&
"No map reference found.");
170 auto waypoint_topology =
_world_map->GetTopology();
173 std::unordered_map<int64_t, std::pair<std::set<crd::RoadId>, std::set<crd::RoadId>>> std_road_connectivity;
174 std::unordered_map<crd::RoadId, bool> is_real_junction;
176 for (
auto &connection : waypoint_topology) {
177 auto &waypoint = connection.first;
178 auto &successor = connection.second;
183 if (waypoint_segment_id == successor_segment_id){
188 using SegIdVectorPair = std::pair<std::vector<SegmentId>, std::vector<SegmentId>>;
189 SegIdVectorPair &connection_first = segment_topology[waypoint_segment_id];
190 SegIdVectorPair &connection_second = segment_topology[successor_segment_id];
191 connection_first.second.push_back(successor_segment_id);
192 connection_second.first.push_back(waypoint_segment_id);
195 bool waypoint_is_junction = waypoint->IsJunction();
196 bool successor_is_junction = successor->IsJunction();
197 if (waypoint_is_junction && !successor_is_junction) {
199 int64_t std_road_id =
static_cast<int64_t
>(successor->GetRoadId());
200 std_road_id = (successor->GetLaneId() < 0) ? -1 * std_road_id : std_road_id;
202 std::set<crd::RoadId> &in_paths = std_road_connectivity[std_road_id].first;
203 in_paths.insert(path_id);
205 if (in_paths.size() >= 2) {
206 for (
auto &in_path_id: in_paths) {
207 is_real_junction[in_path_id] =
true;
213 if (!waypoint_is_junction && successor_is_junction) {
215 int64_t std_road_id =
static_cast<int64_t
>(waypoint->GetRoadId());
216 std_road_id = (waypoint->GetLaneId() < 0) ? -1 * std_road_id : std_road_id;
218 std::set<crd::RoadId> &out_paths = std_road_connectivity[std_road_id].second;
219 out_paths.insert(path_id);
221 if (out_paths.size() >= 2) {
222 for (
auto &out_path_id: out_paths) {
223 is_real_junction[out_path_id] =
true;
231 assert(
_world_map !=
nullptr &&
"No map reference found.");
232 auto raw_dense_topology =
_world_map->GenerateWaypoints(MAP_RESOLUTION);
233 for (
auto &waypoint_ptr: raw_dense_topology) {
236 segment_map[
GetSegmentId(waypoint_ptr)].emplace_back(std::make_shared<SimpleWaypoint>(waypoint_ptr));
242 return cg::Math::DistanceSquared(l1, l2);
244 auto square = [](
float input) {
return std::pow(input, 2);};
246 return (swp1->GetWaypoint()->GetDistance() < swp2->GetWaypoint()->GetDistance());
249 return cg::Math::GetVectorAngle(l1, l2);
251 auto max = [](int16_t x, int16_t y) {
252 return x ^ ((x ^ y) & -(x < y));
256 for (
auto &segment: segment_map) {
257 auto &segment_waypoints = segment.second;
260 ++geodesic_grid_id_counter;
263 std::sort(segment_waypoints.begin(), segment_waypoints.end(), compare_s);
264 auto lane_id = segment_waypoints.front()->GetWaypoint()->GetLaneId();
266 std::reverse(segment_waypoints.begin(), segment_waypoints.end());
270 for (std::size_t i = 0; i < segment_waypoints.size() - 1; ++i) {
271 double distance = std::abs(segment_waypoints.at(i)->GetWaypoint()->GetDistance() - segment_waypoints.at(i+1)->GetWaypoint()->GetDistance());
272 double angle = wpt_angle(segment_waypoints.at(i)->GetTransform().GetForwardVector(), segment_waypoints.at(i+1)->GetTransform().GetForwardVector());
274 int16_t distance_splits =
static_cast<int16_t
>((distance*distance)/
MAX_WPT_DISTANCE);
275 auto max_splits = max(angle_splits, distance_splits);
276 if (max_splits >= 1) {
278 for (uint16_t j = 0; j < max_splits; ++j) {
279 auto next_waypoints = segment_waypoints.at(i)->GetWaypoint()->GetNext(distance/(max_splits+1));
280 if (next_waypoints.size() != 0) {
281 auto new_waypoint = next_waypoints.front();
283 segment_waypoints.insert(segment_waypoints.begin()+
static_cast<int64_t
>(i), std::make_shared<SimpleWaypoint>(new_waypoint));
293 cg::Location grid_edge_location = segment_waypoints.front()->GetLocation();
294 for (std::size_t i = 0; i < segment_waypoints.size() - 1; ++i) {
298 if (distance_squared(grid_edge_location, current_waypoint->GetLocation()) >
300 ++geodesic_grid_id_counter;
301 grid_edge_location = current_waypoint->GetLocation();
303 current_waypoint->SetGeodesicGridId(geodesic_grid_id_counter);
305 current_waypoint->SetNextWaypoint({next_waypoint});
306 next_waypoint->SetPreviousWaypoint({current_waypoint});
309 segment_waypoints.back()->SetGeodesicGridId(geodesic_grid_id_counter);
312 for (
auto swp: segment_waypoints) {
314 auto wpt = swp->GetWaypoint();
315 auto road_id = wpt->GetRoadId();
316 if (wpt->IsJunction() && !is_real_junction.count(road_id)) {
317 swp->SetIsJunction(
false);
319 swp->SetIsJunction(swp->GetWaypoint()->IsJunction());
329 for (
auto &segment : segment_map) {
331 auto &segment_waypoints = segment.second;
333 auto successors =
GetSuccessors(segment_id, segment_topology, segment_map);
334 auto predecessors =
GetPredecessors(segment_id, segment_topology, segment_map);
336 segment_waypoints.front()->SetPreviousWaypoint(predecessors);
337 segment_waypoints.back()->SetNextWaypoint(successors);
342 if (!swp->CheckJunction()) {
349 if (swp->GetNextWaypoint().empty()) {
350 auto neighbour = swp->GetRightWaypoint();
352 neighbour = swp->GetLeftWaypoint();
356 swp->SetNextWaypoint(neighbour->GetNextWaypoint());
357 for (
auto next_waypoint : neighbour->GetNextWaypoint()) {
358 next_waypoint->SetPreviousWaypoint({swp});
380 std::vector<SimpleWaypointPtr> next_waypoints = swp->GetNextWaypoint();
381 std::size_t next_swp_size = next_waypoints.size();
383 if (next_swp_size == 0) {
388 else if (next_swp_size > 1 || (!swp->CheckJunction() && next_waypoints.front()->CheckJunction())) {
392 bool found_landmark=
false;
393 if (next_swp_size <= 1) {
395 auto all_landmarks = swp->GetWaypoint()->GetAllLandmarksInDistance(15.0);
397 if (all_landmarks.empty()) {
401 for (
auto &landmark : all_landmarks) {
402 auto landmark_type = landmark->GetType();
403 if (landmark_type ==
"1000001" || landmark_type ==
"206" || landmark_type ==
"205") {
405 found_landmark=
true;
409 if (!found_landmark) {
417 if (found_landmark || next_swp_size > 1) {
419 for (
auto &next_swp : next_waypoints) {
420 std::vector<SimpleWaypointPtr> traversed_waypoints;
423 if (next_swp_size > 1) {
424 junction_end_waypoint = next_swp;
426 junction_end_waypoint = next_waypoints.front();
429 while (junction_end_waypoint->CheckJunction()){
430 traversed_waypoints.push_back(junction_end_waypoint);
431 std::vector<SimpleWaypointPtr> temp = junction_end_waypoint->GetNextWaypoint();
435 junction_end_waypoint = temp.front();
439 int16_t current_angle =
static_cast<int16_t
>(traversed_waypoints.front()->GetTransform().rotation.yaw);
440 int16_t junction_end_angle =
static_cast<int16_t
>(traversed_waypoints.back()->GetTransform().rotation.yaw);
441 int16_t diff_angle = (junction_end_angle - current_angle) % 360;
442 bool straight = (diff_angle < STRAIGHT_DEG && diff_angle > -
STRAIGHT_DEG) ||
445 bool right = (diff_angle >=
STRAIGHT_DEG && diff_angle <= 180) ||
446 (diff_angle <= -180 && diff_angle >= -360+
STRAIGHT_DEG);
448 auto assign_option = [](
RoadOption ro, std::vector<SimpleWaypointPtr> traversed_waypoints) {
449 for (
auto &twp : traversed_waypoints) {
450 twp->SetRoadOption(ro);
514 const WaypointPtr raw_waypoint = reference_waypoint->GetWaypoint();
515 const crd::element::LaneMarking::LaneChange lane_change = raw_waypoint->GetLaneChange();
521 case crd::element::LaneMarking::LaneChange::Left:
523 const WaypointPtr left_waypoint = raw_waypoint->GetLeft();
524 if (left_waypoint !=
nullptr &&
525 left_waypoint->GetType() == crd::Lane::LaneType::Driving &&
526 (left_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
529 reference_waypoint->SetLeftWaypoint(closest_simple_waypoint);
535 case crd::element::LaneMarking::LaneChange::Right:
537 const WaypointPtr right_waypoint = raw_waypoint->GetRight();
538 if(right_waypoint !=
nullptr &&
539 right_waypoint->GetType() == crd::Lane::LaneType::Driving &&
540 (right_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
543 reference_waypoint->SetRightWaypoint(closest_simple_waypoint);
549 case crd::element::LaneMarking::LaneChange::Both:
552 const WaypointPtr right_waypoint = raw_waypoint->GetRight();
553 if (right_waypoint !=
nullptr &&
554 right_waypoint->GetType() == crd::Lane::LaneType::Driving &&
555 (right_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
558 reference_waypoint->SetRightWaypoint(closest_simple_waypointR);
562 const WaypointPtr left_waypoint = raw_waypoint->GetLeft();
563 if (left_waypoint !=
nullptr &&
564 left_waypoint->GetType() == crd::Lane::LaneType::Driving &&
565 (left_waypoint->GetLaneId() * raw_waypoint->GetLaneId() > 0)) {
568 reference_waypoint->SetLeftWaypoint(closest_simple_waypointL);