ivl 679
ivl/details/array/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_MXARRAY_FUNCTIONS_HPP
00025 #define IVL_DETAILS_ARRAY_MXARRAY_FUNCTIONS_HPP
00026 
00027 namespace ivl {
00028 
00029 #ifndef IVL_MATLAB
00030 /*
00031 template<class T>
00032 array<T>::array(const mxArray* mx) : array<T>()
00033 {
00034         throw esystem();
00035 }
00036 
00037 template<class T>
00038 mxArray* array<T>::mx()
00039 {
00040         throw esystem();
00041         return 0;
00042 }
00043 */
00044 #else
00045 
00046 namespace mx {
00047 
00048 template <class T>
00049 inline
00050 void assign(array<T>& a, const mxArray* mx)
00051 {
00052         if(mx::type_class<T>() == mxUNKNOWN_CLASS) throw etype();
00053         if(mxGetClassID(mx) != mx::type_class<T>()) throw etype();
00054         size_array sz(mxGetNumberOfDimensions(mx),
00055                 reinterpret_cast<const size_t*>(mxGetDimensions(mx)));
00056 
00057         size_t len;
00058         if(sz.length() == 2) {
00059                 if(sz[0] == 1) len = sz[1];
00060                 else if(sz[1] == 1) len = sz[0];
00061                 else throw eshape();
00062         } else if(sz.length() == 1) len = sz[0];
00063         else throw eshape();
00064 
00065         a.resize(len);
00066         const T* ptr = mxGetData(mx);
00067         for(size_t i = 0; i < len; i++) {
00068                 a[i] = ptr[i];
00069         }
00070 }
00071 
00072 template <class T>
00073 inline
00074 void assign(array<array<T> >& a, const mxArray* mx)
00075 {
00076         if(mxGetClassID(mx) != mxCELL_CLASS) throw etype();
00077         size_array sz(mxGetNumberOfDimensions(mx),
00078                 reinterpret_cast<const size_t*>(mxGetDimensions(mx)));
00079 
00080         size_t len;
00081         if(sz.length() == 2) {
00082                 if(sz[0] == 1) len = sz[1];
00083                 else if(sz[1] == 1) len = sz[0];
00084                 else throw eshape();
00085         } else if(sz.length() == 1) len = sz[0];
00086         else throw eshape();
00087 
00088         a.resize(len);
00089         for(size_t i = 0; i < len; i++) {
00090                 assign(a[i], mxGetCell(mx, mwIndex(i)));
00091         }
00092 }
00093 
00094 template <class T>
00095 inline
00096 mxArray* to_mxarray(const array<T>& a)
00097 {
00098         size_array sz(1, a.length());
00099         mxArray* mx = mxCreateNumericArray(1,
00100                 reinterpret_cast<const mwSize*>(sz.get_base_ptr()),
00101                 mx::type_class<T>(), mxREAL);
00102 
00103         size_t len = a.length();
00104         T* ptr = mxGetData(mx);
00105         for(size_t i = 0; i < len; i++) {
00106                 ptr[i] = a[i];
00107         }
00108 
00109         return mx;
00110 }
00111 
00112 template <class T>
00113 inline
00114 mxArray* to_mxarray(const array<array<T> >& a)
00115 {
00116         size_array sz(1, a.length());
00117         mxArray* mx = mxCreateCellArray(1,
00118                 reinterpret_cast<const mwSize*>(sz.get_base_ptr()));
00119         size_t len = a.length();
00120         for(size_t i = 0; i < len; i++) {
00121                 mxSetCell(mx, mwIndex(i), to_mxarray(a[i]));
00122         }
00123 
00124         return mx;
00125 }
00126 
00127 } // namespaace mx
00128 
00129 // construct from Matlab mxArray
00130 template <class T>
00131 inline
00132 array<T>::array(const mxArray* mx)
00133 {
00134         *this = mx;
00135 }
00136 
00137 template<class T>
00138 inline
00139 array<T>& array<T>::operator=(const mxArray* mx)
00140 {
00141         mx::assign_array<T>(*this, mx);
00142 
00143         return *this;
00144 }
00145 
00146 // convert to Matlab mxArray
00147 template <class T>
00148 inline
00149 mxArray* array<T>::mx()
00150 {
00151         return mx::to_mxarray(*this);
00152 }
00153 
00154 #endif //#ifndef IVL_MATLAB
00155 
00156 } //namespace ivl
00157 #endif // IVL_DETAILS_ARRAY_MXARRAY_FUNCTIONS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations