matlab_format.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "matlab_format.h"
  2. #include "STR.h"
  3. #include "find.h"
  4. template <typename DerivedM>
  5. IGL_INLINE const Eigen::WithFormat< DerivedM > igl::matlab_format(
  6. const Eigen::PlainObjectBase<DerivedM> & M,
  7. const std::string name = "")
  8. {
  9. using namespace igl;
  10. using namespace std;
  11. string prefix = "";
  12. if(!name.empty())
  13. {
  14. prefix = name + " = ";
  15. }
  16. return M.format(Eigen::IOFormat(
  17. Eigen::FullPrecision,
  18. 0,
  19. " ",
  20. "\n",
  21. "",
  22. "",
  23. // This seems like a bit of a hack since I would expect the rows to align
  24. // with out this extra spacing on the first line
  25. prefix + "[\n ",
  26. "\n];"));
  27. }
  28. template <typename DerivedS>
  29. IGL_INLINE const std::string
  30. igl::matlab_format(
  31. const Eigen::SparseMatrix<DerivedS> & S,
  32. const std::string name = "")
  33. {
  34. using namespace Eigen;
  35. using namespace igl;
  36. using namespace std;
  37. Matrix<typename Eigen::SparseMatrix<DerivedS>::Scalar,Dynamic,1> I,J,V;
  38. Matrix<DerivedS,Dynamic,Dynamic> SIJV;
  39. find(S,I,J,V);
  40. I.array() += 1;
  41. J.array() += 1;
  42. SIJV.resize(V.rows(),3);
  43. SIJV << I,J,V;
  44. string prefix = "";
  45. string suffix = "";
  46. if(!name.empty())
  47. {
  48. prefix = name + "IJV = ";
  49. suffix = "\n"+name + " = sparse("+name+"IJV(:,1),"+name+"IJV(:,2),"+name+"IJV(:,3));";
  50. }
  51. return STR(""<<
  52. SIJV.format(Eigen::IOFormat(
  53. Eigen::FullPrecision,
  54. 0,
  55. " ",
  56. "\n",
  57. "",
  58. "",
  59. // This seems like a bit of a hack since I would expect the rows to align
  60. // with out this extra spacing on the first line
  61. prefix + "[\n ",
  62. "\n];"))<<suffix);
  63. }
  64. IGL_INLINE Eigen::IOFormat igl::matlab_format()
  65. {
  66. // M = [1 2 3;4 5 6];
  67. // M.format(matlab_format()) produces:
  68. // [
  69. // 1 2 3
  70. // 4 5 6
  71. // ];
  72. return Eigen::IOFormat(
  73. Eigen::FullPrecision,
  74. 0,
  75. " ",
  76. "\n",
  77. "",
  78. "",
  79. // This seems like a bit of a hack since I would expect the rows to align
  80. // with out this extra spacing on the first line
  81. "[\n ",
  82. "\n];");
  83. }
  84. #ifndef IGL_HEADER_ONLY
  85. // Explicit template instanciations
  86. template std::basic_string<char, std::char_traits<char>, std::allocator<char> > const igl::matlab_format<double>(Eigen::SparseMatrix<double, 0, int> const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  87. template Eigen::WithFormat<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const igl::matlab_format<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::string);
  88. template Eigen::WithFormat<Eigen::Array<int, -1, -1, 0, -1, -1> > const igl::matlab_format<Eigen::Array<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Array<int, -1, -1, 0, -1, -1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  89. template Eigen::WithFormat<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const igl::matlab_format<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  90. #endif