@@ -19,6 +19,7 @@ LocalizationStage::LocalizationStage(
1919 const SimulationState &simulation_state,
2020 TrackTraffic &track_traffic,
2121 const LocalMapPtr &local_map,
22+ std::unordered_map<ActorId, std::pair<float , bool >> &large_vehicles,
2223 Parameters ¶meters,
2324 std::vector<ActorId>& marked_for_removal,
2425 LocalizationFrame &output_array,
@@ -28,6 +29,7 @@ LocalizationStage::LocalizationStage(
2829 simulation_state(simulation_state),
2930 track_traffic(track_traffic),
3031 local_map(local_map),
32+ large_vehicles(large_vehicles),
3133 parameters(parameters),
3234 marked_for_removal(marked_for_removal),
3335 output_array(output_array),
@@ -215,6 +217,7 @@ void LocalizationStage::Update(const unsigned long index) {
215217 }
216218 }
217219 ExtendAndFindSafeSpace (actor_id, is_at_junction_entrance, waypoint_buffer);
220+ HandleLargeVehicleJunction (actor_id, is_at_junction_entrance, waypoint_buffer);
218221
219222 // Editing output array
220223 LocalizationData &output = output_array.at (index);
@@ -239,7 +242,7 @@ void LocalizationStage::ExtendAndFindSafeSpace(const ActorId actor_id,
239242
240243 SimpleWaypointPtr junction_end_point = nullptr ;
241244 SimpleWaypointPtr safe_point_after_junction = nullptr ;
242-
245+
243246 if (is_at_junction_entrance
244247 && vehicles_at_junction_entrance.find (actor_id) == vehicles_at_junction_entrance.end ()) {
245248
@@ -321,6 +324,134 @@ void LocalizationStage::ExtendAndFindSafeSpace(const ActorId actor_id,
321324 }
322325}
323326
327+ void LocalizationStage::HandleLargeVehicleJunction (const ActorId actor_id,
328+ const bool is_at_junction_entrance,
329+ Buffer &waypoint_buffer) {
330+
331+ if (large_vehicles.find (actor_id) == large_vehicles.end ()){
332+ return ;
333+ }
334+
335+ SimpleWaypointPtr current_waypoint = nullptr ;
336+ bool is_at_junction = waypoint_buffer.front ()->CheckJunction ();
337+
338+ if (is_at_junction_entrance
339+ && large_vehicles_at_junction_entrance.find (actor_id) == large_vehicles_at_junction_entrance.end ()
340+ && large_vehicles_at_junction.find (actor_id) == large_vehicles_at_junction.end ()) {
341+
342+ large_vehicles_at_junction_entrance.insert (actor_id);
343+
344+ const SimpleWaypointPtr first_waypoint = waypoint_buffer.front ();
345+ const SimpleWaypointPtr last_waypoint = waypoint_buffer.back ();
346+ const SimpleWaypointPtr middle_waypoint = waypoint_buffer.at (static_cast <uint16_t >(waypoint_buffer.size () / 2 ));
347+
348+ float radius = GetThreePointCircleRadius (first_waypoint->GetLocation (),
349+ middle_waypoint->GetLocation (),
350+ last_waypoint->GetLocation ());
351+
352+ std::cout << " Radius: " << radius << std::endl;
353+ if (radius > LARGE_VEHICLES_JUNCTION_MAX_RADIUS){
354+ return ; // Straight path
355+ }
356+
357+ bool entered_junction = false ;
358+ float junction_length = 0 .0f ;
359+ bool is_straight_path = true ;
360+
361+ for (unsigned long i = 0u ; i < waypoint_buffer.size (); ++i) {
362+ current_waypoint = waypoint_buffer.at (i);
363+
364+ if (!entered_junction && current_waypoint->CheckJunction ()) {
365+ entered_junction = true ;
366+ }
367+
368+ if (i > 0 && entered_junction) {
369+ SimpleWaypointPtr prev_waypoint = waypoint_buffer.at (i-1 );
370+ float new_distance = current_waypoint->Distance (prev_waypoint->GetLocation ());
371+ junction_length = junction_length + new_distance;
372+
373+ if (is_straight_path){
374+ RoadOption junction_type = current_waypoint->GetRoadOption ();
375+ if (junction_type == RoadOption::Right){
376+ large_vehicles[actor_id].second = true ;
377+ is_straight_path = false ;
378+ } else if (junction_type == RoadOption::Left) {
379+ large_vehicles[actor_id].second = false ;
380+ is_straight_path = false ;
381+ } else {
382+ }
383+ }
384+ }
385+
386+ if (entered_junction && !current_waypoint->CheckJunction ()){
387+ break ;
388+ }
389+ }
390+ if (!is_straight_path){
391+ large_vehicles[actor_id].first = junction_length;
392+ }
393+
394+ }
395+ else if (is_at_junction
396+ && large_vehicles_at_junction_entrance.find (actor_id) != large_vehicles_at_junction_entrance.end ()) {
397+
398+ large_vehicles_at_junction_entrance.erase (actor_id);
399+ large_vehicles_at_junction.insert (actor_id);
400+ }
401+ else if (!is_at_junction
402+ && large_vehicles_at_junction.find (actor_id) != large_vehicles_at_junction.end ()) {
403+
404+ large_vehicles_at_junction.erase (actor_id);
405+ if (large_vehicles.find (actor_id) != large_vehicles.end ()){
406+ large_vehicles[actor_id].first = 0 .0f ;
407+ }
408+ }
409+ }
410+
411+ float LocalizationStage::GetThreePointCircleRadius (cg::Location first_location,
412+ cg::Location middle_location,
413+ cg::Location last_location) {
414+
415+ float x1 = first_location.x ;
416+ float y1 = first_location.y ;
417+ float x2 = middle_location.x ;
418+ float y2 = middle_location.y ;
419+ float x3 = last_location.x ;
420+ float y3 = last_location.y ;
421+
422+ float x12 = x1 - x2;
423+ float x13 = x1 - x3;
424+ float y12 = y1 - y2;
425+ float y13 = y1 - y3;
426+ float y31 = y3 - y1;
427+ float y21 = y2 - y1;
428+ float x31 = x3 - x1;
429+ float x21 = x2 - x1;
430+
431+ float sx13 = x1 * x1 - x3 * x3;
432+ float sy13 = y1 * y1 - y3 * y3;
433+ float sx21 = x2 * x2 - x1 * x1;
434+ float sy21 = y2 * y2 - y1 * y1;
435+
436+ float f_denom = 2 * (y31 * x12 - y21 * x13);
437+ if (f_denom == 0 ) {
438+ return std::numeric_limits<float >::max ();
439+ }
440+ float f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / f_denom;
441+
442+ float g_denom = 2 * (x31 * y12 - x21 * y13);
443+ if (g_denom == 0 ) {
444+ return std::numeric_limits<float >::max ();
445+ }
446+ float g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / g_denom;
447+
448+ float c = - (x1 * x1 + y1 * y1) - 2 * g * x1 - 2 * f * y1;
449+ float h = -g;
450+ float k = -f;
451+
452+ return std::sqrt (h * h + k * k - c);
453+ }
454+
324455void LocalizationStage::RemoveActor (ActorId actor_id) {
325456 last_lane_change_swpt.erase (actor_id);
326457 vehicles_at_junction.erase (actor_id);
0 commit comments