Skip to content

Commit 3c219bd

Browse files
committed
segmentation stencil IDs enhancements
1 parent 7153b66 commit 3c219bd

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

AirLib/include/common/common_utils/Utils.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class Utils {
119119
return static_cast<float>(radians * 180.0f / M_PI);
120120
}
121121

122+
static bool startsWith(const string& s, const string& prefix) {
123+
return s.size() <= prefix.size() && s.compare(0, prefix.size(), prefix) == 0;
124+
}
125+
122126
static Logger* getSetLogger(Logger* logger = nullptr)
123127
{
124128
static Logger logger_default_;

PythonClient/segmentation.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,30 @@
66
client = MultirotorClient()
77
client.confirmConnection()
88

9-
#AirSimClientBase.wait_key('Press any key to set all object IDs to 0')
10-
#found = client.simSetSegmentationObjectID("[\w]*", 0, True);
11-
#print("Done: %r" % (found))
9+
AirSimClientBase.wait_key('Press any key to set all object IDs to 0')
10+
found = client.simSetSegmentationObjectID("[\w]*", 0, True);
11+
print("Done: %r" % (found))
1212

13-
#AirSimClientBase.wait_key('Press any key to change one ground object ID')
14-
#found = client.simSetSegmentationObjectID("Ground", 20);
15-
#print("Done: %r" % (found))
13+
#for block environment
1614

17-
#AirSimClientBase.wait_key('Press any key to change all ground object ID')
18-
#found = client.simSetSegmentationObjectID("ground[\w]*", 22, True);
19-
#print("Done: %r" % (found))
15+
AirSimClientBase.wait_key('Press any key to change one ground object ID')
16+
found = client.simSetSegmentationObjectID("Ground", 20);
17+
print("Done: %r" % (found))
18+
19+
#regex are case insensetive
20+
AirSimClientBase.wait_key('Press any key to change all ground object ID')
21+
found = client.simSetSegmentationObjectID("ground[\w]*", 22, True);
22+
print("Done: %r" % (found))
2023

2124
##for neighbourhood environment
2225

23-
##set object ID for sky
24-
#found = client.simSetSegmentationObjectID("SkySphere", 42, True);
25-
#print("Done: %r" % (found))
26-
27-
#success = client.simSetSegmentationObjectID("[\w]*", 0, True);
28-
#print('success', success)
29-
#success = client.simSetSegmentationObjectID("birch[\w]*", 2, True);
30-
#print('success', success)
31-
#success = client.simSetSegmentationObjectID("fir[\w]*", 2, True);
32-
#print('success', success)
33-
#success = client.simSetSegmentationObjectID("hedge[\w]*", 5, True);
34-
#print('success', success)
35-
#success = client.simSetSegmentationObjectID("tree[\w]*", 2, True);
36-
#print('success', success)
26+
#set object ID for sky
27+
found = client.simSetSegmentationObjectID("SkySphere", 42, True);
28+
print("Done: %r" % (found))
3729

30+
#below doesn't work yet. You must set CustomDepthStencilValue in Unreal Editor for now
3831
AirSimClientBase.wait_key('Press any key to set Landscape object ID to 128')
39-
found = client.simSetSegmentationObjectID("Landscape", 128);
32+
found = client.simSetSegmentationObjectID("[\w]*", 128, True);
4033
print("Done: %r" % (found))
4134

4235
#get segmentation image in various formats

Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ parameters -> camel_case
2323
*/
2424

2525

26-
typedef common_utils::Utils Utils;
27-
2826
bool UAirBlueprintLib::log_messages_hidden = false;
2927

3028
void UAirBlueprintLib::LogMessageString(const std::string &prefix, const std::string &suffix, LogDebugLevel level, float persist_sec)
@@ -129,12 +127,11 @@ void UAirBlueprintLib::FindAllActor(const UObject* context, TArray<AActor*>& fou
129127
}
130128

131129
template<typename T>
132-
void UAirBlueprintLib::InitializeObjectStencilID(T* mesh)
130+
void UAirBlueprintLib::InitializeObjectStencilID(T* mesh, bool ignore_existing)
133131
{
134-
//mesh->SetRenderCustomDepth(true);
135132
std::string mesh_name = GetMeshName(mesh);
136-
if (mesh_name == "") {
137-
//Utils::DebugBreak();
133+
if (mesh_name == "" || common_utils::Utils::startsWith(mesh_name, "Default_")) {
134+
//common_utils::Utils::DebugBreak();
138135
return;
139136
}
140137
FString name(mesh_name.c_str());
@@ -145,11 +142,24 @@ void UAirBlueprintLib::InitializeObjectStencilID(T* mesh)
145142
for (int idx = 0; idx < max_len; ++idx) {
146143
hash += UKismetStringLibrary::GetCharacterAsNumber(name, idx);
147144
}
148-
//if (mesh->CustomDepthStencilValue == 0) { //if value is already set then don't bother
149-
mesh->CustomDepthStencilValue = hash % 256;
150-
//mesh->SetRenderCustomDepth(true);
151-
//mesh->MarkRenderStateDirty();
152-
//}
145+
if (ignore_existing || mesh->CustomDepthStencilValue == 0) { //if value is already set then don't bother
146+
SetObjectStencilID(mesh, hash % 256);
147+
}
148+
}
149+
150+
template<typename T>
151+
void UAirBlueprintLib::SetObjectStencilID(T* mesh, int object_id)
152+
{
153+
mesh->SetCustomDepthStencilValue(object_id);
154+
mesh->SetRenderCustomDepth(true);
155+
//mesh->SetVisibility(false);
156+
//mesh->SetVisibility(true);
157+
}
158+
159+
void UAirBlueprintLib::SetObjectStencilID(ALandscapeProxy* mesh, int object_id)
160+
{
161+
mesh->CustomDepthStencilValue = object_id;
162+
mesh->bRenderCustomDepth = true;
153163
}
154164

155165
template<class T>
@@ -161,7 +171,7 @@ std::string UAirBlueprintLib::GetMeshName(T* mesh)
161171
return ""; // std::string(TCHAR_TO_UTF8(*(UKismetSystemLibrary::GetDisplayName(mesh))));
162172
}
163173

164-
std::string GetMeshName(ALandscapeProxy* mesh)
174+
std::string UAirBlueprintLib::GetMeshName(ALandscapeProxy* mesh)
165175
{
166176
return std::string(TCHAR_TO_UTF8(*(mesh->GetName())));
167177
}
@@ -183,7 +193,7 @@ void UAirBlueprintLib::InitializeMeshStencilIDs()
183193
}
184194

185195
template<typename T>
186-
void UAirBlueprintLib::SetObjectStencilID(T* mesh, int object_id, const std::string& mesh_name, bool is_name_regex,
196+
void UAirBlueprintLib::SetObjectStencilIDIfMatch(T* mesh, int object_id, const std::string& mesh_name, bool is_name_regex,
187197
const std::regex& name_regex, int& changes)
188198
{
189199
std::string comp_mesh_name = GetMeshName(mesh);
@@ -193,11 +203,7 @@ void UAirBlueprintLib::SetObjectStencilID(T* mesh, int object_id, const std::str
193203
|| (is_name_regex && std::regex_match(comp_mesh_name, name_regex));
194204
if (is_match) {
195205
++changes;
196-
mesh->CustomDepthStencilValue = object_id;
197-
//mesh->SetRenderCustomDepth(false);
198-
//mesh->SetRenderCustomDepth(true);
199-
//mesh->SetVisibility(false);
200-
//mesh->SetVisibility(true);
206+
SetObjectStencilID(mesh, object_id);
201207
}
202208
}
203209
bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object_id,
@@ -211,11 +217,11 @@ bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object
211217
int changes = 0;
212218
for (TObjectIterator<UMeshComponent> comp; comp; ++comp)
213219
{
214-
SetObjectStencilID(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
220+
SetObjectStencilIDIfMatch(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
215221
}
216222
for (TObjectIterator<ALandscapeProxy> comp; comp; ++comp)
217223
{
218-
SetObjectStencilID(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
224+
SetObjectStencilIDIfMatch(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
219225
}
220226

221227
return changes > 0;
@@ -300,7 +306,7 @@ void UAirBlueprintLib::FollowActor(AActor* follower, const AActor* followee, con
300306
float dist = (follower->GetActorLocation() - next_location).Size();
301307
float offset_dist = offset.Size();
302308
float dist_offset = (dist - offset_dist) / offset_dist;
303-
float lerp_alpha = Utils::clip((dist_offset*dist_offset) * 0.01f + 0.01f, 0.0f, 1.0f);
309+
float lerp_alpha = common_utils::Utils::clip((dist_offset*dist_offset) * 0.01f + 0.01f, 0.0f, 1.0f);
304310
next_location = FMath::Lerp(follower->GetActorLocation(), next_location, lerp_alpha);
305311
follower->SetActorLocation(next_location);
306312

Unreal/Plugins/AirSim/Source/AirBlueprintLib.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,18 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
8989

9090
private:
9191
template<typename T>
92-
static void InitializeObjectStencilID(T* obj);
92+
static void InitializeObjectStencilID(T* obj, bool ignore_existing = true);
93+
94+
9395
template<typename T>
94-
static void SetObjectStencilID(T* mesh, int object_id,
96+
static void SetObjectStencilIDIfMatch(T* mesh, int object_id,
9597
const std::string& mesh_name, bool is_name_regex, const std::regex& name_regex, int& changes);
9698

99+
template<typename T>
100+
static void SetObjectStencilID(T* mesh, int object_id);
101+
static void SetObjectStencilID(ALandscapeProxy* mesh, int object_id);
102+
103+
97104
private:
98105
static bool log_messages_hidden;
99106
};

0 commit comments

Comments
 (0)