Skip to content

Commit a71047a

Browse files
committed
Refactored and completed the from/toMatlab() wrappers
Completed the extension of the classes std::vector<double>, std::vector<bool>, std::vector<int>, yarp::sig::Vector with these wrappers.
1 parent 2b4b798 commit a71047a

File tree

4 files changed

+111
-64
lines changed

4 files changed

+111
-64
lines changed

bindings/macrosForMultipleAnalogSensors.i

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// This software may be modified and distributed under the terms of the
55
// BSD-3-Clause license. See the accompanying LICENSE file for details.
66

7-
%define RESET_CONSTANTS
7+
%define RESET_CONSTANTS_IN_EXTENDED_ANALOG_SENSOR_INTERFACE
88
#undef ThreeAxisGyroscopes_EXTENDED_INTERFACE
99
#undef ThreeAxisLinearAccelerometers_EXTENDED_INTERFACE
1010
#undef ThreeAxisMagnetometers_EXTENDED_INTERFACE
@@ -18,7 +18,7 @@
1818

1919
%define EXTENDED_ANALOG_SENSOR_INTERFACE(sensor)
2020

21-
RESET_CONSTANTS
21+
RESET_CONSTANTS_IN_EXTENDED_ANALOG_SENSOR_INTERFACE
2222

2323
#define sensor ## _EXTENDED_INTERFACE 1
2424

bindings/matlab/IVector_fromTo_matlab.i

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
2+
// All rights reserved.
3+
//
4+
// This software may be modified and distributed under the terms of the
5+
// BSD-3-Clause license. See the accompanying LICENSE file for details.
6+
7+
%define RESET_CONSTANTS_IN_TO_MATLAB
8+
#undef mxCreateDoubleMatrixHasComplexFlagParam
9+
#undef mxCreateLogicalMatrixHasComplexFlagParam
10+
%enddef
11+
12+
%define TO_MATLAB(matlabType,mxArrayInitializer)
13+
14+
// Convert to a Matlab vector
15+
mxArray * toMatlab() const
16+
{
17+
// create Matlab vector and map it to pointer 'd'
18+
size_t selfDim = self->size();
19+
#if mxArrayInitializer ## HasComplexFlagParam
20+
mxArray *p = mxArrayInitializer(selfDim, 1, mxREAL);
21+
#else
22+
mxArray *p = mxArrayInitializer(selfDim, 1);
23+
#endif
24+
matlabType* d = static_cast<matlabType*>(mxGetData(p));
25+
26+
// copy items from 'self' to 'd'
27+
for(size_t i=0; i<selfDim; i++)
28+
{
29+
d[i] = static_cast<matlabType>(self->operator[](i));
30+
}
31+
return p;
32+
}
33+
34+
%enddef
35+
36+
37+
%define FROM_MATLAB(matlabType,cppType,mxArrayInitializer,vectorClass)
38+
39+
// Convert from a Matlab vector
40+
void fromMatlab(mxArray * in)
41+
{
42+
// check size
43+
const mwSize * dims = mxGetDimensions(in);
44+
size_t selfDim = self->size();
45+
size_t matlabVecDim = (dims[0] == 1 ? dims[1] : dims[0]);
46+
47+
if (matlabVecDim == selfDim)
48+
{
49+
// map Matlab vector to pointer 'd'
50+
matlabType* d = static_cast<matlabType*>(mxGetData(in));
51+
52+
// copy items from 'd' to 'self'
53+
for(size_t i=0; i<selfDim; i++)
54+
{
55+
self->operator[](i) = static_cast<cppType>(d[i]);
56+
}
57+
return;
58+
} else {
59+
mexErrMsgIdAndTxt("yarp::vectorClass::wrongDimension",
60+
"Wrong vector size. Matlab size: %d. vectorClass size: %d", matlabVecDim, selfDim);
61+
}
62+
}
63+
64+
%enddef
65+
66+
67+
%define RESET(resetValue)
68+
69+
// Reset values
70+
void zero()
71+
{
72+
for(size_t i=0; i < self->size(); i++)
73+
{
74+
self->operator[](i) = resetValue;
75+
}
76+
return;
77+
}
78+
79+
%enddef
80+
81+
RESET_CONSTANTS_IN_TO_MATLAB
82+
#define mxCreateDoubleMatrixHasComplexFlagParam 1
83+
84+
%extend std::vector<double> {
85+
TO_MATLAB(double,mxCreateDoubleMatrix)
86+
FROM_MATLAB(double,double,mxCreateDoubleMatrix,DVector)
87+
RESET(0)
88+
}
89+
90+
%extend std::vector<bool> {
91+
TO_MATLAB(bool,mxCreateLogicalMatrix)
92+
FROM_MATLAB(bool,bool,mxCreateLogicalMatrix,BVector)
93+
RESET(false)
94+
}
95+
96+
%extend std::vector<int> {
97+
TO_MATLAB(double,mxCreateDoubleMatrix)
98+
FROM_MATLAB(double,int,mxCreateDoubleMatrix,IVector)
99+
RESET(0)
100+
}
101+
102+
%extend yarp::sig::Vector {
103+
TO_MATLAB(double,mxCreateDoubleMatrix)
104+
FROM_MATLAB(double,double,mxCreateDoubleMatrix,yarp::sig::Vector)
105+
RESET(0)
106+
}
107+
108+
RESET_CONSTANTS_IN_TO_MATLAB

bindings/yarp.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ MAKE_COMMS(Bottle)
460460

461461
#ifdef SWIGMATLAB
462462
// Extend IVector for handling conversion of vectors from and to Matlab
463-
%include "matlab/IVector_fromTo_matlab.i"
463+
%include "matlab/vectors_fromTo_matlab.i"
464464
#endif
465465

466466
#if defined(SWIGCSHARP)

0 commit comments

Comments
 (0)