parse_rhs.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "parse_rhs.h"
  9. #include <algorithm>
  10. template <typename DerivedV>
  11. IGL_INLINE void igl::matlab::parse_rhs_double(
  12. const mxArray *prhs[],
  13. Eigen::PlainObjectBase<DerivedV> & V)
  14. {
  15. // Use Eigen's map and cast to copy
  16. V = Eigen::Map< Eigen::Matrix<double ,Eigen::Dynamic ,Eigen::Dynamic> >
  17. (mxGetPr(prhs[0]),mxGetM(prhs[0]),mxGetN(prhs[0]))
  18. .cast<typename DerivedV::Scalar>();
  19. }
  20. template <typename DerivedV>
  21. IGL_INLINE void igl::matlab::parse_rhs_index(
  22. const mxArray *prhs[],
  23. Eigen::PlainObjectBase<DerivedV> & V)
  24. {
  25. parse_rhs_double(prhs,V);
  26. V.array() -= 1;
  27. }
  28. template <typename MT>
  29. IGL_INLINE void igl::matlab::parse_rhs(
  30. const mxArray *prhs[],
  31. Eigen::SparseMatrix<MT> & M)
  32. {
  33. const mxArray * mx_data = prhs[0];
  34. // Handle boring case where matrix is actually an empty dense matrix
  35. if(mxGetNumberOfElements(mx_data) == 0)
  36. {
  37. M.resize(0,0);
  38. return;
  39. }
  40. assert(mxIsSparse(mx_data));
  41. assert(mxGetNumberOfDimensions(mx_data) == 2);
  42. //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
  43. const int m = mxGetM(mx_data);
  44. const int n = mxGetN(mx_data);
  45. // TODO: It should be possible to directly load the data into the sparse
  46. // matrix without going through the triplets
  47. // Copy data immediately
  48. double * pr = mxGetPr(mx_data);
  49. mwIndex * ir = mxGetIr(mx_data);
  50. mwIndex * jc = mxGetJc(mx_data);
  51. std::vector<Eigen::Triplet<MT> > MIJV;
  52. MIJV.reserve(mxGetNumberOfElements(mx_data));
  53. // Iterate over outside
  54. int k = 0;
  55. for(int j=0; j<n;j++)
  56. {
  57. // Iterate over inside
  58. while(k<(int)jc[j+1])
  59. {
  60. //cout<<ir[k]<<" "<<j<<" "<<pr[k]<<endl;
  61. assert((int)ir[k]<m);
  62. assert((int)j<n);
  63. MIJV.push_back(Eigen::Triplet<MT >(ir[k],j,pr[k]));
  64. k++;
  65. }
  66. }
  67. M.resize(m,n);
  68. M.setFromTriplets(MIJV.begin(),MIJV.end());
  69. }
  70. #ifdef IGL_STATIC_LIBRARY
  71. template void igl::matlab::parse_rhs_index<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  72. template void igl::matlab::parse_rhs_index<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  73. template void igl::matlab::parse_rhs_double<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  74. template void igl::matlab::parse_rhs_index<Eigen::Matrix<int, -1, 3, 1, -1, 3> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  75. template void igl::matlab::parse_rhs_double<Eigen::Matrix<double, -1, 3, 1, -1, 3> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&);
  76. template void igl::matlab::parse_rhs_double<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  77. template void igl::matlab::parse_rhs_double<Eigen::Matrix<float, -1, -1, 0, -1, -1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  78. #endif