Added scene light events to recorder.

This commit is contained in:
Axel1092 2020-06-29 10:14:06 +02:00 committed by Axel1092
parent 8374172415
commit 41c870438e
12 changed files with 242 additions and 14 deletions

View File

@ -6,7 +6,11 @@
#include "CarlaLight.h" #include "CarlaLight.h"
#include "CarlaLightSubsystem.h" #include "CarlaLightSubsystem.h"
#include "Carla/Game/CarlaStatics.h"
#include <compiler/disable-ue4-macros.h>
#include <boost/container_hash/hash.hpp>
#include <compiler/enable-ue4-macros.h>
UCarlaLight::UCarlaLight() UCarlaLight::UCarlaLight()
{ {
@ -46,7 +50,7 @@ void UCarlaLight::SetLightIntensity(float Intensity)
UpdateLights(); UpdateLights();
} }
float UCarlaLight::GetLightIntensity() float UCarlaLight::GetLightIntensity() const
{ {
return LightIntensity; return LightIntensity;
} }
@ -55,9 +59,10 @@ void UCarlaLight::SetLightColor(FLinearColor Color)
{ {
LightColor = Color; LightColor = Color;
UpdateLights(); UpdateLights();
RecordLightChange();
} }
FLinearColor UCarlaLight::GetLightColor() FLinearColor UCarlaLight::GetLightColor() const
{ {
return LightColor; return LightColor;
} }
@ -66,9 +71,10 @@ void UCarlaLight::SetLightOn(bool bOn)
{ {
bLightOn = bOn; bLightOn = bOn;
UpdateLights(); UpdateLights();
RecordLightChange();
} }
bool UCarlaLight::GetLightOn() bool UCarlaLight::GetLightOn() const
{ {
return bLightOn; return bLightOn;
} }
@ -78,7 +84,7 @@ void UCarlaLight::SetLightType(ELightType Type)
LightType = Type; LightType = Type;
} }
ELightType UCarlaLight::GetLightType() ELightType UCarlaLight::GetLightType() const
{ {
return LightType; return LightType;
} }
@ -105,6 +111,7 @@ void UCarlaLight::SetLightState(carla::rpc::LightState LightState)
LightType = static_cast<ELightType>(LightState._group); LightType = static_cast<ELightType>(LightState._group);
bLightOn = LightState._active; bLightOn = LightState._active;
UpdateLights(); UpdateLights();
RecordLightChange();
} }
FVector UCarlaLight::GetLocation() const FVector UCarlaLight::GetLocation() const
@ -112,7 +119,22 @@ FVector UCarlaLight::GetLocation() const
return GetOwner()->GetActorLocation(); 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);
}
} }

View File

@ -52,25 +52,25 @@ public:
void SetLightIntensity(float Intensity); void SetLightIntensity(float Intensity);
UFUNCTION(BlueprintPure, Category = "Carla Light") UFUNCTION(BlueprintPure, Category = "Carla Light")
float GetLightIntensity(); float GetLightIntensity() const;
UFUNCTION(BlueprintCallable, Category = "Carla Light") UFUNCTION(BlueprintCallable, Category = "Carla Light")
void SetLightColor(FLinearColor Color); void SetLightColor(FLinearColor Color);
UFUNCTION(BlueprintPure, Category = "Carla Light") UFUNCTION(BlueprintPure, Category = "Carla Light")
FLinearColor GetLightColor(); FLinearColor GetLightColor() const;
UFUNCTION(BlueprintCallable, Category = "Carla Light") UFUNCTION(BlueprintCallable, Category = "Carla Light")
void SetLightOn(bool bOn); void SetLightOn(bool bOn);
UFUNCTION(BlueprintPure, Category = "Carla Light") UFUNCTION(BlueprintPure, Category = "Carla Light")
bool GetLightOn(); bool GetLightOn() const;
UFUNCTION(BlueprintCallable, Category = "Carla Light") UFUNCTION(BlueprintCallable, Category = "Carla Light")
void SetLightType(ELightType Type); void SetLightType(ELightType Type);
UFUNCTION(BlueprintPure, Category = "Carla Light") UFUNCTION(BlueprintPure, Category = "Carla Light")
ELightType GetLightType(); ELightType GetLightType() const;
carla::rpc::LightState GetLightState(); carla::rpc::LightState GetLightState();
@ -78,7 +78,11 @@ public:
FVector GetLocation() const; FVector GetLocation() const;
uint32 GetId() const; UFUNCTION(BlueprintPure, Category = "Carla Light")
int GetId() const;
UFUNCTION(BlueprintCallable, Category = "Carla Light")
void SetId(int InId);
protected: protected:
@ -94,4 +98,10 @@ protected:
UPROPERTY(EditAnywhere, Category = "Carla Light") UPROPERTY(EditAnywhere, Category = "Carla Light")
bool bLightOn; bool bLightOn;
UPROPERTY(EditAnywhere, Category = "Carla Light")
int Id = -1;
private:
void RecordLightChange() const;
}; };

View File

@ -22,7 +22,13 @@ void UCarlaLightSubsystem::RegisterLight(UCarlaLight* CarlaLight)
{ {
if(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) void UCarlaLightSubsystem::SetClientStatesdirty(FString ClientThatUpdate)
{ {
for(auto& Client : ClientStates) for(auto& Client : ClientStates)

View File

@ -55,11 +55,12 @@ public:
std::vector<carla::rpc::LightState> LightsToSet, std::vector<carla::rpc::LightState> LightsToSet,
bool DiscardClient = false); bool DiscardClient = false);
UCarlaLight* GetLight(int Id);
private: private:
void SetClientStatesdirty(FString ClientThatUpdate); void SetClientStatesdirty(FString ClientThatUpdate);
TMap<uint32, UCarlaLight* > Lights; TMap<int, UCarlaLight* > Lights;
// Flag for each client to tell if an update needs to be done // Flag for each client to tell if an update needs to be done
TMap<FString, bool> ClientStates; TMap<FString, bool> ClientStates;

View File

@ -297,6 +297,7 @@ void ACarlaRecorder::Clear(void)
Vehicles.Clear(); Vehicles.Clear();
Walkers.Clear(); Walkers.Clear();
LightVehicles.Clear(); LightVehicles.Clear();
LightScenes.Clear();
} }
void ACarlaRecorder::Write(double DeltaSeconds) void ACarlaRecorder::Write(double DeltaSeconds)
@ -321,6 +322,7 @@ void ACarlaRecorder::Write(double DeltaSeconds)
Vehicles.Write(File); Vehicles.Write(File);
Walkers.Write(File); Walkers.Write(File);
LightVehicles.Write(File); LightVehicles.Write(File);
LightScenes.Write(File);
// end // end
Frames.WriteEnd(File); 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<uint8>(Light->GetLightType())
};
LightScenes.Add(LightScene);
}
}
void ACarlaRecorder::AddExistingActors(void) void ACarlaRecorder::AddExistingActors(void)
{ {
// registring all existing actors in first frame // registring all existing actors in first frame

View File

@ -11,6 +11,7 @@
#include "Carla/Actor/ActorDescription.h" #include "Carla/Actor/ActorDescription.h"
#include "CarlaRecorderLightScene.h"
#include "CarlaRecorderLightVehicle.h" #include "CarlaRecorderLightVehicle.h"
#include "CarlaRecorderAnimVehicle.h" #include "CarlaRecorderAnimVehicle.h"
#include "CarlaRecorderAnimWalker.h" #include "CarlaRecorderAnimWalker.h"
@ -30,7 +31,7 @@
class AActor; class AActor;
class UCarlaEpisode; class UCarlaEpisode;
class ACarlaWheeledVehicle; class ACarlaWheeledVehicle;
struct FVehicleLightState; class UCarlaLight;
enum class CarlaRecorderPacketId : uint8_t enum class CarlaRecorderPacketId : uint8_t
{ {
@ -96,6 +97,8 @@ public:
void AddLightVehicle(const CarlaRecorderLightVehicle &LightVehicle); void AddLightVehicle(const CarlaRecorderLightVehicle &LightVehicle);
void AddEventLightSceneChanged(const UCarlaLight* Light);
// set episode // set episode
void SetEpisode(UCarlaEpisode *ThisEpisode) void SetEpisode(UCarlaEpisode *ThisEpisode)
{ {
@ -150,6 +153,7 @@ private:
CarlaRecorderAnimVehicles Vehicles; CarlaRecorderAnimVehicles Vehicles;
CarlaRecorderAnimWalkers Walkers; CarlaRecorderAnimWalkers Walkers;
CarlaRecorderLightVehicles LightVehicles; CarlaRecorderLightVehicles LightVehicles;
CarlaRecorderLightScenes LightScenes;
// replayer // replayer
CarlaReplayer Replayer; CarlaReplayer Replayer;

View File

@ -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 <https://opensource.org/licenses/MIT>.
#include "CarlaRecorderLightScene.h"
#include "CarlaRecorder.h"
#include "CarlaRecorderHelpers.h"
void CarlaRecorderLightScene::Write(std::ofstream &OutFile)
{
WriteValue<int>(OutFile, this->LightId);
WriteValue<float>(OutFile, this->Intensity);
WriteValue<FLinearColor>(OutFile, this->Color);
WriteValue<bool>(OutFile, this->bOn);
WriteValue<uint8>(OutFile, this->Type);
}
void CarlaRecorderLightScene::Read(std::ifstream &InFile)
{
ReadValue<int>(InFile, this->LightId);
ReadValue<float>(InFile, this->Intensity);
ReadValue<FLinearColor>(InFile, this->Color);
ReadValue<bool>(InFile, this->bOn);
ReadValue<uint8>(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<char>(OutFile, static_cast<char>(CarlaRecorderPacketId::SceneLight));
std::streampos PosStart = OutFile.tellp();
// write a dummy packet size
uint32_t Total = 0;
WriteValue<uint32_t>(OutFile, Total);
// write total records
Total = Lights.size();
WriteValue<uint16_t>(OutFile, Total);
for (uint16_t i=0; i<Total; ++i)
Lights[i].Write(OutFile);
// write the real packet size
std::streampos PosEnd = OutFile.tellp();
Total = PosEnd - PosStart - sizeof(uint32_t);
OutFile.seekp(PosStart, std::ios::beg);
WriteValue<uint32_t>(OutFile, Total);
OutFile.seekp(PosEnd, std::ios::beg);
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#pragma once
#include <fstream>
#include <vector>
#include <type_traits>
#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<CarlaRecorderLightScene> Lights;
};

View File

@ -341,6 +341,14 @@ void CarlaReplayer::ProcessToTime(double Time, bool IsFirstTime)
SkipPacket(); SkipPacket();
break; break;
// scene lights animation
case static_cast<char>(CarlaRecorderPacketId::SceneLight):
if (bFrameFound)
ProcessLightScene();
else
SkipPacket();
break;
// frame end // frame end
case static_cast<char>(CarlaRecorderPacketId::FrameEnd): case static_cast<char>(CarlaRecorderPacketId::FrameEnd):
if (bFrameFound) if (bFrameFound)
@ -542,6 +550,20 @@ void CarlaReplayer::ProcessLightVehicle(void)
} }
} }
void CarlaReplayer::ProcessLightScene(void)
{
uint16_t Total;
CarlaRecorderLightScene LightScene;
// read Total walkers
ReadValue<uint16_t>(File, Total);
for (uint16_t i = 0; i < Total; ++i)
{
LightScene.Read(File);
Helper.ProcessReplayerLightScene(LightScene);
}
}
void CarlaReplayer::ProcessPositions(bool IsFirstTime) void CarlaReplayer::ProcessPositions(bool IsFirstTime)
{ {
uint16_t i, Total; uint16_t i, Total;

View File

@ -142,6 +142,7 @@ private:
void ProcessAnimWalker(void); void ProcessAnimWalker(void);
void ProcessLightVehicle(void); void ProcessLightVehicle(void);
void ProcessLightScene(void);
// positions // positions
void UpdatePositions(double Per, double DeltaTime); void UpdatePositions(double Per, double DeltaTime);

View File

@ -12,6 +12,8 @@
#include "Carla/Vehicle/WheeledVehicleAIController.h" #include "Carla/Vehicle/WheeledVehicleAIController.h"
#include "Carla/Walker/WalkerControl.h" #include "Carla/Walker/WalkerControl.h"
#include "Carla/Walker/WalkerController.h" #include "Carla/Walker/WalkerController.h"
#include "Carla/Lights/CarlaLight.h"
#include "Carla/Lights/CarlaLightSubsystem.h"
// create or reuse an actor for replaying // create or reuse an actor for replaying
std::pair<int, FActorView>CarlaReplayerHelper::TryToCreateReplayerActor( std::pair<int, FActorView>CarlaReplayerHelper::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<UCarlaLightSubsystem>();
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<ELightType>(LightScene.Type));
}
}
}
// set the animation for walkers // set the animation for walkers
void CarlaReplayerHelper::ProcessReplayerAnimWalker(CarlaRecorderAnimWalker Walker) void CarlaReplayerHelper::ProcessReplayerAnimWalker(CarlaRecorderAnimWalker Walker)
{ {

View File

@ -50,6 +50,9 @@ public:
// set the vehicle light // set the vehicle light
void ProcessReplayerLightVehicle(CarlaRecorderLightVehicle LightVehicle); void ProcessReplayerLightVehicle(CarlaRecorderLightVehicle LightVehicle);
// set scene lights
void ProcessReplayerLightScene(CarlaRecorderLightScene LightScene);
// replay finish // replay finish
bool ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map<uint32_t, bool> &IsHero); bool ProcessReplayerFinish(bool bApplyAutopilot, bool bIgnoreHero, std::unordered_map<uint32_t, bool> &IsHero);