CARLA
Loading...
Searching...
No Matches
TrafficLightComponent.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
9#include "Kismet/GameplayStatics.h"
11#include "TrafficLightGroup.h"
13#include "TrafficLightManager.h"
16
20
21
22UTrafficLightComponent::UTrafficLightComponent()
23 : Super()
24{
25}
26
27void UTrafficLightComponent::InitializeSign(const carla::road::Map &Map)
28{
29 const double epsilon = 0.00001;
30
31 auto References = GetAllReferencesToThisSignal(Map);
32
33 for (auto& Reference : References)
34 {
35 auto RoadId = Reference.first;
36 const auto* SignalReference = Reference.second;
37 for(auto &validity : SignalReference->GetValidities())
38 {
39 for(auto lane : carla::geom::Math::GenerateRange(validity._from_lane, validity._to_lane))
40 {
41 if(lane == 0)
42 continue;
43
44 carla::road::element::Waypoint signal_waypoint;
45 std::optional<carla::road::element::Waypoint> opt_signal_waypoint = Map.GetWaypoint(
46 RoadId, lane, SignalReference->GetS());
47 if(opt_signal_waypoint){
48 signal_waypoint = opt_signal_waypoint.value();
49 }else{
50 continue;
51 }
52
53 if (Map.IsJunction(RoadId)) {
54 auto predecessors = Map.GetPredecessors(signal_waypoint);
55 if (predecessors.size() == 1) {
56 auto predecessor = predecessors.front();
57 if (!Map.IsJunction(predecessor.road_id)) {
58 signal_waypoint = predecessor;
59 }
60 }
61 }
62
63 if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving)
64 continue;
65
66 float BoxWidth = static_cast<float>(0.5f*Map.GetLaneWidth(signal_waypoint)*0.5);
67 float BoxLength = 1.5f;
68 float BoxHeight = 1.0f;
69
70 BoxWidth = std::max(0.01f, BoxWidth);
71 double LaneLength = Map.GetLane(signal_waypoint).GetLength();
72 double LaneDistance = Map.GetLane(signal_waypoint).GetDistance();
73 float AdditionalDistance = 1.5f;
74 if(lane < 0)
75 {
76 signal_waypoint.s = FMath::Clamp(signal_waypoint.s - (BoxLength + AdditionalDistance),
77 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
78 }
79 else
80 {
81 signal_waypoint.s = FMath::Clamp(signal_waypoint.s + (BoxLength + AdditionalDistance),
82 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
83 }
84 FTransform BoxTransform = Map.ComputeTransform(signal_waypoint);
85 ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld());
86 if (LargeMapManager)
87 {
88 BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
89 }
90 GenerateTrafficLightBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
91 }
92 }
93 }
94}
95
96void UTrafficLightComponent::GenerateTrafficLightBox(const FTransform BoxTransform,
97 const FVector BoxSize)
98{
99 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
100 BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UTrafficLightComponent::OnBeginOverlapTriggerBox);
101 BoxComponent->OnComponentEndOverlap.AddDynamic(this, &UTrafficLightComponent::OnEndOverlapTriggerBox);
102 AddEffectTriggerVolume(BoxComponent);
103}
104
105void UTrafficLightComponent::SetLightState(ETrafficLightState NewState)
106{
107 LightState = NewState;
108 LightChangeDispatcher.Broadcast();
109 if (GetOwner() == nullptr)
110 {
111 return;
112 }
113 if (GetOwner()->Implements<UTrafficLightInterface>())
114 {
115 ITrafficLightInterface::Execute_LightChanged(GetOwner(), LightState);
116 }
117 ATrafficLightBase* OldTrafficLight = Cast<ATrafficLightBase>(GetOwner());
118 if (OldTrafficLight)
119 {
120 OldTrafficLight->LightChangedCompatibility(NewState);
121 }
122
123 for (auto Controller : Vehicles)
124 {
125 if (Controller != nullptr)
126 {
127 Controller->SetTrafficLightState(LightState);
128 }
129 }
130}
131
132void UTrafficLightComponent::OnComponentDestroyed(bool bDestroyingHierarchy)
133{
134 if (TrafficLightController != nullptr)
135 {
136 TrafficLightController->RemoveTrafficLight(this);
137 TrafficLightController = nullptr;
138 }
139 Super::OnComponentDestroyed(bDestroyingHierarchy);
140}
141
142ETrafficLightState UTrafficLightComponent::GetLightState() const
143{
144 return LightState;
145}
146
147void UTrafficLightComponent::SetFrozenGroup(bool InFreeze)
148{
149 if (GetGroup())
150 {
151 GetGroup()->SetFrozenGroup(InFreeze);
152 }
153}
154
155ATrafficLightGroup* UTrafficLightComponent::GetGroup()
156{
157 return
158 TrafficLightController != nullptr ?
159 TrafficLightController->GetGroup() :
160 nullptr;
161}
162
163const ATrafficLightGroup* UTrafficLightComponent::GetGroup() const
164{
165 return
166 TrafficLightController != nullptr ?
167 TrafficLightController->GetGroup() :
168 nullptr;
169}
170
171UTrafficLightController* UTrafficLightComponent::GetController()
172{
173 return TrafficLightController;
174}
175
176const UTrafficLightController* UTrafficLightComponent::GetController() const
177{
178 return TrafficLightController;
179}
180
181void UTrafficLightComponent::OnBeginOverlapTriggerBox(UPrimitiveComponent *OverlappedComp,
182 AActor *OtherActor,
183 UPrimitiveComponent *OtherComp,
184 int32 OtherBodyIndex,
185 bool bFromSweep,
186 const FHitResult &SweepResult)
187{
188 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
189 if (Vehicle)
190 {
191 AWheeledVehicleAIController* VehicleController =
192 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
193 if (VehicleController)
194 {
195 VehicleController->SetTrafficLightState(LightState);
196 Vehicles.Add(VehicleController);
197 VehicleController->SetTrafficLight(Cast<ATrafficLightBase>(GetOwner()));
198 }
199 }
200}
201
202void UTrafficLightComponent::OnEndOverlapTriggerBox(UPrimitiveComponent *OverlappedComp,
203 AActor *OtherActor,
204 UPrimitiveComponent *OtherComp,
205 int32 OtherBodyIndex)
206{
207 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
208 if (Vehicle)
209 {
210 AWheeledVehicleAIController* VehicleController =
211 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
212 if (VehicleController)
213 {
214 VehicleController->SetTrafficLightState(ETrafficLightState::Green);
215 VehicleController->SetTrafficLight(nullptr);
216 Vehicles.Remove(VehicleController);
217 }
218 }
219}
220
221void UTrafficLightComponent::SetController(UTrafficLightController* Controller)
222{
223 TrafficLightController = Controller;
224}
Base class for CARLA wheeled vehicles.
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
void LightChangedCompatibility(ETrafficLightState NewLightState)
Class which implements the state changing of traffic lights.
Wheeled vehicle controller with optional AI.
void SetTrafficLightState(ETrafficLightState InTrafficLightState)
Set traffic light state currently affecting this vehicle.
void SetTrafficLight(ATrafficLightBase *InTrafficLight)
Set traffic light currently affecting this vehicle.
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Maps a controller from OpenDrive.
static std::vector< int > GenerateRange(int a, int b)
Definition Math.cpp:159
LaneType GetType() const
Definition road/Lane.cpp:38
double GetDistance() const
Definition road/Lane.cpp:46
double GetLength() const
Definition road/Lane.cpp:51
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
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