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