Fix #28 Implement collision count for cars and pedestrians

This commit is contained in:
nsubiron 2017-05-09 12:43:36 +02:00
parent fb845bc625
commit 1e5bf2abde
6 changed files with 81 additions and 27 deletions

View File

@ -207,7 +207,7 @@ static bool LineTrace(
if (Success) { if (Success) {
for (FHitResult &Item : OutHits) { for (FHitResult &Item : OutHits) {
if (Item.Component->CustomDepthStencilValue == static_cast<uint8>(CityObjectLabel::Roads)) { if (ATagger::MatchComponent(*Item.Component, ECityObjectLabel::Roads)) {
HitResult = Item; HitResult = Item;
return true; return true;
} }

View File

@ -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) static int32 RoundToMilliseconds(float Seconds)

View File

@ -130,7 +130,11 @@ public:
// =========================================================================== // ===========================================================================
private: private:
void RegisterCollision(AActor *Actor, FVector NormalImpulse); void RegisterCollision(
AActor *Actor,
AActor *OtherActor,
const FVector &NormalImpulse,
const FHitResult &Hit);
void UpdateTimeStamp(float DeltaSeconds); void UpdateTimeStamp(float DeltaSeconds);

View File

@ -251,12 +251,12 @@ void ACarlaVehicleController::ToggleManualMode()
// ============================================================================= // =============================================================================
void ACarlaVehicleController::OnCollisionEvent( void ACarlaVehicleController::OnCollisionEvent(
AActor* /*Actor*/, AActor* Actor,
AActor* OtherActor, AActor* OtherActor,
FVector NormalImpulse, FVector NormalImpulse,
const FHitResult& /*Hit*/) const FHitResult& Hit)
{ {
CarlaPlayerState->RegisterCollision(OtherActor, NormalImpulse); CarlaPlayerState->RegisterCollision(Actor, OtherActor, NormalImpulse, Hit);
} }
// ============================================================================= // =============================================================================

View File

@ -10,10 +10,10 @@
#include "PhysicsEngine/PhysicsAsset.h" #include "PhysicsEngine/PhysicsAsset.h"
#ifdef CARLA_TAGGER_EXTRA_LOG #ifdef CARLA_TAGGER_EXTRA_LOG
static FString GetLabelAsString(const CityObjectLabel Label) static FString GetLabelAsString(const ECityObjectLabel Label)
{ {
switch (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: default:
CARLA_GET_LABEL_STR(None) CARLA_GET_LABEL_STR(None)
CARLA_GET_LABEL_STR(Buildings) CARLA_GET_LABEL_STR(Buildings)
@ -38,32 +38,32 @@ static auto CastEnum(T label)
return static_cast<typename std::underlying_type<T>::type>(label); return static_cast<typename std::underlying_type<T>::type>(label);
} }
static CityObjectLabel GetLabelByFolderName(const FString &String) { static ECityObjectLabel GetLabelByFolderName(const FString &String) {
if (String == "Buildings") return CityObjectLabel::Buildings; if (String == "Buildings") return ECityObjectLabel::Buildings;
else if (String == "Fences") return CityObjectLabel::Fences; else if (String == "Fences") return ECityObjectLabel::Fences;
else if (String == "Pedestrians") return CityObjectLabel::Pedestrians; else if (String == "Pedestrians") return ECityObjectLabel::Pedestrians;
else if (String == "Pole") return CityObjectLabel::Poles; else if (String == "Pole") return ECityObjectLabel::Poles;
else if (String == "Props") return CityObjectLabel::Other; else if (String == "Props") return ECityObjectLabel::Other;
else if (String == "Road") return CityObjectLabel::Roads; else if (String == "Road") return ECityObjectLabel::Roads;
else if (String == "RoadLines") return CityObjectLabel::RoadLines; else if (String == "RoadLines") return ECityObjectLabel::RoadLines;
else if (String == "SideWalk") return CityObjectLabel::Sidewalks; else if (String == "SideWalk") return ECityObjectLabel::Sidewalks;
else if (String == "Vegetation") return CityObjectLabel::Vegetation; else if (String == "Vegetation") return ECityObjectLabel::Vegetation;
else if (String == "Vehicles") return CityObjectLabel::Vehicles; else if (String == "Vehicles") return ECityObjectLabel::Vehicles;
else if (String == "Walls") return CityObjectLabel::Walls; else if (String == "Walls") return ECityObjectLabel::Walls;
else return CityObjectLabel::None; else return ECityObjectLabel::None;
} }
template <typename T> template <typename T>
static CityObjectLabel GetLabelByPath(const T *Object) static ECityObjectLabel GetLabelByPath(const T *Object)
{ {
const FString Path = Object->GetPathName(); const FString Path = Object->GetPathName();
TArray<FString> StringArray; TArray<FString> StringArray;
Path.ParseIntoArray(StringArray, TEXT("/"), false); 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) { static void SetStencilValue(UPrimitiveComponent *comp, const ECityObjectLabel &Label) {
if (Label != CityObjectLabel::None) { if (Label != ECityObjectLabel::None) {
comp->SetRenderCustomDepth(true); comp->SetRenderCustomDepth(true);
comp->SetCustomDepthStencilValue(CastEnum(Label)); comp->SetCustomDepthStencilValue(CastEnum(Label));
} }
@ -111,6 +111,28 @@ void ATagger::TagActorsInLevel(UWorld &World)
} }
} }
ECityObjectLabel ATagger::GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
{
return
(Component.bRenderCustomDepth ?
static_cast<ECityObjectLabel>(Component.CustomDepthStencilValue) :
ECityObjectLabel::None);
}
void ATagger::GetTagsOfTaggedActor(const AActor &Actor, TArray<ECityObjectLabel> &Tags)
{
TArray<UPrimitiveComponent *> Components;
Actor.GetComponents<UPrimitiveComponent>(Components);
for (auto *Component : Components) {
if (Component != nullptr) {
const auto Tag = GetTagOfTaggedComponent(*Component);
if (Tag != ECityObjectLabel::None) {
Tags.Add(Tag);
}
}
}
}
// ============================================================================= // =============================================================================
// -- non-static ATagger functions --------------------------------------------- // -- non-static ATagger functions ---------------------------------------------
// ============================================================================= // =============================================================================

View File

@ -5,7 +5,7 @@
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "Tagger.generated.h" #include "Tagger.generated.h"
enum class CityObjectLabel : uint8 enum class ECityObjectLabel : uint8
{ {
None = 0u, None = 0u,
Buildings = 1u, Buildings = 1u,
@ -33,10 +33,24 @@ class CARLA_API ATagger : public AActor
public: public:
/// Set the tag of an actor.
static void TagActor(const AActor &Actor); static void TagActor(const AActor &Actor);
/// Set the tag of every actor in level.
static void TagActorsInLevel(UWorld &World); 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<ECityObjectLabel> &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(); ATagger();
protected: protected: