Skip to content

Commit

Permalink
Merge branch 'landscape_bug'
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Nov 23, 2017
2 parents 2a30997 + 3c219bd commit 1dd5956
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 91 deletions.
8 changes: 4 additions & 4 deletions AirLib/include/common/VectorMath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,17 @@ class VectorMathT {
yaw = std::atan2f(t3, t4);
}

static Vector3T toAngularVelocity(const QuaternionT& start, const QuaternionT& end, RealT delta_sec)
static Vector3T toAngularVelocity(const QuaternionT& start, const QuaternionT& end, RealT dt)
{
RealT p_s, r_s, y_s;
toEulerianAngle(start, p_s, r_s, y_s);

RealT p_e, r_e, y_e;
toEulerianAngle(end, p_e, r_e, y_e);

RealT p_rate = (p_e - p_s) / delta_sec;
RealT r_rate = (r_e - r_s) / delta_sec;
RealT y_rate = (y_e - y_s) / delta_sec;
RealT p_rate = (p_e - p_s) / dt;
RealT r_rate = (r_e - r_s) / dt;
RealT y_rate = (y_e - y_s) / dt;

//TODO: optimize below
//Sec 1.3, https://ocw.mit.edu/courses/mechanical-engineering/2-154-maneuvering-and-control-of-surface-and-underwater-vehicles-13-49-fall-2004/lecture-notes/lec1.pdf
Expand Down
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
2 changes: 1 addition & 1 deletion AirLib/include/controllers/VehicleConnectorBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VehicleConnectorBase : public UpdatableObject
//pure abstract methods in addition to UpdatableObject

//called when physics gets updated (must be fast, avoid rendering)
virtual void updateRenderedState() = 0;
virtual void updateRenderedState(float dt) = 0;
//called when render changes are required
virtual void updateRendering(float dt) = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RealMultirotorConnector : public VehicleConnectorBase
{
}

virtual void updateRenderedState() override
virtual void updateRenderedState(float dt) override
{
}

Expand Down
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
27 changes: 12 additions & 15 deletions PythonClient/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,34 @@

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);
print("Done: %r" % (found))

#for block environment

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
##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)
#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("[\w]*", 128, True);
print("Done: %r" % (found))

#get segmentation image in various formats
responses = client.simGetImages([
Expand All @@ -48,16 +45,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
117 changes: 77 additions & 40 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 All @@ -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 @@ -128,31 +126,86 @@ 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, bool ignore_existing)
{
std::string mesh_name = GetMeshName(mesh);
if (mesh_name == "" || common_utils::Utils::startsWith(mesh_name, "Default_")) {
//common_utils::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 (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>
std::string UAirBlueprintLib::GetMeshName(T* 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 UAirBlueprintLib::GetMeshName(ALandscapeProxy* mesh)
{
return std::string( TCHAR_TO_UTF8(*(mesh->GetOwner() ? mesh->GetOwner()->GetName() :
""))); //UKismetSystemLibrary::GetDisplayName(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::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);
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;
SetObjectStencilID(mesh, object_id);
}
}
bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object_id,
bool is_name_regex)
{
Expand All @@ -164,27 +217,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;
}
SetObjectStencilIDIfMatch(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
}
for (TObjectIterator<ALandscapeProxy> comp; comp; ++comp)
{
SetObjectStencilIDIfMatch(*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
}

return changes > 0;
Expand Down Expand Up @@ -269,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
23 changes: 22 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,20 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
log_messages_hidden = is_hidden;
}

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


template<typename T>
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
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
2 changes: 1 addition & 1 deletion Unreal/Plugins/AirSim/Source/CameraDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void ACameraDirector::Tick(float DeltaTime)
Super::Tick(DeltaTime);

if (mode_ == ECameraDirectorMode::CAMERA_DIRECTOR_MODE_MANUAL) {
manual_pose_controller_->updateActorPose();
manual_pose_controller_->updateActorPose(DeltaTime);
}
else if (mode_ == ECameraDirectorMode::CAMERA_DIRECTOR_MODE_SPRINGARM_CHASE) {
//do nothing
Expand Down
Loading

0 comments on commit 1dd5956

Please sign in to comment.