From 1e5bf2abdea6e1ebae1e7490050363261d9db3f9 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Tue, 9 May 2017 12:43:36 +0200 Subject: [PATCH] Fix #28 Implement collision count for cars and pedestrians --- Source/Carla/CityMapGenerator.cpp | 2 +- Source/Carla/Game/CarlaPlayerState.cpp | 18 +++++- Source/Carla/Game/CarlaPlayerState.h | 6 +- Source/Carla/Game/CarlaVehicleController.cpp | 6 +- Source/Carla/Tagger.cpp | 60 +++++++++++++------- Source/Carla/Tagger.h | 16 +++++- 6 files changed, 81 insertions(+), 27 deletions(-) diff --git a/Source/Carla/CityMapGenerator.cpp b/Source/Carla/CityMapGenerator.cpp index 1be3588a9..1eb655c91 100644 --- a/Source/Carla/CityMapGenerator.cpp +++ b/Source/Carla/CityMapGenerator.cpp @@ -207,7 +207,7 @@ static bool LineTrace( if (Success) { for (FHitResult &Item : OutHits) { - if (Item.Component->CustomDepthStencilValue == static_cast(CityObjectLabel::Roads)) { + if (ATagger::MatchComponent(*Item.Component, ECityObjectLabel::Roads)) { HitResult = Item; return true; } diff --git a/Source/Carla/Game/CarlaPlayerState.cpp b/Source/Carla/Game/CarlaPlayerState.cpp index 23c46a3e1..022b80d26 100644 --- a/Source/Carla/Game/CarlaPlayerState.cpp +++ b/Source/Carla/Game/CarlaPlayerState.cpp @@ -39,9 +39,23 @@ void ACarlaPlayerState::CopyProperties(APlayerState *PlayerState) } } -void ACarlaPlayerState::RegisterCollision(AActor * /*Actor*/, FVector NormalImpulse) +void ACarlaPlayerState::RegisterCollision( + AActor */*Actor*/, + AActor */*OtherActor*/, + const FVector &NormalImpulse, + const FHitResult &Hit) { - CollisionIntensityOther += NormalImpulse.Size(); + switch (ATagger::GetTagOfTaggedComponent(*Hit.Component)) { + case ECityObjectLabel::Vehicles: + CollisionIntensityCars += NormalImpulse.Size(); + break; + case ECityObjectLabel::Pedestrians: + CollisionIntensityPedestrians += NormalImpulse.Size(); + break; + default: + CollisionIntensityOther += NormalImpulse.Size(); + break; + } } static int32 RoundToMilliseconds(float Seconds) diff --git a/Source/Carla/Game/CarlaPlayerState.h b/Source/Carla/Game/CarlaPlayerState.h index 6f7a82c09..165860cca 100644 --- a/Source/Carla/Game/CarlaPlayerState.h +++ b/Source/Carla/Game/CarlaPlayerState.h @@ -130,7 +130,11 @@ public: // =========================================================================== private: - void RegisterCollision(AActor *Actor, FVector NormalImpulse); + void RegisterCollision( + AActor *Actor, + AActor *OtherActor, + const FVector &NormalImpulse, + const FHitResult &Hit); void UpdateTimeStamp(float DeltaSeconds); diff --git a/Source/Carla/Game/CarlaVehicleController.cpp b/Source/Carla/Game/CarlaVehicleController.cpp index bb54832b6..f4d926243 100644 --- a/Source/Carla/Game/CarlaVehicleController.cpp +++ b/Source/Carla/Game/CarlaVehicleController.cpp @@ -251,12 +251,12 @@ void ACarlaVehicleController::ToggleManualMode() // ============================================================================= void ACarlaVehicleController::OnCollisionEvent( - AActor* /*Actor*/, + AActor* Actor, AActor* OtherActor, FVector NormalImpulse, - const FHitResult& /*Hit*/) + const FHitResult& Hit) { - CarlaPlayerState->RegisterCollision(OtherActor, NormalImpulse); + CarlaPlayerState->RegisterCollision(Actor, OtherActor, NormalImpulse, Hit); } // ============================================================================= diff --git a/Source/Carla/Tagger.cpp b/Source/Carla/Tagger.cpp index 8d95e88c6..3f1734e70 100644 --- a/Source/Carla/Tagger.cpp +++ b/Source/Carla/Tagger.cpp @@ -10,10 +10,10 @@ #include "PhysicsEngine/PhysicsAsset.h" #ifdef CARLA_TAGGER_EXTRA_LOG -static FString GetLabelAsString(const CityObjectLabel Label) +static FString GetLabelAsString(const ECityObjectLabel Label) { switch (Label) { -#define CARLA_GET_LABEL_STR(lbl) case CityObjectLabel:: lbl : return #lbl; +#define CARLA_GET_LABEL_STR(lbl) case ECityObjectLabel:: lbl : return #lbl; default: CARLA_GET_LABEL_STR(None) CARLA_GET_LABEL_STR(Buildings) @@ -38,32 +38,32 @@ static auto CastEnum(T label) return static_cast::type>(label); } -static CityObjectLabel GetLabelByFolderName(const FString &String) { - if (String == "Buildings") return CityObjectLabel::Buildings; - else if (String == "Fences") return CityObjectLabel::Fences; - else if (String == "Pedestrians") return CityObjectLabel::Pedestrians; - else if (String == "Pole") return CityObjectLabel::Poles; - else if (String == "Props") return CityObjectLabel::Other; - else if (String == "Road") return CityObjectLabel::Roads; - else if (String == "RoadLines") return CityObjectLabel::RoadLines; - else if (String == "SideWalk") return CityObjectLabel::Sidewalks; - else if (String == "Vegetation") return CityObjectLabel::Vegetation; - else if (String == "Vehicles") return CityObjectLabel::Vehicles; - else if (String == "Walls") return CityObjectLabel::Walls; - else return CityObjectLabel::None; +static ECityObjectLabel GetLabelByFolderName(const FString &String) { + if (String == "Buildings") return ECityObjectLabel::Buildings; + else if (String == "Fences") return ECityObjectLabel::Fences; + else if (String == "Pedestrians") return ECityObjectLabel::Pedestrians; + else if (String == "Pole") return ECityObjectLabel::Poles; + else if (String == "Props") return ECityObjectLabel::Other; + else if (String == "Road") return ECityObjectLabel::Roads; + else if (String == "RoadLines") return ECityObjectLabel::RoadLines; + else if (String == "SideWalk") return ECityObjectLabel::Sidewalks; + else if (String == "Vegetation") return ECityObjectLabel::Vegetation; + else if (String == "Vehicles") return ECityObjectLabel::Vehicles; + else if (String == "Walls") return ECityObjectLabel::Walls; + else return ECityObjectLabel::None; } template -static CityObjectLabel GetLabelByPath(const T *Object) +static ECityObjectLabel GetLabelByPath(const T *Object) { const FString Path = Object->GetPathName(); TArray StringArray; Path.ParseIntoArray(StringArray, TEXT("/"), false); - return (StringArray.Num() > 3 ? GetLabelByFolderName(StringArray[3]) : CityObjectLabel::None); + return (StringArray.Num() > 3 ? GetLabelByFolderName(StringArray[3]) : ECityObjectLabel::None); } -static void SetStencilValue(UPrimitiveComponent *comp, const CityObjectLabel &Label) { - if (Label != CityObjectLabel::None) { +static void SetStencilValue(UPrimitiveComponent *comp, const ECityObjectLabel &Label) { + if (Label != ECityObjectLabel::None) { comp->SetRenderCustomDepth(true); comp->SetCustomDepthStencilValue(CastEnum(Label)); } @@ -111,6 +111,28 @@ void ATagger::TagActorsInLevel(UWorld &World) } } +ECityObjectLabel ATagger::GetTagOfTaggedComponent(const UPrimitiveComponent &Component) +{ + return + (Component.bRenderCustomDepth ? + static_cast(Component.CustomDepthStencilValue) : + ECityObjectLabel::None); +} + +void ATagger::GetTagsOfTaggedActor(const AActor &Actor, TArray &Tags) +{ + TArray Components; + Actor.GetComponents(Components); + for (auto *Component : Components) { + if (Component != nullptr) { + const auto Tag = GetTagOfTaggedComponent(*Component); + if (Tag != ECityObjectLabel::None) { + Tags.Add(Tag); + } + } + } +} + // ============================================================================= // -- non-static ATagger functions --------------------------------------------- // ============================================================================= diff --git a/Source/Carla/Tagger.h b/Source/Carla/Tagger.h index cf5c5d90e..e912728fc 100644 --- a/Source/Carla/Tagger.h +++ b/Source/Carla/Tagger.h @@ -5,7 +5,7 @@ #include "GameFramework/Actor.h" #include "Tagger.generated.h" -enum class CityObjectLabel : uint8 +enum class ECityObjectLabel : uint8 { None = 0u, Buildings = 1u, @@ -33,10 +33,24 @@ class CARLA_API ATagger : public AActor public: + /// Set the tag of an actor. static void TagActor(const AActor &Actor); + /// Set the tag of every actor in level. static void TagActorsInLevel(UWorld &World); + /// Retrieve the tag of an already tagged component. + static ECityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component); + + /// Retrieve the tags of an already tagged actor. + static void GetTagsOfTaggedActor(const AActor &Actor, TArray &Tags); + + /// Return true if @a Component has been tagged with the given @a Tag. + static bool MatchComponent(const UPrimitiveComponent &Component, ECityObjectLabel Tag) + { + return (Tag == GetTagOfTaggedComponent(Component)); + } + ATagger(); protected: