diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.cpp index 814eb8930..d62db5562 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.cpp @@ -100,6 +100,20 @@ UBoxComponent* USignComponent::GenerateTriggerBox(const FTransform &BoxTransform return BoxComponent; } +UBoxComponent* USignComponent::GenerateTriggerBox(const FTransform &BoxTransform, + const FVector &BoxSize) +{ + AActor *ParentActor = GetOwner(); + UBoxComponent *BoxComponent = NewObject(ParentActor); + BoxComponent->RegisterComponent(); + BoxComponent->AttachToComponent( + ParentActor->GetRootComponent(), + FAttachmentTransformRules::KeepRelativeTransform); + BoxComponent->SetWorldTransform(BoxTransform); + BoxComponent->SetBoxExtent(BoxSize, true); + return BoxComponent; +} + void USignComponent::AddEffectTriggerVolume(UBoxComponent* TriggerVolume) { EffectTriggerVolumes.Add(TriggerVolume); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.h index 8b0bafe5a..e30d278b3 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/SignComponent.h @@ -67,6 +67,9 @@ protected: UBoxComponent* GenerateTriggerBox(const FTransform &BoxTransform, float BoxSize); + UBoxComponent* GenerateTriggerBox(const FTransform &BoxTransform, + const FVector &BoxSize); + private: UPROPERTY(Category = "Traffic Sign", EditAnywhere) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.cpp index 1f0726041..b0c1f501d 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.cpp @@ -8,6 +8,8 @@ #include "TrafficLightState.h" #include +#include "Carla/Game/CarlaStatics.h" +#include "Carla/MapGen/LargeMapManager.h" #include #include #include @@ -38,27 +40,43 @@ void UStopSignComponent::InitializeSign(const carla::road::Map &Map) if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving) continue; - // Get 90% of the half size of the width of the lane - float BoxSize = static_cast( - 0.9*Map.GetLaneWidth(signal_waypoint)*0.5); + auto box_waypoint = signal_waypoint; + // Prevent adding the bounding box inside the intersection + if (Map.IsJunction(RoadId)) { + auto previous_waypoints = Map.GetPrevious(signal_waypoint, 2.0); + if (previous_waypoints.size() == 1) { + box_waypoint = previous_waypoints.front(); + } + } + + // Get 50% of the half size of the width of the lane + float BoxWidth = static_cast( + 0.5f*Map.GetLaneWidth(box_waypoint)*0.5); + float BoxLength = 1.5f; + float BoxHeight = 1.0f; // Prevent a situation where the road width is 0, // this could happen in a lane that is just appearing - BoxSize = std::max(0.01f, BoxSize); + BoxWidth = std::max(0.01f, BoxWidth); // Get min and max - double LaneLength = Map.GetLane(signal_waypoint).GetLength(); - double LaneDistance = Map.GetLane(signal_waypoint).GetDistance(); + double LaneLength = Map.GetLane(box_waypoint).GetLength(); + double LaneDistance = Map.GetLane(box_waypoint).GetDistance(); if(lane < 0) { - signal_waypoint.s = FMath::Clamp(signal_waypoint.s - BoxSize, + box_waypoint.s = FMath::Clamp(box_waypoint.s - BoxWidth, LaneDistance + epsilon, LaneDistance + LaneLength - epsilon); } else { - signal_waypoint.s = FMath::Clamp(signal_waypoint.s + BoxSize, + box_waypoint.s = FMath::Clamp(box_waypoint.s + BoxWidth, LaneDistance + epsilon, LaneDistance + LaneLength - epsilon); } - float UnrealBoxSize = 100*BoxSize; - GenerateStopBox(Map.ComputeTransform(signal_waypoint), UnrealBoxSize); + FTransform BoxTransform = Map.ComputeTransform(box_waypoint); + ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld()); + if (LargeMapManager) + { + BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); + } + GenerateStopBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight)); auto Predecessors = Map.GetPredecessors(signal_waypoint); for(auto &Prev : Predecessors) @@ -110,7 +128,14 @@ void UStopSignComponent::InitializeSign(const carla::road::Map &Map) // This could happen in a lane that is just appearing BoxSize = std::max(0.01f, BoxSize); float UEBoxSize = 100*BoxSize; - GenerateCheckBox(Map.ComputeTransform(NextWaypoint), UEBoxSize); + + FTransform BoxTransform = Map.ComputeTransform(NextWaypoint); + ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld()); + if (LargeMapManager) + { + BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); + } + GenerateCheckBox(BoxTransform, UEBoxSize); while (true) { auto Next = Map.GetNext(NextWaypoint, 2*BoxSize); @@ -123,8 +148,12 @@ void UStopSignComponent::InitializeSign(const carla::road::Map &Map) { break; } - - GenerateCheckBox(Map.ComputeTransform(NextWaypoint), UEBoxSize); + BoxTransform = Map.ComputeTransform(NextWaypoint); + if (LargeMapManager) + { + BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); + } + GenerateCheckBox(BoxTransform, UEBoxSize); } // Cover the road before the junction // Hard coded anticipation time (boxes placed prior to the junction) @@ -166,7 +195,7 @@ void UStopSignComponent::InitializeSign(const carla::road::Map &Map) } void UStopSignComponent::GenerateStopBox(const FTransform BoxTransform, - float BoxSize) + const FVector BoxSize) { UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize); BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UStopSignComponent::OnOverlapBeginStopEffectBox); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.h index a4d1415ac..51fb0727c 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/StopSignComponent.h @@ -23,7 +23,7 @@ public: private: void GenerateStopBox(const FTransform BoxTransform, - float BoxSize); + const FVector BoxSize); void GenerateCheckBox(const FTransform BoxTransform, float BoxSize); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp index 5f1f63682..23563e713 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp @@ -39,43 +39,53 @@ void UTrafficLightComponent::InitializeSign(const carla::road::Map &Map) auto signal_waypoint = Map.GetWaypoint( RoadId, lane, SignalReference->GetS()).get(); + // Prevent adding the bounding box inside the intersection + if (Map.IsJunction(RoadId)) { + auto previous_waypoints = Map.GetPrevious(signal_waypoint, 2.0); + if (previous_waypoints.size() == 1) { + signal_waypoint = previous_waypoints.front(); + } + } + if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving) continue; - // Get 90% of the half size of the width of the lane - float BoxSize = static_cast( - 0.9f*Map.GetLaneWidth(signal_waypoint)*0.5); + // Get 50% of the half size of the width of the lane + float BoxWidth = static_cast( + 0.5f*Map.GetLaneWidth(signal_waypoint)*0.5); + float BoxLength = 1.5f; + float BoxHeight = 1.0f; + // Prevent a situation where the road width is 0, // this could happen in a lane that is just appearing - BoxSize = std::max(0.01f, BoxSize); + BoxWidth = std::max(0.01f, BoxWidth); // Get min and max double LaneLength = Map.GetLane(signal_waypoint).GetLength(); double LaneDistance = Map.GetLane(signal_waypoint).GetDistance(); if(lane < 0) { - signal_waypoint.s = FMath::Clamp(signal_waypoint.s - BoxSize, + signal_waypoint.s = FMath::Clamp(signal_waypoint.s - BoxWidth, LaneDistance + epsilon, LaneDistance + LaneLength - epsilon); } else { - signal_waypoint.s = FMath::Clamp(signal_waypoint.s + BoxSize, + signal_waypoint.s = FMath::Clamp(signal_waypoint.s + BoxWidth, LaneDistance + epsilon, LaneDistance + LaneLength - epsilon); } - float UnrealBoxSize = 100*BoxSize; FTransform BoxTransform = Map.ComputeTransform(signal_waypoint); ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld()); if (LargeMapManager) { BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); } - GenerateTrafficLightBox(BoxTransform, UnrealBoxSize); + GenerateTrafficLightBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight)); } } } } void UTrafficLightComponent::GenerateTrafficLightBox(const FTransform BoxTransform, - float BoxSize) + const FVector BoxSize) { UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize); BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UTrafficLightComponent::OnOverlapTriggerBox); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h index 7e7216c63..2efc79770 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h @@ -68,7 +68,7 @@ private: void GenerateTrafficLightBox( const FTransform BoxTransform, - float BoxSize); + const FVector BoxSize); UPROPERTY(Category = "Traffic Light", EditAnywhere) ETrafficLightState LightState; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.cpp index 48dba4943..629d5b659 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.cpp @@ -8,6 +8,8 @@ #include "TrafficLightState.h" #include +#include "Carla/Game/CarlaStatics.h" +#include "Carla/MapGen/LargeMapManager.h" #include "Carla/Vehicle/CarlaWheeledVehicle.h" #include #include @@ -40,27 +42,43 @@ void UYieldSignComponent::InitializeSign(const carla::road::Map &Map) if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving) continue; - // Get 90% of the half size of the width of the lane - float BoxSize = static_cast( - 0.9*Map.GetLaneWidth(signal_waypoint)/2.0); + auto box_waypoint = signal_waypoint; + // Prevent adding the bounding box inside the intersection + if (Map.IsJunction(RoadId)) { + auto previous_waypoints = Map.GetPrevious(signal_waypoint, 2.0); + if (previous_waypoints.size() == 1) { + box_waypoint = previous_waypoints.front(); + } + } + + // Get 50% of the half size of the width of the lane + float BoxWidth = static_cast( + 0.5f*Map.GetLaneWidth(box_waypoint)*0.5); + float BoxLength = 1.5f; + float BoxHeight = 1.0f; // Prevent a situation where the road width is 0, // this could happen in a lane that is just appearing - BoxSize = std::max(0.01f, BoxSize); + BoxWidth = std::max(0.01f, BoxWidth); // Get min and max - double LaneLength = Map.GetLane(signal_waypoint).GetLength(); - double LaneDistance = Map.GetLane(signal_waypoint).GetDistance(); + double LaneLength = Map.GetLane(box_waypoint).GetLength(); + double LaneDistance = Map.GetLane(box_waypoint).GetDistance(); if(lane < 0) { - signal_waypoint.s = FMath::Clamp(signal_waypoint.s - BoxSize, + box_waypoint.s = FMath::Clamp(box_waypoint.s - BoxWidth, LaneDistance + epsilon, LaneDistance + LaneLength - epsilon); } else { - signal_waypoint.s = FMath::Clamp(signal_waypoint.s + BoxSize, + box_waypoint.s = FMath::Clamp(box_waypoint.s + BoxWidth, LaneDistance + epsilon, LaneDistance + LaneLength - epsilon); } - float UnrealBoxSize = 100*BoxSize; - GenerateYieldBox(Map.ComputeTransform(signal_waypoint), UnrealBoxSize); + FTransform BoxTransform = Map.ComputeTransform(box_waypoint); + ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld()); + if (LargeMapManager) + { + BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); + } + GenerateYieldBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight)); auto Predecessors = Map.GetPredecessors(signal_waypoint); for(auto &Prev : Predecessors) @@ -113,7 +131,15 @@ void UYieldSignComponent::InitializeSign(const carla::road::Map &Map) // this could happen in a lane that is just appearing BoxSize = std::max(0.01f, BoxSize); float UEBoxSize = 100*BoxSize; - GenerateCheckBox(Map.ComputeTransform(NextWaypoint), UEBoxSize); + + FTransform BoxTransform = Map.ComputeTransform(NextWaypoint); + ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld()); + if (LargeMapManager) + { + BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); + } + GenerateCheckBox(BoxTransform, UEBoxSize); + while (true) { auto Next = Map.GetNext(NextWaypoint, 2*BoxSize); @@ -126,8 +152,13 @@ void UYieldSignComponent::InitializeSign(const carla::road::Map &Map) { break; } + BoxTransform = Map.ComputeTransform(NextWaypoint); + if (LargeMapManager) + { + BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform); + } + GenerateCheckBox(BoxTransform, UEBoxSize); - GenerateCheckBox(Map.ComputeTransform(NextWaypoint), UEBoxSize); } // Cover the road before the junction // Hard coded anticipation time (boxes placed prior to the junction) @@ -169,7 +200,7 @@ void UYieldSignComponent::InitializeSign(const carla::road::Map &Map) } void UYieldSignComponent::GenerateYieldBox(const FTransform BoxTransform, - float BoxSize) + const FVector BoxSize) { UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize); BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UYieldSignComponent::OnOverlapBeginYieldEffectBox); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.h index 895e15ccb..a0a9be23c 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/YieldSignComponent.h @@ -23,7 +23,7 @@ public: private: void GenerateYieldBox(const FTransform BoxTransform, - float BoxSize); + const FVector BoxSize); void GenerateCheckBox(const FTransform BoxTransform, float BoxSize);