matlab_format.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <vector>
  15. namespace igl
  16. {
  17. /// This is a routine to print a matrix using format suitable for pasting into
  18. /// the matlab IDE
  19. ///
  20. /// @tparam DerivedM e.g. derived from Eigen::MatrixXd
  21. /// @param[in] input some matrix to be formatted
  22. /// @param[in] name name of matrix (optional)
  23. /// @return Formatted matrix
  24. ///
  25. /// #### Example:
  26. /// \code{cpp}
  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. /// \endcode
  41. template <typename DerivedM>
  42. IGL_INLINE const Eigen::WithFormat< DerivedM > matlab_format(
  43. const Eigen::DenseBase<DerivedM> & M,
  44. const std::string name = "");
  45. /// \overload
  46. ///
  47. /// \brief Add +1 to every entry before formatting.
  48. template <typename DerivedM>
  49. IGL_INLINE std::string matlab_format_index(
  50. const Eigen::MatrixBase<DerivedM> & M,
  51. const std::string name = "");
  52. template <typename T>
  53. IGL_INLINE std::string matlab_format_index(
  54. const std::vector<T> & Mvec,
  55. const std::string name = "");
  56. /// Same but for sparse matrices. Print IJV format into an auxiliary variable
  57. /// and then print a call to sparse which will construct the sparse matrix
  58. ///
  59. /// #### Example:
  60. /// \code{cpp}
  61. /// // S := [0 2 3;4 5 0];
  62. /// cout<<matlab_format(S,"S")<<endl;
  63. /// // Prints:
  64. /// // SIJV = [
  65. /// // 2 1 4
  66. /// // 1 2 2
  67. /// // 2 2 5
  68. /// // 1 3 3
  69. /// // ];
  70. /// // S = sparse(SIJV(:,1),SIJV(:,2),SIJV(:,3));
  71. /// \endcode
  72. ///
  73. template <typename DerivedS>
  74. IGL_INLINE const std::string matlab_format(
  75. const Eigen::SparseMatrix<DerivedS> & S,
  76. const std::string name = "");
  77. /// \overload
  78. /// \brief Scalars.
  79. IGL_INLINE const std::string matlab_format(
  80. const double v,
  81. const std::string name = "");
  82. /// \overload
  83. IGL_INLINE const std::string matlab_format(
  84. const float v,
  85. const std::string name = "");
  86. /// Just build and return the format.
  87. /// @return eigen IOFormat object
  88. ///
  89. /// #### Example:
  90. /// \code{cpp}
  91. /// // M := [1 2 3;4 5 6];
  92. /// cout<<M.format(matlab_format())<<endl;
  93. /// // Prints:
  94. /// // [
  95. /// // 1 2 3
  96. /// // 4 5 6
  97. /// // ];
  98. /// \endcode
  99. IGL_INLINE Eigen::IOFormat matlab_format();
  100. }
  101. #ifndef IGL_STATIC_LIBRARY
  102. # include "matlab_format.cpp"
  103. #endif
  104. #endif