matlab_format.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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. #ifndef IGL_MATLAB_FORMAT_H
  9. #define IGL_MATLAB_FORMAT_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <Eigen/Sparse>
  13. #include <string>
  14. namespace igl
  15. {
  16. // This is a routine to print a matrix using format suitable for pasting into
  17. // the matlab IDE
  18. //
  19. // Templates:
  20. // DerivedM e.g. derived from MatrixXd
  21. // Input:
  22. // input some matrix to be formated
  23. // name name of matrix
  24. // Returns Formated matrix
  25. //
  26. // Example:
  27. // // M := [1 2 3;4 5 6];
  28. // cout<<matlab_format(M)<<endl;
  29. // // Prints:
  30. // // [
  31. // // 1 2 3
  32. // // 4 5 6
  33. // // ];
  34. // cout<<matlab_format(M,"M")<<endl;
  35. // // Prints:
  36. // // M = [
  37. // // 1 2 3
  38. // // 4 5 6
  39. // // ];
  40. template <typename DerivedM>
  41. IGL_INLINE const Eigen::WithFormat< DerivedM > matlab_format(
  42. const Eigen::PlainObjectBase<DerivedM> & M,
  43. const std::string name = "");
  44. // Same but for sparse matrices. Print IJV format into an auxillary variable
  45. // and then print a call to sparse which will construct the sparse matrix
  46. // Example:
  47. // // S := [0 2 3;4 5 0];
  48. // cout<<matlab_format(S,"S")<<endl;
  49. // // Prints:
  50. // // SIJV = [
  51. // // 2 1 4
  52. // // 1 2 2
  53. // // 2 2 5
  54. // // 1 3 3
  55. // // ];
  56. // // S = sparse(SIJV(:,1),SIJV(:,2),SIJV(:,3));
  57. //
  58. template <typename DerivedS>
  59. IGL_INLINE const std::string matlab_format(
  60. const Eigen::SparseMatrix<DerivedS> & S,
  61. const std::string name = "");
  62. // Return just IOFormat
  63. //
  64. // Example:
  65. // // M := [1 2 3;4 5 6];
  66. // cout<<M.format(matlab_format())<<endl;
  67. // // Prints:
  68. // // [
  69. // // 1 2 3
  70. // // 4 5 6
  71. // // ];
  72. IGL_INLINE Eigen::IOFormat matlab_format();
  73. }
  74. #ifndef IGL_STATIC_LIBRARY
  75. # include "matlab_format.cpp"
  76. #endif
  77. #endif