parse_rhs.cpp 3.0 KB

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