12using Point2D = bg::model::point<double, 2, bg::cs::cartesian>;
16using constants::WaypointSelection::JUNCTION_LOOK_AHEAD;
36 bool collision_hazard =
false;
37 float available_distance_margin = std::numeric_limits<float>::infinity();
43 const unsigned long look_ahead_index =
GetTargetWaypoint(ego_buffer, JUNCTION_LOOK_AHEAD).second;
47 std::vector<ActorId> collision_candidate_ids;
50 float collision_radius_square =
SQUARE(COLLISION_RADIUS_RATE * velocity + COLLISION_RADIUS_MIN);
51 if (velocity < 2.0f) {
53 const float collision_radius_stop = COLLISION_RADIUS_STOP + length;
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
65 && std::abs(ego_location.
z - overlapping_actor_location.
z) < VERTICAL_OVERLAP_THRESHOLD) {
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) {
73 const cg::Location &e_loc = ego_location;
74 const cg::Location &loc_1 = simulation_state.GetLocation(a_id_1);
75 const cg::Location &loc_2 = simulation_state.GetLocation(a_id_2);
76 return (cg::Math::DistanceSquared(e_loc, loc_1) < cg::Math::DistanceSquared(e_loc, loc_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;
125 float velocity_extension = VEL_EXT_FACTOR * velocity;
126 bbox_extension = BOUNDARY_EXTENSION_MINIMUM + velocity_extension * velocity_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
207 ||
cg::Math::Dot(boundary_end->GetForwardVector(), current_point->GetForwardVector()) < COS_10_DEGREES
208 || reached_distance) {
210 const cg::Vector3D heading_vector = current_point->GetForwardVector();
211 const cg::Location location = current_point->GetLocation();
213 perpendicular_vector = perpendicular_vector.
MakeUnitVector(EPSILON);
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;
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();
348 bool ego_stopped_by_light = reference_tl_state.
tl_state != TLS::Green && reference_tl_state.
tl_state != TLS::Off;
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)) {
378 const float specific_distance_margin = std::max(reference_lead_distance, MIN_REFERENCE_DISTANCE);
380 -
static_cast<double>(specific_distance_margin), 0.0));
419 return {hazard, available_distance_margin};
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
static constexpr auto DistanceSquared(const Vector3D &a, const Vector3D &b)
static constexpr auto Dot(const Vector3D &a, const Vector3D &b)
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)
GeodesicBoundaryMap geodesic_boundary_map
const std::vector< ActorId > & vehicle_id_list
const SimulationState & simulation_state
GeometryComparison GetGeometryBetweenActors(const ActorId reference_vehicle_id, const ActorId other_actor_id)
LocationVector GetGeodesicBoundary(const ActorId actor_id)
Polygon GetPolygon(const LocationVector &boundary)
void RemoveActor(const ActorId actor_id) override
CollisionFrame & output_array
LocationVector GetBoundary(const ActorId actor_id)
float GetBoundingBoxExtention(const ActorId actor_id)
RandomGenerator & random_device
const TrackTraffic & track_traffic
const BufferMap & buffer_map
void Update(const unsigned long index) override
CollisionLockMap collision_locks
GeometryComparisonMap geometry_cache
const Parameters & parameters
std::pair< bool, float > NegotiateCollision(const ActorId reference_vehicle_id, const ActorId other_actor_id, const uint64_t reference_junction_look_ahead_index)
float GetPercentageIgnoreWalkers(const ActorId &actor_id) const
Method to get % to ignore any walker.
float GetDistanceToLeadingVehicle(const ActorId &actor_id) const
Method to query distance to leading vehicle for a given vehicle.
bool GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const
Method to query collision avoidance rule between a pair of vehicles.
float GetPercentageIgnoreVehicles(const ActorId &actor_id) const
Method to get % to ignore any vehicle.
This class holds the state of all the vehicles in the simlation.
bool ContainsActor(ActorId actor_id) const
cg::Location GetLocation(const ActorId actor_id) const
cg::Vector3D GetHeading(const ActorId actor_id) const
cg::Vector3D GetVelocity(const ActorId actor_id) const
ActorType GetType(const ActorId actor_id) const
cg::Vector3D GetDimensions(const ActorId actor_id) const
TrafficLightState GetTLS(const ActorId actor_id) const
ActorIdSet GetOverlappingVehicles(ActorId actor_id) const
carla::rpc::TrafficLightState TLS
bg::model::point< double, 2, bg::cs::cartesian > Point2D
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
std::vector< CollisionHazardData > CollisionFrame
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
TargetWPInfo GetTargetWaypoint(const Buffer &waypoint_buffer, const float &target_point_distance)
std::unordered_set< ActorId > ActorIdSet
std::vector< cg::Location > LocationVector
std::unordered_map< carla::ActorId, Buffer > BufferMap
bg::model::polygon< bg::model::d2::point_xy< double > > Polygon
std::pair< SimpleWaypointPtr, uint64_t > TargetWPInfo
Method to return the wayPoints from the waypoint Buffer by using target point distance.
This file contains definitions of common data structures used in traffic manager.
Vector3D MakeUnitVector(const float epsilon=2.0f *std::numeric_limits< float >::epsilon()) const
Return a unit-length copy of this vector.
float available_distance_margin
double distance_to_lead_vehicle
double initial_lock_distance
double reference_vehicle_to_other_geodesic
double inter_geodesic_distance
double other_vehicle_to_reference_geodesic
double inter_bbox_distance