diff --git a/wrappers/matlab/MatlabParamParser.h b/wrappers/matlab/MatlabParamParser.h index 5be51f40f4..d7dc60b7b3 100644 --- a/wrappers/matlab/MatlabParamParser.h +++ b/wrappers/matlab/MatlabParamParser.h @@ -33,25 +33,29 @@ class MatlabParamParser using unsigned_t = std::integral_constant; using value_t = typename std::conditional::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::value, int64_t, uint64_t>::type; }; // pointers are cast to uint64_t because matlab doesn't have a concept of pointers template struct mx_wrapper::value>::type> { - static const mxClassID value = mxUINT64_CLASS; + // static const mxClassID value = mxUINT64_CLASS; + using value = std::integral_constant; using type = uint64_t; }; // bools are exposed as matlab's native logical type template <> struct mx_wrapper { - static const mxClassID value = mxLOGICAL_CLASS; +// static const mxClassID value = mxLOGICAL_CLASS; + using value = std::integral_constant; using type = mxLogical; }; // floating points are exposed as matlab's native double type template struct mx_wrapper::value>::type> { - static const mxClassID value = mxDOUBLE_CLASS; +// static const mxClassID value = mxDOUBLE_CLASS; + using value = std::integral_constant; using type = double; }; // integral types are exposed like enums @@ -62,7 +66,8 @@ class MatlabParamParser using unsigned_t = std::integral_constant; using value_t = typename std::conditional::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::value, int64_t, uint64_t>::type; }; // by default non-basic types are wrapped as pointers @@ -143,7 +148,7 @@ template struct MatlabParamParser::mx_wrapper_fns; // 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(mxGetData(cell)); // cast object to correct C++ type and then store it in the matrix @@ -262,7 +267,7 @@ template static typename std::enable_if::value, st template static typename std::enable_if::value, mxArray*>::type MatlabParamParser::wrap_array(const T* var, size_t length) { - auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper::value, mxREAL); + auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper::value::value, mxREAL); auto ptr = static_cast::type*>(mxGetData(cells)); for (int x = 0; x < length; ++x) ptr[x] = reinterpret_cast::type>(traits_trampoline::to_internal(T(var[x]))); @@ -272,7 +277,7 @@ template static typename std::enable_if::value, m template static typename std::enable_if::value, mxArray*>::type MatlabParamParser::wrap_array(const T* var, size_t length) { - auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper::value, mxREAL); + auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper::value::value, mxREAL); auto ptr = static_cast::type*>(mxGetData(cells)); for (int x = 0; x < length; ++x) ptr[x] = mx_wrapper::type(var[x]); diff --git a/wrappers/matlab/librealsense_mex.cpp b/wrappers/matlab/librealsense_mex.cpp index d74f8bb97c..d0ac4f55c2 100644 --- a/wrappers/matlab/librealsense_mex.cpp +++ b/wrappers/matlab/librealsense_mex.cpp @@ -361,7 +361,7 @@ void make_factory(){ auto option = MatlabParamParser::parse(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(inv[0]); auto option = MatlabParamParser::parse(inv[1]); @@ -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(inv[0]); + }); factory->record(options_factory); } { @@ -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 [?] @@ -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"); } } \ No newline at end of file diff --git a/wrappers/matlab/types.h b/wrappers/matlab/types.h index 999504969e..13f895efd0 100644 --- a/wrappers/matlab/types.h +++ b/wrappers/matlab/types.h @@ -34,7 +34,8 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns::type>; + mxArray* data_cell = mxCreateNumericMatrix(3, 4, data_wrapper_t::value::value, mxREAL); auto data_ptr = static_cast(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); @@ -42,15 +43,38 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns static mxArray* MatlabParamParser::mx_wrapper_fns::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; + auto cells = mxCreateNumericMatrix(1, 3, wrapper_t::value::value, mxREAL); + auto ptr = static_cast(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::wrap(rs2_quaternion&& val) +{ + using wrapper_t = mx_wrapper; + auto cells = mxCreateNumericMatrix(1, 4, wrapper_t::value::value, mxREAL); + auto ptr = static_cast(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::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 @@ -103,8 +127,8 @@ template<> static mxArray* MatlabParamParser::mx_wrapper_fns:: for (int i = 0; i < len; ++i) { mexLock(); // lock once for each created pointer - outp[i] = reinterpret_cast(new type_traits::rs2_internal_t(var)); - outp[i + len] = i; + outp[0 * len + i] = reinterpret_cast(new type_traits::rs2_internal_t(var)); + outp[1 * len + i] = i; } return vec; @@ -150,7 +174,8 @@ template struct MatlabParamParser::mx_wrapper_fns; + mxArray *cell = mxCreateNumericMatrix(1, 1, wrapper_t::value::value, mxREAL); auto *outp = static_cast(mxGetData(cell)); auto inptr = type_traits::rs2_internal_t(var); rs2_frame_add_ref(inptr, nullptr); @@ -174,23 +199,23 @@ template struct MatlabParamParser::mx_wrapper_fns static mxArray* MatlabParamParser::wrap_array(const rs2::vertex* var, size_t length) { using wrapper_t = mx_wrapper; - auto cells = mxCreateNumericMatrix(3, length, wrapper_t::value, mxREAL); + auto cells = mxCreateNumericMatrix(length, 3, wrapper_t::value::value, mxREAL); auto ptr = static_cast(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(const rs2::texture_coordinate* var, size_t length) { using wrapper_t = mx_wrapper; - auto cells = mxCreateNumericMatrix(2, length, wrapper_t::value, mxREAL); + auto cells = mxCreateNumericMatrix(length, 2, wrapper_t::value::value, mxREAL); auto ptr = static_cast(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; }