|
| 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 |
0 commit comments