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//为carla::rpc命名空间创建别名crp
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 //// 一系列条件判断,根据输入的文件夹名称返回对应的城市场景对象标签枚举值
24 if (String == "Building") return crp::CityObjectLabel::Buildings;
25 else if (String == "Fence") return crp::CityObjectLabel::Fences;
26 else if (String == "Pedestrian") return crp::CityObjectLabel::Pedestrians;
27 else if (String == "Pole") return crp::CityObjectLabel::Poles;
28 else if (String == "Other") return crp::CityObjectLabel::Other;
29 else if (String == "Road") return crp::CityObjectLabel::Roads;
30 else if (String == "RoadLine") return crp::CityObjectLabel::RoadLines;
31 else if (String == "SideWalk") return crp::CityObjectLabel::Sidewalks;
32 else if (String == "TrafficSign") return crp::CityObjectLabel::TrafficSigns;
33 else if (String == "Vegetation") return crp::CityObjectLabel::Vegetation;
34 else if (String == "Car") return crp::CityObjectLabel::Car;
35 else if (String == "Wall") return crp::CityObjectLabel::Walls;
36 else if (String == "Sky") return crp::CityObjectLabel::Sky;
37 else if (String == "Ground") return crp::CityObjectLabel::Ground;
38 else if (String == "Bridge") return crp::CityObjectLabel::Bridge;
39 else if (String == "RailTrack") return crp::CityObjectLabel::RailTrack;
40 else if (String == "GuardRail") return crp::CityObjectLabel::GuardRail;
41 else if (String == "TrafficLight") return crp::CityObjectLabel::TrafficLight;
42 else if (String == "Static") return crp::CityObjectLabel::Static;
43 else if (String == "Dynamic") return crp::CityObjectLabel::Dynamic;
44 else if (String == "Water") return crp::CityObjectLabel::Water;
45 else if (String == "Terrain") return crp::CityObjectLabel::Terrain;
46 else if (String == "Truck") return crp::CityObjectLabel::Truck;
47 else if (String == "Motorcycle") return crp::CityObjectLabel::Motorcycle;
48 else if (String == "Bicycle") return crp::CityObjectLabel::Bicycle;
49 else if (String == "Bus") return crp::CityObjectLabel::Bus;
50 else if (String == "Rider") return crp::CityObjectLabel::Rider;
51 else if (String == "Train") return crp::CityObjectLabel::Train;
52 else return crp::CityObjectLabel::None;
53}
54//设置模板值和渲染深度函数
56 UPrimitiveComponent &Component,
57 const crp::CityObjectLabel &Label,
58 const bool bSetRenderCustomDepth) {
59 Component.SetCustomDepthStencilValue(CastEnum(Label));
60 Component.SetRenderCustomDepth(
61 bSetRenderCustomDepth &&
62 (Label != crp::CityObjectLabel::None));
63}
64//判断是否为物体
66{
67 return (Label == crp::CityObjectLabel::Pedestrians ||
68 Label == crp::CityObjectLabel::TrafficSigns ||
69 Label == crp::CityObjectLabel::Car ||
70 Label == crp::CityObjectLabel::Train ||
71 Label == crp::CityObjectLabel::Bicycle ||
72 Label == crp::CityObjectLabel::Motorcycle ||
73 Label == crp::CityObjectLabel::Bus ||
74 Label == crp::CityObjectLabel::Rider ||
75 Label == crp::CityObjectLabel::Truck ||
76 Label == crp::CityObjectLabel::TrafficLight);
77}
78
79/**
80 * @brief 获得实例分割中参与者所标注的颜色
81 * @param Actor 所需要判断显示颜色的参与者
82 * @param Label
83 * @return
84*/
85//获取参与者标签颜色函数
87{
88 uint32 id = Actor.GetUniqueID();
89 // TODO: Warn if id > 0xffff.
90
91 // 像语义分割一样编码标签和 id
92 // TODO: 从红色R通道和可能的A通道借用比特?
93 FLinearColor Color(0.0f, 0.0f, 0.0f, 1.0f);
94 Color.R = CastEnum(Label) / 255.0f;
95 Color.G = ((id & 0x00ff) >> 0) / 255.0f;
96 Color.B = ((id & 0xff00) >> 8) / 255.0f;
97
98 return Color;
99}
100
101
102// =============================================================================
103// -- ATagger类的静态函数 -------------------------------------------------
104// =============================================================================
105
106void ATagger::TagActor(const AActor &Actor, bool bTagForSemanticSegmentation)
107{
108#ifdef CARLA_TAGGER_EXTRA_LOG
109 UE_LOG(LogCarla, Log, TEXT("Actor: %s"), *Actor.GetName());
110#endif // CARLA_TAGGER_EXTRA_LOG
111
112 // 遍历静态网格.
113 //标记单个参与者函数
114 TArray<UStaticMeshComponent *> StaticMeshComponents;
115 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
116 for (UStaticMeshComponent *Component : StaticMeshComponents) {
117 auto Label = GetLabelByPath(Component->GetStaticMesh());
118 if (Label == crp::CityObjectLabel::Pedestrians &&
119 Cast<ACarlaWheeledVehicle>(&Actor))
120 {
121 Label = crp::CityObjectLabel::Rider;
122 }
123 // 设置组件的模板值和渲染深度
125#ifdef CARLA_TAGGER_EXTRA_LOG
126 UE_LOG(LogCarla, Log, TEXT(" + StaticMeshComponent: %s"), *Component->GetName());
127 UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetTagAsString(Label));
128#endif // CARLA_TAGGER_EXTRA_LOG
129
130 if(!Component->IsVisible() || !Component->GetStaticMesh())
131 {
132 continue;
133 // 查找或创建附加到该组件的带标签的组件,并设置其颜色
134 }
135
136 // 查找附加到此组件上的带标签的组件
137 UTaggedComponent *TaggedComponent = NULL;
138 // 遍历骨骼网格组件,逻辑与静态网格组件类似
139 TArray<USceneComponent *> AttachedComponents = Component->GetAttachChildren();
140 for (USceneComponent *SceneComponent : AttachedComponents) {
141 UTaggedComponent *TaggedSceneComponent = Cast<UTaggedComponent>(SceneComponent);
142 if (IsValid(TaggedSceneComponent)) {
143 TaggedComponent = TaggedSceneComponent;
144#ifdef CARLA_TAGGER_EXTRA_LOG
145 UE_LOG(LogCarla, Log, TEXT(" - Found Tag"));
146#endif // CARLA_TAGGER_EXTRA_LOG
147 break;
148 }
149 }
150
151 // 如果没有找到,则创建一个新的带标签的组件,并将其附加到此组件上
152 if (!TaggedComponent) {
153 TaggedComponent = NewObject<UTaggedComponent>(Component);
154 TaggedComponent->SetupAttachment(Component);
155 TaggedComponent->RegisterComponent();
156#ifdef CARLA_TAGGER_EXTRA_LOG
157 UE_LOG(LogCarla, Log, TEXT(" - Added Tag"));
158#endif // CARLA_TAGGER_EXTRA_LOG
159 }
160
161 // 设置带标签的组件颜色
162 FLinearColor Color = GetActorLabelColor(Actor, Label);
163#ifdef CARLA_TAGGER_EXTRA_LOG
164 UE_LOG(LogCarla, Log, TEXT(" - Color: %s"), *Color.ToString());
165#endif // CARLA_TAGGER_EXTRA_LOG
166
167 TaggedComponent->SetColor(Color);
168 TaggedComponent->MarkRenderStateDirty();
169 }
170
171 // 遍历骨骼网格
172 TArray<USkeletalMeshComponent *> SkeletalMeshComponents;
173 Actor.GetComponents<USkeletalMeshComponent>(SkeletalMeshComponents);
174 for (USkeletalMeshComponent *Component : SkeletalMeshComponents) {
175 auto Label = GetLabelByPath(Component->GetPhysicsAsset());
176 if (Label == crp::CityObjectLabel::Pedestrians &&
177 Cast<ACarlaWheeledVehicle>(&Actor))
178 {
179 Label = crp::CityObjectLabel::Rider;
180 }
182#ifdef CARLA_TAGGER_EXTRA_LOG
183 UE_LOG(LogCarla, Log, TEXT(" + SkeletalMeshComponent: %s"), *Component->GetName());
184 UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetTagAsString(Label));
185#endif // CARLA_TAGGER_EXTRA_LOG
186
187 if(!Component->IsVisible() || !Component->GetSkeletalMeshRenderData())
188 {
189 continue;
190 }
191
192 // 查找附加到此组件上的带标签的组件
193 UTaggedComponent *TaggedComponent = NULL;
194 TArray<USceneComponent *> AttachedComponents = Component->GetAttachChildren();
195 for (USceneComponent *SceneComponent : AttachedComponents) {
196 UTaggedComponent *TaggedSceneComponent = Cast<UTaggedComponent>(SceneComponent);
197 if (IsValid(TaggedSceneComponent)) {
198 TaggedComponent = TaggedSceneComponent;
199#ifdef CARLA_TAGGER_EXTRA_LOG
200 UE_LOG(LogCarla, Log, TEXT(" - Found Tag"));
201#endif // CARLA_TAGGER_EXTRA_LOG
202 break;
203 }
204 }
205
206 // 如果没有找到,则创建一个新的带标签的组件,并将其附加到此组件
207 if (!TaggedComponent) {
208 TaggedComponent = NewObject<UTaggedComponent>(Component);
209 TaggedComponent->SetupAttachment(Component);
210 TaggedComponent->RegisterComponent();
211#ifdef CARLA_TAGGER_EXTRA_LOG
212 UE_LOG(LogCarla, Log, TEXT(" - Added Tag"));
213#endif // CARLA_TAGGER_EXTRA_LOG
214 }
215
216 // 设置带标签组件的颜色
217 FLinearColor Color = GetActorLabelColor(Actor, Label);
218#ifdef CARLA_TAGGER_EXTRA_LOG
219 UE_LOG(LogCarla, Log, TEXT(" - Color: %s"), *Color.ToString());
220#endif // CARLA_TAGGER_EXTRA_LOG
221
222 TaggedComponent->SetColor(Color);
223 TaggedComponent->MarkRenderStateDirty();
224 TaggedComponent->SetComponentTickEnabled(true);
225
226 }
227}
228//标记单个参与者函数
229void ATagger::TagActorsInLevel(UWorld &World, bool bTagForSemanticSegmentation)
230{
231 for (TActorIterator<AActor> it(&World); it; ++it) {
233 }
234}
235//标记世界中所有参与者函数
236void ATagger::TagActorsInLevel(ULevel &Level, bool bTagForSemanticSegmentation)
237{
238 for (AActor * Actor : Level.Actors) {
240 }
241}
242//获取标记参与者的标签函数
243void ATagger::GetTagsOfTaggedActor(const AActor &Actor, TSet<crp::CityObjectLabel> &Tags)
244{
245 TArray<UPrimitiveComponent *> Components;
246 Actor.GetComponents<UPrimitiveComponent>(Components);
247 for (auto *Component : Components) {
248 if (Component != nullptr) {
249 const auto Tag = GetTagOfTaggedComponent(*Component);
250 if (Tag != crp::CityObjectLabel::None) {
251 Tags.Add(Tag);
252 }
253 }
254 }
255}
256//将标签转换为字符串函数
258{
259 switch (Label) {
260#define CARLA_GET_LABEL_STR(lbl) case crp::CityObjectLabel:: lbl : return TEXT(#lbl);
261 default:
263 CARLA_GET_LABEL_STR(Buildings)
264 CARLA_GET_LABEL_STR(Fences)
266 CARLA_GET_LABEL_STR(Pedestrians)
268 CARLA_GET_LABEL_STR(RoadLines)
270 CARLA_GET_LABEL_STR(Sidewalks)
271 CARLA_GET_LABEL_STR(TrafficSigns)
272 CARLA_GET_LABEL_STR(Vegetation)
276 CARLA_GET_LABEL_STR(Ground)
277 CARLA_GET_LABEL_STR(Bridge)
278 CARLA_GET_LABEL_STR(RailTrack)
279 CARLA_GET_LABEL_STR(GuardRail)
281 CARLA_GET_LABEL_STR(Static)
282 CARLA_GET_LABEL_STR(Dynamic)
284 CARLA_GET_LABEL_STR(Terrain)
286 CARLA_GET_LABEL_STR(Motorcycle)
287 CARLA_GET_LABEL_STR(Bicycle)
291
292#undef CARLA_GET_LABEL_STR
293 }
294}
295
296// =============================================================================
297// -- non-static ATagger functions ---------------------------------------------
298// =============================================================================
299//类的默认构造函数
301{
302 PrimaryActorTick.bCanEverTick = false;
303}
304//编辑后属性更改处理函数
305#if WITH_EDITOR
306void ATagger::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
307{
308 Super::PostEditChangeProperty(PropertyChangedEvent);
309 if (PropertyChangedEvent.Property) {
310 if (bTriggerTagObjects && (GetWorld() != nullptr)) {
312 }
313 }
314 bTriggerTagObjects = false;
315}
316#endif // 与编辑器集成
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld Actor
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)
检索已标记的角色的标记。CityObjectLabel::None 为 未添加到数组中。
Definition Tagger.cpp:243
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
检索已标记组件的标记。
Definition Tagger.h:52
static bool IsThing(const crp::CityObjectLabel &Label)
Definition Tagger.cpp:65
static FString GetTagAsString(crp::CityObjectLabel Tag)
检索已标记的角色的标记。CityObjectLabel::None 为 未添加到数组中。
Definition Tagger.cpp:257
static crp::CityObjectLabel GetLabelByFolderName(const FString &String)
计算文件夹路径对应的标签的方法
Definition Tagger.cpp:22
bool bTriggerTagObjects
Definition Tagger.h:102
ATagger()
Definition Tagger.cpp:300
static crp::CityObjectLabel GetLabelByPath(const T *Object)
计算特定对象对应的 label 的方法 使用存储它的文件夹路径
Definition Tagger.h:77
static void SetStencilValue(UPrimitiveComponent &Component, const crp::CityObjectLabel &Label, const bool bSetRenderCustomDepth)
Definition Tagger.cpp:55
bool bTagForSemanticSegmentation
Definition Tagger.h:105
static void TagActor(const AActor &Actor, bool bTagForSemanticSegmentation)
设置角色的标签。
Definition Tagger.cpp:106
static void TagActorsInLevel(UWorld &World, bool bTagForSemanticSegmentation)
设置 level 中每个 actor 的标签。
Definition Tagger.cpp:229
static FLinearColor GetActorLabelColor(const AActor &Actor, const crp::CityObjectLabel &Label)
获得实例分割中参与者所标注的颜色
Definition Tagger.cpp:86
void SetColor(FLinearColor color)