prepare_lhs.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  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::DenseBase<DerivedV> & V,
  13. mxArray *plhs[])
  14. {
  15. const auto m = V.rows();
  16. const auto n = V.cols();
  17. plhs[0] = mxCreateDoubleMatrix(m,n, mxREAL);
  18. Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> >
  19. map(mxGetPr(plhs[0]),m,n);
  20. map = V.template cast<double>();
  21. }
  22. template <typename DerivedV>
  23. IGL_INLINE void igl::matlab::prepare_lhs_logical(
  24. const Eigen::DenseBase<DerivedV> & V,
  25. mxArray *plhs[])
  26. {
  27. const auto m = V.rows();
  28. const auto n = V.cols();
  29. plhs[0] = mxCreateLogicalMatrix(m,n);
  30. Eigen::Map< Eigen::Matrix<mxLogical,Eigen::Dynamic,Eigen::Dynamic> >
  31. map(static_cast<mxLogical*>(mxGetData(plhs[0])),m,n);
  32. map = V.template cast<mxLogical>();
  33. }
  34. template <typename DerivedV>
  35. IGL_INLINE void igl::matlab::prepare_lhs_index(
  36. const Eigen::DenseBase<DerivedV> & V,
  37. mxArray *plhs[])
  38. {
  39. // Treat indices as reals
  40. const auto Vd = (V.template cast<double>().array()+1).eval();
  41. return prepare_lhs_double(Vd,plhs);
  42. }
  43. template <typename Vtype>
  44. IGL_INLINE void igl::matlab::prepare_lhs_double(
  45. const Eigen::SparseMatrix<Vtype> & M,
  46. mxArray *plhs[])
  47. {
  48. const auto m = M.rows();
  49. const auto n = M.cols();
  50. // THIS WILL NOT WORK FOR ROW-MAJOR
  51. assert(n==M.outerSize());
  52. const int nzmax = M.nonZeros();
  53. plhs[0] = mxCreateSparse(m, n, nzmax, mxREAL);
  54. mxArray * mx_data = plhs[0];
  55. // Copy data immediately
  56. double * pr = mxGetPr(mx_data);
  57. mwIndex * ir = mxGetIr(mx_data);
  58. mwIndex * jc = mxGetJc(mx_data);
  59. // Iterate over outside
  60. int k = 0;
  61. for(int j=0; j<M.outerSize();j++)
  62. {
  63. jc[j] = k;
  64. // Iterate over inside
  65. for(typename Eigen::SparseMatrix<Vtype>::InnerIterator it (M,j); it; ++it)
  66. {
  67. // copy (cast to double)
  68. pr[k] = it.value();
  69. ir[k] = it.row();
  70. k++;
  71. }
  72. }
  73. jc[M.outerSize()] = k;
  74. }
  75. template <typename Vtype>
  76. IGL_INLINE void igl::matlab::prepare_lhs_double(
  77. const std::vector<Vtype> & V,
  78. mxArray *plhs[])
  79. {
  80. plhs[0] = mxCreateCellMatrix(V.size(), 1);
  81. for(int i=0; i<V.size(); i++)
  82. {
  83. const auto m = V[i].rows();
  84. const auto n = V[i].cols();
  85. mxArray * ai = mxCreateDoubleMatrix(m,n, mxREAL);
  86. Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> >
  87. map(mxGetPr(ai),m,n);
  88. map = V[i].template cast<double>();
  89. mxSetCell(plhs[0],i,ai);
  90. }
  91. }
  92. #ifdef IGL_STATIC_LIBRARY
  93. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, 3, 3, 0, 3, 3> >(Eigen::DenseBase<Eigen::Matrix<double, 3, 3, 0, 3, 3> > const&, mxArray_tag**);
  94. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::DenseBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, mxArray_tag**);
  95. template void igl::matlab::prepare_lhs_index<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
  96. template void igl::matlab::prepare_lhs_index<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
  97. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::DenseBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, mxArray_tag**);
  98. template void igl::matlab::prepare_lhs_index<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, mxArray_tag**);
  99. template void igl::matlab::prepare_lhs_logical<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
  100. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
  101. template void igl::matlab::prepare_lhs_logical<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
  102. template void igl::matlab::prepare_lhs_index<Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, mxArray_tag**);
  103. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, 3, 1, -1, 3> >(Eigen::DenseBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, mxArray_tag**);
  104. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<int, 1, -1, 1, 1, -1> >(Eigen::DenseBase<Eigen::Matrix<int, 1, -1, 1, 1, -1> > const&, mxArray_tag**);
  105. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<int, 1, 3, 1, 1, 3> >(Eigen::DenseBase<Eigen::Matrix<int, 1, 3, 1, 1, 3> > const&, mxArray_tag**);
  106. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<float, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
  107. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<float, -1, 3, 0, -1, 3> >(Eigen::DenseBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> > const&, mxArray_tag**);
  108. template void igl::matlab::prepare_lhs_double<double>(Eigen::SparseMatrix<double, 0, int> const&, mxArray_tag**);
  109. template void igl::matlab::prepare_lhs_double<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::vector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, std::allocator<Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&, mxArray_tag**);
  110. #endif