CARLA
Loading...
Searching...
No Matches
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
8#include "Carla.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
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
52static 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
61void 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
69void 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
77void 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
102void 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
117void 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
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}
static void ShowOnlyFilter_WarnUnknown(const TCHAR *Attribute, const FString &Unknown)
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
static crp::CityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component)
Retrieve the tag of an already tagged component.
Definition Tagger.cpp:213
static crp::CityObjectLabel GetTagFromString(FString Tag)
Transform a string into a CityObjectLabel.
Definition Tagger.cpp:235
A registry of all the Carla actors.
FCarlaActor * FindCarlaActor(IdType Id)
A view over an actor and its properties.
Definition CarlaActor.h:25
AActor * GetActor()
Definition CarlaActor.h:91
A simulation episode.
const FActorRegistry & GetActorRegistry() const
TArray< TWeakObjectPtr< AActor > > CachedActors
void Refresh(UWorld *World, const UCarlaEpisode *Episode)
Rebuild the cached actor list if the world changed since the last call.
void SetShowOnlyTags(const FString &CommaSeparated)
TSet< Label > ShadowCatcherTags
TSet< Label > ShowOnlyTags
void SetShadowCatcherTags(const FString &CommaSeparated)
void Rescan(UWorld *World, const UCarlaEpisode *Episode)
void ApplyTo(USceneCaptureComponent2D &Capture) const
Push the current list into Capture (or restore full-scene rendering when the filter is disabled).
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.
void SetShowOnlyActorIds(const FString &CommaSeparated)
TSet< uint32 > ActorIds