1 // This file is part of libigl, a simple c++ geometry processing library.
2 //
3 // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public License
6 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
7 // obtain one at http://mozilla.org/MPL/2.0/.
8 #include "prepare_lhs.h"
9 #include <algorithm>
10 template <typename DerivedV>
prepare_lhs_double(const Eigen::PlainObjectBase<DerivedV> & V,mxArray * plhs[])11 IGL_INLINE void igl::matlab::prepare_lhs_double(
12 const Eigen::PlainObjectBase<DerivedV> & V,
13 mxArray *plhs[])
14 {
15 using namespace std;
16 using namespace Eigen;
17 const int m = V.rows();
18 const int n = V.cols();
19 plhs[0] = mxCreateDoubleMatrix(m,n, mxREAL);
20 Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> >
21 map(mxGetPr(plhs[0]),m,n);
22 map = V.template cast<double>();
23 }
24
25 template <typename DerivedV>
prepare_lhs_logical(const Eigen::PlainObjectBase<DerivedV> & V,mxArray * plhs[])26 IGL_INLINE void igl::matlab::prepare_lhs_logical(
27 const Eigen::PlainObjectBase<DerivedV> & V,
28 mxArray *plhs[])
29 {
30 using namespace std;
31 using namespace Eigen;
32 const int m = V.rows();
33 const int n = V.cols();
34 plhs[0] = mxCreateLogicalMatrix(m,n);
35 mxLogical * Vp = static_cast<mxLogical*>(mxGetData(plhs[0]));
36 Eigen::Map< Eigen::Matrix<mxLogical,Eigen::Dynamic,Eigen::Dynamic> >
37 map(static_cast<mxLogical*>(mxGetData(plhs[0])),m,n);
38 map = V.template cast<mxLogical>();
39 }
40
41 template <typename DerivedV>
prepare_lhs_index(const Eigen::PlainObjectBase<DerivedV> & V,mxArray * plhs[])42 IGL_INLINE void igl::matlab::prepare_lhs_index(
43 const Eigen::PlainObjectBase<DerivedV> & V,
44 mxArray *plhs[])
45 {
46 // Treat indices as reals
47 const auto Vd = (V.template cast<double>().array()+1).eval();
48 return prepare_lhs_double(Vd,plhs);
49 }
50
51 template <typename Vtype>
prepare_lhs_double(const Eigen::SparseMatrix<Vtype> & M,mxArray * plhs[])52 IGL_INLINE void igl::matlab::prepare_lhs_double(
53 const Eigen::SparseMatrix<Vtype> & M,
54 mxArray *plhs[])
55 {
56 using namespace std;
57 const int m = M.rows();
58 const int n = M.cols();
59 // THIS WILL NOT WORK FOR ROW-MAJOR
60 assert(n==M.outerSize());
61 const int nzmax = M.nonZeros();
62 plhs[0] = mxCreateSparse(m, n, nzmax, mxREAL);
63 mxArray * mx_data = plhs[0];
64 // Copy data immediately
65 double * pr = mxGetPr(mx_data);
66 mwIndex * ir = mxGetIr(mx_data);
67 mwIndex * jc = mxGetJc(mx_data);
68
69 // Iterate over outside
70 int k = 0;
71 for(int j=0; j<M.outerSize();j++)
72 {
73 jc[j] = k;
74 // Iterate over inside
75 for(typename Eigen::SparseMatrix<Vtype>::InnerIterator it (M,j); it; ++it)
76 {
77 // copy (cast to double)
78 pr[k] = it.value();
79 ir[k] = it.row();
80 k++;
81 }
82 }
83 jc[M.outerSize()] = k;
84
85 }
86
87 #ifdef IGL_STATIC_LIBRARY
88 template void igl::matlab::prepare_lhs_index<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
89 template void igl::matlab::prepare_lhs_index<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
90 template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, mxArray_tag**);
91 template void igl::matlab::prepare_lhs_index<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, mxArray_tag**);
92 template void igl::matlab::prepare_lhs_logical<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
93 template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
94 template void igl::matlab::prepare_lhs_logical<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
95 template void igl::matlab::prepare_lhs_index<Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, mxArray_tag**);
96 template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, mxArray_tag**);
97 template void igl::matlab::prepare_lhs_double<Eigen::Matrix<int, 1, -1, 1, 1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, 1, -1, 1, 1, -1> > const&, mxArray_tag**);
98 template void igl::matlab::prepare_lhs_double<Eigen::Matrix<int, 1, 3, 1, 1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, mxArray_tag**);
99 #endif
100