Skip to content

Commit

Permalink
Refactored and completed the from/toMatlab() wrappers
Browse files Browse the repository at this point in the history
Completed the extension of the classes std::vector<double>,
std::vector<bool>, std::vector<int>, yarp::sig::Vector with
these wrappers.
  • Loading branch information
nunoguedelha committed Jun 19, 2018
1 parent 2b4b798 commit a71047a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 64 deletions.
4 changes: 2 additions & 2 deletions bindings/macrosForMultipleAnalogSensors.i
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// This software may be modified and distributed under the terms of the
// BSD-3-Clause license. See the accompanying LICENSE file for details.

%define RESET_CONSTANTS
%define RESET_CONSTANTS_IN_EXTENDED_ANALOG_SENSOR_INTERFACE
#undef ThreeAxisGyroscopes_EXTENDED_INTERFACE
#undef ThreeAxisLinearAccelerometers_EXTENDED_INTERFACE
#undef ThreeAxisMagnetometers_EXTENDED_INTERFACE
Expand All @@ -18,7 +18,7 @@

%define EXTENDED_ANALOG_SENSOR_INTERFACE(sensor)

RESET_CONSTANTS
RESET_CONSTANTS_IN_EXTENDED_ANALOG_SENSOR_INTERFACE

#define sensor ## _EXTENDED_INTERFACE 1

Expand Down
61 changes: 0 additions & 61 deletions bindings/matlab/IVector_fromTo_matlab.i

This file was deleted.

108 changes: 108 additions & 0 deletions bindings/matlab/vectors_fromTo_matlab.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
// All rights reserved.
//
// This software may be modified and distributed under the terms of the
// BSD-3-Clause license. See the accompanying LICENSE file for details.

%define RESET_CONSTANTS_IN_TO_MATLAB
#undef mxCreateDoubleMatrixHasComplexFlagParam
#undef mxCreateLogicalMatrixHasComplexFlagParam
%enddef

%define TO_MATLAB(matlabType,mxArrayInitializer)

// Convert to a Matlab vector
mxArray * toMatlab() const
{
// create Matlab vector and map it to pointer 'd'
size_t selfDim = self->size();
#if mxArrayInitializer ## HasComplexFlagParam
mxArray *p = mxArrayInitializer(selfDim, 1, mxREAL);
#else
mxArray *p = mxArrayInitializer(selfDim, 1);
#endif
matlabType* d = static_cast<matlabType*>(mxGetData(p));

// copy items from 'self' to 'd'
for(size_t i=0; i<selfDim; i++)
{
d[i] = static_cast<matlabType>(self->operator[](i));
}
return p;
}

%enddef


%define FROM_MATLAB(matlabType,cppType,mxArrayInitializer,vectorClass)

// Convert from a Matlab vector
void fromMatlab(mxArray * in)
{
// check size
const mwSize * dims = mxGetDimensions(in);
size_t selfDim = self->size();
size_t matlabVecDim = (dims[0] == 1 ? dims[1] : dims[0]);

if (matlabVecDim == selfDim)
{
// map Matlab vector to pointer 'd'
matlabType* d = static_cast<matlabType*>(mxGetData(in));

// copy items from 'd' to 'self'
for(size_t i=0; i<selfDim; i++)
{
self->operator[](i) = static_cast<cppType>(d[i]);
}
return;
} else {
mexErrMsgIdAndTxt("yarp::vectorClass::wrongDimension",
"Wrong vector size. Matlab size: %d. vectorClass size: %d", matlabVecDim, selfDim);
}
}

%enddef


%define RESET(resetValue)

// Reset values
void zero()
{
for(size_t i=0; i < self->size(); i++)
{
self->operator[](i) = resetValue;
}
return;
}

%enddef

RESET_CONSTANTS_IN_TO_MATLAB
#define mxCreateDoubleMatrixHasComplexFlagParam 1

%extend std::vector<double> {
TO_MATLAB(double,mxCreateDoubleMatrix)
FROM_MATLAB(double,double,mxCreateDoubleMatrix,DVector)
RESET(0)
}

%extend std::vector<bool> {
TO_MATLAB(bool,mxCreateLogicalMatrix)
FROM_MATLAB(bool,bool,mxCreateLogicalMatrix,BVector)
RESET(false)
}

%extend std::vector<int> {
TO_MATLAB(double,mxCreateDoubleMatrix)
FROM_MATLAB(double,int,mxCreateDoubleMatrix,IVector)
RESET(0)
}

%extend yarp::sig::Vector {
TO_MATLAB(double,mxCreateDoubleMatrix)
FROM_MATLAB(double,double,mxCreateDoubleMatrix,yarp::sig::Vector)
RESET(0)
}

RESET_CONSTANTS_IN_TO_MATLAB
2 changes: 1 addition & 1 deletion bindings/yarp.i
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ MAKE_COMMS(Bottle)

#ifdef SWIGMATLAB
// Extend IVector for handling conversion of vectors from and to Matlab
%include "matlab/IVector_fromTo_matlab.i"
%include "matlab/vectors_fromTo_matlab.i"
#endif

#if defined(SWIGCSHARP)
Expand Down

0 comments on commit a71047a

Please sign in to comment.