prepare_lhs.cpp 3.7 KB

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