Skip to content

Commit

Permalink
Finish matlab half off all classes for initial release, add first exa…
Browse files Browse the repository at this point in the history
…mple
  • Loading branch information
Lior Ramati committed Aug 14, 2018
1 parent 7ecf0a5 commit e25691b
Show file tree
Hide file tree
Showing 37 changed files with 948 additions and 107 deletions.
62 changes: 57 additions & 5 deletions wrappers/matlab/MatlabParamParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ class MatlabParamParser
using value = value_t;
using type = typename std::conditional<std::is_same<value_t, signed_t>::value, int64_t, uint64_t>::type;
};
// uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, and int64_t
// are exposed as the relevant native Matlab type
template <> struct mx_wrapper<uint8_t> {
using value = std::integral_constant<mxClassID, mxUINT8_CLASS>;
using type = uint8_t;
};
template <> struct mx_wrapper<uint16_t> {
using value = std::integral_constant<mxClassID, mxUINT16_CLASS>;
using type = uint16_t;
};
template <> struct mx_wrapper<uint32_t> {
using value = std::integral_constant<mxClassID, mxUINT32_CLASS>;
using type = uint32_t;
};
template <> struct mx_wrapper<uint64_t> {
using value = std::integral_constant<mxClassID, mxUINT64_CLASS>;
using type = uint64_t;
};
template <> struct mx_wrapper<int8_t> {
using value = std::integral_constant<mxClassID, mxINT8_CLASS>;
using type = int8_t;
};
template <> struct mx_wrapper<int16_t> {
using value = std::integral_constant<mxClassID, mxINT16_CLASS>;
using type = int16_t;
};
template <> struct mx_wrapper<int32_t> {
using value = std::integral_constant<mxClassID, mxINT32_CLASS>;
using type = int32_t;
};
template <> struct mx_wrapper<int64_t> {
using value = std::integral_constant<mxClassID, mxINT64_CLASS>;
using type = int64_t;
};
// 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:
Expand All @@ -83,7 +117,7 @@ class MatlabParamParser
struct traits_trampoline {
private:
template <typename T> struct detector {
struct fallback { int to_internal, from_internal; };
struct fallback { int to_internal, from_internal, use_cells; };
struct derived : type_traits<T>, fallback {};
template <typename U, U> struct checker;
typedef char ao1[1];
Expand All @@ -92,9 +126,16 @@ class MatlabParamParser
template <typename U> static ao2& check_to(...);
template <typename U> static ao1& check_from(checker<int fallback::*, &U::from_internal> *);
template <typename U> static ao2& check_from(...);
template <typename U> static ao1& check_cells(checker<int fallback::*, &U::use_cells> *);
template <typename U> static ao2& check_cells(...);
// template <typename, typename = void> struct use_cells_t : false_type {};
// template <typename U> struct use_cells_t<U, typename std::enable_if<sizeof(check_cells<U>(0)) == 2>::type>
// : T::use_cells {};

enum { has_to = sizeof(check_to<derived>(0)) == 2 };
enum { has_from = sizeof(check_from<derived>(0)) == 2 };
// enum { use_cells = use_cells_t<derived>::value };
enum { use_cells = sizeof(check_cells<derived>(0)) == 2 };
};
template <typename T> using internal_t = typename type_traits<T>::rs2_internal_t;
public:
Expand All @@ -111,7 +152,7 @@ class MatlabParamParser
// 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); }

template <typename T> using use_cells = std::integral_constant<bool, detector<T>::use_cells>;
};
public:
MatlabParamParser() {};
Expand All @@ -125,7 +166,8 @@ class MatlabParamParser
template <typename T> static typename std::enable_if<!is_basic_type<T>::value, std::vector<T>>::type parse_array(const mxArray* cells);
template <typename T> static typename std::enable_if<is_basic_type<T>::value, std::vector<T>>::type parse_array(const mxArray* cells);

template <typename T> static typename std::enable_if<!is_basic_type<T>::value, mxArray*>::type wrap_array(const T* var, size_t length);
template <typename T> static typename std::enable_if<!is_basic_type<T>::value && !traits_trampoline::use_cells<T>::value, mxArray*>::type wrap_array(const T* var, size_t length);
template <typename T> static typename std::enable_if<!is_basic_type<T>::value && traits_trampoline::use_cells<T>::value, mxArray*>::type wrap_array(const T* var, size_t length);
template <typename T> static typename std::enable_if<is_basic_type<T>::value, mxArray*>::type wrap_array(const T* var, size_t length);
};

Expand Down Expand Up @@ -264,8 +306,8 @@ template <typename T> static typename std::enable_if<is_basic_type<T>::value, st
}
return ret;
}

template <typename T> static typename std::enable_if<!is_basic_type<T>::value, mxArray*>::type MatlabParamParser::wrap_array(const T* var, size_t length)
template <typename T> static typename std::enable_if<!is_basic_type<T>::value && !MatlabParamParser::traits_trampoline::use_cells<T>::value, mxArray*>::type
MatlabParamParser::wrap_array(const T* var, size_t length)
{
auto cells = mxCreateNumericMatrix(1, length, MatlabParamParser::mx_wrapper<T>::value::value, mxREAL);
auto ptr = static_cast<typename mx_wrapper<T>::type*>(mxGetData(cells));
Expand All @@ -275,6 +317,16 @@ template <typename T> static typename std::enable_if<!is_basic_type<T>::value, m
return cells;
}

template <typename T> static typename std::enable_if<!is_basic_type<T>::value && MatlabParamParser::traits_trampoline::use_cells<T>::value, mxArray*>::type
MatlabParamParser::wrap_array(const T* var, size_t length)
{
auto cells = mxCreateCellMatrix(1, length);
for (int x = 0; x < length; ++x)
mxSetCell(cells, x, wrap(T(var[x])));

return cells;
}

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::value, mxREAL);
Expand Down
32 changes: 32 additions & 0 deletions wrappers/matlab/align.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
% Wraps librealsense2 align class
classdef align < handle
properties (SetAccess = private, Hidden = true)
objectHandle;
end
methods
% Constructor
function this = align(align_to)
narginchk(1, 1);
validateattributes(align_to, {'realsense.stream', 'numeric'}, {'scalar', 'nonnegative', 'real', 'integer', '<=', realsense.stream.count});
this.objectHandle = realsense.librealsense_mex('rs2::align', 'new', int64(align_to));
end

% Destructor
function delete(this)
if (this.objectHandle ~= 0)
realsense.librealsense_mex('rs2::align', 'delete', this.objectHandle);
end
end

% Functions
function frames = process(this, frame)
narginchk(2, 2);
validateattributes(frame, {'realsense.frame'}, {'scalar'}, '', 'frame', 2);
if ~frame.is('frameset')
error('Expected input number 2, frame, to be a frameset');
end
out = realsense.librealsense_mex('rs2::align', 'process', this.objectHandle, frame.objectHandle);
frames = realsense.frame(out);
end
end
end
23 changes: 23 additions & 0 deletions wrappers/matlab/colorizer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% Wraps librealsense2 colorizer class
classdef colorizer < realsense.options
methods
% Constructor
function this = colorizer()
out = realsense.librealsense_mex('rs2::colorizer', 'new');
this = [email protected](out);
end

% Destructor (uses base class destructor)

% Functions
function video_frame = colorize(this, depth)
narginchk(2, 2)
validateattributes(depth, {'realsense.frame'}, {'scalar'}, '', 'depth', 2);
if ~depth.is('depth_frame')
error('Expected input number 2, depth, to be a depth_frame');
end
out = realsense.librealsense_mex('rs2::colorizer', 'colorize', this.objectHandle, depth.objectHandle);
video_frame = realsense.video_frame(out);
end
end
end
127 changes: 127 additions & 0 deletions wrappers/matlab/config.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
% Wraps librealsense2 config class
classdef config < handle
properties (SetAccess = private, Hidden = true)
objectHandle;
end
methods
% Constructor
function this = pipeline_profile()
this.objectHandle = realsense.librealsense_mex('rs2::config', 'new');
end
% Destructor
function delete(this)
if (this.objectHandle ~= 0)
realsense.librealsense_mex('rs2::config', 'delete', this.objectHandle);
end
end

% Functions
function enable_stream(this, varargin)
narginchk(2, 7);
validateattributes(varargin{1}, {'realsense.stream', 'numeric'}, {'scalar', 'nonnegative', 'real', 'integer', '<=', realsense.stream.count}, '', 'stream_type', 2);
which = 'none'
switch nargin
case 2
which = 'enable_stream#stream';
case 3
if isa(varargin{2}, 'realsense.format')
validateattributes(varargin{2}, {'realsense.format'}, {'scalar'}, '', 'format', 3);
which = 'enable_stream#format';
else
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'real', 'integer'}, '', 'stream_index', 3);
which = 'enable_stream#stream';
end
case 4
if isa(varargin{2}, 'realsense.format')
validateattributes(varargin{2}, {'realsense.format'}, {'scalar'}, '', 'format', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'framerate', 4);
which = 'enable_stream#format';
elseif isa(varargin{3}, 'realsense.format')
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'real', 'integer'}, '', 'stream_index', 3);
validateattributes(varargin{3}, {'realsense.format'}, {'scalar'}, '', 'format', 4);
which = 'enable_stream#extended';
else
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'width', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'height', 4);
which = 'enable_stream#size';
end
case 5
if isa(varargin{3}, 'realsense.format')
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'real', 'integer'}, '', 'stream_index', 3);
validateattributes(varargin{3}, {'realsense.format'}, {'scalar'}, '', 'format', 4);
validateattributes(varargin{4}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'framerate', 5);
which = 'enable_stream#extended';
elseif isa(varargin{4}, 'realsense.format')
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'width', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'height', 4);
validateattributes(varargin{4}, {'realsense.format'}, {'scalar'}, '', 'format', 5);
which = 'enable_stream#size';
else
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'real', 'integer'}, '', 'stream_index', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'width', 4);
validateattributes(varargin{4}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'height', 5);
which = 'enable_stream#full';
end
case 6
if isa(varargin{4}, 'realsense.format')
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'width', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'height', 4);
validateattributes(varargin{4}, {'realsense.format'}, {'scalar'}, '', 'format', 5);
validateattributes(varargin{5}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'framerate', 6);
which = 'enable_stream#size';
else
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'real', 'integer'}, '', 'stream_index', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'width', 4);
validateattributes(varargin{4}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'height', 5);
validateattributes(varargin{5}, {'realsense.format', 'numeric'}, {'scalar', 'nonnegative', 'real', 'integer', '<=', realsense.format.count}, '', 'format', 6);
which = 'enable_stream#full';
end
case 7
validateattributes(varargin{2}, {'numeric'}, {'scalar', 'real', 'integer'}, '', 'stream_index', 3);
validateattributes(varargin{3}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'width', 4);
validateattributes(varargin{4}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'height', 5);
validateattributes(varargin{5}, {'realsense.format', 'numeric'}, {'scalar', 'nonnegative', 'real', 'integer', '<=', realsense.format.count}, '', 'format', 6);
validateattributes(varargin{6}, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'framerate', 7);
end
args = num2cell(int64([varargin{:}]));
realsense.librealsense_mex('rs2::config', which, this.objectHandle, args{:});
end
function enable_all_streams(this)
realsense.librealsense_mex('rs2::config', 'enable_all_streams', this.objectHandle);
end
function enable_device(this, serial)
narginchk(2, 2);
validateattributes(serial, {'char', 'string'}, {'scalartext'}, '', 'serial', 2);
realsense.librealsense_mex('rs2::config', 'enable_device', this.objectHandle, serial);
end
function enable_device_from_file(this, file_name, repeat_playback)
narginchk(2, 3);
validateattributes(file_name, {'char', 'string'}, {'scalartext'}, '', 'file_name', 2);
if nargin==2:
realsense.librealsense_mex('rs2::config', 'enable_device_from_file', this.objectHandle, file_name);
else
validateattributes(repeat_playback, {'logical', 'numeric'}, {'scalar', 'real'}, '', 'repeat_playback', 3);
realsense.librealsense_mex('rs2::config', 'enable_device_from_file', this.objectHandle, file_name, logical(repeat_playback));
end
end
function enable_record_to_file(this, serialv)
narginchk(2, 2);
validateattributes(file_name, {'char', 'string'}, {'scalartext'}, '', 'file_name', 2);
realsense.librealsense_mex('rs2::config', 'enable_record_to_file', this.objectHandle, file_name);
end
function disable_stream(this, stream, index)
narginchk(2, 3);
validateattributes(stream, {'realsense.stream', 'numeric'}, {'scalar', 'nonnegative', 'real', 'integer', '<=', realsense.stream.count}, '', 'stream', 2);
if nargin == 2
out = realsense.librealsense_mex('rs2::config', 'disable_stream', this.objectHandle, int64(stream));
else
validateattributes(index, {'numeric'}, {'scalar', 'nonnegative', 'real', 'integer'}, '', 'index', 3);
out = realsense.librealsense_mex('rs2::config', 'disable_stream', this.objectHandle, int64(stream), int64(index));
end
stream = realsense.stream_profile(out{:});
end
function disable_all_streams(this)
realsense.librealsense_mex('rs2::config', 'disable_all_streams', this.objectHandle);
end
end
end
20 changes: 12 additions & 8 deletions wrappers/matlab/context.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ function delete(this)
% Functions
function device_array = query_devices(this)
arr = realsense.librealsense_mex('rs2::context', 'query_devices', this.objectHandle);
device_array = arrayfun(@realsense.device, arr(:,1), arr(:,2), 'UniformOutput', false);
% TODO: Might be cell array
device_array = arrayfun(@(x) realsense.device(x{:}{:}), arr, 'UniformOutput', false);
end
function sensor_array = query_all_sensors(this)
arr = realsense.librealsense_mex('rs2::context', 'query_all_sensors', this.objectHandle);
sensor_array = arrayfun(@realsense.sensor, arr, 'UniformOutput', false);
end
function device = get_sensor_parent(this, sensor)
if (isa(sensor, 'sensor'))
device = realsense.device(realsense.librealsense_mex('rs2::context', 'get_sensor_parent', this.objectHandle, sensor.objectHandle));
else
error('sensor must be a sensor');
end
narginchk(2, 2);
validateattributes(sensor, {'realsense.sensor'}, {'scalar'}, '', 'sensor', 2);
out = realsense.librealsense_mex('rs2::context', 'get_sensor_parent', this.objectHandle, sensor.objectHandle);
device = realsense.device(out);
end
function playback = load_device(this, file)
% TODO: finalize once playback is wrapped
playback = realsense.playback(realsense.librealsense_mex('rs2::context', 'load_device', this.objectHandle, file));
narginchk(2, 2);
validateattributes(file, {'string', 'char'}, {'scalartext', 'nonempty'}, '', 'file', 2);
out = realsense.librealsense_mex('rs2::context', 'load_device', this.objectHandle, file);
playback = realsense.playback(out(1), out(2));
end
function unload_device(this, file)
narginchk(2, 2);
validateattributes(file, {'string', 'char'}, {'scalartext', 'nonempty'}, '', 'file', 2);
realsense.librealsense_mex('rs2::context', 'unload_device', this.objectHandle, file);
end
end
Expand Down
14 changes: 14 additions & 0 deletions wrappers/matlab/decimation_filter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
% Wraps librealsense2 decimation_filter class
classdef decimation_filter < realsense.process_interface
methods
% Constructor
function this = decimation_filter()
out = realsense.librealsense_mex('rs2::decimation_filter', 'new');
this = [email protected]_interface(out);
end

% Destructor (uses base class destructor)

% Functions
end
end
2 changes: 1 addition & 1 deletion wrappers/matlab/depth_sensor.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
methods
% Constructor
function this = depth_sensor(handle)
this = [email protected](handle);
this = [email protected](handle);
end

% Destructor (uses base class destructor)
Expand Down
2 changes: 1 addition & 1 deletion wrappers/matlab/depth_stereo_sensor.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
methods
% Constructor
function this = depth_stereo_sensor(handle)
this = [email protected]_sensor(handle);
this = [email protected]_sensor(handle);
end

% Destructor (uses base class destructor)
Expand Down
Loading

0 comments on commit e25691b

Please sign in to comment.