ivl 679
ivl/details/array_2d/functions/mxarray_functions.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_DETAILS_ARRAY_2D_MXARRAY_FUNCTIONS_HPP
00025 #define IVL_DETAILS_ARRAY_2D_MXARRAY_FUNCTIONS_HPP
00026 
00027 namespace ivl {
00028 
00029 #ifndef IVL_MATLAB
00030 /*
00031 template<class T>
00032 array_2d<T>::array_2d(const mxArray* mx) : array<T>()
00033 {
00034         throw esystem();
00035 }
00036 
00037 template<class T>
00038 mxArray* array_2d<T>::mx()
00039 {
00040         throw esystem();
00041         return 0;
00042 }
00043 */
00044 #else
00045 
00046 namespace mx {
00047 
00048 // Assign from mxArray
00049 
00050 template <class T>
00051 inline
00052 void assign(array_2d<T>& a, const mxArray* mx)
00053 {
00054         if(mx::type_class<T>() == mxUNKNOWN_CLASS) throw etype();
00055         if(mxGetClassID(mx) != mx::type_class<T>()) throw etype();
00056         if(mxGetNumberOfDimensions(mx) != 2) throw eshape();
00057 
00058         size_array sz(mxGetNumberOfDimensions(mx),
00059                 reinterpret_cast<const size_t*>(mxGetDimensions(mx)));
00060         a.resize(sz);
00061 
00062         size_t len = a.length();
00063 
00064         const T* ptr = mxGetData(mx);
00065         for(size_t i = 0; i < len; i++) {
00066                 a[i] = ptr[i];
00067         }
00068 }
00069 
00070 template <class T>
00071 inline
00072 void assign(array_2d<array_2d<T> >& a, const mxArray* mx)
00073 {
00074         if(mx::type_class<T>() == mxUNKNOWN_CLASS) throw etype();
00075         if(mxGetClassID(mx) != mx::type_class<T>()) throw etype();
00076         if(mxGetNumberOfDimensions(mx) != 2) throw eshape();
00077 
00078         size_array sz(mxGetNumberOfDimensions(mx),
00079                 reinterpret_cast<const size_t*>(mxGetDimensions(mx)));
00080         a.resize(sz);
00081 
00082         size_t len = a.length();
00083 
00084         for(size_t i = 0; i < len; i++) {
00085                 assign(a[i], mxGetCell(mx, mwIndex(i)));
00086         }
00087 }
00088 
00089 template <class T>
00090 inline
00091 void assign(array_2d<array<T> >& a, const mxArray* mx)
00092 {
00093         if(mx::type_class<T>() == mxUNKNOWN_CLASS) throw etype();
00094         if(mxGetClassID(mx) != mx::type_class<T>()) throw etype();
00095         if(mxGetNumberOfDimensions(mx) != 2) throw eshape();
00096 
00097         size_array sz(mxGetNumberOfDimensions(mx),
00098                 reinterpret_cast<const size_t*>(mxGetDimensions(mx)));
00099         a.resize(sz);
00100 
00101         size_t len = a.length();
00102 
00103         for(size_t i = 0; i < len; i++) {
00104                 assign(a[i], mxGetCell(mx, mwIndex(i)));
00105         }
00106 }
00107 
00108 template <class T>
00109 inline
00110 void assign(array<array_2d<T> >& a, const mxArray* mx)
00111 {
00112         if(mxGetClassID(mx) != mxCELL_CLASS) throw etype();
00113         size_array sz(mxGetNumberOfDimensions(mx),
00114                 reinterpret_cast<const size_t*>(mxGetDimensions(mx)));
00115 
00116         size_t len;
00117         if(sz.length() == 2) {
00118                 if(sz[0] == 1) len = sz[1];
00119                 else if(sz[1] == 1) len = sz[0];
00120                 else throw eshape();
00121         } else if(sz.length() == 1) len = sz[0];
00122         else throw eshape();
00123 
00124         a.resize(len);
00125         for(size_t i = 0; i < len; i++) {
00126                 assign(a[i], mxGetCell(mx, mwIndex(i)));
00127         }
00128 }
00129 
00130 // Convert to mxarray
00131 
00132 template <class T>
00133 inline
00134 mxArray* to_mxarray(const array_2d<T>& a)
00135 {
00136         mxArray* mx = mxCreateNumericMatrix(a.rows(), a.columns(),
00137                 mx::type_class<T>(), mxREAL);
00138 
00139         size_t len = a.length();
00140         T* ptr = mxGetData(mx);
00141         for(size_t i = 0; i < len; i++) {
00142                 ptr[i] = a[i];
00143         }
00144 
00145         return mx;
00146 }
00147 
00148 template <class T>
00149 inline
00150 mxArray* to_mxarray(const array_2d<array_2d<T> >& a)
00151 {
00152         mxArray* mx = mxCreateCellMatrix(a.rows(), a.columns(),
00153                 mx::type_class<T>(), mxREAL);
00154 
00155         size_t len = a.length();
00156         for(size_t i = 0; i < len; i++) {
00157                 mxSetCell(mx, mwIndex(i), to_mxarray(a[i]));
00158         }
00159 
00160         return mx;
00161 }
00162 
00163 template <class T>
00164 inline
00165 mxArray* to_mxarray(const array_2d<array<T> >& a)
00166 {
00167         mxArray* mx = mxCreateCellMatrix(a.rows(), a.columns(),
00168                 mx::type_class<T>(), mxREAL);
00169 
00170         size_t len = a.length();
00171         for(size_t i = 0; i < len; i++) {
00172                 mxSetCell(mx, mwIndex(i), to_mxarray(a[i]));
00173         }
00174 
00175         return mx;
00176 }
00177 
00178 template <class T>
00179 inline
00180 mxArray* to_mxarray(const array<array_2d<T> >& a)
00181 {
00182         size_array sz(1, a.length());
00183         mxArray* mx = mxCreateCellArray(1,
00184                 reinterpret_cast<const mwSize*>(sz.get_base_ptr()));
00185         size_t len = a.length();
00186         for(size_t i = 0; i < len; i++) {
00187                 mxSetCell(mx, mwIndex(i), to_mxarray(a[i]));
00188         }
00189 
00190         return mx;
00191 }
00192 
00193 } // namespace mx
00194 
00195 // construct from Matlab mxArray
00196 template <class T>
00197 inline
00198 array_2d<T>::array_2d(const mxArray* mx) :
00199         array_2d<T, array_2d<T> >(0, 0)
00200 {
00201         *this = mx;
00202 }
00203 
00204 template <class T>
00205 inline
00206 array_2d<T>& array_2d<T>::operator=(const mxArray* mx)
00207 {
00208         assign(*this, mx);
00209         return *this;
00210 }
00211 
00212 // convert to Matlab mxArray
00213 template <class T>
00214 inline
00215 mxArray* array_2d<T>::mx()
00216 {
00217         return to_mxarray(*this);
00218 }
00219 
00220 #endif //#ifndef IVL_MATLAB
00221 
00222 } //namespace ivl
00223 #endif // IVL_DETAILS_ARRAY_2D_MXARRAY_FUNCTIONS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations