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