22 const double epsilon = 0.00001;
24 auto References = GetAllReferencesToThisSignal(Map);
26 for (
auto& Reference : References)
28 auto RoadId = Reference.first;
29 const auto* SignalReference = Reference.second;
30 TSet<carla::road::RoadId> SignalPredecessors;
32 for(
auto &validity : SignalReference->GetValidities())
40 RoadId, lane, SignalReference->GetS()).
get();
42 if(Map.
GetLane(signal_waypoint).
GetType() != cr::Lane::LaneType::Driving)
45 auto box_waypoint = signal_waypoint;
49 if (predecessors.size() == 1) {
50 auto predecessor = predecessors.front();
52 box_waypoint = predecessor;
58 float BoxWidth =
static_cast<float>(
60 float BoxLength = 1.5f;
61 float BoxHeight = 1.0f;
64 BoxWidth = std::max(0.01f, BoxWidth);
69 float AdditionalDistance = 1.5f;
72 box_waypoint.s = FMath::Clamp(box_waypoint.s - (BoxLength + AdditionalDistance),
73 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
77 box_waypoint.s = FMath::Clamp(box_waypoint.s + (BoxLength + AdditionalDistance),
78 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
86 GenerateYieldBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
89 for(
auto &Prev : Predecessors)
91 if(!SignalPredecessors.Contains(Prev.road_id))
93 SignalPredecessors.Add(Prev.road_id);
106 const auto &ConflictingRoads =
Junction->GetConflictsOfRoad(
RoadId);
107 for(
const auto &Conflict : ConflictingRoads)
110 for(
auto& Waypoint : Waypoints)
113 bool bHasSamePredecessor =
false;
115 for(
auto &Prev : Predecessors)
117 if(SignalPredecessors.Contains(Prev.road_id))
119 bHasSamePredecessor =
true;
122 if(bHasSamePredecessor)
127 if(Map.
GetLane(Waypoint).
GetType() != cr::Lane::LaneType::Driving)
131 auto CurrentWaypoint = Waypoint;
132 auto NextWaypoint = CurrentWaypoint;
133 float BoxSize =
static_cast<float>(
137 BoxSize = std::max(0.01f, BoxSize);
138 float UEBoxSize = 100*BoxSize;
146 GenerateCheckBox(BoxTransform, UEBoxSize);
150 auto Next = Map.
GetNext(NextWaypoint, 2*BoxSize);
151 if (Next.size() != 1)
155 NextWaypoint = Next.front();
156 if(NextWaypoint.road_id != Waypoint.road_id)
165 GenerateCheckBox(BoxTransform, UEBoxSize);
170 double AnticipationTime = 0.1;
171 auto Previous = Map.
GetPrevious(Waypoint, 2*BoxSize);
172 std::queue<std::pair<float, carla::road::element::Waypoint>>
174 for (
auto & Prev : Previous)
176 WaypointQueue.push({AnticipationTime, Prev});
178 while (!WaypointQueue.empty())
180 auto CurrentElement = WaypointQueue.front();
190 float RemainingTime = CurrentElement.first - BoxSize/Speed;
191 if(RemainingTime > 0)
193 Previous = Map.
GetPrevious(CurrentElement.second, 2*BoxSize);
194 for (
auto & Prev : Previous)
196 WaypointQueue.push({RemainingTime, Prev});
207 void UYieldSignComponent::GenerateYieldBox(
const FTransform BoxTransform,
208 const FVector BoxSize)
210 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
211 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UYieldSignComponent::OnOverlapBeginYieldEffectBox);
212 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UYieldSignComponent::OnOverlapEndYieldEffectBox);
213 AddEffectTriggerVolume(BoxComponent);
216 void UYieldSignComponent::GenerateCheckBox(
const FTransform BoxTransform,
219 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
220 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UYieldSignComponent::OnOverlapBeginYieldCheckBox);
221 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UYieldSignComponent::OnOverlapEndYieldCheckBox);
224 void UYieldSignComponent::GiveWayIfPossible()
226 if (VehiclesToCheck.Num() == 0)
228 for (
auto Vehicle : VehiclesInYield)
231 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
237 if(VehiclesInYield.Num())
239 for (
auto Vehicle : VehiclesInYield)
242 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
246 DelayedGiveWay(0.5f);
251 void UYieldSignComponent::DelayedGiveWay(
float Delay)
253 FTimerHandle TimerHandler;
254 GetWorld()->GetTimerManager().
255 SetTimer(TimerHandler,
this, &UYieldSignComponent::GiveWayIfPossible, Delay);
258 void UYieldSignComponent::OnOverlapBeginYieldEffectBox(
UPrimitiveComponent *OverlappedComp,
261 int32 OtherBodyIndex,
263 const FHitResult &SweepResult)
269 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
270 if (VehicleController)
272 VehiclesInYield.Add(Vehicle);
273 RemoveSameVehicleInBothLists();
282 int32 OtherBodyIndex)
287 VehiclesInYield.Remove(Vehicle);
294 int32 OtherBodyIndex,
296 const FHitResult &SweepResult)
301 if(!VehiclesInYield.Contains(Vehicle))
303 if (!VehiclesToCheck.Contains(Vehicle))
305 VehiclesToCheck.Add(Vehicle, 0);
316 int32 OtherBodyIndex)
321 if(VehiclesToCheck.Contains(Vehicle))
324 if(VehiclesToCheck[Vehicle] <= 0)
326 VehiclesToCheck.Remove(Vehicle);
333 void UYieldSignComponent::RemoveSameVehicleInBothLists()
335 for(
auto* Vehicle : VehiclesInYield)
337 if(VehiclesToCheck.Contains(Vehicle))
339 VehiclesToCheck.Remove(Vehicle);
std::vector< Waypoint > GetPrevious(Waypoint waypoint, double distance) const
Return the list of waypoints at distance in the reversed direction that a vehicle at waypoint could d...
boost::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
const Lane & GetLane(Waypoint waypoint) const
======================================================================== – Road information --------...
double GetDistance() const
Junction * GetJunction(JuncId id)
const T * GetInfo(const double s) const
void SetTrafficLightState(ETrafficLightState InTrafficLightState)
Set traffic light state currently affecting this vehicle.
std::vector< Waypoint > GetNext(Waypoint waypoint, double distance) const
Return the list of waypoints at distance such that a vehicle at waypoint could drive to...
Wheeled vehicle controller with optional AI.
geom::Transform ComputeTransform(Waypoint waypoint) const
bool IsJunction(RoadId road_id) const
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
static std::vector< int > GenerateRange(int a, int b)
std::vector< Waypoint > GenerateWaypointsInRoad(RoadId road_id, Lane::LaneType lane_type=Lane::LaneType::Driving) const
Generate waypoints at the entry of each lane of the specified road.
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
JuncId GetJunctionId(RoadId road_id) const
Base class for CARLA wheeled vehicles.
double GetLaneWidth(Waypoint waypoint) const
carla::SharedPtr< carla::client::Junction > Junction