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