Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MatLabInterface.hh
1 #ifndef __MATLABINTERFACE_HH__
2 #define __MATLABINTERFACE_HH__
3 
4 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
5 
6  Copyright (C) 2003-2009 (see file CONTACT for details)
7  Multimediale Systeme der Informationsverarbeitung
8  Institut fuer Informatik
9  Christian-Albrechts-Universitaet Kiel
10 
11 
12  BIAS is free software; you can redistribute it and/or modify
13  it under the terms of the GNU Lesser General Public License as published by
14  the Free Software Foundation; either version 2.1 of the License, or
15  (at your option) any later version.
16 
17  BIAS is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU Lesser General Public License for more details.
21 
22  You should have received a copy of the GNU Lesser General Public License
23  along with BIAS; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26 
27 /******************************************************************************/
28 
29 /**
30  * This header file provides some functions (and a little howto) that should
31  * make the usage of BIAS-libraries from within MATLAB ((C) by The MathWorks)
32  * a bit easier.
33  *
34  * All that is needed to be done to use C/C++ code from within MATLAB is to
35  * write a small (shared) library -called mex-file- that exports the the
36  * function:
37  *
38  * void mexFunction(int nlhs, mxArray *plhs[],
39  * int nrhs, const mxArray *prhs[])
40  *
41  * param nlhs: the number of returned vectors/matrices
42  * param plhs: array of pointers to the returned vectors/matrices
43  *
44  * param nrhs: the number of vectors/matrices taken as arguments
45  * param prhs: array of pointers to the vectors/matrices taken as arguments
46  *
47  * This function correspondes to a call from MATLAB:
48  * [ plhs[0] .. plhs[nlhs-1] ] = mexFileName(prhs[0], .. , prhs[nrhs-1])
49  *
50  * When the lib has been build (e.g. libMyLib.so) you'll have to rename it to
51  * the form NameOfYourFunc.mexglx (Linux) or NameOfYourFunc.dll (Windows).
52  * Afterwards copy it to your matlab workspace folder.
53  * Be shure that MATLAB is able to find all libs needed to run your mex-file.
54  * For Linux you might include the BIAS-library path in the LD_LIBRARY_PATH
55  * environment variable. Also it's advisable to include the standard-c libraries
56  * into the lib-path (e.g. /usr/lib/ for linux) because else MATLAB might load
57  * wrong dependencies.
58  *
59  * See the Examples-directory for a simple mex-library example.
60  *
61  * IMPORTANT: For now there's a problem with the OpenMP-library to be loaded
62  * by MATLAB. So be sure to turn off OpenMP in BIAS (->CMake).
63  *
64  * Author: apetersen 04/04/2008
65  *
66  * TODO:
67  * -let libExampleMEX.so be copied/moved to ./Examples dir with correct name
68  * (e.g. matMult.mexglx)
69  * -get OpenMP running (should be possible)
70  * -test for windows-builds
71  */
72 
73 //get all definitions
74 #include <bias_config.h>
75 
76 
77 //OpenMP-library not loadable under MATLAB (for now)
78 #ifdef BIAS_HAVE_OPENMP
79 # error MATLAB cannot load OpenMP-lib, please re-configure with USE_OPENMP=OFF
80 #endif //BIAS_HAVE_OPENMP
81 
82 
83 #ifndef BIAS_HAVE_MATLAB
84 # error MATLAB mex-headers are needed, please re-configure with USE_MATLAB=ON
85 #endif //BIAS_HAVE_MATLAB
86 
87 
88 /** The definitions for MATLAB's functions and datatypes */
89 #include <mex.h>
90 
91 #include <Base/Math/Vector.hh>
92 #include <Base/Math/Matrix.hh>
93 
94 /** Use this to deliver error msgs to MATLAB (stops execution) */
95 #define MATLABERR(x) mexErrMsgTxt(x)
96 
97 /** Use this to deliver warning msgs to MATLAB (execution continues) */
98 #define MATLABWARN(x) mexWarnMsgTxt(x)
99 
100 // Some functions for converting types (see example)
101 
102 namespace BIAS {
103 
104  /** @brief Convert MATLAB-array to BIAS::Vector */
105  inline int MxArrToBIASVector(const mxArray* mxVec,
107  {
108  if (mxVec == NULL) {
109  BIASERR("Given mxArray-pointer is NULL!");
110  return -1;
111  }
112 
113  //be sure we have real-valued variables with double precision
114  if (!mxIsDouble(mxVec) || mxIsComplex(mxVec)) {
115  BIASERR("Input is complex or not of double precision!");
116  return -2;
117  }
118 
119  const unsigned int m = mxGetM(mxVec);
120  const unsigned int n = mxGetN(mxVec);
121 
122  //be sure we have a vector (mx1 or 1xn)
123  if (m!=1 && n!=1) {
124  BIASERR("Input is no vector, dim: " <<m<< "x" <<n<<" !");
125  return -3;
126  }
127 
128  //resize vec and copy values
129  vec.newsize((m>n)?m:n);
130  for (unsigned int i=0; i<((m>n)?m:n); i++)
131  vec[i] = mxGetPr(mxVec)[i];
132 
133  return 0;
134  }
135 
136  /** @brief Convert MATLAB-array to BIAS::Matrix */
137  inline int MxArrToBIASMatrix(const mxArray* mxMat,
139  {
140  if (mxMat == NULL) {
141  BIASERR("Given mxArray-pointer is NULL!");
142  return -1;
143  }
144 
145  //be sure we have real-valued variables with double precision
146  if (!mxIsDouble(mxMat) || mxIsComplex(mxMat)) {
147  BIASERR("Input is complex or not of double precision!");
148  return -2;
149  }
150 
151  const unsigned int m = mxGetM(mxMat);
152  const unsigned int n = mxGetN(mxMat);
153 
154  //resize vec and copy values
155  mat.newsize(m ,n);
156  const double* in = mxGetPr(mxMat);
157  for (unsigned int j=0; j<n; j++)
158  for (unsigned int i=0; i<m; i++)
159  mat[i][j] = in[j*m + i];//mxArrays ist 'serialized' in columns
160 
161  return 0;
162  }
163 
164  /** @brief Convert BIAS::Vector to MATLAB-array, inclusive allocation! */
166  mxArray* &mxVec)
167  {
168  if (mxVec != NULL) {
169  BIASERR("Given mxArray-pointer is not NULL, memory leak possible!");
170  return -1;
171  }
172 
173  mxVec = mxCreateDoubleMatrix(vec.Size(), 1, mxREAL);
174 
175  if (mxVec == NULL) {
176  BIASERR("Error allocating mxArray, with (m,n)=("<<vec.Size()
177  <<","<<1<<")");
178  return -2;
179  }
180 
181  for (unsigned int i=0; i<vec.Size(); i++)
182  mxGetPr(mxVec)[i] = vec[i];
183 
184  return 0;
185  }
186 
187  /** @brief Convert BIAS::Matrix to MATLAB-array, inclusive allocation! */
189  mxArray* &mxMat)
190  {
191  if (mxMat != NULL) {
192  BIASERR("Given mxArray-pointer is not NULL, memory leak possible!");
193  return -1;
194  }
195 
196  unsigned int m = mat.GetRows();
197  unsigned int n = mat.GetCols();
198 
199  mxMat = mxCreateDoubleMatrix(m, n, mxREAL);
200 
201  if (mxMat == NULL) {
202  BIASERR("Error allocating mxArray, with (m,n)=("<<m<<","<<n<<")");
203  return -2;
204  }
205 
206  for (unsigned int j=0; j<n; j++)
207  for (unsigned int i=0; i<m; i++)
208  mxGetPr(mxMat)[j*m + i] = mat[i][j];
209 
210  return 0;
211  }
212 
213 } //namespace BIAS
214 
215 #endif //__MATLABINTERFACE_HH__
int MxArrToBIASMatrix(const mxArray *mxMat, BIAS::Matrix< double > &mat)
Convert MATLAB-array to BIAS::Matrix.
int BIASVectorToMxArr(const BIAS::Vector< double > &vec, mxArray *&mxVec)
Convert BIAS::Vector to MATLAB-array, inclusive allocation!
int MxArrToBIASVector(const mxArray *mxVec, BIAS::Vector< double > &vec)
Convert MATLAB-array to BIAS::Vector.
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
int BIASMatrixToMxArr(const BIAS::Matrix< double > &mat, mxArray *&mxMat)
Convert BIAS::Matrix to MATLAB-array, inclusive allocation!
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
unsigned int GetRows() const
Definition: Matrix.hh:202
unsigned int GetCols() const
Definition: Matrix.hh:204