Skip to content

Commit

Permalink
Landscape issue (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Nov 22, 2017
1 parent 09c884a commit 7153b66
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 71 deletions.
2 changes: 1 addition & 1 deletion PythonClient/PythonClient.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>e2049e20-b6dd-474e-8bca-1c8dc54725aa</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>hello_car.py</StartupFile>
<StartupFile>segmentation.py</StartupFile>
<SearchPath>
</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
Expand Down
66 changes: 35 additions & 31 deletions PythonClient/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@

from AirSimClient import *

client = CarClient()
client = MultirotorClient()
client.confirmConnection()

AirSimClientBase.wait_key('Press any key to set all object IDs to 0')
found = client.simSetSegmentationObjectID("[\w]*", 0, True);
#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))

#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)

AirSimClientBase.wait_key('Press any key to set Landscape object ID to 128')
found = client.simSetSegmentationObjectID("Landscape", 128);
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))

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)

#get segmentation image in various formats
responses = client.simGetImages([
ImageRequest(0, AirSimImageType.Segmentation), #depth visualiztion image
Expand All @@ -48,16 +52,16 @@

if response.pixels_as_float:
print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
AirSimClientBase.write_pfm(os.path.normpath(filename + '.pfm'), AirSimClientBase.getPfmArray(response))
#AirSimClientBase.write_pfm(os.path.normpath(filename + '.pfm'), AirSimClientBase.getPfmArray(response))
elif response.compress: #png format
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
AirSimClientBase.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
#AirSimClientBase.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
else: #uncompressed array - numpy demo
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is fliped vertically
AirSimClientBase.write_png(os.path.normpath(filename + '.numpy.png'), img_rgba) #write to png
#AirSimClientBase.write_png(os.path.normpath(filename + '.numpy.png'), img_rgba) #write to png

#find unique colors
print(np.unique(img_rgba[:,:,0], return_counts=True)) #red
Expand Down
105 changes: 68 additions & 37 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include "Kismet/GameplayStatics.h"
#include "GameFramework/RotatingMovementComponent.h"
#include <exception>
#include <regex>
#include "common/common_utils/Utils.hpp"
#include "Components/StaticMeshComponent.h"
#include "EngineUtils.h"
#include "UObjectIterator.h"
//#include "Runtime/Foliage/Public/FoliageType.h"
#include "Kismet/KismetStringLibrary.h"
#include "Engine/Engine.h"

Expand Down Expand Up @@ -128,31 +128,78 @@ void UAirBlueprintLib::FindAllActor(const UObject* context, TArray<AActor*>& fou
UGameplayStatics::GetAllActorsOfClass(context == nullptr ? GEngine : context, T::StaticClass(), foundActors);
}

std::string UAirBlueprintLib::GetMeshName(UMeshComponent* mesh)
template<typename T>
void UAirBlueprintLib::InitializeObjectStencilID(T* mesh)
{
//mesh->SetRenderCustomDepth(true);
std::string mesh_name = GetMeshName(mesh);
if (mesh_name == "") {
//Utils::DebugBreak();
return;
}
FString name(mesh_name.c_str());
int hash = 5;
int max_len = name.Len() - name.Len() / 4; //remove training numerical suffixes
if (max_len < 3)
max_len = name.Len();
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();
//}
}

template<class T>
std::string UAirBlueprintLib::GetMeshName(T* mesh)
{
return std::string( TCHAR_TO_UTF8(*(mesh->GetOwner() ? mesh->GetOwner()->GetName() :
""))); //UKismetSystemLibrary::GetDisplayName(mesh)
if (mesh->GetOwner())
return std::string(TCHAR_TO_UTF8(*(mesh->GetOwner()->GetName())));
else
return ""; // std::string(TCHAR_TO_UTF8(*(UKismetSystemLibrary::GetDisplayName(mesh))));
}

std::string GetMeshName(ALandscapeProxy* mesh)
{
return std::string(TCHAR_TO_UTF8(*(mesh->GetName())));
}

void UAirBlueprintLib::InitializeMeshStencilIDs()
{
for (TObjectIterator<UMeshComponent> comp; comp; ++comp)
{
UMeshComponent *mesh = *comp;
mesh->SetRenderCustomDepth(true);
std::string mesh_name = GetMeshName(mesh);
if (mesh_name == "")
continue;
FString name(mesh_name.c_str());
int hash = 0;
for (int idx = 0; idx < name.Len() && idx < 3; ++idx) {
hash += UKismetStringLibrary::GetCharacterAsNumber(name, idx);
}
mesh->CustomDepthStencilValue = hash % 256;
mesh->MarkRenderStateDirty();
InitializeObjectStencilID(*comp);
}
//for (TObjectIterator<UFoliageType> comp; comp; ++comp)
//{
// InitializeObjectStencilID(*comp);
//}
for (TObjectIterator<ALandscapeProxy> comp; comp; ++comp)
{
InitializeObjectStencilID(*comp);
}
}

template<typename T>
void UAirBlueprintLib::SetObjectStencilID(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);
if (comp_mesh_name == "")
return;
bool is_match = (!is_name_regex && (comp_mesh_name == mesh_name))
|| (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);
}
}
bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object_id,
bool is_name_regex)
{
Expand All @@ -164,27 +211,11 @@ bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object
int changes = 0;
for (TObjectIterator<UMeshComponent> comp; comp; ++comp)
{
UMeshComponent *mesh = *comp;

std::string comp_mesh_name = GetMeshName(mesh);
if (comp_mesh_name == "")
continue;

bool is_match = (!is_name_regex && (comp_mesh_name == mesh_name))
|| (is_name_regex && std::regex_match(comp_mesh_name, name_regex));

if (is_match) {
++changes;
//mesh->SetRenderCustomDepth(false);
//mesh->SetRenderCustomDepth(true);
mesh->CustomDepthStencilValue = object_id;
//mesh->SetVisibility(false);
//mesh->SetVisibility(true);
mesh->MarkRenderStateDirty();

//if (! is_name_regex)
// return true;
}
SetObjectStencilID(*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);
}

return changes > 0;
Expand Down
16 changes: 15 additions & 1 deletion Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
#include "Components/InputComponent.h"
#include "GameFramework/PlayerInput.h"
#include <string>
#include <regex>
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Kismet/KismetMathLibrary.h"
#include "Components/MeshComponent.h"
#include "LandscapeProxy.h"
#include "AirBlueprintLib.generated.h"


UENUM(BlueprintType)
enum class LogDebugLevel : uint8 {
Informational UMETA(DisplayName="Informational"),
Expand Down Expand Up @@ -49,7 +52,11 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
bool is_name_regex = false);
static int GetMeshStencilID(const std::string& mesh_name);
static void InitializeMeshStencilIDs();
static std::string GetMeshName(UMeshComponent* mesh);

template<class T>
static std::string GetMeshName(T* mesh);
static std::string GetMeshName(ALandscapeProxy* mesh);


template<class UserClass>
static FInputActionBinding& BindActionToKey(const FName action_name, const FKey in_key, UserClass* actor,
Expand Down Expand Up @@ -80,6 +87,13 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
log_messages_hidden = is_hidden;
}

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

private:
static bool log_messages_hidden;
};
Expand Down
2 changes: 1 addition & 1 deletion Unreal/Plugins/AirSim/Source/AirSim.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public AirSim(ReadOnlyTargetRules Target) : base(Target)
//below is no longer supported in 4.16
bEnableExceptions = true;

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ImageWrapper", "RenderCore", "RHI", "PhysXVehicles" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ImageWrapper", "RenderCore", "RHI", "PhysXVehicles", "Landscape" });
PrivateDependencyModuleNames.AddRange(new string[] { "UMG", "Slate", "SlateCore" });

//suppress VC++ proprietary warnings
Expand Down

0 comments on commit 7153b66

Please sign in to comment.