Browse Source

use igl::prepare_lhs in mex example

Former-commit-id: a79c08c6fed7f5069ff52da9c8cf3360846bdb96
Alec Jacobson 11 years ago
parent
commit
08c3d95ed6
1 changed files with 20 additions and 63 deletions
  1. 20 63
      tutorial/603_MEX/readOBJ_mex.cpp

+ 20 - 63
tutorial/603_MEX/readOBJ_mex.cpp

@@ -1,57 +1,11 @@
 #include "mex.h"
 
 #define IGL_HEADER_ONLY
-#include <Eigen/Core>
 #include <igl/readOBJ.h>
+#include <igl/matlab/prepare_lhs.h>
+#include <Eigen/Core>
 
 
-using namespace std;
-using namespace Eigen;
-
-extern void _main();
-
-Eigen::MatrixXd readMatrix(const mxArray* mat)
-{
-  double* ptr = mxGetPr(mat);
-
-  int m = mxGetM(mat);
-  int n = mxGetN(mat);
-
-  Eigen::MatrixXd	V;
-  V.resize(m,n);
-  for(int j=0;j<n;j++)
-    for(int i=0;i<m;++i)
-      V(i,j) = *(ptr++);
-
-  return V;
-}
-
-mxArray* writeMatrix(const MatrixXd& mat)
-{
-  mxArray* ret = 0;
-
-  if (mat.size() == 0)
-  {
-    ret =	mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
-  }
-  else
-  {
-    int M = mat.rows();
-    int N = mat.cols();
-
-    ret = mxCreateNumericMatrix(M, N, mxDOUBLE_CLASS, mxREAL);
-    double* pointer = mxGetPr(ret);
-
-    /* Copy data into the mxArray */
-    int count = 0;
-    for ( int j = 0; j < N; ++j )
-      for (int i = 0; i < M; ++i)
-        pointer[count++] = mat(i,j);
-  }
-
-  return ret;
-}
-
 void mexFunction(
      int          nlhs,
      mxArray      *plhs[],
@@ -59,15 +13,13 @@ void mexFunction(
      const mxArray *prhs[]
      )
 {
+  using namespace Eigen;
   /* Check for proper number of arguments */
 
-  if (nrhs != 1) {
-  mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin",
-      "readOBJ requires 1 input arguments, the path of the file to open");
-  }
-  else if (nlhs != 2) {
-  mexErrMsgIdAndTxt("MATLAB:mexcpp:nargout",
-      "readOBJ generates two output argument, V and F.");
+  if (nrhs != 1) 
+  {
+    mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin",
+        "readOBJ requires 1 input arguments, the path of the file to open");
   }
 
   // Read the file path
@@ -77,14 +29,19 @@ void mexFunction(
   MatrixXi F;
 
   // Read the mesh
-  igl::readOBJ(file_path,V,F);
-
-  // Matlab is indexing matrices from 1
-  F = F.array() + 1;
-
-  // Return the matrices to matlab, after converting them to double
-  plhs[0] = writeMatrix(V);
-  plhs[1] = writeMatrix(F.cast<double>());
+  if(!igl::readOBJ(file_path,V,F))
+  {
+    mexErrMsgIdAndTxt("MATLAB:mexcpp:fileio", "igl::readOBJ failed.");
+  }
+  // Return the matrices to matlab
+  switch(nlhs)
+  {
+    case 2:
+      igl::prepare_lhs_index(F,plhs+1);
+    case 1:
+      igl::prepare_lhs_double(V,plhs);
+    default: break;
+  }
 
   return;
 }