Jelajahi Sumber

unify code for vectors

Former-commit-id: 9ed162c928464e82df5be689001c9f08e4743ff0
Alec Jacobson 8 tahun lalu
induk
melakukan
8a2fb2a3ec
2 mengubah file dengan 22 tambahan dan 60 penghapusan
  1. 15 56
      include/igl/writeDMAT.cpp
  2. 7 4
      include/igl/writeDMAT.h

+ 15 - 56
include/igl/writeDMAT.cpp

@@ -6,16 +6,15 @@
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can 
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "writeDMAT.h"
+#include "list_to_matrix.h"
+#include <Eigen/Core>
 
 #include <cstdio>
-#ifndef IGL_NO_EIGEN
-#  include <Eigen/Core>
-#endif
 
-template <class Mat>
+template <typename DerivedW>
 IGL_INLINE bool igl::writeDMAT(
   const std::string file_name, 
-  const Mat & W,
+  const Eigen::MatrixBase<DerivedW> & W,
   const bool ascii)
 {
   FILE * fp = fopen(file_name.c_str(),"wb");
@@ -43,6 +42,7 @@ IGL_INLINE bool igl::writeDMAT(
     fprintf(fp,"0 0\n");
     // first line contains number of rows and number of columns
     fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
+    // reader assumes the binary part is double precision
     Eigen::MatrixXd Wd = W.template cast<double>();
     fwrite(Wd.data(),sizeof(double),Wd.size(),fp);
     //// Loop over columns slowly
@@ -63,70 +63,29 @@ IGL_INLINE bool igl::writeDMAT(
 template <typename Scalar>
 IGL_INLINE bool igl::writeDMAT(
   const std::string file_name, 
-  const std::vector<std::vector<Scalar> > W)
+  const std::vector<std::vector<Scalar> > & W,
+  const bool ascii)
 {
-  FILE * fp = fopen(file_name.c_str(),"w");
-  if(fp == NULL)
-  {
-    fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
-    return false; 
-  }
-  int num_rows = (int)W.size();
-  int num_cols = 0;
-  if(num_rows > 0)
-  {
-    num_cols = W[0].size();
-  }
-  // first line contains number of columns and number of rows
-  fprintf(fp,"%d %d\n",num_cols,num_rows);
-  // Loop over columns slowly
-  for(int j = 0;j < num_cols;j++)
-  {
-    // loop over rows (down columns) quickly
-    for(int i = 0;i < num_rows;i++)
-    {
-      // better be rectangular
-      assert((int)W[i].size() > j);
-      fprintf(fp,"%0.15lf\n",(double)W[i][j]);
-    }
-  }
-  fclose(fp);
-  return true;
+  Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> mW;
+  list_to_matrix(W,mW);
+  return igl::writeDMAT(file_name,mW,ascii);
 }
 
 template <typename Scalar>
 IGL_INLINE bool igl::writeDMAT(
   const std::string file_name, 
-  const std::vector<Scalar > W)
+  const std::vector<Scalar > & W,
+  const bool ascii)
 {
-  FILE * fp = fopen(file_name.c_str(),"w");
-  if(fp == NULL)
-  {
-    fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
-    return false; 
-  }
-  int num_rows = (int)W.size();
-  int num_cols = 0;
-  if(num_rows > 0)
-  {
-    num_cols = 1;
-  }
-  // first line contains number of columns and number of rows
-  fprintf(fp,"%d %d\n",num_cols,num_rows);
-  // loop over rows (down columns) quickly
-  for(int i = 0;i < num_rows;i++)
-  {
-    fprintf(fp,"%0.15lf\n",(double)W[i]);
-  }
-  fclose(fp);
-  return true;
+  Eigen::Matrix<Scalar,Eigen::Dynamic,1> mW;
+  list_to_matrix(W,mW);
+  return igl::writeDMAT(file_name,mW,ascii);
 }
 
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template specialization
 // generated by autoexplicit.sh
 template bool igl::writeDMAT<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&,bool);
-template bool igl::writeDMAT<double>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >);
 template bool igl::writeDMAT<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, bool);
 template bool igl::writeDMAT<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, bool);
 template bool igl::writeDMAT<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, bool);

+ 7 - 4
include/igl/writeDMAT.h

@@ -9,6 +9,7 @@
 #define IGL_WRITEDMAT_H
 #include "igl_inline.h"
 // See writeDMAT.h for a description of the .dmat file type
+#include <Eigen/Core>
 #include <string>
 #include <vector>
 namespace igl
@@ -23,19 +24,21 @@ namespace igl
   //   ascii  write ascii file {true}
   // Returns true on success, false on error
   //
-  template <class Mat>
+  template <typename DerivedW>
   IGL_INLINE bool writeDMAT(
     const std::string file_name, 
-    const Mat & W,
+    const Eigen::MatrixBase<DerivedW> & W,
     const bool ascii=true);
   template <typename Scalar>
   IGL_INLINE bool writeDMAT(
     const std::string file_name, 
-    const std::vector<std::vector<Scalar> > W);
+    const std::vector<std::vector<Scalar> > & W,
+    const bool ascii=true);
   template <typename Scalar>
   IGL_INLINE bool writeDMAT(
     const std::string file_name, 
-    const std::vector<Scalar > W);
+    const std::vector<Scalar > &W,
+    const bool ascii=true);
 }
 
 #ifndef IGL_STATIC_LIBRARY