From 41c870438eb578540746ea948d84f203f81d58d8 Mon Sep 17 00:00:00 2001 From: Axel1092 Date: Mon, 29 Jun 2020 10:14:06 +0200 Subject: [PATCH] Added scene light events to recorder. --- .../Carla/Source/Carla/Lights/CarlaLight.cpp | 34 ++++++++-- .../Carla/Source/Carla/Lights/CarlaLight.h | 20 ++++-- .../Carla/Lights/CarlaLightSubsystem.cpp | 17 ++++- .../Source/Carla/Lights/CarlaLightSubsystem.h | 3 +- .../Source/Carla/Recorder/CarlaRecorder.cpp | 19 ++++++ .../Source/Carla/Recorder/CarlaRecorder.h | 6 +- .../Recorder/CarlaRecorderLightScene.cpp | 65 +++++++++++++++++++ .../Carla/Recorder/CarlaRecorderLightScene.h | 42 ++++++++++++ .../Source/Carla/Recorder/CarlaReplayer.cpp | 22 +++++++ .../Source/Carla/Recorder/CarlaReplayer.h | 1 + .../Carla/Recorder/CarlaReplayerHelper.cpp | 24 +++++++ .../Carla/Recorder/CarlaReplayerHelper.h | 3 + 12 files changed, 242 insertions(+), 14 deletions(-) create mode 100644 Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.cpp create mode 100644 Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.h diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.cpp index 8a7bf83d7..007b17ca7 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.cpp @@ -6,7 +6,11 @@ #include "CarlaLight.h" #include "CarlaLightSubsystem.h" +#include "Carla/Game/CarlaStatics.h" +#include +#include +#include UCarlaLight::UCarlaLight() { @@ -46,7 +50,7 @@ void UCarlaLight::SetLightIntensity(float Intensity) UpdateLights(); } -float UCarlaLight::GetLightIntensity() +float UCarlaLight::GetLightIntensity() const { return LightIntensity; } @@ -55,9 +59,10 @@ void UCarlaLight::SetLightColor(FLinearColor Color) { LightColor = Color; UpdateLights(); + RecordLightChange(); } -FLinearColor UCarlaLight::GetLightColor() +FLinearColor UCarlaLight::GetLightColor() const { return LightColor; } @@ -66,9 +71,10 @@ void UCarlaLight::SetLightOn(bool bOn) { bLightOn = bOn; UpdateLights(); + RecordLightChange(); } -bool UCarlaLight::GetLightOn() +bool UCarlaLight::GetLightOn() const { return bLightOn; } @@ -78,7 +84,7 @@ void UCarlaLight::SetLightType(ELightType Type) LightType = Type; } -ELightType UCarlaLight::GetLightType() +ELightType UCarlaLight::GetLightType() const { return LightType; } @@ -105,6 +111,7 @@ void UCarlaLight::SetLightState(carla::rpc::LightState LightState) LightType = static_cast(LightState._group); bLightOn = LightState._active; UpdateLights(); + RecordLightChange(); } FVector UCarlaLight::GetLocation() const @@ -112,7 +119,22 @@ FVector UCarlaLight::GetLocation() const return GetOwner()->GetActorLocation(); } -uint32 UCarlaLight::GetId() const +int UCarlaLight::GetId() const { - return GetUniqueID(); + return Id; +} + +void UCarlaLight::SetId(int InId) +{ + Id = InId; +} + +void UCarlaLight::RecordLightChange() const +{ + auto* Episode = UCarlaStatics::GetCurrentEpisode(GetWorld()); + auto* Recorder = Episode->GetRecorder(); + if (Recorder && Recorder->IsEnabled()) + { + Recorder->AddEventLightSceneChanged(this); + } } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.h index 73fcfced1..29f92a17a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLight.h @@ -52,25 +52,25 @@ public: void SetLightIntensity(float Intensity); UFUNCTION(BlueprintPure, Category = "Carla Light") - float GetLightIntensity(); + float GetLightIntensity() const; UFUNCTION(BlueprintCallable, Category = "Carla Light") void SetLightColor(FLinearColor Color); UFUNCTION(BlueprintPure, Category = "Carla Light") - FLinearColor GetLightColor(); + FLinearColor GetLightColor() const; UFUNCTION(BlueprintCallable, Category = "Carla Light") void SetLightOn(bool bOn); UFUNCTION(BlueprintPure, Category = "Carla Light") - bool GetLightOn(); + bool GetLightOn() const; UFUNCTION(BlueprintCallable, Category = "Carla Light") void SetLightType(ELightType Type); UFUNCTION(BlueprintPure, Category = "Carla Light") - ELightType GetLightType(); + ELightType GetLightType() const; carla::rpc::LightState GetLightState(); @@ -78,7 +78,11 @@ public: FVector GetLocation() const; - uint32 GetId() const; + UFUNCTION(BlueprintPure, Category = "Carla Light") + int GetId() const; + + UFUNCTION(BlueprintCallable, Category = "Carla Light") + void SetId(int InId); protected: @@ -94,4 +98,10 @@ protected: UPROPERTY(EditAnywhere, Category = "Carla Light") bool bLightOn; + UPROPERTY(EditAnywhere, Category = "Carla Light") + int Id = -1; + + private: + + void RecordLightChange() const; }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.cpp index 35daca691..4a9c3665f 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.cpp @@ -22,7 +22,13 @@ void UCarlaLightSubsystem::RegisterLight(UCarlaLight* CarlaLight) { if(CarlaLight) { - Lights.Add(CarlaLight->GetId(), CarlaLight); + auto LightId = CarlaLight->GetId(); + if (Lights.Contains(LightId)) + { + UE_LOG(LogCarla, Warning, TEXT("Light Id overlapping")); + return; + } + Lights.Add(LightId, CarlaLight); } } @@ -85,6 +91,15 @@ void UCarlaLightSubsystem::SetLights( } +UCarlaLight* UCarlaLightSubsystem::GetLight(int Id) +{ + if (Lights.Contains(Id)) + { + return Lights[Id]; + } + return nullptr; +} + void UCarlaLightSubsystem::SetClientStatesdirty(FString ClientThatUpdate) { for(auto& Client : ClientStates) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.h index 680cb1306..bd0b10b8d 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Lights/CarlaLightSubsystem.h @@ -55,11 +55,12 @@ public: std::vector LightsToSet, bool DiscardClient = false); + UCarlaLight* GetLight(int Id); private: void SetClientStatesdirty(FString ClientThatUpdate); - TMap Lights; + TMap Lights; // Flag for each client to tell if an update needs to be done TMap ClientStates; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp index 82cc48989..27c6aba21 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp @@ -297,6 +297,7 @@ void ACarlaRecorder::Clear(void) Vehicles.Clear(); Walkers.Clear(); LightVehicles.Clear(); + LightScenes.Clear(); } void ACarlaRecorder::Write(double DeltaSeconds) @@ -321,6 +322,7 @@ void ACarlaRecorder::Write(double DeltaSeconds) Vehicles.Write(File); Walkers.Write(File); LightVehicles.Write(File); + LightScenes.Write(File); // end Frames.WriteEnd(File); @@ -425,6 +427,23 @@ void ACarlaRecorder::AddLightVehicle(const CarlaRecorderLightVehicle &LightVehic } } +void ACarlaRecorder::AddEventLightSceneChanged(const UCarlaLight* Light) +{ + if (Enabled) + { + CarlaRecorderLightScene LightScene = + { + Light->GetId(), + Light->GetLightIntensity(), + Light->GetLightColor(), + Light->GetLightOn(), + static_cast(Light->GetLightType()) + }; + + LightScenes.Add(LightScene); + } +} + void ACarlaRecorder::AddExistingActors(void) { // registring all existing actors in first frame diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.h index 0f7a7acb7..ff1203d9f 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.h @@ -11,6 +11,7 @@ #include "Carla/Actor/ActorDescription.h" +#include "CarlaRecorderLightScene.h" #include "CarlaRecorderLightVehicle.h" #include "CarlaRecorderAnimVehicle.h" #include "CarlaRecorderAnimWalker.h" @@ -30,7 +31,7 @@ class AActor; class UCarlaEpisode; class ACarlaWheeledVehicle; -struct FVehicleLightState; +class UCarlaLight; enum class CarlaRecorderPacketId : uint8_t { @@ -96,6 +97,8 @@ public: void AddLightVehicle(const CarlaRecorderLightVehicle &LightVehicle); + void AddEventLightSceneChanged(const UCarlaLight* Light); + // set episode void SetEpisode(UCarlaEpisode *ThisEpisode) { @@ -150,6 +153,7 @@ private: CarlaRecorderAnimVehicles Vehicles; CarlaRecorderAnimWalkers Walkers; CarlaRecorderLightVehicles LightVehicles; + CarlaRecorderLightScenes LightScenes; // replayer CarlaReplayer Replayer; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.cpp new file mode 100644 index 000000000..2ef0f23bb --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.cpp @@ -0,0 +1,65 @@ +// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#include "CarlaRecorderLightScene.h" +#include "CarlaRecorder.h" +#include "CarlaRecorderHelpers.h" + + +void CarlaRecorderLightScene::Write(std::ofstream &OutFile) +{ + WriteValue(OutFile, this->LightId); + WriteValue(OutFile, this->Intensity); + WriteValue(OutFile, this->Color); + WriteValue(OutFile, this->bOn); + WriteValue(OutFile, this->Type); +} +void CarlaRecorderLightScene::Read(std::ifstream &InFile) +{ + ReadValue(InFile, this->LightId); + ReadValue(InFile, this->Intensity); + ReadValue(InFile, this->Color); + ReadValue(InFile, this->bOn); + ReadValue(InFile, this->Type); +} + +// --------------------------------------------- + +void CarlaRecorderLightScenes::Clear(void) +{ + Lights.clear(); +} + +void CarlaRecorderLightScenes::Add(const CarlaRecorderLightScene &Vehicle) +{ + Lights.push_back(Vehicle); +} + +void CarlaRecorderLightScenes::Write(std::ofstream &OutFile) +{ + // write the packet id + WriteValue(OutFile, static_cast(CarlaRecorderPacketId::SceneLight)); + + std::streampos PosStart = OutFile.tellp(); + + // write a dummy packet size + uint32_t Total = 0; + WriteValue(OutFile, Total); + + // write total records + Total = Lights.size(); + WriteValue(OutFile, Total); + + for (uint16_t i=0; i(OutFile, Total); + OutFile.seekp(PosEnd, std::ios::beg); +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.h new file mode 100644 index 000000000..2aae5240a --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorderLightScene.h @@ -0,0 +1,42 @@ +// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include +#include +#include + +#pragma pack(push, 1) +struct CarlaRecorderLightScene +{ + + int LightId; + float Intensity; + FLinearColor Color; + bool bOn; + uint8 Type; + + void Read(std::ifstream &InFile); + + void Write(std::ofstream &OutFile); +}; +#pragma pack(pop) + +struct CarlaRecorderLightScenes +{ +public: + + void Add(const CarlaRecorderLightScene &InObj); + + void Clear(void); + + void Write(std::ofstream &OutFile); + +private: + + std::vector Lights; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.cpp index b5737d511..1d789cf68 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.cpp @@ -341,6 +341,14 @@ void CarlaReplayer::ProcessToTime(double Time, bool IsFirstTime) SkipPacket(); break; + // scene lights animation + case static_cast(CarlaRecorderPacketId::SceneLight): + if (bFrameFound) + ProcessLightScene(); + else + SkipPacket(); + break; + // frame end case static_cast(CarlaRecorderPacketId::FrameEnd): if (bFrameFound) @@ -542,6 +550,20 @@ void CarlaReplayer::ProcessLightVehicle(void) } } +void CarlaReplayer::ProcessLightScene(void) +{ + uint16_t Total; + CarlaRecorderLightScene LightScene; + + // read Total walkers + ReadValue(File, Total); + for (uint16_t i = 0; i < Total; ++i) + { + LightScene.Read(File); + Helper.ProcessReplayerLightScene(LightScene); + } +} + void CarlaReplayer::ProcessPositions(bool IsFirstTime) { uint16_t i, Total; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.h index cb5f08eae..6071dd75a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayer.h @@ -142,6 +142,7 @@ private: void ProcessAnimWalker(void); void ProcessLightVehicle(void); + void ProcessLightScene(void); // positions void UpdatePositions(double Per, double DeltaTime); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp index c41051eb1..c98852dc5 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp @@ -12,6 +12,8 @@ #include "Carla/Vehicle/WheeledVehicleAIController.h" #include "Carla/Walker/WalkerControl.h" #include "Carla/Walker/WalkerController.h" +#include "Carla/Lights/CarlaLight.h" +#include "Carla/Lights/CarlaLightSubsystem.h" // create or reuse an actor for replaying std::pairCarlaReplayerHelper::TryToCreateReplayerActor( @@ -349,6 +351,28 @@ void CarlaReplayerHelper::ProcessReplayerLightVehicle(CarlaRecorderLightVehicle } } +void CarlaReplayerHelper::ProcessReplayerLightScene(CarlaRecorderLightScene LightScene) +{ + check(Episode != nullptr); + UWorld* World = Episode->GetWorld(); + if(World) + { + UCarlaLightSubsystem* CarlaLightSubsystem = World->GetSubsystem(); + if (!CarlaLightSubsystem) + { + return; + } + auto* CarlaLight = CarlaLightSubsystem->GetLight(LightScene.LightId); + if (CarlaLight) + { + CarlaLight->SetLightIntensity(LightScene.Intensity); + CarlaLight->SetLightColor(LightScene.Color); + CarlaLight->SetLightOn(LightScene.bOn); + CarlaLight->SetLightType(static_cast(LightScene.Type)); + } + } +} + // set the animation for walkers void CarlaReplayerHelper::ProcessReplayerAnimWalker(CarlaRecorderAnimWalker Walker) { diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h index cc73333dc..6ea341ea0 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h @@ -50,6 +50,9 @@ public: // set the vehicle light void ProcessReplayerLightVehicle(CarlaRecorderLightVehicle LightVehicle); + // set scene lights + void ProcessReplayerLightScene(CarlaRecorderLightScene LightScene); + // replay finish bool ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map &IsHero);