9#include "Kismet/GameplayStatics.h"
22UTrafficLightComponent::UTrafficLightComponent()
29 const double epsilon = 0.00001;
31 auto References = GetAllReferencesToThisSignal(Map);
33 for (
auto& Reference : References)
35 auto RoadId = Reference.first;
36 const auto* SignalReference = Reference.second;
37 for(
auto &validity : SignalReference->GetValidities())
39 for(
auto lane :
carla::geom::Math::GenerateRange(validity._from_lane, validity._to_lane))
45 boost::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.get();
50 UE_LOG(LogCarla, Error, TEXT(
"signal_waypoint is not valid") );
56 if (predecessors.size() == 1) {
57 auto predecessor = predecessors.front();
59 signal_waypoint = predecessor;
64 if(Map.
GetLane(signal_waypoint).
GetType() != cr::Lane::LaneType::Driving)
68 float BoxWidth =
static_cast<float>(
70 float BoxLength = 1.5f;
71 float BoxHeight = 1.0f;
75 BoxWidth = std::max(0.01f, BoxWidth);
80 float AdditionalDistance = 1.5f;
83 signal_waypoint.
s = FMath::Clamp(signal_waypoint.
s - (BoxLength + AdditionalDistance),
84 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
88 signal_waypoint.
s = FMath::Clamp(signal_waypoint.
s + (BoxLength + AdditionalDistance),
89 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
97 GenerateTrafficLightBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
103void UTrafficLightComponent::GenerateTrafficLightBox(
const FTransform BoxTransform,
104 const FVector BoxSize)
106 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
107 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UTrafficLightComponent::OnBeginOverlapTriggerBox);
108 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UTrafficLightComponent::OnEndOverlapTriggerBox);
109 AddEffectTriggerVolume(BoxComponent);
114 LightState = NewState;
115 LightChangeDispatcher.Broadcast();
116 if (GetOwner()->Implements<UTrafficLightInterface>())
118 ITrafficLightInterface::Execute_LightChanged(GetOwner(), LightState);
126 for (
auto Controller : Vehicles)
128 if (Controller !=
nullptr)
130 Controller->SetTrafficLightState(LightState);
140void UTrafficLightComponent::SetFrozenGroup(
bool InFreeze)
144 GetGroup()->SetFrozenGroup(InFreeze);
150 return TrafficLightController->GetGroup();
155 return TrafficLightController->GetGroup();
160 return TrafficLightController;
165 return TrafficLightController;
171 int32 OtherBodyIndex,
173 const FHitResult &SweepResult)
179 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
180 if (VehicleController)
183 Vehicles.Add(VehicleController);
184 VehicleController->
SetTrafficLight(Cast<ATrafficLightBase>(GetOwner()));
192 int32 OtherBodyIndex)
198 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
199 if (VehicleController)
203 Vehicles.Remove(VehicleController);
210 TrafficLightController = Controller;
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.
double GetDistance() const
bool IsJunction(RoadId road_id) const
double GetLaneWidth(Waypoint waypoint) const
boost::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
geom::Transform ComputeTransform(Waypoint waypoint) const
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
const Lane & GetLane(Waypoint waypoint) const
========================================================================
This file contains definitions of common data structures used in traffic manager.