Skip to content

Commit

Permalink
minor edits, implemented a few more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lior Ramati committed Aug 14, 2018
1 parent d072e7d commit 30e7fc9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
21 changes: 13 additions & 8 deletions wrappers/matlab/MatlabParamParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,29 @@ class MatlabParamParser
using unsigned_t = std::integral_constant<mxClassID, mxUINT64_CLASS>;
using value_t = typename std::conditional<bool(std::is_signed<typename std::underlying_type<T>::type>::value), signed_t, unsigned_t>::type;
public:
static const mxClassID value = value_t::value;
// static const mxClassID value = /*value_t::value*/ signed_t::value;
using value = signed_t;
using type = typename std::conditional<std::is_same<value_t, signed_t>::value, int64_t, uint64_t>::type;
};
// pointers are cast to uint64_t because matlab doesn't have a concept of pointers
template <typename T> struct mx_wrapper<T, typename std::enable_if<std::is_pointer<T>::value>::type>
{
static const mxClassID value = mxUINT64_CLASS;
// static const mxClassID value = mxUINT64_CLASS;
using value = std::integral_constant<mxClassID, mxUINT64_CLASS>;
using type = uint64_t;
};
// bools are exposed as matlab's native logical type
template <> struct mx_wrapper<bool, void>
{
static const mxClassID value = mxLOGICAL_CLASS;
// static const mxClassID value = mxLOGICAL_CLASS;
using value = std::integral_constant<mxClassID, mxLOGICAL_CLASS>;
using type = mxLogical;
};
// floating points are exposed as matlab's native double type
template <typename T> struct mx_wrapper<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
{
static const mxClassID value = mxDOUBLE_CLASS;
// static const mxClassID value = mxDOUBLE_CLASS;
using value = std::integral_constant<mxClassID, mxDOUBLE_CLASS>;
using type = double;
};
// integral types are exposed like enums
Expand All @@ -62,7 +66,8 @@ class MatlabParamParser
using unsigned_t = std::integral_constant<mxClassID, mxUINT64_CLASS>;
using value_t = typename std::conditional<std::is_signed<T>::value, signed_t, unsigned_t>::type;
public:
static const mxClassID value = value_t::value;
// static const mxClassID value = value_t::value;
using value = value_t;
using type = typename std::conditional<std::is_same<value_t, signed_t>::value, int64_t, uint64_t>::type;
};
// by default non-basic types are wrapped as pointers
Expand Down Expand Up @@ -143,7 +148,7 @@ template <typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::
using wrapper = mx_wrapper<T>;

// request 1x1 matlab matrix of correct type
mxArray* cell = mxCreateNumericMatrix(1, 1, wrapper::value, mxREAL);
mxArray* cell = mxCreateNumericMatrix(1, 1, wrapper::value::value, mxREAL);
// 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
Expand Down Expand Up @@ -262,7 +267,7 @@ template <typename T> static typename std::enable_if<is_basic_type<T>::value, st

template <typename T> static typename std::enable_if<!is_basic_type<T>::value, mxArray*>::type MatlabParamParser::wrap_array(const T* var, size_t length)
{
auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper<T>::value, mxREAL);
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] = reinterpret_cast<typename mx_wrapper<T>::type>(traits_trampoline::to_internal<T>(T(var[x])));
Expand All @@ -272,7 +277,7 @@ template <typename T> static typename std::enable_if<!is_basic_type<T>::value, m

template <typename T> static typename std::enable_if<is_basic_type<T>::value, mxArray*>::type MatlabParamParser::wrap_array(const T* var, size_t length)
{
auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper<T>::value, mxREAL);
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]);
Expand Down
11 changes: 8 additions & 3 deletions wrappers/matlab/librealsense_mex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ void make_factory(){
auto option = MatlabParamParser::parse<rs2_option>(inv[1]);
outv[0] = MatlabParamParser::wrap(thiz.get_option_description(option));
});
options_factory.record("get_option_description", 1, 3, [](int outc, mxArray* outv[], int inc, const mxArray* inv[])
options_factory.record("get_option_value_description", 1, 3, [](int outc, mxArray* outv[], int inc, const mxArray* inv[])
{
auto thiz = MatlabParamParser::parse<rs2::options>(inv[0]);
auto option = MatlabParamParser::parse<rs2_option>(inv[1]);
Expand Down Expand Up @@ -394,7 +394,10 @@ void make_factory(){
outv[0] = MatlabParamParser::wrap(thiz.is_option_read_only(option));
});
// rs2::options::operator= [?/HOW]
// rs2::options::destructor [?]
options_factory.record("delete", 0, 1, [](int outc, mxArray* outv[], int inc, const mxArray* inv[])
{
MatlabParamParser::destroy<rs2::options>(inv[0]);
});
factory->record(options_factory);
}
{
Expand Down Expand Up @@ -587,7 +590,7 @@ void make_factory(){
outv[0] = MatlabParamParser::wrap(false);
}
});
// rs2::device::as [Pure Matlab]
// rs2::device::as [Can't actually be done as pure matlab because matlab is dumb and handle objects don't work the way I want]
factory->record(device_factory);
}
// rs2::debug_protocol [?]
Expand Down Expand Up @@ -1112,5 +1115,7 @@ void mexFunction(int nOutParams, mxArray *outParams[], int nInParams, const mxAr
f_data.f(nOutParams, outParams, nInParams - 2, inParams + 2); // "eat" the two function specifiers
} catch (std::exception &e) {
mexErrMsgTxt(e.what());
} catch (...) {
mexErrMsgTxt("An unknown error occured");
}
}
59 changes: 42 additions & 17 deletions wrappers/matlab/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,47 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_motion_device_i
mxSetField(cell, 0, "bias_variances", MatlabParamParser::wrap_array(val.bias_variances, 3));

// have to do data field manually for now because of multidimensional array. hope to extend make_array to cover this case
mxArray* data_cell = mxCreateNumericMatrix(3, 4, mxDOUBLE_CLASS, mxREAL);
using data_wrapper_t = mx_wrapper<std::remove_all_extents<decltype(rs2_motion_device_intrinsic::data)>::type>;
mxArray* data_cell = mxCreateNumericMatrix(3, 4, data_wrapper_t::value::value, mxREAL);
auto data_ptr = static_cast<double*>(mxGetData(data_cell));
for (int y = 0; y < 3; ++y) for (int x = 0; x < 4; ++x) data_ptr[y + 3*x] = val.data[y][x];
mxSetField(cell, 0, "data", data_cell);
return cell;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_vector>::wrap(rs2_vector&& val)
{
mexErrMsgTxt("wrap(rs2_vector) Not Implemented yet!");
throw std::runtime_error("wrap(rs2_vector) Not Implemented!");
return nullptr;
using wrapper_t = mx_wrapper<float>;
auto cells = mxCreateNumericMatrix(1, 3, wrapper_t::value::value, mxREAL);
auto ptr = static_cast<typename wrapper_t::type*>(mxGetData(cells));
ptr[0] = wrapper_t::type(val.x);
ptr[1] = wrapper_t::type(val.y);
ptr[2] = wrapper_t::type(val.z);
return cells;
}
template<> static 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);
auto ptr = static_cast<typename wrapper_t::type*>(mxGetData(cells));
ptr[0] = wrapper_t::type(val.x);
ptr[1] = wrapper_t::type(val.y);
ptr[2] = wrapper_t::type(val.z);
ptr[3] = wrapper_t::type(val.w);
return cells;
}
template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2_pose>::wrap(rs2_pose&& val)
{
mexErrMsgTxt("wrap(rs2_pose) Not Implemented yet!");
throw std::runtime_error("wrap(rs2_pose) Not Implemented!");
return nullptr;
const char* fnames[] = { "translation", "velocity", "acceleration", "rotation", "angular_velocity", "angular_acceleration", "tracker_confidence", "mapper_confidence" };
mxArray* cell = mxCreateStructMatrix(1, 1, 8, fnames);
mxSetField(cell, 0, "translation", MatlabParamParser::wrap(std::move(val.translation)));
mxSetField(cell, 0, "velocity", MatlabParamParser::wrap(std::move(val.velocity)));
mxSetField(cell, 0, "acceleration", MatlabParamParser::wrap(std::move(val.acceleration)));
mxSetField(cell, 0, "rotation", MatlabParamParser::wrap(std::move(val.rotation)));
mxSetField(cell, 0, "angular_velocity", MatlabParamParser::wrap(std::move(val.angular_velocity)));
mxSetField(cell, 0, "angular_acceleration", MatlabParamParser::wrap(std::move(val.angular_acceleration)));
mxSetField(cell, 0, "tracker_confidence", MatlabParamParser::wrap(std::move(val.tracker_confidence)));
mxSetField(cell, 0, "mapper_confidence", MatlabParamParser::wrap(std::move(val.mapper_confidence)));
return cell;
}

// rs_types.hpp
Expand Down Expand Up @@ -103,8 +127,8 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns<rs2::device_list>::
for (int i = 0; i < len; ++i)
{
mexLock(); // lock once for each created pointer
outp[i] = reinterpret_cast<uint64_t>(new type_traits<rs2::device_list>::rs2_internal_t(var));
outp[i + len] = i;
outp[0 * len + i] = reinterpret_cast<uint64_t>(new type_traits<rs2::device_list>::rs2_internal_t(var));
outp[1 * len + i] = i;
}

return vec;
Expand Down Expand Up @@ -150,7 +174,8 @@ template<typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::e
{
static mxArray* wrap(T&& var)
{
mxArray *cell = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
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);
rs2_frame_add_ref(inptr, nullptr);
Expand All @@ -174,23 +199,23 @@ template<typename T> struct MatlabParamParser::mx_wrapper_fns<T, typename std::e
template <> static mxArray* MatlabParamParser::wrap_array<rs2::vertex>(const rs2::vertex* var, size_t length)
{
using wrapper_t = mx_wrapper<float>;
auto cells = mxCreateNumericMatrix(3, length, wrapper_t::value, mxREAL);
auto cells = mxCreateNumericMatrix(length, 3, wrapper_t::value::value, mxREAL);
auto ptr = static_cast<typename wrapper_t::type*>(mxGetData(cells));
for (int x = 0; x < length; ++x) {
ptr[3*x + 0] = wrapper_t::type(var[x].x);
ptr[3*x + 1] = wrapper_t::type(var[x].y);
ptr[3*x + 2] = wrapper_t::type(var[x].z);
ptr[0 * length + x] = wrapper_t::type(var[x].x);
ptr[1 * length + x] = wrapper_t::type(var[x].y);
ptr[2 * length + x] = wrapper_t::type(var[x].z);
}
return cells;
}
template <> static mxArray* MatlabParamParser::wrap_array<rs2::texture_coordinate>(const rs2::texture_coordinate* var, size_t length)
{
using wrapper_t = mx_wrapper<float>;
auto cells = mxCreateNumericMatrix(2, length, wrapper_t::value, mxREAL);
auto cells = mxCreateNumericMatrix(length, 2, wrapper_t::value::value, mxREAL);
auto ptr = static_cast<typename wrapper_t::type*>(mxGetData(cells));
for (int x = 0; x < length; ++x) {
ptr[3 * x + 0] = wrapper_t::type(var[x].u);
ptr[3 * x + 1] = wrapper_t::type(var[x].v);
ptr[0 * length + x] = wrapper_t::type(var[x].u);
ptr[1 * length + x] = wrapper_t::type(var[x].v);
}
return cells;
}
Expand Down

0 comments on commit 30e7fc9

Please sign in to comment.