CARLA
Loading...
Searching...
No Matches
SpeedLimitComponent.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
12
18
19#include <util/ue-header-guard-begin.h>
20#include "Game/CarlaStatics.h"
21#include <util/ue-header-guard-end.h>
22
23void USpeedLimitComponent::SetSpeedLimit(float Limit)
24{
25 SpeedLimit = Limit;
26}
27
28void USpeedLimitComponent::InitializeSign(const carla::road::Map &Map)
29{
30 const double epsilon = 0.00001;
31
32 auto References = GetAllReferencesToThisSignal(Map);
33 auto* Signal = GetSignal(Map);
34 if (Signal)
35 {
36 SetSpeedLimit(Signal->GetValue());
37 }
38
39 for (auto& Reference : References)
40 {
41 auto RoadId = Reference.first;
42 const auto* SignalReference = Reference.second;
43 for(auto &val : SignalReference->GetValidities())
44 {
45 for(auto lane : carla::geom::Math::GenerateRange(val._from_lane, val._to_lane))
46 {
47 if(lane == 0)
48 continue;
49
50 auto signal_waypoint = Map.GetWaypoint(
51 RoadId, lane, SignalReference->GetS()).value();
52
53 if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving)
54 continue;
55
56 // Get 90% of the half size of the width of the lane
57 float BoxSize = static_cast<float>(
58 0.7f*Map.GetLaneWidth(signal_waypoint)*0.5);
59 // Prevent a situation where the road width is 0,
60 // this could happen in a lane that is just appearing
61 BoxSize = std::max(0.01f, BoxSize);
62 // Get min and max
63 double LaneLength = Map.GetLane(signal_waypoint).GetLength();
64 double LaneDistance = Map.GetLane(signal_waypoint).GetDistance();
65 if(lane < 0)
66 {
67 signal_waypoint.s = FMath::Clamp(signal_waypoint.s - BoxSize,
68 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
69 }
70 else
71 {
72 signal_waypoint.s = FMath::Clamp(signal_waypoint.s + BoxSize,
73 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
74 }
75 float UnrealBoxSize = 100*BoxSize;
76 FTransform BoxTransform = Map.ComputeTransform(signal_waypoint);
77 ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld());
78 if (LargeMapManager)
79 {
80 BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
81 }
82 GenerateSpeedBox(BoxTransform, UnrealBoxSize);
83 }
84 }
85 }
86
87}
88
89void USpeedLimitComponent::GenerateSpeedBox(const FTransform BoxTransform, float BoxSize)
90{
91 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
92 BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &USpeedLimitComponent::OnOverlapBeginSpeedLimitBox);
93 AddEffectTriggerVolume(BoxComponent);
94}
95
96void USpeedLimitComponent::OnOverlapBeginSpeedLimitBox(UPrimitiveComponent *OverlappedComp,
97 AActor *OtherActor,
98 UPrimitiveComponent *OtherComp,
99 int32 OtherBodyIndex,
100 bool bFromSweep,
101 const FHitResult &SweepResult)
102{
103 ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
104 if (Vehicle)
105 {
106 auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
107 if (Controller)
108 {
109 Controller->SetSpeedLimit(SpeedLimit);
110 }
111 }
112}
Base class for CARLA wheeled vehicles.
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
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
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
const Lane & GetLane(Waypoint waypoint) const
========================================================================
Definition road/Map.cpp:994