prepare_lhs.cpp 5.7 KB

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