MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Tools/Matlab/iMatlab/mexVNLHelpers.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman and others
00010 //   at MIT 2001-2002 and Oxford University 2003-2005.
00011 //   email: pnewman@robots.ox.ac.uk. 
00012 //      
00013 //   This file is part of a  MOOS Instrument. 
00014 //        
00015 //   This program is free software; you can redistribute it and/or 
00016 //   modify it under the terms of the GNU General Public License as 
00017 //   published by the Free Software Foundation; either version 2 of the 
00018 //   License, or (at your option) any later version. 
00019 //          
00020 //   This program is distributed in the hope that it will be useful, 
00021 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00022 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00023 //   General Public License for more details. 
00024 //            
00025 //   You should have received a copy of the GNU General Public License 
00026 //   along with this program; if not, write to the Free Software 
00027 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00028 //   02111-1307, USA. 
00029 //
00031 #include "mexVNLHelpers.h"
00032 
00033 
00034 bool  Matlab2Double(double & dfVal,const mxArray * pMLA)
00035 {
00036     if(!mxIsDouble(pMLA))
00037         return false;
00038 
00039     dfVal = mxGetScalar(pMLA);
00040     return true;
00041 }
00042 
00043 bool  Matlab2String(std::string & sStr,const mxArray * pMLA)
00044 {
00045     /* Input must be a string. */
00046     if ( mxIsChar(pMLA) != 1)
00047     {
00048         return false;
00049     }
00050     
00051     /* Input must be a row vector. */
00052     if (mxGetM(pMLA)!=1)
00053     {
00054         mexPrintf("Input must be a row vector.");
00055         return false;
00056     }
00057     
00058     /* Get the length of the input string. */
00059     int buflen = (mxGetM(pMLA) * mxGetN(pMLA)) + 1;
00060     
00061     /* Allocate memory for input and output strings. */
00062     void * input_buf=mxCalloc(buflen, sizeof(char));
00063     
00064     /* Copy the string data from prhs[0] into a C string 
00065     * input_ buf.
00066     * If the string array contains several rows, they are copied,
00067     * one column at a time, into one long string array.
00068     */
00069     int status = mxGetString(pMLA, (char*)input_buf, buflen);
00070     
00071     if(status!=0)
00072     {
00073         mexErrMsgTxt("Bad String extraction.");
00074         return false;
00075     }
00076     
00077     //yay!
00078     sStr  = std::string ((char*)input_buf);
00079     
00080     return true;
00081     
00082                 
00083 }
00084 #ifdef HAVE_VNL
00085 bool MatrixMatlab2VectorVNL(const mxArray * pMLA,vnl_vector<double> & V)
00086 {
00087     int nRows = mxGetM(pMLA);
00088     int nCols = mxGetN(pMLA);
00089     
00090     if(! ((nCols==1)^(nRows==1)) )
00091     {
00092         mexPrintf("MatrixMatlab2VectorVNL::Error pMLA is not a vector\n");
00093         return false;
00094     }
00095     
00096     
00097     int nEls = nRows*nCols;
00098     if(V.size()!=nEls)
00099     {
00100         V.set_size(nEls);
00101     }
00102     
00103     double *pData = mxGetPr(pMLA);
00104     
00105     for(int i = 0;i<nEls;i++)
00106         V[i] = pData[i];
00107     
00108     
00109     return true;
00110 }
00111 
00112 bool MatrixMatlab2MatrixVNL(const mxArray * pMLA,vnl_matrix<double> & M)
00113 {
00114     int nRows = mxGetM(pMLA);
00115     int nCols = mxGetN(pMLA);
00116     
00117     //    mexPrintf("Matlab::Matrix is %d by %d \n", nRows,nCols);
00118     
00119     
00120     if(M.rows()!=nRows || M.cols()!=nCols)
00121     {
00122         M.set_size(nRows,nCols);
00123     }
00124     
00125     //  mexPrintf("VNL::Matrix is %d by %d \n", M.rows(),M.cols());
00126     
00127     double *pData = mxGetPr(pMLA);
00128     for(int i = 0;i<nRows;i++)
00129     {
00130         for(int j = 0;j<nCols;j++)
00131         {    
00132             double dfVal = pData[ (j)*nRows+i];
00133             M(i,j) = dfVal;
00134             //            mexPrintf("VNL::M[%d][%d] = %g\n",i,j,dfVal);
00135         }
00136     }
00137     
00138     //    mexPrintf("VNL Matrix is %d by %d and e(0,0) is %f\n", M.rows(),M.cols(),M(0,0));
00139     
00140     return true;
00141 }
00142 
00143 bool MatrixVNL2MatrixMatlab( mxArray * & pMLA,const vnl_matrix<double> & M)
00144 {
00145     int nRows = M.rows();
00146     int nCols = M.cols();
00147     
00148     pMLA  = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
00149     double *pData = mxGetPr(pMLA);
00150     
00151     for(int i = 0;i<nRows;i++)
00152     {
00153         for(int j = 0;j<nCols;j++)
00154         {    
00155             pData[ (j)*nRows+i] = M(i,j);
00156         }
00157     }
00158     
00159     return true;
00160 }
00161 
00162 bool VectorVNL2MatrixMatlab( mxArray * & pMLA,const vnl_vector<double> & V)
00163 {
00164     int nRows = V.size();
00165     
00166     pMLA  = mxCreateDoubleMatrix(V.size(), 1, mxREAL);
00167     double *pData = mxGetPr(pMLA);
00168     for(int i = 0;i<nRows;i++)
00169     {
00170         pData[i] = V[i];
00171     }
00172     
00173     return true;
00174 }
00175 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines