24 const double epsilon = 0.00001;
26 auto References = GetAllReferencesToThisSignal(Map);
28 for (
auto& Reference : References)
30 auto RoadId = Reference.first;
31 const auto* SignalReference = Reference.second;
32 TSet<carla::road::RoadId> SignalPredecessors;
34 for(
auto &validity : SignalReference->GetValidities())
42 RoadId, lane, SignalReference->GetS()).value();
47 auto box_waypoint = signal_waypoint;
51 if (predecessors.size() == 1) {
52 auto predecessor = predecessors.front();
54 box_waypoint = predecessor;
60 float BoxWidth =
static_cast<float>(
62 float BoxLength = 1.5f;
63 float BoxHeight = 1.0f;
66 BoxWidth = std::max(0.01f, BoxWidth);
71 float AdditionalDistance = 1.5f;
74 box_waypoint.s = FMath::Clamp(box_waypoint.s - (BoxLength + AdditionalDistance),
75 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
79 box_waypoint.s = FMath::Clamp(box_waypoint.s + (BoxLength + AdditionalDistance),
80 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
88 GenerateStopBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
91 for(
auto &Prev : Predecessors)
93 if(!SignalPredecessors.Contains(Prev.road_id))
95 SignalPredecessors.Add(Prev.road_id);
106 if(
Junction->RoadHasConflicts(RoadId))
108 const auto &ConflictingRoads =
Junction->GetConflictsOfRoad(RoadId);
109 for(
const auto &Conflict : ConflictingRoads)
112 for(
auto& Waypoint : Waypoints)
115 bool bHasSamePredecessor =
false;
117 for(
auto &Prev : Predecessors)
119 if(SignalPredecessors.Contains(Prev.road_id))
121 bHasSamePredecessor =
true;
124 if(bHasSamePredecessor)
132 auto CurrentWaypoint = Waypoint;
133 auto NextWaypoint = CurrentWaypoint;
134 float BoxSize =
static_cast<float>(
138 BoxSize = std::max(0.01f, BoxSize);
139 float UEBoxSize = 100*BoxSize;
147 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);
169 double AnticipationTime = 0.1;
170 auto Previous = Map.
GetPrevious(Waypoint, 2*BoxSize);
171 std::queue<std::pair<float, carla::road::element::Waypoint>>
173 for (
auto & Prev : Previous)
175 WaypointQueue.push({AnticipationTime, Prev});
177 while (!WaypointQueue.empty())
179 auto CurrentElement = WaypointQueue.front();
187 Speed = InfoSpeed->GetSpeed();
189 float RemainingTime = CurrentElement.first - BoxSize/Speed;
190 if(RemainingTime > 0)
192 Previous = Map.
GetPrevious(CurrentElement.second, 2*BoxSize);
193 for (
auto & Prev : Previous)
195 WaypointQueue.push({RemainingTime, Prev});
206void UStopSignComponent::GenerateStopBox(
const FTransform BoxTransform,
207 const FVector BoxSize)
209 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
210 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapBeginStopEffectBox);
211 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapEndStopEffectBox);
212 AddEffectTriggerVolume(BoxComponent);
215void UStopSignComponent::GenerateCheckBox(
const FTransform BoxTransform,
218 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
219 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapBeginStopCheckBox);
220 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapEndStopCheckBox);
223void UStopSignComponent::GiveWayIfPossible()
225 if (VehiclesToCheck.Num() == 0)
227 for (
auto Vehicle : VehiclesInStop)
230 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
236 if(VehiclesInStop.Num())
238 for (
auto Vehicle : VehiclesInStop)
241 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
245 DelayedGiveWay(1.0f);
250void UStopSignComponent::DelayedGiveWay(
float Delay)
252 FTimerHandle TimerHandler;
253 GetWorld()->GetTimerManager().
254 SetTimer(TimerHandler,
this, &UStopSignComponent::GiveWayIfPossible, Delay);
257void UStopSignComponent::OnOverlapBeginStopEffectBox(UPrimitiveComponent *OverlappedComp,
259 UPrimitiveComponent *OtherComp,
260 int32 OtherBodyIndex,
262 const FHitResult &SweepResult)
268 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
269 if (VehicleController)
275 DelayedGiveWay(2.0f);
278 RemoveSameVehicleInBothLists();
281void UStopSignComponent::OnOverlapEndStopEffectBox(UPrimitiveComponent *OverlappedComp,
283 UPrimitiveComponent *OtherComp,
284 int32 OtherBodyIndex)
289 VehiclesInStop.Remove(
Vehicle);
293void UStopSignComponent::OnOverlapBeginStopCheckBox(UPrimitiveComponent *OverlappedComp,
295 UPrimitiveComponent *OtherComp,
296 int32 OtherBodyIndex,
298 const FHitResult &SweepResult)
303 if(!VehiclesInStop.Contains(
Vehicle))
305 if (!VehiclesToCheck.Contains(
Vehicle))
307 VehiclesToCheck.Add(
Vehicle, 0);
315void UStopSignComponent::OnOverlapEndStopCheckBox(UPrimitiveComponent *OverlappedComp,
317 UPrimitiveComponent *OtherComp,
318 int32 OtherBodyIndex)
323 if(VehiclesToCheck.Contains(
Vehicle))
326 if(VehiclesToCheck[
Vehicle] <= 0)
328 VehiclesToCheck.Remove(
Vehicle);
332 DelayedGiveWay(0.5f);
335void UStopSignComponent::RemoveSameVehicleInBothLists()
337 for(
auto*
Vehicle : VehiclesInStop)
339 if(VehiclesToCheck.Contains(
Vehicle))
341 VehiclesToCheck.Remove(
Vehicle);
Base class for CARLA wheeled vehicles.
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
Wheeled vehicle controller with optional AI.
void SetTrafficLightState(ETrafficLightState InTrafficLightState)
Set traffic light state currently affecting this vehicle.
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
static std::vector< int > GenerateRange(int a, int b)
double GetDistance() const
JuncId GetJunctionId(RoadId road_id) const
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...
bool IsJunction(RoadId road_id) const
double GetLaneWidth(Waypoint waypoint) const
std::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
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.
Junction * GetJunction(JuncId id)
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.
geom::Transform ComputeTransform(Waypoint waypoint) const
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
const Lane & GetLane(Waypoint waypoint) const
========================================================================
const T * GetInfo(const double s) const
carla::SharedPtr< carla::client::Junction > Junction