Skip to content

Commit

Permalink
segmentation stencil IDs enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Nov 23, 2017
1 parent 7153b66 commit 3c219bd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
4 changes: 4 additions & 0 deletions AirLib/include/common/common_utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class Utils {
return static_cast<float>(radians * 180.0f / M_PI);
}

static bool startsWith(const string& s, const string& prefix) {
return s.size() <= prefix.size() && s.compare(0, prefix.size(), prefix) == 0;
}

static Logger* getSetLogger(Logger* logger = nullptr)
{
static Logger logger_default_;
Expand Down
41 changes: 17 additions & 24 deletions PythonClient/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,30 @@
client = MultirotorClient()
client.confirmConnection()

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

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

#AirSimClientBase.wait_key('Press any key to change all ground object ID')
#found = client.simSetSegmentationObjectID("ground[\w]*", 22, True);
#print("Done: %r" % (found))
AirSimClientBase.wait_key('Press any key to change one ground object ID')
found = client.simSetSegmentationObjectID("Ground", 20);
print("Done: %r" % (found))

#regex are case insensetive
AirSimClientBase.wait_key('Press any key to change all ground object ID')
found = client.simSetSegmentationObjectID("ground[\w]*", 22, True);
print("Done: %r" % (found))

##for neighbourhood environment

##set object ID for sky
#found = client.simSetSegmentationObjectID("SkySphere", 42, True);
#print("Done: %r" % (found))

#success = client.simSetSegmentationObjectID("[\w]*", 0, True);
#print('success', success)
#success = client.simSetSegmentationObjectID("birch[\w]*", 2, True);
#print('success', success)
#success = client.simSetSegmentationObjectID("fir[\w]*", 2, True);
#print('success', success)
#success = client.simSetSegmentationObjectID("hedge[\w]*", 5, True);
#print('success', success)
#success = client.simSetSegmentationObjectID("tree[\w]*", 2, True);
#print('success', success)
#set object ID for sky
found = client.simSetSegmentationObjectID("SkySphere", 42, True);
print("Done: %r" % (found))

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

#get segmentation image in various formats
Expand Down
48 changes: 27 additions & 21 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ parameters -> camel_case
*/


typedef common_utils::Utils Utils;

bool UAirBlueprintLib::log_messages_hidden = false;

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

template<typename T>
void UAirBlueprintLib::InitializeObjectStencilID(T* mesh)
void UAirBlueprintLib::InitializeObjectStencilID(T* mesh, bool ignore_existing)
{
//mesh->SetRenderCustomDepth(true);
std::string mesh_name = GetMeshName(mesh);
if (mesh_name == "") {
//Utils::DebugBreak();
if (mesh_name == "" || common_utils::Utils::startsWith(mesh_name, "Default_")) {
//common_utils::Utils::DebugBreak();
return;
}
FString name(mesh_name.c_str());
Expand All @@ -145,11 +142,24 @@ void UAirBlueprintLib::InitializeObjectStencilID(T* mesh)
for (int idx = 0; idx < max_len; ++idx) {
hash += UKismetStringLibrary::GetCharacterAsNumber(name, idx);
}
//if (mesh->CustomDepthStencilValue == 0) { //if value is already set then don't bother
mesh->CustomDepthStencilValue = hash % 256;
//mesh->SetRenderCustomDepth(true);
//mesh->MarkRenderStateDirty();
//}
if (ignore_existing || mesh->CustomDepthStencilValue == 0) { //if value is already set then don't bother
SetObjectStencilID(mesh, hash % 256);
}
}

template<typename T>
void UAirBlueprintLib::SetObjectStencilID(T* mesh, int object_id)
{
mesh->SetCustomDepthStencilValue(object_id);
mesh->SetRenderCustomDepth(true);
//mesh->SetVisibility(false);
//mesh->SetVisibility(true);
}

void UAirBlueprintLib::SetObjectStencilID(ALandscapeProxy* mesh, int object_id)
{
mesh->CustomDepthStencilValue = object_id;
mesh->bRenderCustomDepth = true;
}

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

std::string GetMeshName(ALandscapeProxy* mesh)
std::string UAirBlueprintLib::GetMeshName(ALandscapeProxy* mesh)
{
return std::string(TCHAR_TO_UTF8(*(mesh->GetName())));
}
Expand All @@ -183,7 +193,7 @@ void UAirBlueprintLib::InitializeMeshStencilIDs()
}

template<typename T>
void UAirBlueprintLib::SetObjectStencilID(T* mesh, int object_id, const std::string& mesh_name, bool is_name_regex,
void UAirBlueprintLib::SetObjectStencilIDIfMatch(T* mesh, int object_id, const std::string& mesh_name, bool is_name_regex,
const std::regex& name_regex, int& changes)
{
std::string comp_mesh_name = GetMeshName(mesh);
Expand All @@ -193,11 +203,7 @@ void UAirBlueprintLib::SetObjectStencilID(T* mesh, int object_id, const std::str
|| (is_name_regex && std::regex_match(comp_mesh_name, name_regex));
if (is_match) {
++changes;
mesh->CustomDepthStencilValue = object_id;
//mesh->SetRenderCustomDepth(false);
//mesh->SetRenderCustomDepth(true);
//mesh->SetVisibility(false);
//mesh->SetVisibility(true);
SetObjectStencilID(mesh, object_id);
}
}
bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object_id,
Expand All @@ -211,11 +217,11 @@ bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object
int changes = 0;
for (TObjectIterator<UMeshComponent> comp; comp; ++comp)
{
SetObjectStencilID(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
SetObjectStencilIDIfMatch(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
}
for (TObjectIterator<ALandscapeProxy> comp; comp; ++comp)
{
SetObjectStencilID(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
SetObjectStencilIDIfMatch(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
}

return changes > 0;
Expand Down Expand Up @@ -300,7 +306,7 @@ void UAirBlueprintLib::FollowActor(AActor* follower, const AActor* followee, con
float dist = (follower->GetActorLocation() - next_location).Size();
float offset_dist = offset.Size();
float dist_offset = (dist - offset_dist) / offset_dist;
float lerp_alpha = Utils::clip((dist_offset*dist_offset) * 0.01f + 0.01f, 0.0f, 1.0f);
float lerp_alpha = common_utils::Utils::clip((dist_offset*dist_offset) * 0.01f + 0.01f, 0.0f, 1.0f);
next_location = FMath::Lerp(follower->GetActorLocation(), next_location, lerp_alpha);
follower->SetActorLocation(next_location);

Expand Down
11 changes: 9 additions & 2 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary

private:
template<typename T>
static void InitializeObjectStencilID(T* obj);
static void InitializeObjectStencilID(T* obj, bool ignore_existing = true);


template<typename T>
static void SetObjectStencilID(T* mesh, int object_id,
static void SetObjectStencilIDIfMatch(T* mesh, int object_id,
const std::string& mesh_name, bool is_name_regex, const std::regex& name_regex, int& changes);

template<typename T>
static void SetObjectStencilID(T* mesh, int object_id);
static void SetObjectStencilID(ALandscapeProxy* mesh, int object_id);


private:
static bool log_messages_hidden;
};
Expand Down

0 comments on commit 3c219bd

Please sign in to comment.