prepare_lhs.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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>
  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. template <typename DerivedV>
  25. IGL_INLINE void igl::matlab::prepare_lhs_logical(
  26. const Eigen::PlainObjectBase<DerivedV> & V,
  27. mxArray *plhs[])
  28. {
  29. using namespace std;
  30. using namespace Eigen;
  31. const int m = V.rows();
  32. const int n = V.cols();
  33. plhs[0] = mxCreateLogicalMatrix(m,n);
  34. mxLogical * Vp = static_cast<mxLogical*>(mxGetData(plhs[0]));
  35. Eigen::Map< Eigen::Matrix<mxLogical,Eigen::Dynamic,Eigen::Dynamic> >
  36. map(static_cast<mxLogical*>(mxGetData(plhs[0])),m,n);
  37. map = V.template cast<mxLogical>();
  38. }
  39. template <typename DerivedV>
  40. IGL_INLINE void igl::matlab::prepare_lhs_index(
  41. const Eigen::PlainObjectBase<DerivedV> & V,
  42. mxArray *plhs[])
  43. {
  44. // Treat indices as reals
  45. const auto Vd = (V.template cast<double>().array()+1).eval();
  46. return prepare_lhs_double(Vd,plhs);
  47. }
  48. template <typename Vtype>
  49. IGL_INLINE void igl::matlab::prepare_lhs_double(
  50. const Eigen::SparseMatrix<Vtype> & M,
  51. mxArray *plhs[])
  52. {
  53. using namespace std;
  54. const int m = M.rows();
  55. const int n = M.cols();
  56. // THIS WILL NOT WORK FOR ROW-MAJOR
  57. assert(n==M.outerSize());
  58. const int nzmax = M.nonZeros();
  59. plhs[0] = mxCreateSparse(m, n, nzmax, mxREAL);
  60. mxArray * mx_data = plhs[0];
  61. // Copy data immediately
  62. double * pr = mxGetPr(mx_data);
  63. mwIndex * ir = mxGetIr(mx_data);
  64. mwIndex * jc = mxGetJc(mx_data);
  65. // Iterate over outside
  66. int k = 0;
  67. for(int j=0; j<M.outerSize();j++)
  68. {
  69. jc[j] = k;
  70. // Iterate over inside
  71. for(typename Eigen::SparseMatrix<Vtype>::InnerIterator it (M,j); it; ++it)
  72. {
  73. // copy (cast to double)
  74. pr[k] = it.value();
  75. ir[k] = it.row();
  76. k++;
  77. }
  78. }
  79. jc[M.outerSize()] = k;
  80. }
  81. #ifdef IGL_STATIC_LIBRARY
  82. 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**);
  83. 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**);
  84. 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**);
  85. 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**);
  86. 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**);
  87. 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**);
  88. 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**);
  89. 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**);
  90. 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**);
  91. 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**);
  92. 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**);
  93. #endif