Skip to content

Commit

Permalink
Unity: Update to new Image APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
rajat2004 committed Feb 12, 2021
1 parent 54ad8bb commit 6d011f4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 65 deletions.
56 changes: 0 additions & 56 deletions Unity/AirLibWrapper/AirsimWrapper/Source/PawnSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,6 @@ const NedTransform& PawnSimApi::getNedTransform() const
return ned_transform_;
}

std::vector<PawnSimApi::ImageCaptureBase::ImageResponse> PawnSimApi::getImages(
const std::vector<ImageCaptureBase::ImageRequest>& requests) const
{
std::vector<ImageCaptureBase::ImageResponse> responses;
const ImageCaptureBase* camera = getImageCapture();
camera->getImages(requests, responses);
return responses;
}

std::vector<uint8_t> PawnSimApi::getImage(const std::string& camera_name, ImageCaptureBase::ImageType image_type) const
{
std::vector<ImageCaptureBase::ImageRequest> request = { ImageCaptureBase::ImageRequest(camera_name, image_type) };
const std::vector<ImageCaptureBase::ImageResponse>& response = getImages(request);
if (response.size() > 0)
return response.at(0).image_data_uint8;
else
return std::vector<uint8_t>();
}

msr::airlib::RCData PawnSimApi::getRCData() const
{
AirSimRCData rcDataFromUnity = GetRCData(getVehicleName().c_str());
Expand Down Expand Up @@ -195,43 +176,6 @@ void PawnSimApi::allowPassthroughToggleInput()
PrintLogMessage("enable_passthrough_on_collisions: ", state_.passthrough_enabled ? "true" : "false", params_.vehicle_name.c_str(), ErrorLogSeverity::Information);
}

msr::airlib::CameraInfo PawnSimApi::getCameraInfo(const std::string& camera_name) const
{
AirSimCameraInfo airsim_camera_info = GetCameraInfo(camera_name.c_str(), params_.vehicle_name.c_str()); // Into Unity
msr::airlib::CameraInfo camera_info;
camera_info.pose.position.x() = airsim_camera_info.pose.position.x;
camera_info.pose.position.y() = airsim_camera_info.pose.position.y;
camera_info.pose.position.z() = airsim_camera_info.pose.position.z;
camera_info.pose.orientation.x() = airsim_camera_info.pose.orientation.x;
camera_info.pose.orientation.y() = airsim_camera_info.pose.orientation.y;
camera_info.pose.orientation.z() = airsim_camera_info.pose.orientation.z;
camera_info.pose.orientation.w() = airsim_camera_info.pose.orientation.w;
camera_info.fov = airsim_camera_info.fov;
return camera_info;
}

void PawnSimApi::setCameraPose(const std::string& camera_name, const msr::airlib::Pose& pose)
{
SetCameraPose(camera_name.c_str(), UnityUtilities::Convert_to_AirSimPose(pose), params_.vehicle_name.c_str());
}

void PawnSimApi::setCameraFoV(const std::string& camera_name, float fov_degrees)
{
SetCameraFoV(camera_name.c_str(), fov_degrees, params_.vehicle_name.c_str());
}

void PawnSimApi::setDistortionParam(const std::string& camera_name, const std::string& param_name, float value)
{
// not implemented
}

std::vector<float> PawnSimApi::getDistortionParams(const std::string& camera_name) const
{
// not implemented
std::vector<float> params(5, 0.0);
return params;
}


//parameters in NED frame
PawnSimApi::Pose PawnSimApi::getPose() const
Expand Down
8 changes: 0 additions & 8 deletions Unity/AirLibWrapper/AirsimWrapper/Source/PawnSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class PawnSimApi : public msr::airlib::VehicleSimApiBase
typedef msr::airlib::real_T real_T;
typedef msr::airlib::Utils Utils;
typedef msr::airlib::AirSimSettings::VehicleSetting VehicleSetting;
typedef msr::airlib::ImageCaptureBase ImageCaptureBase;

public:
struct Params {
Expand Down Expand Up @@ -67,15 +66,8 @@ class PawnSimApi : public msr::airlib::VehicleSimApiBase
virtual void resetImplementation() override;
virtual void update() override;
virtual const UnityImageCapture* getImageCapture() const override;
virtual std::vector<ImageCaptureBase::ImageResponse> getImages(const std::vector<ImageCaptureBase::ImageRequest>& request) const override;
virtual std::vector<uint8_t> getImage(const std::string& camera_name, ImageCaptureBase::ImageType image_type) const override;
virtual Pose getPose() const override;
virtual void setPose(const Pose& pose, bool ignore_collision) override;
virtual msr::airlib::CameraInfo getCameraInfo(const std::string& camera_name) const override;
virtual void setCameraPose(const std::string& camera_name, const Pose& pose) override;
virtual void setCameraFoV(const std::string& camera_name, float fov_degrees) override;
virtual void setDistortionParam(const std::string& camera_name, const std::string& param_name, float value) override;
virtual std::vector<float> getDistortionParams(const std::string& camera_name) const override;
virtual CollisionInfo getCollisionInfo() const override;
virtual int getRemoteControlID() const override;
virtual msr::airlib::RCData getRCData() const override;
Expand Down
64 changes: 64 additions & 0 deletions Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,68 @@ void WorldSimApi::setWind(const Vector3r& wind) const
simmode_->setWind(wind);
};

msr::airlib::CameraInfo WorldSimApi::getCameraInfo(const std::string& camera_name, const std::string& vehicle_name, bool external) const
{
if (external)
throw std::invalid_argument(common_utils::Utils::stringf("external field is not supported on Unity Image APIs").c_str());

AirSimCameraInfo airsim_camera_info = GetCameraInfo(camera_name.c_str(), vehicle_name.c_str()); // Into Unity
msr::airlib::CameraInfo camera_info;
camera_info.pose = UnityUtilities::Convert_to_Pose(airsim_camera_info.pose);
camera_info.fov = airsim_camera_info.fov;
return camera_info;
}

void WorldSimApi::setCameraPose(const std::string& camera_name, const msr::airlib::Pose& pose,
const std::string& vehicle_name, bool external)
{
if (external)
throw std::invalid_argument(common_utils::Utils::stringf("external field is not supported on Unity Image APIs").c_str());

SetCameraPose(camera_name.c_str(), UnityUtilities::Convert_to_AirSimPose(pose), vehicle_name.c_str());
}

void WorldSimApi::setCameraFoV(const std::string& camera_name, float fov_degrees,
const std::string& vehicle_name, bool external)
{
if (external)
throw std::invalid_argument(common_utils::Utils::stringf("external field is not supported on Unity Image APIs").c_str());

SetCameraFoV(camera_name.c_str(), fov_degrees, vehicle_name.c_str());
}

void WorldSimApi::setDistortionParam(const std::string& camera_name, const std::string& param_name, float value,
const std::string& vehicle_name, bool external)
{
throw std::invalid_argument(common_utils::Utils::stringf("setDistortionParam is not supported on unity").c_str());
}

std::vector<float> WorldSimApi::getDistortionParams(const std::string& camera_name, const std::string& vehicle_name, bool external) const
{
throw std::invalid_argument(common_utils::Utils::stringf("getDistortionParams is not supported on unity").c_str());

std::vector<float> params(5, 0.0);
return params;
}

std::vector<WorldSimApi::ImageCaptureBase::ImageResponse> WorldSimApi::getImages(
const std::vector<ImageCaptureBase::ImageRequest>& requests, const std::string& vehicle_name, bool external) const
{
std::vector<ImageCaptureBase::ImageResponse> responses;
const ImageCaptureBase* camera = simmode_->getVehicleSimApi(vehicle_name)->getImageCapture();
camera->getImages(requests, responses);
return responses;
}

std::vector<uint8_t> WorldSimApi::getImage(const std::string& camera_name, ImageCaptureBase::ImageType image_type,
const std::string& vehicle_name, bool external) const
{
std::vector<ImageCaptureBase::ImageRequest> request{ ImageCaptureBase::ImageRequest(camera_name, image_type) };
const auto& response = getImages(request);
if (response.size() > 0)
return response.at(0).image_data_uint8;
else
return std::vector<uint8_t>();
}

#pragma endregion
19 changes: 18 additions & 1 deletion Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "api/WorldSimApiBase.hpp"
#include "./SimMode/SimModeBase.h"
#include "SimMode/SimModeBase.h"
#include "AirSimStructs.hpp"

class WorldSimApi : public msr::airlib::WorldSimApiBase
Expand All @@ -10,6 +10,7 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase
typedef msr::airlib::Pose Pose;
typedef msr::airlib::Vector3r Vector3r;
typedef msr::airlib::MeshPositionVertexBuffersResponse MeshPositionVertexBuffersResponse;
typedef msr::airlib::ImageCaptureBase ImageCaptureBase;

WorldSimApi(SimModeBase* simmode, std::string vehicle_name);
virtual ~WorldSimApi();
Expand Down Expand Up @@ -65,6 +66,22 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase
virtual void setWind(const Vector3r& wind) const override;
virtual bool createVoxelGrid(const Vector3r& position, const int& x_size, const int& y_size, const int& z_size, const float& res, const std::string& output_file) override;

// Image APIs
virtual msr::airlib::CameraInfo getCameraInfo(const std::string& camera_name, const std::string& vehicle_name = "", bool external = false) const override;
virtual void setCameraPose(const std::string& camera_name, const msr::airlib::Pose& pose,
const std::string& vehicle_name = "", bool external = false) override;
virtual void setCameraFoV(const std::string& camera_name, float fov_degrees,
const std::string& vehicle_name = "", bool external = false) override;
virtual void setDistortionParam(const std::string& camera_name, const std::string& param_name, float value,
const std::string& vehicle_name = "", bool external = false) override;
virtual std::vector<float> getDistortionParams(const std::string& camera_name, const std::string& vehicle_name = "",
bool external = false) const override;

virtual std::vector<ImageCaptureBase::ImageResponse> getImages(const std::vector<ImageCaptureBase::ImageRequest>& requests,
const std::string& vehicle_name = "", bool external = false) const override;
virtual std::vector<uint8_t> getImage(const std::string& camera_name, msr::airlib::ImageCaptureBase::ImageType image_type,
const std::string& vehicle_name = "", bool external = false) const override;

private:
SimModeBase * simmode_;
std::string vehicle_name_;
Expand Down

0 comments on commit 6d011f4

Please sign in to comment.