Browse Source

matlab formating for printing

Former-commit-id: 0d0226951f23e64e0c650519b4bdc095a9f46642
Alec Jacobson (jalec 12 years ago
parent
commit
fb286e6f8f
3 changed files with 168 additions and 0 deletions
  1. 1 0
      include/igl/find.cpp
  2. 92 0
      include/igl/matlab_format.cpp
  3. 75 0
      include/igl/matlab_format.h

+ 1 - 0
include/igl/find.cpp

@@ -57,5 +57,6 @@ IGL_INLINE void igl::find(
 // Explicit template specialization
 // generated by autoexplicit.sh
 template void igl::find<double, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
+template void igl::find<double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
 // generated by autoexplicit.sh
 #endif

+ 92 - 0
include/igl/matlab_format.cpp

@@ -0,0 +1,92 @@
+#include "matlab_format.h"
+#include "STR.h"
+#include "find.h"
+
+template <typename DerivedM>
+IGL_INLINE const Eigen::WithFormat< DerivedM > igl::matlab_format(
+  const Eigen::PlainObjectBase<DerivedM> & M,
+  const std::string name = "")
+{
+  using namespace igl;
+  using namespace std;
+  string prefix = "";
+  if(!name.empty())
+  {
+    prefix = name + " = ";
+  }
+  return M.format(Eigen::IOFormat(
+    Eigen::FullPrecision,
+    0,
+    " ",
+    "\n",
+    "",
+    "",
+    // This seems like a bit of a hack since I would expect the rows to align
+    // with out this extra spacing on the first line
+    prefix + "[\n  ",
+    "\n];"));
+}
+
+template <typename DerivedS>
+IGL_INLINE const std::string
+igl::matlab_format(
+  const Eigen::SparseMatrix<DerivedS> & S,
+  const std::string name = "")
+{
+  using namespace Eigen;
+  using namespace igl;
+  using namespace std;
+  Matrix<typename Eigen::SparseMatrix<DerivedS>::Scalar,Dynamic,1> I,J,V;
+  Matrix<DerivedS,Dynamic,Dynamic> SIJV;
+  find(S,I,J,V);
+  I.array() += 1;
+  J.array() += 1;
+  SIJV.resize(V.rows(),3);
+  SIJV << I,J,V;
+  string prefix = "";
+  string suffix = "";
+  if(!name.empty())
+  {
+    prefix = name + "IJV = ";
+    suffix = "\n"+name + " = sparse("+name+"IJV(:,1),"+name+"IJV(:,2),"+name+"IJV(:,3));";
+  }
+  return STR(""<<
+    SIJV.format(Eigen::IOFormat(
+    Eigen::FullPrecision,
+    0,
+    " ",
+    "\n",
+    "",
+    "",
+    // This seems like a bit of a hack since I would expect the rows to align
+    // with out this extra spacing on the first line
+    prefix + "[\n  ",
+    "\n];"))<<suffix);
+}
+
+IGL_INLINE Eigen::IOFormat igl::matlab_format()
+{
+  // M = [1 2 3;4 5 6];
+  // M.format(matlab_format()) produces:
+  // [
+  //   1 2 3
+  //   4 5 6
+  // ];
+  return Eigen::IOFormat(
+    Eigen::FullPrecision,
+    0,
+    " ",
+    "\n",
+    "",
+    "",
+    // This seems like a bit of a hack since I would expect the rows to align
+    // with out this extra spacing on the first line
+    "[\n  ",
+    "\n];");
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template instanciations
+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> >);
+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);
+#endif

+ 75 - 0
include/igl/matlab_format.h

@@ -0,0 +1,75 @@
+#ifndef IGL_MATLAB_FORMAT_H
+#define IGL_MATLAB_FORMAT_H
+
+#include "igl_inline.h"
+
+#include <Eigen/Core>
+#include <Eigen/Sparse>
+
+namespace igl
+{
+  // This is a routine to print a matrix using format suitable for pasting into
+  // the matlab IDE
+  //
+  // Templates:
+  //   DerivedM  e.g. derived from MatrixXd
+  // Input:
+  //   input  some matrix to be formated
+  //   name  name of matrix
+  // Returns  Formated matrix
+  //
+  // Example:
+  // // M := [1 2 3;4 5 6];
+  // cout<<matlab_format(M)<<endl;
+  // // Prints:
+  // // [
+  // //   1 2 3
+  // //   4 5 6
+  // // ];
+  // cout<<matlab_format(M,"M")<<endl;
+  // // Prints:
+  // // M = [
+  // //   1 2 3
+  // //   4 5 6
+  // // ];
+  template <typename DerivedM>
+  IGL_INLINE const Eigen::WithFormat< DerivedM > matlab_format(
+    const Eigen::PlainObjectBase<DerivedM> & M,
+    const std::string name = "");
+  // Same but for sparse matrices. Print IJV format into an auxillary variable
+  // and then print a call to sparse which will construct the sparse matrix
+  // Example:
+  // // S := [0 2 3;4 5 0];
+  // cout<<matlab_format(S,"S")<<endl;
+  // // Prints:
+  // // SIJV = [
+  // //   2 1 4
+  // //   1 2 2
+  // //   2 2 5
+  // //   1 3 3
+  // // ];
+  // // S = sparse(SIJV(:,1),SIJV(:,2),SIJV(:,3));
+  //
+  template <typename DerivedS>
+  IGL_INLINE const std::string matlab_format(
+    const Eigen::SparseMatrix<DerivedS> & S,
+    const std::string name = "");
+  // Return just IOFormat 
+  //
+  // Example:
+  // // M := [1 2 3;4 5 6];
+  // cout<<M.format(matlab_format())<<endl;
+  // // Prints:
+  // // [
+  // //   1 2 3
+  // //   4 5 6
+  // // ];
+  IGL_INLINE Eigen::IOFormat matlab_format();
+}
+
+#ifdef IGL_HEADER_ONLY
+#  include "matlab_format.cpp"
+#endif
+
+#endif
+