46 const float vehicle_speed = vehicle_velocity_vector.
Length();
55 const float horizon_square =
SQUARE(horizon_length);
64 if (!waypoint_buffer.empty() &&
65 cg::Math::DistanceSquared(waypoint_buffer.front()->GetLocation(),
69 auto number_of_pops = waypoint_buffer.size();
70 for (uint64_t j = 0u; j < number_of_pops; ++j) {
75 bool is_at_junction_entrance =
false;
76 if (!waypoint_buffer.empty()) {
78 float dot_product =
DeviationDotProduct(vehicle_location, heading_vector, waypoint_buffer.front()->GetLocation());
79 while (dot_product <= 0.0f && !waypoint_buffer.empty()) {
81 if (!waypoint_buffer.empty()) {
82 dot_product =
DeviationDotProduct(vehicle_location, heading_vector, waypoint_buffer.front()->GetLocation());
86 if (!waypoint_buffer.empty()) {
90 bool front_waypoint_junction = front_waypoint->CheckJunction();
91 is_at_junction_entrance = !front_waypoint_junction && look_ahead_point->CheckJunction();
92 if (!is_at_junction_entrance) {
93 std::vector<SimpleWaypointPtr> last_passed_waypoints = front_waypoint->GetPreviousWaypoint();
94 if (last_passed_waypoints.size() == 1) {
95 is_at_junction_entrance = !last_passed_waypoints.front()->CheckJunction() && front_waypoint_junction;
98 if (is_at_junction_entrance
100 &&
local_map->GetMapName() ==
"Carla/Maps/Town03"
102 is_at_junction_entrance =
false;
107 while (!is_at_junction_entrance
108 && !waypoint_buffer.empty()
109 && waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) > horizon_square + horizon_square
110 && !waypoint_buffer.back()->CheckJunction()) {
116 if (waypoint_buffer.empty()) {
123 bool force_lane_change = lane_change_info.
change_lane;
124 bool lane_change_direction = lane_change_info.
direction;
132 const bool is_random_left_change = perc_random_leftlanechange >=
random_device.
next();
133 const bool is_random_right_change = perc_random_rightlanechange >=
random_device.
next();
136 if (is_keep_right || is_random_right_change) {
137 force_lane_change =
true;
138 lane_change_direction =
true;
140 if (is_random_left_change) {
141 if (!force_lane_change) {
142 force_lane_change =
true;
143 lane_change_direction =
false;
155 bool done_with_previous_lane_change =
true;
156 if (!recently_not_executed_lane_change) {
157 float distance_frm_previous = cg::Math::DistanceSquared(
last_lane_change_swpt.at(actor_id)->GetLocation(), vehicle_location);
158 done_with_previous_lane_change = distance_frm_previous > lane_change_distance;
162 bool front_waypoint_not_junction = !front_waypoint->CheckJunction();
164 if (auto_or_force_lane_change
165 && front_waypoint_not_junction
166 && (recently_not_executed_lane_change || done_with_previous_lane_change)) {
169 force_lane_change, lane_change_direction);
171 if (change_over_point !=
nullptr) {
177 auto number_of_pops = waypoint_buffer.size();
178 for (uint64_t j = 0u; j < number_of_pops; ++j) {
188 if (!imported_path.empty()) {
190 ImportPath(imported_path, waypoint_buffer, actor_id, horizon_square);
192 }
else if (!imported_actions.empty()) {
194 ImportRoute(imported_actions, waypoint_buffer, actor_id, horizon_square);
200 while (waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) <= horizon_square) {
202 std::vector<SimpleWaypointPtr> next_waypoints = furthest_waypoint->GetNextWaypoint();
203 uint64_t selection_index = 0u;
205 if (next_waypoints.size() > 1) {
207 selection_index =
static_cast<uint64_t
>(r_sample*next_waypoints.size()*0.01);
208 }
else if (next_waypoints.size() == 0) {
210 std::cout <<
"This map has dead-end roads, please change the set_open_street_map parameter to true" << std::endl;
217 if (next_wp_selection->GetId() == waypoint_buffer.front()->GetId()){
229 if (is_at_junction_entrance) {
232 output.
safe_point = safe_space_end_points.second;
243 const bool is_at_junction_entrance,
244 Buffer &waypoint_buffer) {
249 if (is_at_junction_entrance
252 bool entered_junction =
false;
253 bool past_junction =
false;
254 bool safe_point_found =
false;
260 for (
unsigned long i = 0u; i < waypoint_buffer.size() && !safe_point_found; ++i) {
261 current_waypoint = waypoint_buffer.at(i);
262 if (!entered_junction && current_waypoint->CheckJunction()) {
263 entered_junction =
true;
264 junction_begin_point = current_waypoint;
266 if (entered_junction && !past_junction && !current_waypoint->CheckJunction()) {
267 past_junction =
true;
268 junction_end_point = current_waypoint;
270 if (past_junction && junction_end_point->DistanceSquared(current_waypoint) > safe_distance_squared) {
271 safe_point_found =
true;
272 safe_point_after_junction = current_waypoint;
277 if (!safe_point_found) {
280 while (!past_junction && !abort) {
281 NodeList next_waypoints = current_waypoint->GetNextWaypoint();
282 if (!next_waypoints.empty()) {
283 current_waypoint = next_waypoints.front();
285 if (!current_waypoint->CheckJunction()) {
286 past_junction =
true;
287 junction_end_point = current_waypoint;
294 while (!safe_point_found && !abort) {
295 std::vector<SimpleWaypointPtr> next_waypoints = current_waypoint->GetNextWaypoint();
296 if ((junction_end_point->DistanceSquared(current_waypoint) > safe_distance_squared)
297 || next_waypoints.size() > 1
298 || current_waypoint->CheckJunction()) {
300 safe_point_found =
true;
301 safe_point_after_junction = current_waypoint;
303 if (!next_waypoints.empty()) {
304 current_waypoint = next_waypoints.front();
313 if (junction_end_point !=
nullptr &&
314 safe_point_after_junction !=
nullptr &&
317 junction_end_point =
nullptr;
318 safe_point_after_junction =
nullptr;
323 else if (!is_at_junction_entrance
342 const float vehicle_speed,
343 bool force,
bool direction) {
353 if (!waypoint_buffer.empty()) {
363 bool obstacle_too_close =
false;
364 float minimum_squared_distance = std::numeric_limits<float>::infinity();
365 ActorId obstacle_actor_id = 0u;
366 for (
auto i = blocking_vehicles.begin();
367 i != blocking_vehicles.end() && !obstacle_too_close && !force;
369 const ActorId &other_actor_id = *i;
374 const cg::Location other_location = other_current_waypoint->GetLocation();
376 const cg::Vector3D reference_heading = current_waypoint->GetForwardVector();
377 cg::Vector3D reference_to_other = other_location - current_waypoint->GetLocation();
378 const cg::Vector3D other_heading = other_current_waypoint->GetForwardVector();
380 WaypointPtr current_raw_waypoint = current_waypoint->GetWaypoint();
381 WaypointPtr other_current_raw_waypoint = other_current_waypoint->GetWaypoint();
385 if (!current_waypoint->CheckJunction()
386 && !other_current_waypoint->CheckJunction()
387 && other_current_raw_waypoint->GetRoadId() == current_raw_waypoint->GetRoadId()
388 && other_current_raw_waypoint->GetLaneId() == current_raw_waypoint->GetLaneId()
389 && cg::Math::Dot(reference_heading, reference_to_other) > 0.0f
391 float squared_distance = cg::Math::DistanceSquared(vehicle_location, other_location);
396 minimum_squared_distance = squared_distance;
397 obstacle_actor_id = other_actor_id;
400 obstacle_too_close =
true;
407 if (!obstacle_too_close && obstacle_actor_id != 0u && !force) {
410 const auto other_neighbouring_lanes = {other_current_waypoint->GetLeftWaypoint(),
411 other_current_waypoint->GetRightWaypoint()};
414 bool distant_left_lane_free =
false;
415 bool distant_right_lane_free =
false;
418 bool left_right =
true;
419 for (
auto &candidate_lane_wp : other_neighbouring_lanes) {
420 if (candidate_lane_wp !=
nullptr &&
424 distant_left_lane_free =
true;
426 distant_right_lane_free =
true;
428 left_right = !left_right;
433 if (distant_right_lane_free && right_waypoint !=
nullptr
435 change_over_point = right_waypoint;
436 }
else if (distant_left_lane_free && left_waypoint !=
nullptr
438 change_over_point = left_waypoint;
441 if (direction && right_waypoint !=
nullptr) {
442 change_over_point = right_waypoint;
443 }
else if (!direction && left_waypoint !=
nullptr) {
444 change_over_point = left_waypoint;
448 if (change_over_point !=
nullptr) {
451 while (change_over_point->DistanceSquared(starting_point) <
SQUARE(change_over_distance) &&
452 !change_over_point->CheckJunction()) {
453 change_over_point = change_over_point->GetNextWaypoint().front();
458 return change_over_point;
464 auto number_of_pops = waypoint_buffer.size();
465 for (uint64_t j = 0u; j < number_of_pops - 1; ++j) {
477 while (!imported_path.empty() && waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) <= horizon_square) {
482 std::vector<SimpleWaypointPtr> next_waypoints = latest_waypoint->GetNextWaypoint();
483 uint64_t selection_index = 0u;
486 if (next_waypoints.size() > 1) {
487 const float imported_road_id = imported->GetWaypoint()->GetRoadId();
488 float min_distance = std::numeric_limits<float>::infinity();
489 for (uint64_t k = 0u; k < next_waypoints.size(); ++k) {
491 while (!junction_end_point->CheckJunction()) {
492 junction_end_point = junction_end_point->GetNextWaypoint().front();
494 while (junction_end_point->CheckJunction()) {
495 junction_end_point = junction_end_point->GetNextWaypoint().front();
497 while (next_waypoints.at(k)->DistanceSquared(junction_end_point) < 50.0f) {
498 junction_end_point = junction_end_point->GetNextWaypoint().front();
500 float jep_road_id = junction_end_point->GetWaypoint()->GetRoadId();
501 if (jep_road_id == imported_road_id) {
505 float distance = junction_end_point->DistanceSquared(imported);
506 if (distance < min_distance) {
507 min_distance = distance;
511 }
else if (next_waypoints.size() == 0) {
513 std::cout <<
"This map has dead-end roads, please change the set_open_street_map parameter to true" << std::endl;
521 if (next_wp_selection->DistanceSquared(imported) < 30.0f) {
522 imported_path.erase(imported_path.begin());
523 std::vector<SimpleWaypointPtr> possible_waypoints = next_wp_selection->GetNextWaypoint();
524 if (std::find(possible_waypoints.begin(), possible_waypoints.end(), imported) != possible_waypoints.end()) {
529 latest_imported = imported_path.front();
530 imported =
local_map->GetWaypoint(latest_imported);
535 if (imported_path.empty()) {
547 auto number_of_pops = waypoint_buffer.size();
548 for (uint64_t j = 0u; j < number_of_pops - 1; ++j) {
556 while (!imported_actions.empty() && waypoint_buffer.back()->DistanceSquared(waypoint_buffer.front()) <= horizon_square) {
559 RoadOption latest_road_option = latest_waypoint->GetRoadOption();
561 std::vector<SimpleWaypointPtr> next_waypoints = latest_waypoint->GetNextWaypoint();
562 uint16_t selection_index = 0u;
563 if (next_waypoints.size() > 1) {
564 for (uint16_t i=0; i<next_waypoints.size(); ++i) {
565 if (next_waypoints.at(i)->GetRoadOption() == next_road_option) {
569 if (i == next_waypoints.size() - 1) {
570 std::cout <<
"We couldn't find the RoadOption you were looking for. This route might diverge from the one expected." << std::endl;
574 }
else if (next_waypoints.size() == 0) {
576 std::cout <<
"This map has dead-end roads, please change the set_open_street_map parameter to true" << std::endl;
586 if (latest_road_option != next_wp_selection->GetRoadOption() && next_road_option == next_wp_selection->GetRoadOption()) {
587 imported_actions.erase(imported_actions.begin());
588 next_road_option =
static_cast<RoadOption>(imported_actions.front());
591 if (imported_actions.empty()) {
601 auto waypoint_buffer =
buffer_map.at(actor_id);
603 bool is_lane_change =
false;
606 is_lane_change =
true;
609 bool left_heading = (heading_vector.
x * relative_vector.
y - heading_vector.
y * relative_vector.
x) > 0.0f;
613 for (
auto &swpt : waypoint_buffer) {
616 if (!is_lane_change) {
618 return std::make_pair(road_opt, swpt->GetWaypoint());
623 auto distance_lane_change = cg::Math::DistanceSquared(actual_location, lane_change);
624 auto distance_other_action = cg::Math::DistanceSquared(actual_location, swpt->GetLocation());
625 if (distance_lane_change < distance_other_action)
return next_action;
626 else return std::make_pair(road_opt, swpt->GetWaypoint());
635 auto waypoint_buffer =
buffer_map.at(actor_id);
638 bool is_lane_change =
false;
640 RoadOption last_road_opt = buffer_front->GetRoadOption();
641 action_buffer.push_back(std::make_pair(last_road_opt, buffer_front->GetWaypoint()));
644 is_lane_change =
true;
647 bool left_heading = (heading_vector.
x * relative_vector.
y - heading_vector.
y * relative_vector.
x) > 0.0f;
651 for (
auto &wpt : waypoint_buffer) {
652 RoadOption current_road_opt = wpt->GetRoadOption();
653 if (current_road_opt != last_road_opt) {
654 action_buffer.push_back(std::make_pair(current_road_opt, wpt->GetWaypoint()));
655 last_road_opt = current_road_opt;
658 if (is_lane_change) {
660 auto distance_lane_change = cg::Math::DistanceSquared(waypoint_buffer.front()->GetLocation(), lane_change.second->GetTransform().location);
661 for (uint16_t i = 0; i < action_buffer.size(); ++i) {
662 auto distance_action = cg::Math::DistanceSquared(waypoint_buffer.front()->GetLocation(), waypoint_buffer.at(i)->GetLocation());
665 if (i == action_buffer.size()-1) {
666 action_buffer.push_back(lane_change);
668 }
else if (distance_action > distance_lane_change) {
669 action_buffer.insert(action_buffer.begin()+i, lane_change);
674 return action_buffer;