CARLA
Tagger.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "Carla.h"
8 #include "Tagger.h"
9 #include "TaggedComponent.h"
10 
11 #include "Components/SkeletalMeshComponent.h"
12 #include "Components/StaticMeshComponent.h"
13 #include "Engine/SkeletalMesh.h"
14 #include "Engine/StaticMesh.h"
15 #include "EngineUtils.h"
16 #include "PhysicsEngine/PhysicsAsset.h"
17 
18 namespace crp = carla::rpc;
19 
20 template <typename T>
21 static auto CastEnum(T label)
22 {
23  return static_cast<typename std::underlying_type<T>::type>(label);
24 }
25 
27  if (String == "Building") return crp::CityObjectLabel::Buildings;
28  else if (String == "Fence") return crp::CityObjectLabel::Fences;
29  else if (String == "Pedestrian") return crp::CityObjectLabel::Pedestrians;
30  else if (String == "Pole") return crp::CityObjectLabel::Poles;
31  else if (String == "Other") return crp::CityObjectLabel::Other;
32  else if (String == "Road") return crp::CityObjectLabel::Roads;
33  else if (String == "RoadLine") return crp::CityObjectLabel::RoadLines;
34  else if (String == "SideWalk") return crp::CityObjectLabel::Sidewalks;
35  else if (String == "TrafficSign") return crp::CityObjectLabel::TrafficSigns;
36  else if (String == "Vegetation") return crp::CityObjectLabel::Vegetation;
37  else if (String == "Vehicles") return crp::CityObjectLabel::Vehicles;
38  else if (String == "Wall") return crp::CityObjectLabel::Walls;
39  else if (String == "Sky") return crp::CityObjectLabel::Sky;
40  else if (String == "Ground") return crp::CityObjectLabel::Ground;
41  else if (String == "Bridge") return crp::CityObjectLabel::Bridge;
42  else if (String == "RailTrack") return crp::CityObjectLabel::RailTrack;
43  else if (String == "GuardRail") return crp::CityObjectLabel::GuardRail;
44  else if (String == "TrafficLight") return crp::CityObjectLabel::TrafficLight;
45  else if (String == "Static") return crp::CityObjectLabel::Static;
46  else if (String == "Dynamic") return crp::CityObjectLabel::Dynamic;
47  else if (String == "Water") return crp::CityObjectLabel::Water;
48  else if (String == "Terrain") return crp::CityObjectLabel::Terrain;
49  else return crp::CityObjectLabel::None;
50 }
51 
53  UPrimitiveComponent &Component,
54  const crp::CityObjectLabel &Label,
55  const bool bSetRenderCustomDepth) {
56  Component.SetCustomDepthStencilValue(CastEnum(Label));
57  Component.SetRenderCustomDepth(
58  bSetRenderCustomDepth &&
59  (Label != crp::CityObjectLabel::None));
60 }
61 
63 {
64  return (Label == crp::CityObjectLabel::Pedestrians ||
68 }
69 
71 {
72  uint32 id = Actor.GetUniqueID();
73  // TODO: Warn if id > 0xffff.
74 
75  // Encode label and id like semantic segmentation does
76  // TODO: Steal bits from R channel and maybe A channel?
77  FLinearColor Color(0.0f, 0.0f, 0.0f, 1.0f);
78  Color.R = CastEnum(Label) / 255.0f;
79  Color.G = ((id & 0x00ff) >> 0) / 255.0f;
80  Color.B = ((id & 0xff00) >> 8) / 255.0f;
81 
82  return Color;
83 }
84 
85 
86 // =============================================================================
87 // -- static ATagger functions -------------------------------------------------
88 // =============================================================================
89 
91 {
92 #ifdef CARLA_TAGGER_EXTRA_LOG
93  UE_LOG(LogCarla, Log, TEXT("Actor: %s"), *Actor.GetName());
94 #endif // CARLA_TAGGER_EXTRA_LOG
95 
96  // Iterate static meshes.
97  TArray<UStaticMeshComponent *> StaticMeshComponents;
98  Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
99  for (UStaticMeshComponent *Component : StaticMeshComponents) {
100  const auto Label = GetLabelByPath(Component->GetStaticMesh());
101  SetStencilValue(*Component, Label, bTagForSemanticSegmentation);
102 #ifdef CARLA_TAGGER_EXTRA_LOG
103  UE_LOG(LogCarla, Log, TEXT(" + StaticMeshComponent: %s"), *Component->GetName());
104  UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetTagAsString(Label));
105 #endif // CARLA_TAGGER_EXTRA_LOG
106 
107  if(!Component->IsVisible() || !Component->GetStaticMesh())
108  {
109  continue;
110  }
111 
112  // Find a tagged component that is attached to this component
113  UTaggedComponent *TaggedComponent = NULL;
114  TArray<USceneComponent *> AttachedComponents = Component->GetAttachChildren();
115  for (USceneComponent *SceneComponent : AttachedComponents) {
116  UTaggedComponent *TaggedSceneComponent = Cast<UTaggedComponent>(SceneComponent);
117  if (IsValid(TaggedSceneComponent)) {
118  TaggedComponent = TaggedSceneComponent;
119 #ifdef CARLA_TAGGER_EXTRA_LOG
120  UE_LOG(LogCarla, Log, TEXT(" - Found Tag"));
121 #endif // CARLA_TAGGER_EXTRA_LOG
122  break;
123  }
124  }
125 
126  // If not found, then create new tagged component and attach it to this component
127  if (!TaggedComponent) {
128  TaggedComponent = NewObject<UTaggedComponent>(Component);
129  TaggedComponent->SetupAttachment(Component);
130  TaggedComponent->RegisterComponent();
131 #ifdef CARLA_TAGGER_EXTRA_LOG
132  UE_LOG(LogCarla, Log, TEXT(" - Added Tag"));
133 #endif // CARLA_TAGGER_EXTRA_LOG
134  }
135 
136  // Set tagged component color
137  FLinearColor Color = GetActorLabelColor(Actor, Label);
138 #ifdef CARLA_TAGGER_EXTRA_LOG
139  UE_LOG(LogCarla, Log, TEXT(" - Color: %s"), *Color.ToString());
140 #endif // CARLA_TAGGER_EXTRA_LOG
141 
142  TaggedComponent->SetColor(Color);
143  TaggedComponent->MarkRenderStateDirty();
144  }
145 
146  // Iterate skeletal meshes.
147  TArray<USkeletalMeshComponent *> SkeletalMeshComponents;
148  Actor.GetComponents<USkeletalMeshComponent>(SkeletalMeshComponents);
149  for (USkeletalMeshComponent *Component : SkeletalMeshComponents) {
150  const auto Label = GetLabelByPath(Component->GetPhysicsAsset());
151  SetStencilValue(*Component, Label, bTagForSemanticSegmentation);
152 #ifdef CARLA_TAGGER_EXTRA_LOG
153  UE_LOG(LogCarla, Log, TEXT(" + SkeletalMeshComponent: %s"), *Component->GetName());
154  UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetTagAsString(Label));
155 #endif // CARLA_TAGGER_EXTRA_LOG
156 
157  if(!Component->IsVisible() || !Component->GetSkeletalMeshRenderData())
158  {
159  continue;
160  }
161 
162  // Find a tagged component that is attached to this component
163  UTaggedComponent *TaggedComponent = NULL;
164  TArray<USceneComponent *> AttachedComponents = Component->GetAttachChildren();
165  for (USceneComponent *SceneComponent : AttachedComponents) {
166  UTaggedComponent *TaggedSceneComponent = Cast<UTaggedComponent>(SceneComponent);
167  if (IsValid(TaggedSceneComponent)) {
168  TaggedComponent = TaggedSceneComponent;
169 #ifdef CARLA_TAGGER_EXTRA_LOG
170  UE_LOG(LogCarla, Log, TEXT(" - Found Tag"));
171 #endif // CARLA_TAGGER_EXTRA_LOG
172  break;
173  }
174  }
175 
176  // If not found, then create new tagged component and attach it to this component
177  if (!TaggedComponent) {
178  TaggedComponent = NewObject<UTaggedComponent>(Component);
179  TaggedComponent->SetupAttachment(Component);
180  TaggedComponent->RegisterComponent();
181 #ifdef CARLA_TAGGER_EXTRA_LOG
182  UE_LOG(LogCarla, Log, TEXT(" - Added Tag"));
183 #endif // CARLA_TAGGER_EXTRA_LOG
184  }
185 
186  // Set tagged component color
187  FLinearColor Color = GetActorLabelColor(Actor, Label);
188 #ifdef CARLA_TAGGER_EXTRA_LOG
189  UE_LOG(LogCarla, Log, TEXT(" - Color: %s"), *Color.ToString());
190 #endif // CARLA_TAGGER_EXTRA_LOG
191 
192  TaggedComponent->SetColor(Color);
193  TaggedComponent->MarkRenderStateDirty();
194 
195  }
196 }
197 
199 {
200  for (TActorIterator<AActor> it(&World); it; ++it) {
201  TagActor(**it, bTagForSemanticSegmentation);
202  }
203 }
204 
206 {
207  for (AActor * Actor : Level.Actors) {
208  TagActor(*Actor, bTagForSemanticSegmentation);
209  }
210 }
211 
212 void ATagger::GetTagsOfTaggedActor(const AActor &Actor, TSet<crp::CityObjectLabel> &Tags)
213 {
214  TArray<UPrimitiveComponent *> Components;
215  Actor.GetComponents<UPrimitiveComponent>(Components);
216  for (auto *Component : Components) {
217  if (Component != nullptr) {
218  const auto Tag = GetTagOfTaggedComponent(*Component);
219  if (Tag != crp::CityObjectLabel::None) {
220  Tags.Add(Tag);
221  }
222  }
223  }
224 }
225 
227 {
228  switch (Label) {
229 #define CARLA_GET_LABEL_STR(lbl) case crp::CityObjectLabel:: lbl : return TEXT(#lbl);
230  default:
231  CARLA_GET_LABEL_STR(None)
232  CARLA_GET_LABEL_STR(Buildings)
233  CARLA_GET_LABEL_STR(Fences)
235  CARLA_GET_LABEL_STR(Pedestrians)
236  CARLA_GET_LABEL_STR(Poles)
237  CARLA_GET_LABEL_STR(RoadLines)
238  CARLA_GET_LABEL_STR(Roads)
239  CARLA_GET_LABEL_STR(Sidewalks)
240  CARLA_GET_LABEL_STR(TrafficSigns)
241  CARLA_GET_LABEL_STR(Vegetation)
242  CARLA_GET_LABEL_STR(Vehicles)
243  CARLA_GET_LABEL_STR(Walls)
245  CARLA_GET_LABEL_STR(Ground)
246  CARLA_GET_LABEL_STR(Bridge)
247  CARLA_GET_LABEL_STR(RailTrack)
248  CARLA_GET_LABEL_STR(GuardRail)
250  CARLA_GET_LABEL_STR(Static)
251  CARLA_GET_LABEL_STR(Dynamic)
252  CARLA_GET_LABEL_STR(Water)
253  CARLA_GET_LABEL_STR(Terrain)
254 #undef CARLA_GET_LABEL_STR
255  }
256 }
257 
258 // =============================================================================
259 // -- non-static ATagger functions ---------------------------------------------
260 // =============================================================================
261 
263 {
264  PrimaryActorTick.bCanEverTick = false;
265 }
266 
267 #if WITH_EDITOR
268 void ATagger::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
269 {
270  Super::PostEditChangeProperty(PropertyChangedEvent);
271  if (PropertyChangedEvent.Property) {
272  if (bTriggerTagObjects && (GetWorld() != nullptr)) {
274  }
275  }
276  bTriggerTagObjects = false;
277 }
278 #endif // WITH_EDITOR
static void SetStencilValue(UPrimitiveComponent &Component, const crp::CityObjectLabel &Label, const bool bSetRenderCustomDepth)
Definition: Tagger.cpp:52
sensor::data::Color Color
static crp::CityObjectLabel GetLabelByPath(const T *Object)
Method that computes the label corresponding to an specific object using the folder path in which it ...
Definition: Tagger.h:77
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
Definition: Tagger.h:52
static void GetTagsOfTaggedActor(const AActor &Actor, TSet< crp::CityObjectLabel > &Tags)
Retrieve the tags of an already tagged actor.
Definition: Tagger.cpp:212
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
ATagger()
Definition: Tagger.cpp:262
static auto CastEnum(T label)
Definition: Tagger.cpp:21
static void TagActorsInLevel(UWorld &World, bool bTagForSemanticSegmentation)
Set the tag of every actor in level.
Definition: Tagger.cpp:198
carla::SharedPtr< cc::Actor > Actor
static FString GetTagAsString(crp::CityObjectLabel Tag)
Retrieve the tags of an already tagged actor.
Definition: Tagger.cpp:226
bool bTriggerTagObjects
Definition: Tagger.h:102
bool bTagForSemanticSegmentation
Definition: Tagger.h:105
#define CARLA_GET_LABEL_STR(lbl)
static FLinearColor GetActorLabelColor(const AActor &Actor, const crp::CityObjectLabel &Label)
Definition: Tagger.cpp:70
static void TagActor(const AActor &Actor, bool bTagForSemanticSegmentation)
Set the tag of an actor.
Definition: Tagger.cpp:90
static bool IsThing(const crp::CityObjectLabel &Label)
Definition: Tagger.cpp:62
static crp::CityObjectLabel GetLabelByFolderName(const FString &String)
Method that computes the label corresponding to a folder path.
Definition: Tagger.cpp:26
void SetColor(FLinearColor color)