/* * Copyright (c) 2002-2006 Samit Basu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include "Array.hpp" #include "mex.h" #include "MexInterface.hpp" template void ArrayComplexToMexComplex(T*src, S*dst_real, S*dst_imag, int count) { for (int i=0;i void MexComplexToArrayComplex(T*src_real, T*src_imag, S*dst, int count) { for (int i=0;i void ArrayRealToMexReal(T*src, S*dst, int count) { for (int i=0;i void MexRealToArrayReal(T*src, S*dst, int count) { for (int i=0;inumber_of_dims;i++) dim.set(i,array_ptr->dims[i]); int count = mxGetNumberOfElements(array_ptr); switch (array_ptr->classID) { case mxUINT32_CLASS: cls = FM_UINT32; dp = Array::allocateArray(cls,count); MexRealToArrayReal((uint32*)array_ptr->realdata, (uint32*)dp,count); break; case mxINT32_CLASS: cls = FM_INT32; dp = Array::allocateArray(cls,count); MexRealToArrayReal((int32*)array_ptr->realdata, (int32*)dp,count); break; case mxUINT16_CLASS: cls = FM_UINT16; dp = Array::allocateArray(cls,count); MexRealToArrayReal((uint16*)array_ptr->realdata, (uint16*)dp,count); break; case mxINT16_CLASS: cls = FM_INT16; dp = Array::allocateArray(cls,count); MexRealToArrayReal((int16*)array_ptr->realdata, (int16*)dp,count); break; case mxUINT8_CLASS: cls = FM_UINT8; dp = Array::allocateArray(cls,count); MexRealToArrayReal((uint8*)array_ptr->realdata, (uint8*)dp,count); break; case mxINT8_CLASS: cls = FM_INT8; dp = Array::allocateArray(cls,count); MexRealToArrayReal((int8*)array_ptr->realdata, (int8*)dp,count); break; case mxSINGLE_CLASS: cls = FM_FLOAT; dp = Array::allocateArray(cls,count); MexRealToArrayReal((float*)array_ptr->realdata, (float*)dp,count); break; case mxDOUBLE_CLASS: cls = FM_DOUBLE; dp = Array::allocateArray(cls,count); MexRealToArrayReal((double*)array_ptr->realdata, (double*)dp,count); } return Array::Array(cls,dim,dp); } Array ArrayFromMexArrayComplex(mxArray *array_ptr) { Class cls; Dimensions dim; void *dp; for (int i=0;inumber_of_dims;i++) dim.set(i,array_ptr->dims[i]); int count = mxGetNumberOfElements(array_ptr); if (array_ptr->classID != mxDOUBLE_CLASS) { cls = FM_COMPLEX; switch(array_ptr->classID) { case mxUINT32_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((uint32*)array_ptr->realdata, (uint32*)array_ptr->imagdata, (float*)dp,count); break; case mxINT32_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((int32*)array_ptr->realdata, (int32*)array_ptr->imagdata, (float*)dp,count); case mxUINT16_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((uint16*)array_ptr->realdata, (uint16*)array_ptr->imagdata, (float*)dp,count); break; case mxINT16_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((int16*)array_ptr->realdata, (int16*)array_ptr->imagdata, (float*)dp,count); case mxUINT8_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((uint8*)array_ptr->realdata, (uint8*)array_ptr->imagdata, (float*)dp,count); break; case mxINT8_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((int8*)array_ptr->realdata, (int8*)array_ptr->imagdata, (float*)dp,count); break; case mxSINGLE_CLASS: dp = Array::allocateArray(FM_COMPLEX,count); MexComplexToArrayComplex((float*)array_ptr->realdata, (float*)array_ptr->imagdata, (float*)dp,count); } } else { cls = FM_DCOMPLEX; dp = Array::allocateArray(FM_DCOMPLEX,count); MexComplexToArrayComplex((double*)array_ptr->realdata, (double*)array_ptr->imagdata, (double*)dp,count); } return Array::Array(cls,dim,dp); } Array ArrayFromMexArray(mxArray *array_ptr) { if (array_ptr->classID == mxCELL_CLASS) { Dimensions dim; for (int i=0;inumber_of_dims;i++) dim.set(i,array_ptr->dims[i]); int N = mxGetNumberOfElements(array_ptr); mxArray** dp = (mxArray**) array_ptr->realdata; Array* cp = new Array[N]; for (int i=0;iiscomplex) { return ArrayFromMexArrayComplex(array_ptr); } else { return ArrayFromMexArrayReal(array_ptr); } } } template mxArray* MexArrayFromRealArray(Array array, mxClassID classID) { // Convert array dimensions into a simple integer array int num_dim = array.dimensions().getLength(); int *dim_vec = (int*) malloc(sizeof(int)*num_dim); for (int i=0;irealdata; int N = mxGetNumberOfElements(ret); for (int i=0;i mxArray* MexArrayFromComplexArray(Array array, mxClassID classID) { // Convert array dimensions into a simple integer array int num_dim = array.dimensions().getLength(); int *dim_vec = (int*) malloc(sizeof(int)*num_dim); for (int i=0;irealdata; mxType *dp_i = (mxType*) ret->imagdata; int N = mxGetNumberOfElements(ret); for (int i=0;irealdata; int N = mxGetNumberOfElements(ret); for (int i=0;i(array,mxLOGICAL_CLASS); case FM_UINT8: return MexArrayFromRealArray(array,mxUINT8_CLASS); case FM_INT8: return MexArrayFromRealArray(array,mxINT8_CLASS); case FM_UINT16: return MexArrayFromRealArray(array,mxUINT16_CLASS); case FM_INT16: return MexArrayFromRealArray(array,mxINT16_CLASS); case FM_UINT32: return MexArrayFromRealArray(array,mxUINT32_CLASS); case FM_INT32: return MexArrayFromRealArray(array,mxINT32_CLASS); case FM_FLOAT: return MexArrayFromRealArray(array,mxSINGLE_CLASS); case FM_DOUBLE: return MexArrayFromRealArray(array,mxDOUBLE_CLASS); case FM_COMPLEX: return MexArrayFromComplexArray(array,mxSINGLE_CLASS); case FM_DCOMPLEX: return MexArrayFromComplexArray(array,mxDOUBLE_CLASS); case FM_STRING: return MexArrayFromRealArray(array,mxCHAR_CLASS); } return NULL; }