10 namespace traffic_manager {
12 using Point2D = bg::model::point<double, 2, bg::cs::cartesian>;
15 using namespace constants::Collision;
19 const std::vector<ActorId> &vehicle_id_list,
26 : vehicle_id_list(vehicle_id_list),
27 simulation_state(simulation_state),
28 buffer_map(buffer_map),
29 track_traffic(track_traffic),
30 parameters(parameters),
31 output_array(output_array),
32 random_device(random_device) {}
36 bool collision_hazard =
false;
37 float available_distance_margin = std::numeric_limits<float>::infinity();
47 std::vector<ActorId> collision_candidate_ids;
51 if (velocity < 2.0f) {
54 collision_radius_square =
SQUARE(collision_radius_stop);
56 if (distance_to_leading > collision_radius_square) {
57 collision_radius_square =
SQUARE(distance_to_leading);
60 for (
ActorId overlapping_actor_id : overlapping_actors) {
63 if (overlapping_actor_id != ego_actor_id
66 collision_candidate_ids.push_back(overlapping_actor_id);
71 std::sort(collision_candidate_ids.begin(), collision_candidate_ids.end(),
72 [
this, &ego_location](
const ActorId &a_id_1,
const ActorId &a_id_2) {
80 for (
auto iter = collision_candidate_ids.begin();
81 iter != collision_candidate_ids.end() && !collision_hazard;
83 const ActorId other_actor_id = *iter;
92 if (negotiation_result.first) {
97 collision_hazard =
true;
98 obstacle_id = other_actor_id;
99 available_distance_margin = negotiation_result.second;
108 output_element.
hazard = collision_hazard;
123 float bbox_extension;
134 bbox_extension = lock_boundary_length;
138 return bbox_extension;
145 float forward_extension = 0.0f;
153 float bbox_x = dimensions.
x;
154 float bbox_y = dimensions.
y;
156 const cg::Vector3D x_boundary_vector = heading_vector * (bbox_x + forward_extension);
158 const cg::Vector3D y_boundary_vector = perpendicular_vector * (bbox_y + forward_extension);
163 location +
cg::Location(x_boundary_vector - y_boundary_vector),
164 location +
cg::Location(-1.0f * x_boundary_vector - y_boundary_vector),
165 location +
cg::Location(-1.0f * x_boundary_vector + y_boundary_vector),
166 location +
cg::Location(x_boundary_vector + y_boundary_vector),
169 return bbox_boundary;
183 bbox_extension = std::max(specific_lead_distance, bbox_extension);
184 const float bbox_extension_square =
SQUARE(bbox_extension);
189 const float width = dimensions.
y;
190 const float length = dimensions.
x;
195 const uint64_t boundary_start_index = target_wp_info.second;
201 bool reached_distance =
false;
202 for (uint64_t j = boundary_start_index; !reached_distance && (j < waypoint_buffer.size()); ++j) {
203 if (boundary_start->DistanceSquared(current_point) > bbox_extension_square || j == waypoint_buffer.size() - 1) {
204 reached_distance =
true;
206 if (boundary_end ==
nullptr 208 || reached_distance) {
210 const cg::Vector3D heading_vector = current_point->GetForwardVector();
211 const cg::Location location = current_point->GetLocation();
215 const cg::Vector3D scaled_perpendicular = perpendicular_vector * width;
216 left_boundary.push_back(location +
cg::Location(scaled_perpendicular));
217 right_boundary.push_back(location +
cg::Location(-1.0f * scaled_perpendicular));
219 boundary_end = current_point;
222 current_point = waypoint_buffer.at(j);
230 std::reverse(right_boundary.begin(), right_boundary.end());
231 geodesic_boundary.insert(geodesic_boundary.end(), right_boundary.begin(), right_boundary.end());
232 geodesic_boundary.insert(geodesic_boundary.end(), bbox.begin(), bbox.end());
233 geodesic_boundary.insert(geodesic_boundary.end(), left_boundary.begin(), left_boundary.end());
236 geodesic_boundary = bbox;
242 return geodesic_boundary;
249 bg::append(boundary_polygon.outer(),
Point2D(location.x, location.y));
251 bg::append(boundary_polygon.outer(),
Point2D(boundary.front().x, boundary.front().y));
253 return boundary_polygon;
257 const ActorId other_actor_id) {
260 std::pair<ActorId, ActorId> key_parts;
261 if (reference_vehicle_id < other_actor_id) {
262 key_parts = {reference_vehicle_id, other_actor_id};
264 key_parts = {other_actor_id, reference_vehicle_id};
267 uint64_t actor_id_key = 0u;
268 actor_id_key |= key_parts.first;
270 actor_id_key |= key_parts.second;
277 double mref_veh_other = comparision_result.reference_vehicle_to_other_geodesic;
278 comparision_result.reference_vehicle_to_other_geodesic = comparision_result.other_vehicle_to_reference_geodesic;
279 comparision_result.other_vehicle_to_reference_geodesic = mref_veh_other;
289 const double reference_vehicle_to_other_geodesic = bg::distance(reference_polygon, other_geodesic_polygon);
290 const double other_vehicle_to_reference_geodesic = bg::distance(other_polygon, reference_geodesic_polygon);
291 const auto inter_geodesic_distance = bg::distance(reference_geodesic_polygon, other_geodesic_polygon);
292 const auto inter_bbox_distance = bg::distance(reference_polygon, other_polygon);
294 comparision_result = {reference_vehicle_to_other_geodesic,
295 other_vehicle_to_reference_geodesic,
296 inter_geodesic_distance,
297 inter_bbox_distance};
302 return comparision_result;
307 const uint64_t reference_junction_look_ahead_index) {
310 float available_distance_margin = std::numeric_limits<float>::infinity();
318 cg::Vector3D reference_to_other = other_location - reference_location;
324 cg::Vector3D other_to_reference = reference_location - other_location;
334 float inter_vehicle_length = reference_vehicle_length + other_vehicle_length;
335 float ego_detection_range =
SQUARE(ego_bounding_box_extension + inter_vehicle_length);
336 float cross_detection_range =
SQUARE(ego_bounding_box_extension + inter_vehicle_length + other_bounding_box_extension);
339 bool other_vehicle_in_ego_range = inter_vehicle_distance < ego_detection_range;
340 bool other_vehicles_in_cross_detection_range = inter_vehicle_distance < cross_detection_range;
341 float reference_heading_to_other_dot =
cg::Math::Dot(reference_heading, reference_to_other);
342 bool other_vehicle_in_front = reference_heading_to_other_dot > 0;
343 const Buffer &reference_vehicle_buffer =
buffer_map.at(reference_vehicle_id);
345 bool ego_inside_junction = closest_point->CheckJunction();
349 SimpleWaypointPtr look_ahead_point = reference_vehicle_buffer.at(reference_junction_look_ahead_index);
350 bool ego_at_junction_entrance = !closest_point->CheckJunction() && look_ahead_point->CheckJunction();
353 if (!(ego_at_junction_entrance && ego_at_traffic_light && ego_stopped_by_light)
354 && ((ego_inside_junction && other_vehicles_in_cross_detection_range)
355 || (!ego_inside_junction && other_vehicle_in_front && other_vehicle_in_ego_range))) {
365 bool ego_angular_priority = reference_heading_to_other_dot<
cg::Math::Dot(other_heading, other_to_reference);
368 bool lower_priority = !ego_path_priority && (other_path_priority || !ego_angular_priority);
369 bool blocked_by_other_or_lower_priority = !ego_path_clear || (other_path_clear && lower_priority);
370 bool yield_pre_crash = !vehicle_bbox_touching && blocked_by_other_or_lower_priority;
371 bool yield_post_crash = vehicle_bbox_touching && !ego_angular_priority;
373 if (geodesic_path_bbox_touching && (yield_pre_crash || yield_post_crash)) {
380 - static_cast<double>(specific_distance_margin), 0.0));
419 return {hazard, available_distance_margin};
bool GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const
Method to query collision avoidance rule between a pair of vehicles.
TargetWPInfo GetTargetWaypoint(const Buffer &waypoint_buffer, const float &target_point_distance)
GeometryComparison GetGeometryBetweenActors(const ActorId reference_vehicle_id, const ActorId other_actor_id)
Polygon GetPolygon(const LocationVector &boundary)
double initial_lock_distance
CollisionLockMap collision_locks
std::vector< cg::Location > LocationVector
static const float SQUARE_ROOT_OF_TWO
LocationVector GetBoundary(const ActorId actor_id)
double inter_bbox_distance
static const float WALKER_TIME_EXTENSION
float available_distance_margin
float GetBoundingBoxExtention(const ActorId actor_id)
static const float COLLISION_RADIUS_MIN
LocationVector GetGeodesicBoundary(const ActorId actor_id)
const BufferMap & buffer_map
static const float MIN_REFERENCE_DISTANCE
void reverse(I begin, I end)
GeometryComparisonMap geometry_cache
TrafficLightState GetTLS(const ActorId actor_id) const
This class holds the state of all the vehicles in the simlation.
This file contains definitions of common data structures used in traffic manager. ...
cg::Vector3D GetDimensions(const ActorId actor_id) const
static const float COLLISION_RADIUS_RATE
std::unordered_set< ActorId > ActorIdSet
static const float BOUNDARY_EXTENSION_MINIMUM
ActorType GetType(const ActorId actor_id) const
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
static const float COS_10_DEGREES
static auto DistanceSquared(const Vector3D &a, const Vector3D &b)
std::unordered_map< carla::ActorId, Buffer > BufferMap
double inter_geodesic_distance
static const float MAX_LOCKING_EXTENSION
cg::Location GetLocation(const ActorId actor_id) const
static const float VEL_EXT_FACTOR
const Parameters & parameters
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
GeodesicBoundaryMap geodesic_boundary_map
const TrackTraffic & track_traffic
void Update(const unsigned long index) override
RandomGenerator & random_device
bool ContainsActor(ActorId actor_id) const
double other_vehicle_to_reference_geodesic
void RemoveActor(const ActorId actor_id) override
ActorIdSet GetOverlappingVehicles(ActorId actor_id) const
static const float LOCKING_DISTANCE_PADDING
float GetPercentageIgnoreVehicles(const ActorId &actor_id) const
Method to get % to ignore any vehicle.
static const float VERTICAL_OVERLAP_THRESHOLD
float GetDistanceToLeadingVehicle(const ActorId &actor_id) const
Method to query distance to leading vehicle for a given vehicle.
cg::Vector3D GetHeading(const ActorId actor_id) const
const SimulationState & simulation_state
bg::model::point< double, 2, bg::cs::cartesian > Point2D
static auto Dot(const Vector3D &a, const Vector3D &b)
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
bg::model::polygon< bg::model::d2::point_xy< double > > Polygon
float GetPercentageIgnoreWalkers(const ActorId &actor_id) const
Method to get % to ignore any walker.
double distance_to_lead_vehicle
const std::vector< ActorId > & vehicle_id_list
std::pair< SimpleWaypointPtr, uint64_t > TargetWPInfo
Method to return the wayPoints from the waypoint Buffer by using target point distance.
static const float OVERLAP_THRESHOLD
cg::Vector3D GetVelocity(const ActorId actor_id) const
Vector3D MakeSafeUnitVector(const float epsilon) const
void sort(I begin, I end, const Pred &pred)
CollisionFrame & output_array
static constexpr double EPSILON
We use this epsilon to shift the waypoints away from the edges of the lane sections to avoid floating...
std::vector< CollisionHazardData > CollisionFrame
static const float JUNCTION_LOOK_AHEAD
CollisionStage(const std::vector< ActorId > &vehicle_id_list, const SimulationState &simulation_state, const BufferMap &buffer_map, const TrackTraffic &track_traffic, const Parameters ¶meters, CollisionFrame &output_array, RandomGenerator &random_device)
static const float COLLISION_RADIUS_STOP
double reference_vehicle_to_other_geodesic
std::pair< bool, float > NegotiateCollision(const ActorId reference_vehicle_id, const ActorId other_actor_id, const uint64_t reference_junction_look_ahead_index)