prepare_lhs.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. plhs[0] = mxCreateDoubleMatrix(V.rows(),V.cols(), mxREAL);
  18. double * Vp = mxGetPr(plhs[0]);
  19. typedef typename DerivedV::Scalar Scalar;
  20. const Scalar * V_data;
  21. Matrix<Scalar, DerivedV::ColsAtCompileTime, DerivedV::RowsAtCompileTime, RowMajor> VT;
  22. if(DerivedV::IsRowMajor)
  23. {
  24. VT = V.transpose();
  25. V_data = VT.data();
  26. }else
  27. {
  28. V_data = V.data();
  29. }
  30. copy(V_data,V_data+V.size(),Vp);
  31. }
  32. template <typename DerivedV>
  33. IGL_INLINE void igl::matlab::prepare_lhs_logical(
  34. const Eigen::PlainObjectBase<DerivedV> & V,
  35. mxArray *plhs[])
  36. {
  37. using namespace std;
  38. using namespace Eigen;
  39. plhs[0] = mxCreateLogicalMatrix(V.rows(),V.cols());
  40. mxLogical * Vp = static_cast<mxLogical*>(mxGetData(plhs[0]));
  41. typedef typename DerivedV::Scalar Scalar;
  42. const Scalar * V_data;
  43. Matrix<Scalar, DerivedV::ColsAtCompileTime, DerivedV::RowsAtCompileTime, RowMajor> VT;
  44. if(DerivedV::IsRowMajor)
  45. {
  46. VT = V.transpose();
  47. V_data = VT.data();
  48. }else
  49. {
  50. V_data = V.data();
  51. }
  52. copy(V_data,V_data+V.size(),Vp);
  53. }
  54. template <typename DerivedV>
  55. IGL_INLINE void igl::matlab::prepare_lhs_index(
  56. const Eigen::PlainObjectBase<DerivedV> & V,
  57. mxArray *plhs[])
  58. {
  59. // Treat indices as reals
  60. const auto Vd = (V.template cast<double>().array()+1).eval();
  61. return prepare_lhs_double(Vd,plhs);
  62. }
  63. #ifdef IGL_STATIC_LIBRARY
  64. 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**);
  65. 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**);
  66. 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**);
  67. 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**);
  68. 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**);
  69. 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**);
  70. #endif