matlab_format.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. /// @tparam DerivedM e.g. derived from MatrixXd
  20. /// @param[in] input some matrix to be formatted
  21. /// @param[in] name name of matrix (optional)
  22. /// @return Formatted matrix
  23. ///
  24. /// #### Example:
  25. /// \code{cpp}
  26. /// // M := [1 2 3;4 5 6];
  27. /// cout<<matlab_format(M)<<endl;
  28. /// // Prints:
  29. /// // [
  30. /// // 1 2 3
  31. /// // 4 5 6
  32. /// // ];
  33. /// cout<<matlab_format(M,"M")<<endl;
  34. /// // Prints:
  35. /// // M = [
  36. /// // 1 2 3
  37. /// // 4 5 6
  38. /// // ];
  39. /// \endcode
  40. template <typename DerivedM>
  41. IGL_INLINE const Eigen::WithFormat< DerivedM > matlab_format(
  42. const Eigen::DenseBase<DerivedM> & M,
  43. const std::string name = "");
  44. /// \overload
  45. ///
  46. /// \brief Add +1 to every entry before formatting.
  47. template <typename DerivedM>
  48. IGL_INLINE std::string matlab_format_index(
  49. const Eigen::MatrixBase<DerivedM> & M,
  50. const std::string name = "");
  51. /// Same but for sparse matrices. Print IJV format into an auxiliary variable
  52. /// and then print a call to sparse which will construct the sparse matrix
  53. ///
  54. /// #### Example:
  55. /// \code{cpp}
  56. /// // S := [0 2 3;4 5 0];
  57. /// cout<<matlab_format(S,"S")<<endl;
  58. /// // Prints:
  59. /// // SIJV = [
  60. /// // 2 1 4
  61. /// // 1 2 2
  62. /// // 2 2 5
  63. /// // 1 3 3
  64. /// // ];
  65. /// // S = sparse(SIJV(:,1),SIJV(:,2),SIJV(:,3));
  66. /// \endcode
  67. ///
  68. template <typename DerivedS>
  69. IGL_INLINE const std::string matlab_format(
  70. const Eigen::SparseMatrix<DerivedS> & S,
  71. const std::string name = "");
  72. /// \overload
  73. /// \brief Scalars.
  74. IGL_INLINE const std::string matlab_format(
  75. const double v,
  76. const std::string name = "");
  77. /// \overload
  78. IGL_INLINE const std::string matlab_format(
  79. const float v,
  80. const std::string name = "");
  81. /// Just build and return the format.
  82. /// @return eigen IOFormat object
  83. ///
  84. /// #### Example:
  85. /// \code{cpp}
  86. /// // M := [1 2 3;4 5 6];
  87. /// cout<<M.format(matlab_format())<<endl;
  88. /// // Prints:
  89. /// // [
  90. /// // 1 2 3
  91. /// // 4 5 6
  92. /// // ];
  93. /// \endcode
  94. IGL_INLINE Eigen::IOFormat matlab_format();
  95. }
  96. #ifndef IGL_STATIC_LIBRARY
  97. # include "matlab_format.cpp"
  98. #endif
  99. #endif