CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
Sensor
ShowOnlyFilter.cpp
Go to the documentation of this file.
1
// Copyright (c) 2026 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/Sensor/ShowOnlyFilter.h
"
8
#include "
Carla.h
"
9
#include "
Carla/Game/CarlaEpisode.h
"
10
#include "
Carla/Game/Tagger.h
"
11
12
#include <util/ue-header-guard-begin.h>
13
#include "Components/PrimitiveComponent.h"
14
#include "Components/SceneCaptureComponent2D.h"
15
#include "Engine/World.h"
16
#include "EngineUtils.h"
17
#include "GameFramework/Actor.h"
18
#include <util/ue-header-guard-end.h>
19
20
bool
FCarlaShowOnlyFilter::ParseLabels
(
21
const
FString &CommaSeparated,
22
TSet<Label> &OutLabels,
23
FString &OutUnknown)
24
{
25
OutLabels.Reset();
26
OutUnknown.Reset();
27
TArray<FString> Tokens;
28
CommaSeparated.ParseIntoArray(Tokens, TEXT(
","
),
true
);
29
bool
bAllKnown =
true
;
30
for
(FString &Token : Tokens)
31
{
32
Token.TrimStartAndEndInline();
33
if
(Token.IsEmpty())
34
{
35
continue
;
36
}
37
// ATagger::GetTagFromString is the mapping the tagger itself uses for
38
// component tags; it is a case-insensitive substring match, so "car",
39
// "Car" and "Cars" all name CityObjectLabel::Car.
40
const
Label
Parsed =
ATagger::GetTagFromString
(Token);
41
if
(Parsed == Label::None)
42
{
43
bAllKnown =
false
;
44
OutUnknown += (OutUnknown.IsEmpty() ? TEXT(
""
) : TEXT(
","
)) + Token;
45
continue
;
46
}
47
OutLabels.Add(Parsed);
48
}
49
return
bAllKnown;
50
}
51
52
static
void
ShowOnlyFilter_WarnUnknown
(
const
TCHAR *Attribute,
const
FString &Unknown)
53
{
54
if
(!Unknown.IsEmpty())
55
{
56
UE_LOG(LogCarla, Warning,
57
TEXT(
"%s: unknown semantic label(s) \"%s\" ignored"
), Attribute, *Unknown);
58
}
59
}
60
61
void
FCarlaShowOnlyFilter::SetShowOnlyTags
(
const
FString &CommaSeparated)
62
{
63
FString Unknown;
64
ParseLabels
(CommaSeparated,
ShowOnlyTags
, Unknown);
65
ShowOnlyFilter_WarnUnknown
(TEXT(
"show_only_tags"
), Unknown);
66
bDirty
=
true
;
67
}
68
69
void
FCarlaShowOnlyFilter::SetShadowCatcherTags
(
const
FString &CommaSeparated)
70
{
71
FString Unknown;
72
ParseLabels
(CommaSeparated,
ShadowCatcherTags
, Unknown);
73
ShowOnlyFilter_WarnUnknown
(TEXT(
"shadow_catcher_tags"
), Unknown);
74
bDirty
=
true
;
75
}
76
77
void
FCarlaShowOnlyFilter::SetShowOnlyActorIds
(
const
FString &CommaSeparated)
78
{
79
ActorIds
.Reset();
80
TArray<FString> Tokens;
81
CommaSeparated.ParseIntoArray(Tokens, TEXT(
","
),
true
);
82
for
(FString &Token : Tokens)
83
{
84
Token.TrimStartAndEndInline();
85
if
(Token.IsEmpty())
86
{
87
continue
;
88
}
89
if
(Token.IsNumeric())
90
{
91
ActorIds
.Add(
static_cast<
uint32
>
(FCString::Atoi64(*Token)));
92
}
93
else
94
{
95
UE_LOG(LogCarla, Warning,
96
TEXT(
"show_only_actor_ids: \"%s\" is not an actor id, ignored"
), *Token);
97
}
98
}
99
bDirty
=
true
;
100
}
101
102
void
FCarlaShowOnlyFilter::Refresh
(UWorld *World,
const
UCarlaEpisode
*Episode)
103
{
104
if
(!
IsEnabled
() || World ==
nullptr
)
105
{
106
return
;
107
}
108
const
int32 ActorCount = World->GetActorCount();
109
if
(
bDirty
|| ActorCount !=
LastActorCount
)
110
{
111
Rescan
(World, Episode);
112
LastActorCount
= ActorCount;
113
bDirty
=
false
;
114
}
115
}
116
117
void
FCarlaShowOnlyFilter::Rescan
(UWorld *World,
const
UCarlaEpisode
*Episode)
118
{
119
const
double
Start = FPlatformTime::Seconds();
120
CachedActors
.Reset();
121
122
TSet<Label> Wanted =
ShowOnlyTags
;
123
Wanted.Append(
ShadowCatcherTags
);
124
125
if
(Wanted.Num() > 0)
126
{
127
TInlineComponentArray<UPrimitiveComponent *> Primitives;
128
for
(TActorIterator<AActor> It(World); It; ++It)
129
{
130
AActor
*Actor = *It;
131
if
(!IsValid(Actor))
132
{
133
continue
;
134
}
135
Primitives.Reset();
136
Actor->GetComponents<UPrimitiveComponent>(Primitives);
137
for
(UPrimitiveComponent *Primitive : Primitives)
138
{
139
if
(Primitive !=
nullptr
&&
140
Wanted.Contains(
ATagger::GetTagOfTaggedComponent
(*Primitive)))
141
{
142
CachedActors
.Add(Actor);
143
break
;
144
}
145
}
146
}
147
}
148
149
if
(
ActorIds
.Num() > 0 && Episode !=
nullptr
)
150
{
151
const
FActorRegistry
&Registry = Episode->
GetActorRegistry
();
152
for
(
const
uint32 Id :
ActorIds
)
153
{
154
const
FCarlaActor
*View = Registry.
FindCarlaActor
(Id);
155
if
(View ==
nullptr
)
156
{
157
continue
;
158
}
159
AActor
*Actor =
const_cast<
AActor
*
>
(View->
GetActor
());
160
if
(IsValid(Actor))
161
{
162
CachedActors
.AddUnique(Actor);
163
}
164
}
165
}
166
167
UE_LOG(LogCarla, Verbose,
168
TEXT(
"ShowOnlyFilter: %d actor(s) selected out of %d in %.2f ms"
),
169
CachedActors
.Num(), World->GetActorCount(),
170
(FPlatformTime::Seconds() - Start) * 1000.0);
171
}
172
173
void
FCarlaShowOnlyFilter::ApplyTo
(
USceneCaptureComponent2D
&Capture)
const
174
{
175
if
(!
IsEnabled
())
176
{
177
if
(Capture.PrimitiveRenderMode == ESceneCapturePrimitiveRenderMode::PRM_UseShowOnlyList)
178
{
179
Capture.PrimitiveRenderMode = ESceneCapturePrimitiveRenderMode::PRM_RenderScenePrimitives;
180
Capture.ShowOnlyActors.Reset();
181
Capture.ClearShowOnlyComponents();
182
}
183
return
;
184
}
185
Capture.PrimitiveRenderMode = ESceneCapturePrimitiveRenderMode::PRM_UseShowOnlyList;
186
Capture.ShowOnlyActors.Reset(
CachedActors
.Num());
187
for
(
const
TWeakObjectPtr<AActor> &Weak :
CachedActors
)
188
{
189
if
(
AActor
*Actor = Weak.Get())
190
{
191
Capture.ShowOnlyActors.Add(Actor);
192
}
193
}
194
}
CarlaEpisode.h
Carla.h
ShowOnlyFilter_WarnUnknown
static void ShowOnlyFilter_WarnUnknown(const TCHAR *Attribute, const FString &Unknown)
Definition
ShowOnlyFilter.cpp:52
ShowOnlyFilter.h
Tagger.h
AActor
ATagger::GetTagOfTaggedComponent
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
Definition
Tagger.cpp:213
ATagger::GetTagFromString
static crp::CityObjectLabel GetTagFromString(FString Tag)
Transform a string into a CityObjectLabel.
Definition
Tagger.cpp:235
FActorRegistry
A registry of all the Carla actors.
Definition
ActorRegistry.h:19
FActorRegistry::FindCarlaActor
FCarlaActor * FindCarlaActor(IdType Id)
Definition
ActorRegistry.h:67
FCarlaActor
A view over an actor and its properties.
Definition
CarlaActor.h:25
FCarlaActor::GetActor
AActor * GetActor()
Definition
CarlaActor.h:91
UCarlaEpisode
A simulation episode.
Definition
CarlaEpisode.h:45
UCarlaEpisode::GetActorRegistry
const FActorRegistry & GetActorRegistry() const
Definition
CarlaEpisode.h:170
USceneCaptureComponent2D
FCarlaShowOnlyFilter::CachedActors
TArray< TWeakObjectPtr< AActor > > CachedActors
Definition
ShowOnlyFilter.h:94
FCarlaShowOnlyFilter::Refresh
void Refresh(UWorld *World, const UCarlaEpisode *Episode)
Rebuild the cached actor list if the world changed since the last call.
Definition
ShowOnlyFilter.cpp:102
FCarlaShowOnlyFilter::SetShowOnlyTags
void SetShowOnlyTags(const FString &CommaSeparated)
Definition
ShowOnlyFilter.cpp:61
FCarlaShowOnlyFilter::ShadowCatcherTags
TSet< Label > ShadowCatcherTags
Definition
ShowOnlyFilter.h:91
FCarlaShowOnlyFilter::LastActorCount
int32 LastActorCount
Definition
ShowOnlyFilter.h:95
FCarlaShowOnlyFilter::ShowOnlyTags
TSet< Label > ShowOnlyTags
Definition
ShowOnlyFilter.h:90
FCarlaShowOnlyFilter::SetShadowCatcherTags
void SetShadowCatcherTags(const FString &CommaSeparated)
Definition
ShowOnlyFilter.cpp:69
FCarlaShowOnlyFilter::Rescan
void Rescan(UWorld *World, const UCarlaEpisode *Episode)
Definition
ShowOnlyFilter.cpp:117
FCarlaShowOnlyFilter::bDirty
bool bDirty
Definition
ShowOnlyFilter.h:96
FCarlaShowOnlyFilter::ApplyTo
void ApplyTo(USceneCaptureComponent2D &Capture) const
Push the current list into Capture (or restore full-scene rendering when the filter is disabled).
Definition
ShowOnlyFilter.cpp:173
FCarlaShowOnlyFilter::Label
carla::rpc::CityObjectLabel Label
Definition
ShowOnlyFilter.h:50
FCarlaShowOnlyFilter::ParseLabels
static bool ParseLabels(const FString &CommaSeparated, TSet< Label > &OutLabels, FString &OutUnknown)
Parse a comma separated list of label names into OutLabels using the tagger's own name mapping.
Definition
ShowOnlyFilter.cpp:20
FCarlaShowOnlyFilter::SetShowOnlyActorIds
void SetShowOnlyActorIds(const FString &CommaSeparated)
Definition
ShowOnlyFilter.cpp:77
FCarlaShowOnlyFilter::ActorIds
TSet< uint32 > ActorIds
Definition
ShowOnlyFilter.h:92
FCarlaShowOnlyFilter::IsEnabled
bool IsEnabled() const
Definition
ShowOnlyFilter.h:63
Generated by
1.18.0