From 40dc7bd7c51eece02a532f9e410a40ada714eb48 Mon Sep 17 00:00:00 2001 From: dotero Date: Tue, 10 Mar 2020 18:52:08 +0100 Subject: [PATCH] Rollback component serialization and updated querries to use the new components if possible --- .../carla/sensor/data/ActorDynamicState.h | 4 +- .../Source/Carla/Sensor/WorldObserver.cpp | 35 +++--- .../Source/Carla/Traffic/TrafficLightBase.cpp | 101 ++++++++++++++---- .../Carla/Traffic/TrafficLightController.cpp | 26 +++++ .../Carla/Traffic/TrafficLightController.h | 12 +++ 5 files changed, 137 insertions(+), 41 deletions(-) diff --git a/LibCarla/source/carla/sensor/data/ActorDynamicState.h b/LibCarla/source/carla/sensor/data/ActorDynamicState.h index de966bec0..9f44ff70f 100644 --- a/LibCarla/source/carla/sensor/data/ActorDynamicState.h +++ b/LibCarla/source/carla/sensor/data/ActorDynamicState.h @@ -119,8 +119,6 @@ namespace detail { geom::Vector3D acceleration; - uint32_t num_components; - union TypeDependentState { detail::TrafficLightData traffic_light_data; detail::VehicleData vehicle_data; @@ -131,7 +129,7 @@ namespace detail { #pragma pack(pop) static_assert( - sizeof(ActorDynamicState) == 97u, + sizeof(ActorDynamicState) == 93u, "Invalid ActorDynamicState size! " "If you modified this class please update the size here, else you may " "comment this assert, but your platform may have compatibility issues " diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp index e8256b6bf..0599d669e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp @@ -55,6 +55,7 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg else if (AType::Walker == View.GetActorType()) { + UE_LOG(LogCarla, Warning, TEXT(" FWorldObserver_GetActorState Walker")); auto Walker = Cast(View.GetActor()); auto Controller = Walker != nullptr ? Cast(Walker->GetController()) : nullptr; if (Controller != nullptr) @@ -68,11 +69,13 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg if (TrafficLight != nullptr) { - UActorComponent* TrafficLightComponent = TrafficLight->FindComponentByClass(); + UTrafficLightComponent* TrafficLightComponent = + Cast(TrafficLight->FindComponentByClass()); + + using TLS = carla::rpc::TrafficLightState; if(TrafficLightComponent == nullptr) { // Old way: traffic lights are actors - using TLS = carla::rpc::TrafficLightState; state.traffic_light_data.state = static_cast(TrafficLight->GetTrafficLightState()); state.traffic_light_data.green_time = TrafficLight->GetGreenTime(); state.traffic_light_data.yellow_time = TrafficLight->GetYellowTime(); @@ -80,6 +83,18 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg state.traffic_light_data.elapsed_time = TrafficLight->GetElapsedTime(); state.traffic_light_data.time_is_frozen = TrafficLight->GetTimeIsFrozen(); state.traffic_light_data.pole_index = TrafficLight->GetPoleIndex(); + } else { + UTrafficLightController* Controller = TrafficLightComponent->GetController(); + ATrafficLightGroup* Group = TrafficLightComponent->GetGroup(); + + state.traffic_light_data.state = static_cast(TrafficLightComponent->GetLightState()); + state.traffic_light_data.green_time = Controller->GetGreenTime(); + state.traffic_light_data.yellow_time = Controller->GetYellowTime(); + state.traffic_light_data.red_time = Controller->GetRedTime(); + state.traffic_light_data.elapsed_time = Group->GetElapsedTime(); + state.traffic_light_data.time_is_frozen = Group->IsFrozen(); + // Nobody is using this right now, perhaps we should remove it? + state.traffic_light_data.pole_index = TrafficLight->GetPoleIndex(); } } } @@ -192,25 +207,9 @@ static carla::Buffer FWorldObserver_Serialize( carla::geom::Vector3D{Velocity.X, Velocity.Y, Velocity.Z}, FWorldObserver_GetAngularVelocity(*View.GetActor()), FWorldObserver_GetAcceleration(View, Velocity, DeltaSeconds), - 0, FWorldObserver_GetActorState(View, Registry), }; - - TArray ComponentsState; - FWorldObserver_GetActorComponentsState(View, Registry, ComponentsState); - info.num_components = ComponentsState.Num(); - write_data(info); - - if(info.num_components > 0) { - total_size += info.num_components * sizeof(ComponentDynamicState); - buffer.resize(total_size); - - for(auto& CompState : ComponentsState) - { - write_data(CompState); - } - } } // Shrink buffer diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp index 35968560d..97bba884e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp @@ -105,23 +105,30 @@ void ATrafficLightBase::PostEditChangeProperty(FPropertyChangedEvent &Event) void ATrafficLightBase::SetTrafficLightState(const ETrafficLightState InState) { - ElapsedTime = 0.0f; - State = InState; - SetTrafficSignState(ToTrafficSignState(State)); - for (auto Controller : Vehicles) - { - if (Controller != nullptr) + UTrafficLightComponent* TrafficLightComponent = + Cast(FindComponentByClass()); + + if(TrafficLightComponent) { + TrafficLightComponent->SetLightState(InState); + } else { + ElapsedTime = 0.0f; + State = InState; + SetTrafficSignState(ToTrafficSignState(State)); + for (auto Controller : Vehicles) { - Controller->SetTrafficLightState(State); - if (State == ETrafficLightState::Green) + if (Controller != nullptr) { - Controller->SetTrafficLight(nullptr); + Controller->SetTrafficLightState(State); + if (State == ETrafficLightState::Green) + { + Controller->SetTrafficLight(nullptr); + } } } - } - if (State == ETrafficLightState::Green) - { - Vehicles.Empty(); + if (State == ETrafficLightState::Green) + { + Vehicles.Empty(); + } } OnTrafficLightStateChanged(State); } @@ -178,7 +185,18 @@ void ATrafficLightBase::UnNotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle) void ATrafficLightBase::SetGreenTime(float InGreenTime) { - GreenTime = InGreenTime; + UTrafficLightComponent* TrafficLightComponent = + Cast(FindComponentByClass()); + + if(TrafficLightComponent) { + UTrafficLightController* TrafficLightController = + TrafficLightComponent->GetController(); + check(TrafficLightController) + TrafficLightController->SetGreenTime(InGreenTime); + + } else { + GreenTime = InGreenTime; + } } float ATrafficLightBase::GetGreenTime() const @@ -188,7 +206,17 @@ float ATrafficLightBase::GetGreenTime() const void ATrafficLightBase::SetYellowTime(float InYellowTime) { - YellowTime = InYellowTime; + UTrafficLightComponent* TrafficLightComponent = + Cast(FindComponentByClass()); + + if(TrafficLightComponent) { + UTrafficLightController* TrafficLightController = + TrafficLightComponent->GetController(); + check(TrafficLightController) + TrafficLightController->SetYellowTime(InYellowTime); + } else { + YellowTime = InYellowTime; + } } float ATrafficLightBase::GetYellowTime() const @@ -198,7 +226,17 @@ float ATrafficLightBase::GetYellowTime() const void ATrafficLightBase::SetRedTime(float InRedTime) { - RedTime = InRedTime; + UTrafficLightComponent* TrafficLightComponent = + Cast(FindComponentByClass()); + + if(TrafficLightComponent) { + UTrafficLightController* TrafficLightController = + TrafficLightComponent->GetController(); + check(TrafficLightController) + TrafficLightController->SetRedTime(InRedTime); + } else { + RedTime = InRedTime; + } } float ATrafficLightBase::GetRedTime() const @@ -218,10 +256,17 @@ void ATrafficLightBase::SetElapsedTime(float InElapsedTime) void ATrafficLightBase::SetTimeIsFrozen(bool InTimeIsFrozen) { - TimeIsFrozen = InTimeIsFrozen; - if (!TimeIsFrozen) - { - ElapsedTime = 0.0f; + UTrafficLightComponent* TrafficLightComponent = + Cast(FindComponentByClass()); + + if(TrafficLightComponent) { + TrafficLightComponent->SetFrozenGroup(InTimeIsFrozen); + } else { + TimeIsFrozen = InTimeIsFrozen; + if (!TimeIsFrozen) + { + ElapsedTime = 0.0f; + } } } @@ -242,6 +287,22 @@ int ATrafficLightBase::GetPoleIndex() const TArray ATrafficLightBase::GetGroupTrafficLights() const { + UTrafficLightComponent* TrafficLightComponent = + Cast(FindComponentByClass()); + + if(TrafficLightComponent) { + TArray result; + UTrafficLightController* TrafficLightController = + TrafficLightComponent->GetController(); + check(TrafficLightController) + + for(auto& TLComp : TrafficLightController->GetTrafficLights()) + { + result.Add(Cast(GetOwner())); + } + + return result; + } return GroupTrafficLights; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp index 02b29e1d0..4e7e8622c 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp @@ -78,6 +78,21 @@ void UTrafficLightController::ResetState() SetTrafficLightsState(GetCurrentState().State); } +void UTrafficLightController::SetYellowTime(float NewTime) +{ + SetStateTime(ETrafficLightState::Yellow, NewTime); +} + +void UTrafficLightController::SetRedTime(float NewTime) +{ + SetStateTime(ETrafficLightState::Red, NewTime); +} + +void UTrafficLightController::SetGreenTime(float NewTime) +{ + SetStateTime(ETrafficLightState::Green, NewTime); +} + float UTrafficLightController::GetGreenTime() const { return GetStateTime(ETrafficLightState::Green); @@ -93,6 +108,17 @@ float UTrafficLightController::GetRedTime() const return GetStateTime(ETrafficLightState::Red); } +void UTrafficLightController::SetStateTime(const ETrafficLightState State, float NewTime) +{ + for(auto& LightState : LightStates) + { + if(LightState.State == State) + { + LightState.Time = NewTime; + } + } +} + float UTrafficLightController::GetStateTime(const ETrafficLightState State) const { for(auto& LightState : LightStates) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h index b421872cc..3b7fe853c 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h @@ -75,6 +75,15 @@ public: UFUNCTION(Category = "Traffic Controller", BlueprintCallable) void ResetState(); + UFUNCTION(Category = "Traffic Controller", BlueprintCallable) + void SetYellowTime(float NewTime); + + UFUNCTION(Category = "Traffic Controller", BlueprintCallable) + void SetRedTime(float NewTime); + + UFUNCTION(Category = "Traffic Controller", BlueprintCallable) + void SetGreenTime(float NewTime); + UFUNCTION(Category = "Traffic Controller", BlueprintCallable) float GetGreenTime() const; @@ -84,8 +93,11 @@ public: UFUNCTION(Category = "Traffic Controller", BlueprintCallable) float GetRedTime() const; + private: + void SetStateTime(const ETrafficLightState State, float NewTime); + float GetStateTime(const ETrafficLightState State) const; UPROPERTY(Category = "Traffic Controller", EditAnywhere)