Skip to content

Commit

Permalink
Add patch supplied by Christian Diaz of TUM to enable compilation on …
Browse files Browse the repository at this point in the history
…additional platforms
  • Loading branch information
Lior Ramati committed Oct 24, 2018
1 parent 853c42f commit 8267753
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
35 changes: 17 additions & 18 deletions wrappers/matlab/MatlabParamParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
#include <array>
#include <type_traits>

namespace std
{
template <bool B> using bool_constant = integral_constant<bool, B>;
}

template <typename T> using is_basic_type = std::bool_constant<std::is_arithmetic<T>::value || std::is_pointer<T>::value || std::is_enum<T>::value>;
template <typename T> struct is_array_type : std::false_type {};
template <typename T> struct is_array_type<std::vector<T>> : std::true_type { using inner_t = T; };

// TODO: consider turning MatlabParamParser into a namespace. Might help make lots of things cleaner. can keep things hidden via nested namespace MatlabParamParser::detail
class MatlabParamParser
// TODO: consider using nested impl/detail namespace
namespace MatlabParamParser
{
private:
// in charge of which overloads to use

// helper information for overloaded functions
Expand Down Expand Up @@ -106,14 +110,12 @@ class MatlabParamParser
};
// by default non-basic types are wrapped as pointers
template <typename T> struct mx_wrapper<T, typename std::enable_if<!is_basic_type<T>::value>::type> : mx_wrapper<void*> {};
public:
template <typename T, typename = void> struct type_traits {
// KEEP THESE COMMENTED. They are only here to show the signature should you choose
// to declare these functions
// KEEP THESE COMMENTED. They are only here to show the signature
// should you choose to declare these functions
// static rs2_internal_t* to_internal(T&& val);
// static T from_internal(rs2_internal_t* ptr);
};
private:
struct traits_trampoline {
private:
template <typename T> struct detector {
Expand Down Expand Up @@ -148,15 +150,12 @@ class MatlabParamParser

// selected if type_traits<T>::from_internal exists
template <typename T> static typename std::enable_if<detector<T>::has_from, T>::type
from_internal(typename internal_t<T>* ptr) { return type_traits<T>::from_internal(ptr); }
from_internal(internal_t<T>* ptr) { return type_traits<T>::from_internal(ptr); }
// selected if it doesnt
template <typename T> static typename std::enable_if<!detector<T>::has_from, T>::type
from_internal(typename internal_t<T>* ptr) { return T(*ptr); }
from_internal(internal_t<T>* ptr) { return T(*ptr); }
template <typename T> using use_cells = std::integral_constant<bool, detector<T>::use_cells>;
};
public:
MatlabParamParser() {};
~MatlabParamParser() {};

// TODO: try/catch->err msg?
template <typename T> static T parse(const mxArray* cell) { return mx_wrapper_fns<T>::parse(cell); }
Expand Down Expand Up @@ -196,7 +195,7 @@ template <typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::
// access matrix's data as pointer to correct C++ type
auto *outp = static_cast<typename wrapper::type*>(mxGetData(cell));
// cast object to correct C++ type and then store it in the matrix
*outp = wrapper::type(var);
*outp = typename wrapper::type(var);
return cell;
}
static void destroy(const mxArray* cell)
Expand Down Expand Up @@ -248,19 +247,19 @@ template<typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::e
};

// overload for wrapping C-strings. TODO: do we need special parsing too?
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<const char *>::wrap(const char*&& str)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<const char *>::wrap(const char*&& str)
{
return mxCreateString(str);
}

template<> static std::string MatlabParamParser::mx_wrapper_fns<std::string>::parse(const mxArray* cell)
template<> std::string MatlabParamParser::mx_wrapper_fns<std::string>::parse(const mxArray* cell)
{
auto str = mxArrayToString(cell);
auto ret = std::string(str);
mxFree(str);
return ret;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<std::string>::wrap(std::string&& str)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<std::string>::wrap(std::string&& str)
{
return mx_wrapper_fns<const char*>::wrap(str.c_str());
}
Expand Down Expand Up @@ -334,8 +333,8 @@ template <typename T> static typename std::enable_if<is_basic_type<T>::value, mx
auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper<T>::value::value, mxREAL);
auto ptr = static_cast<typename mx_wrapper<T>::type*>(mxGetData(cells));
for (int x = 0; x < length; ++x)
ptr[x] = mx_wrapper<T>::type(var[x]);
ptr[x] = typename mx_wrapper<T>::type(var[x]);

return cells;
}
#include "types.h"
#include "types.h"
30 changes: 15 additions & 15 deletions wrappers/matlab/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include <array>

// C API
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_extrinsics>::wrap(rs2_extrinsics&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2_extrinsics>::wrap(rs2_extrinsics&& val)
{
const char* fnames[] = { "rotation", "translation" };
mxArray* cell = mxCreateStructMatrix(1, 1, 2, fnames);
mxSetField(cell, 0, "rotation", MatlabParamParser::wrap_array(val.rotation, 9));
mxSetField(cell, 0, "translation", MatlabParamParser::wrap_array(val.translation, 3));
return cell;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_intrinsics>::wrap(rs2_intrinsics&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2_intrinsics>::wrap(rs2_intrinsics&& val)
{
const char* fnames[] = { "width", "height", "ppx", "ppy", "fx", "fy", "model", "coeffs" };
mxArray* cell = mxCreateStructMatrix(1, 1, 8, fnames);
Expand All @@ -26,7 +26,7 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_intrinsics>::wr
mxSetField(cell, 0, "coeffs", MatlabParamParser::wrap_array(val.coeffs, 5));
return cell;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_motion_device_intrinsic>::wrap(rs2_motion_device_intrinsic&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2_motion_device_intrinsic>::wrap(rs2_motion_device_intrinsic&& val)
{
const char* fnames[] = { "data", "noise_variances", "bias_variances"};
mxArray* cell = mxCreateStructMatrix(1, 1, 3, fnames);
Expand All @@ -41,7 +41,7 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_motion_device_i
mxSetField(cell, 0, "data", data_cell);
return cell;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_vector>::wrap(rs2_vector&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2_vector>::wrap(rs2_vector&& val)
{
using wrapper_t = mx_wrapper<float>;
auto cells = mxCreateNumericMatrix(1, 3, wrapper_t::value::value, mxREAL);
Expand All @@ -51,7 +51,7 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_vector>::wrap(r
ptr[2] = wrapper_t::type(val.z);
return cells;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_quaternion>::wrap(rs2_quaternion&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2_quaternion>::wrap(rs2_quaternion&& val)
{
using wrapper_t = mx_wrapper<decltype(rs2_quaternion::x)>;
auto cells = mxCreateNumericMatrix(1, 4, wrapper_t::value::value, mxREAL);
Expand All @@ -62,7 +62,7 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_quaternion>::wr
ptr[3] = wrapper_t::type(val.w);
return cells;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_pose>::wrap(rs2_pose&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2_pose>::wrap(rs2_pose&& val)
{
const char* fnames[] = { "translation", "velocity", "acceleration", "rotation", "angular_velocity", "angular_acceleration", "tracker_confidence", "mapper_confidence" };
mxArray* cell = mxCreateStructMatrix(1, 1, 8, fnames);
Expand All @@ -78,7 +78,7 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_pose>::wrap(rs2
}

// rs_types.hpp
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2::option_range>::wrap(rs2::option_range&& val)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2::option_range>::wrap(rs2::option_range&& val)
{
const char* fnames[] = { "min", "max", "step", "def" };
mxArray* cell = mxCreateStructMatrix(1, 1, 4, fnames);
Expand Down Expand Up @@ -116,7 +116,7 @@ template<> struct MatlabParamParser::mx_wrapper_fns<rs2::region_of_interest>
// rs_record_playback.hpp

// rs_device.hpp
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2::device_list>::wrap(rs2::device_list&& var)
template<> mxArray* MatlabParamParser::mx_wrapper_fns<rs2::device_list>::wrap(rs2::device_list&& var)
{
// Device list is sent as a native array of (ptr, id) pairs to preserve lazy instantiation of devices
size_t len = var.size();
Expand Down Expand Up @@ -156,7 +156,7 @@ template<typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::e
auto cells = mxCreateCellMatrix(1, 2);
auto handle_cell = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
auto handle_ptr = static_cast<uint64_t*>(mxGetData(cells));
*handle_ptr = reinterpret_cast<uint64_t>(type_traits<T>::rs2_internal_t(val));
*handle_ptr = reinterpret_cast<uint64_t>(typename type_traits<T>::rs2_internal_t(val));

auto own_cell = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
auto own_ptr = static_cast<uint64_t*>(mxGetData(cells));
Expand Down Expand Up @@ -191,26 +191,26 @@ template<typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::e
using wrapper_t = mx_wrapper<T>;
mxArray *cell = mxCreateNumericMatrix(1, 1, wrapper_t::value::value, mxREAL);
auto *outp = static_cast<uint64_t*>(mxGetData(cell));
auto inptr = type_traits<T>::rs2_internal_t(var);
auto inptr = typename type_traits<T>::rs2_internal_t(var);
rs2_frame_add_ref(inptr, nullptr);
mexLock(); // Wrapper holds a reference to the internal type, which won't get destroyed until the wrapper lets go
*outp = reinterpret_cast<uint64_t>(inptr);
return cell;
}
static T parse(const mxArray* cell)
{
auto inptr = mx_wrapper_fns<type_traits<T>::rs2_internal_t>::parse(cell);
auto inptr = mx_wrapper_fns<typename type_traits<T>::rs2_internal_t>::parse(cell);
rs2_frame_add_ref(inptr, nullptr);
return T(inptr);
}
static void destroy(const mxArray* cell)
{
auto inptr = mx_wrapper_fns<type_traits<T>::rs2_internal_t>::parse(cell);
auto inptr = mx_wrapper_fns<typename type_traits<T>::rs2_internal_t>::parse(cell);
rs2_release_frame(inptr);
mexUnlock(); // Wrapper holds a reference to the internal type, which won't get destroyed until the wrapper lets go
}
};
template <> static mxArray* MatlabParamParser::wrap_array<rs2::vertex>(const rs2::vertex* var, size_t length)
template <> mxArray* MatlabParamParser::wrap_array<rs2::vertex>(const rs2::vertex* var, size_t length)
{
using wrapper_t = mx_wrapper<float>;
auto cells = mxCreateNumericMatrix(length, 3, wrapper_t::value::value, mxREAL);
Expand All @@ -222,7 +222,7 @@ template <> static mxArray* MatlabParamParser::wrap_array<rs2::vertex>(const rs2
}
return cells;
}
template <> static mxArray* MatlabParamParser::wrap_array<rs2::texture_coordinate>(const rs2::texture_coordinate* var, size_t length)
template <> mxArray* MatlabParamParser::wrap_array<rs2::texture_coordinate>(const rs2::texture_coordinate* var, size_t length)
{
using wrapper_t = mx_wrapper<float>;
auto cells = mxCreateNumericMatrix(length, 2, wrapper_t::value::value, mxREAL);
Expand All @@ -235,7 +235,7 @@ template <> static mxArray* MatlabParamParser::wrap_array<rs2::texture_coordinat
}

// rs_processing.hpp
template<> static rs2::process_interface* MatlabParamParser::mx_wrapper_fns<rs2::process_interface*>::parse(const mxArray* cell)
template<> rs2::process_interface* MatlabParamParser::mx_wrapper_fns<rs2::process_interface*>::parse(const mxArray* cell)
{
using traits_t = type_traits<rs2::process_interface>;
auto ptr = static_cast<traits_t::rs2_internal_t*>(mxGetData(cell));
Expand Down

0 comments on commit 8267753

Please sign in to comment.