libidx
/home/rex/ebltrunk/core/libidx/include/matlab.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2011 by Soumith Chintala and Pierre Sermanet *
00003  *   soumith@gmail.com, pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef MATLAB_HPP
00034 #define MATLAB_HPP
00035 
00036 namespace ebl {
00037 
00038   template <typename T>
00039   idx<T> matlab::load_matrix(const char *name) {
00040     if (!fp) eblthrow("null file pointer");
00041 #ifdef __MATLAB__
00042   
00043     // get variable corresponding to name
00044     mxArray *var = matGetVariable(fp, name);
00045     if (!var)
00046       eblthrow("variable \"" << name << "\" not found in matlab object");
00047     int ndims = mxGetNumberOfDimensions(var);
00048     if (ndims > MAXDIMS)
00049       eblthrow("cannot load matrix with " << ndims 
00050                << " dimensions, libidx was compiled to support " << MAXDIMS 
00051                << " at most. Modify MAXDIMS and recompile.");
00052     intg nelements = mxGetNumberOfElements(var);
00053     const mwSize *dims = mxGetDimensions(var);
00054     // allocate matrix
00055     idxdim d;
00056     for (uint i = 0; i < ndims; ++i)
00057       d.insert_dim(i, dims[i]);
00058     idx<T> m(d);
00059 
00060     // load data
00061     mxClassID type = mxGetClassID(var);
00062     switch (type) {
00063     case mxCELL_CLASS: eblthrow("cannot load matrix from type cell"); break ;
00064     case mxSTRUCT_CLASS: eblthrow("cannot load matrix from type struct"); break ;
00065     case mxLOGICAL_CLASS: eblthrow("cannot load matrix from type logical"); break ;
00066     case mxFUNCTION_CLASS: eblthrow("cannot load matrix from type function"); break ;
00067     case mxINT8_CLASS:
00068     case mxUINT8_CLASS:
00069     case mxCHAR_CLASS: read_cast_matrix<ubyte>(var, m); break ;
00070     case mxINT16_CLASS: read_cast_matrix<int16>(var, m); break ;
00071     case mxUINT16_CLASS: read_cast_matrix<uint16>(var, m); break ;
00072     case mxINT32_CLASS: read_cast_matrix<uint32>(var, m); break ;
00073     case mxUINT32_CLASS: read_cast_matrix<uint32>(var, m); break ;
00074     case mxINT64_CLASS: read_cast_matrix<int64>(var, m); break ;
00075     case mxUINT64_CLASS: read_cast_matrix<uint64>(var, m); break ;
00076     case mxSINGLE_CLASS: read_cast_matrix<float>(var, m); break ;
00077     case mxDOUBLE_CLASS: read_cast_matrix<double>(var, m); break ;
00078     case mxVOID_CLASS: eblthrow("unsupported type: void");
00079     case mxUNKNOWN_CLASS: eblthrow("unknown type"); break ;
00080     }
00081     // delete array
00082     if (var) mxDestroyArray(var);
00083 #else
00084     idx<T> m;
00085 #endif
00086     return m;
00087   }
00088 
00089   template <typename Tmatlab, typename T>
00090   void matlab::read_cast_matrix(mxArray *var, idx<T> &m) {
00091 #ifdef __MATLAB__
00092     // allocate a temporary matrix with same type as original matrix type
00093     idx<Tmatlab> tmp(m.get_idxdim());
00094     // load data
00095     void *data = mxGetData(var);
00096     // copy to idx 
00097     memcpy(m.idx_ptr(), (Tmatlab*) data, m.nelements() * sizeof (Tmatlab));
00098     // copy-cast
00099     idx_copy(tmp, m);
00100 #endif
00101   }
00102 
00103 } // namespace ebl
00104 
00105 #endif /* MATLAB_HPP_ */