CARLA
Loading...
Searching...
No Matches
StopSignComponent.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "StopSignComponent.h"
8#include "TrafficLightState.h"
12
17
18#include <queue>
19
20
21void UStopSignComponent::InitializeSign(const carla::road::Map &Map)
22{
23
24 const double epsilon = 0.00001;
25
26 auto References = GetAllReferencesToThisSignal(Map);
27
28 for (auto& Reference : References)
29 {
30 auto RoadId = Reference.first;
31 const auto* SignalReference = Reference.second;
32 TSet<carla::road::RoadId> SignalPredecessors;
33 // Stop box
34 for(auto &validity : SignalReference->GetValidities())
35 {
36 for(auto lane : carla::geom::Math::GenerateRange(validity._from_lane, validity._to_lane))
37 {
38 if(lane == 0)
39 continue;
40
41 auto signal_waypoint = Map.GetWaypoint(
42 RoadId, lane, SignalReference->GetS()).value();
43
44 if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving)
45 continue;
46
47 auto box_waypoint = signal_waypoint;
48 // Prevent adding the bounding box inside the intersection
49 if (Map.IsJunction(RoadId)) {
50 auto predecessors = Map.GetPredecessors(box_waypoint);
51 if (predecessors.size() == 1) {
52 auto predecessor = predecessors.front();
53 if (!Map.IsJunction(predecessor.road_id)) {
54 box_waypoint = predecessor;
55 }
56 }
57 }
58
59 // Get 50% of the half size of the width of the lane
60 float BoxWidth = static_cast<float>(
61 0.5f*Map.GetLaneWidth(box_waypoint)*0.5);
62 float BoxLength = 1.5f;
63 float BoxHeight = 1.0f;
64 // Prevent a situation where the road width is 0,
65 // this could happen in a lane that is just appearing
66 BoxWidth = std::max(0.01f, BoxWidth);
67 // Get min and max
68 double LaneLength = Map.GetLane(box_waypoint).GetLength();
69 double LaneDistance = Map.GetLane(box_waypoint).GetDistance();
70 // Safe distance to avoid overlapping the bounding box with the intersection
71 float AdditionalDistance = 1.5f;
72 if(lane < 0)
73 {
74 box_waypoint.s = FMath::Clamp(box_waypoint.s - (BoxLength + AdditionalDistance),
75 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
76 }
77 else
78 {
79 box_waypoint.s = FMath::Clamp(box_waypoint.s + (BoxLength + AdditionalDistance),
80 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
81 }
82 FTransform BoxTransform = Map.ComputeTransform(box_waypoint);
83 ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld());
84 if (LargeMapManager)
85 {
86 BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
87 }
88 GenerateStopBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
89
90 auto Predecessors = Map.GetPredecessors(signal_waypoint);
91 for(auto &Prev : Predecessors)
92 {
93 if(!SignalPredecessors.Contains(Prev.road_id))
94 {
95 SignalPredecessors.Add(Prev.road_id);
96 }
97 }
98 }
99 }
100
101 //Check boxes
102 if(Map.IsJunction(RoadId))
103 {
104 auto JuncId = Map.GetJunctionId(RoadId);
105 const auto * Junction = Map.GetJunction(JuncId);
106 if(Junction->RoadHasConflicts(RoadId))
107 {
108 const auto &ConflictingRoads = Junction->GetConflictsOfRoad(RoadId);
109 for(const auto &Conflict : ConflictingRoads)
110 {
111 auto Waypoints = Map.GenerateWaypointsInRoad(Conflict);
112 for(auto& Waypoint : Waypoints)
113 {
114 // Skip roads that share the same previous road
115 bool bHasSamePredecessor = false;
116 auto Predecessors = Map.GetPredecessors(Waypoint);
117 for(auto &Prev : Predecessors)
118 {
119 if(SignalPredecessors.Contains(Prev.road_id))
120 {
121 bHasSamePredecessor = true;
122 }
123 }
124 if(bHasSamePredecessor)
125 {
126 continue;
127 }
128 if(Map.GetLane(Waypoint).GetType() != cr::Lane::LaneType::Driving)
129 continue;
130
131 // Cover the road within the junction
132 auto CurrentWaypoint = Waypoint;
133 auto NextWaypoint = CurrentWaypoint;
134 float BoxSize = static_cast<float>(
135 0.9*Map.GetLaneWidth(NextWaypoint)*0.5);
136 // Prevent a situation where the road width is 0
137 // This could happen in a lane that is just appearing
138 BoxSize = std::max(0.01f, BoxSize);
139 float UEBoxSize = 100*BoxSize;
140
141 FTransform BoxTransform = Map.ComputeTransform(NextWaypoint);
142 ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld());
143 if (LargeMapManager)
144 {
145 BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
146 }
147 GenerateCheckBox(BoxTransform, UEBoxSize);
148 while (true)
149 {
150 auto Next = Map.GetNext(NextWaypoint, 2*BoxSize);
151 if (Next.size() != 1)
152 {
153 break;
154 }
155 NextWaypoint = Next.front();
156 if(NextWaypoint.road_id != Waypoint.road_id)
157 {
158 break;
159 }
160 BoxTransform = Map.ComputeTransform(NextWaypoint);
161 if (LargeMapManager)
162 {
163 BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
164 }
165 GenerateCheckBox(BoxTransform, UEBoxSize);
166 }
167 // Cover the road before the junction
168 // Hard coded anticipation time (boxes placed prior to the junction)
169 double AnticipationTime = 0.1;
170 auto Previous = Map.GetPrevious(Waypoint, 2*BoxSize);
171 std::queue<std::pair<float, carla::road::element::Waypoint>>
172 WaypointQueue;
173 for (auto & Prev : Previous)
174 {
175 WaypointQueue.push({AnticipationTime, Prev});
176 }
177 while (!WaypointQueue.empty())
178 {
179 auto CurrentElement = WaypointQueue.front();
180 WaypointQueue.pop();
181 GenerateCheckBox(Map.ComputeTransform(CurrentElement.second), UEBoxSize);
182
183 float Speed = 40;
184 auto* InfoSpeed = Map.GetLane(CurrentElement.second).GetRoad()->GetInfo<carla::road::element::RoadInfoSpeed>(CurrentElement.second.s);
185 if(InfoSpeed)
186 {
187 Speed = InfoSpeed->GetSpeed();
188 }
189 float RemainingTime = CurrentElement.first - BoxSize/Speed;
190 if(RemainingTime > 0)
191 {
192 Previous = Map.GetPrevious(CurrentElement.second, 2*BoxSize);
193 for (auto & Prev : Previous)
194 {
195 WaypointQueue.push({RemainingTime, Prev});
196 }
197 }
198 }
199 }
200 }
201 }
202 }
203 }
204}
205
206void UStopSignComponent::GenerateStopBox(const FTransform BoxTransform,
207 const FVector BoxSize)
208{
209 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
210 BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UStopSignComponent::OnOverlapBeginStopEffectBox);
211 BoxComponent->OnComponentEndOverlap.AddDynamic(this, &UStopSignComponent::OnOverlapEndStopEffectBox);
212 AddEffectTriggerVolume(BoxComponent);
213}
214
215void UStopSignComponent::GenerateCheckBox(const FTransform BoxTransform,
216 float BoxSize)
217{
218 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
219 BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UStopSignComponent::OnOverlapBeginStopCheckBox);
220 BoxComponent->OnComponentEndOverlap.AddDynamic(this, &UStopSignComponent::OnOverlapEndStopCheckBox);
221}
222
223void UStopSignComponent::GiveWayIfPossible()
224{
225 if (VehiclesToCheck.Num() == 0)
226 {
227 for (auto Vehicle : VehiclesInStop)
228 {
229 AWheeledVehicleAIController* VehicleController =
230 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
231 VehicleController->SetTrafficLightState(ETrafficLightState::Green);
232 }
233 }
234 else
235 {
236 if(VehiclesInStop.Num())
237 {
238 for (auto Vehicle : VehiclesInStop)
239 {
240 AWheeledVehicleAIController* VehicleController =
241 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
242 VehicleController->SetTrafficLightState(ETrafficLightState::Red);
243 }
244 // 1 second delay
245 DelayedGiveWay(1.0f);
246 }
247 }
248}
249
250void UStopSignComponent::DelayedGiveWay(float Delay)
251{
252 FTimerHandle TimerHandler;
253 GetWorld()->GetTimerManager().
254 SetTimer(TimerHandler, this, &UStopSignComponent::GiveWayIfPossible, Delay);
255}
256
257void UStopSignComponent::OnOverlapBeginStopEffectBox(UPrimitiveComponent *OverlappedComp,
258 AActor *OtherActor,
259 UPrimitiveComponent *OtherComp,
260 int32 OtherBodyIndex,
261 bool bFromSweep,
262 const FHitResult &SweepResult)
263{
264 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
265 if (Vehicle)
266 {
267 AWheeledVehicleAIController* VehicleController =
268 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
269 if (VehicleController)
270 {
271 VehicleController->SetTrafficLightState(ETrafficLightState::Red);
272 VehiclesInStop.Add(Vehicle);
273
274 // 2 second delay for stop
275 DelayedGiveWay(2.0f);
276 }
277 }
278 RemoveSameVehicleInBothLists();
279}
280
281void UStopSignComponent::OnOverlapEndStopEffectBox(UPrimitiveComponent *OverlappedComp,
282 AActor *OtherActor,
283 UPrimitiveComponent *OtherComp,
284 int32 OtherBodyIndex)
285{
286 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
287 if (Vehicle)
288 {
289 VehiclesInStop.Remove(Vehicle);
290 }
291}
292
293void UStopSignComponent::OnOverlapBeginStopCheckBox(UPrimitiveComponent *OverlappedComp,
294 AActor *OtherActor,
295 UPrimitiveComponent *OtherComp,
296 int32 OtherBodyIndex,
297 bool bFromSweep,
298 const FHitResult &SweepResult)
299{
300 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
301 if (Vehicle)
302 {
303 if(!VehiclesInStop.Contains(Vehicle))
304 {
305 if (!VehiclesToCheck.Contains(Vehicle))
306 {
307 VehiclesToCheck.Add(Vehicle, 0);
308 }
309 VehiclesToCheck[Vehicle]++;
310 }
311 GiveWayIfPossible();
312 }
313}
314
315void UStopSignComponent::OnOverlapEndStopCheckBox(UPrimitiveComponent *OverlappedComp,
316 AActor *OtherActor,
317 UPrimitiveComponent *OtherComp,
318 int32 OtherBodyIndex)
319{
320 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
321 if (Vehicle)
322 {
323 if(VehiclesToCheck.Contains(Vehicle))
324 {
325 VehiclesToCheck[Vehicle]--;
326 if(VehiclesToCheck[Vehicle] <= 0)
327 {
328 VehiclesToCheck.Remove(Vehicle);
329 }
330 }
331 // 0.5s delay
332 DelayedGiveWay(0.5f);
333 }
334}
335void UStopSignComponent::RemoveSameVehicleInBothLists()
336{
337 for(auto* Vehicle : VehiclesInStop)
338 {
339 if(VehiclesToCheck.Contains(Vehicle))
340 {
341 VehiclesToCheck.Remove(Vehicle);
342 }
343 }
344}
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)
Definition Math.cpp:159
Road * GetRoad() const
Definition road/Lane.cpp:29
LaneType GetType() const
Definition road/Lane.cpp:38
double GetDistance() const
Definition road/Lane.cpp:46
double GetLength() const
Definition road/Lane.cpp:51
JuncId GetJunctionId(RoadId road_id) const
Definition road/Map.cpp:303
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...
Definition road/Map.cpp:718
bool IsJunction(RoadId road_id) const
Definition road/Map.cpp:307
double GetLaneWidth(Waypoint waypoint) const
Definition road/Map.cpp:290
std::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
Definition road/Map.cpp:217
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.
Definition road/Map.cpp:669
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.
Definition road/Map.cpp:854
geom::Transform ComputeTransform(Waypoint waypoint) const
Definition road/Map.cpp:278
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
Definition road/Map.cpp:643
const Lane & GetLane(Waypoint waypoint) const
========================================================================
Definition road/Map.cpp:994
const T * GetInfo(const double s) const
Definition road/Road.h:122
carla::SharedPtr< carla::client::Junction > Junction