matlab_format.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 specialization
  86. // generated by autoexplicit.sh
  87. template Eigen::WithFormat<Eigen::Matrix<int, 4, 1, 0, 4, 1> > const igl::matlab_format<Eigen::Matrix<int, 4, 1, 0, 4, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, 4, 1, 0, 4, 1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  88. 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> >);
  89. 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);
  90. 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> >);
  91. 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> >);
  92. 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> >);
  93. 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::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  94. 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> >);
  95. template Eigen::WithFormat<Eigen::Matrix<double, 4, 4, 0, 4, 4> > const igl::matlab_format<Eigen::Matrix<double, 4, 4, 0, 4, 4> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 4, 4, 0, 4, 4> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  96. template Eigen::WithFormat<Eigen::Matrix<double, -1, 4, 0, -1, 4> > const igl::matlab_format<Eigen::Matrix<double, -1, 4, 0, -1, 4> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 4, 0, -1, 4> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  97. template Eigen::WithFormat<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const igl::matlab_format<Eigen::Matrix<double, -1, 2, 0, -1, 2> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >);
  98. #endif