23 const double epsilon = 0.00001;
25 auto References = GetAllReferencesToThisSignal(Map);
27 for (
auto& Reference : References)
29 auto RoadId = Reference.first;
30 const auto* SignalReference = Reference.second;
31 TSet<carla::road::RoadId> SignalPredecessors;
33 for(
auto &validity : SignalReference->GetValidities())
41 RoadId, lane, SignalReference->GetS()).
get();
43 if(Map.
GetLane(signal_waypoint).
GetType() != cr::Lane::LaneType::Driving)
46 auto box_waypoint = signal_waypoint;
50 if (predecessors.size() == 1) {
51 auto predecessor = predecessors.front();
53 box_waypoint = predecessor;
59 float BoxWidth =
static_cast<float>(
61 float BoxLength = 1.5f;
62 float BoxHeight = 1.0f;
65 BoxWidth = std::max(0.01f, BoxWidth);
70 float AdditionalDistance = 1.5f;
73 box_waypoint.s = FMath::Clamp(box_waypoint.s - (BoxLength + AdditionalDistance),
74 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
78 box_waypoint.s = FMath::Clamp(box_waypoint.s + (BoxLength + AdditionalDistance),
79 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
87 GenerateStopBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
90 for(
auto &Prev : Predecessors)
92 if(!SignalPredecessors.Contains(Prev.road_id))
94 SignalPredecessors.Add(Prev.road_id);
107 const auto &ConflictingRoads =
Junction->GetConflictsOfRoad(
RoadId);
108 for(
const auto &Conflict : ConflictingRoads)
111 for(
auto& Waypoint : Waypoints)
114 bool bHasSamePredecessor =
false;
116 for(
auto &Prev : Predecessors)
118 if(SignalPredecessors.Contains(Prev.road_id))
120 bHasSamePredecessor =
true;
123 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);
149 auto Next = Map.
GetNext(NextWaypoint, 2*BoxSize);
150 if (Next.size() != 1)
154 NextWaypoint = Next.front();
155 if(NextWaypoint.road_id != Waypoint.road_id)
164 GenerateCheckBox(BoxTransform, UEBoxSize);
168 double AnticipationTime = 0.1;
169 auto Previous = Map.
GetPrevious(Waypoint, 2*BoxSize);
170 std::queue<std::pair<float, carla::road::element::Waypoint>>
172 for (
auto & Prev : Previous)
174 WaypointQueue.push({AnticipationTime, Prev});
176 while (!WaypointQueue.empty())
178 auto CurrentElement = WaypointQueue.front();
188 float RemainingTime = CurrentElement.first - BoxSize/Speed;
189 if(RemainingTime > 0)
191 Previous = Map.
GetPrevious(CurrentElement.second, 2*BoxSize);
192 for (
auto & Prev : Previous)
194 WaypointQueue.push({RemainingTime, Prev});
205 void UStopSignComponent::GenerateStopBox(
const FTransform BoxTransform,
206 const FVector BoxSize)
208 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
209 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapBeginStopEffectBox);
210 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapEndStopEffectBox);
211 AddEffectTriggerVolume(BoxComponent);
214 void UStopSignComponent::GenerateCheckBox(
const FTransform BoxTransform,
217 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
218 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapBeginStopCheckBox);
219 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UStopSignComponent::OnOverlapEndStopCheckBox);
222 void UStopSignComponent::GiveWayIfPossible()
224 if (VehiclesToCheck.Num() == 0)
226 for (
auto Vehicle : VehiclesInStop)
229 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
235 if(VehiclesInStop.Num())
237 for (
auto Vehicle : VehiclesInStop)
240 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
244 DelayedGiveWay(1.0f);
249 void UStopSignComponent::DelayedGiveWay(
float Delay)
251 FTimerHandle TimerHandler;
252 GetWorld()->GetTimerManager().
253 SetTimer(TimerHandler,
this, &UStopSignComponent::GiveWayIfPossible, Delay);
259 int32 OtherBodyIndex,
261 const FHitResult &SweepResult)
267 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
268 if (VehicleController)
271 VehiclesInStop.Add(Vehicle);
274 DelayedGiveWay(2.0f);
277 RemoveSameVehicleInBothLists();
283 int32 OtherBodyIndex)
288 VehiclesInStop.Remove(Vehicle);
295 int32 OtherBodyIndex,
297 const FHitResult &SweepResult)
302 if(!VehiclesInStop.Contains(Vehicle))
304 if (!VehiclesToCheck.Contains(Vehicle))
306 VehiclesToCheck.Add(Vehicle, 0);
317 int32 OtherBodyIndex)
322 if(VehiclesToCheck.Contains(Vehicle))
325 if(VehiclesToCheck[Vehicle] <= 0)
327 VehiclesToCheck.Remove(Vehicle);
331 DelayedGiveWay(0.5f);
334 void UStopSignComponent::RemoveSameVehicleInBothLists()
336 for(
auto* Vehicle : VehiclesInStop)
338 if(VehiclesToCheck.Contains(Vehicle))
340 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