From 8a2fb2a3ecca0bb3e8eba0a67ae9fa3bd0185460 Mon Sep 17 00:00:00 2001 From: Alec Jacobson Date: Wed, 23 Nov 2016 21:56:37 -0500 Subject: [PATCH] unify code for vectors Former-commit-id: 9ed162c928464e82df5be689001c9f08e4743ff0 --- include/igl/writeDMAT.cpp | 71 +++++++++------------------------------ include/igl/writeDMAT.h | 11 +++--- 2 files changed, 22 insertions(+), 60 deletions(-) diff --git a/include/igl/writeDMAT.cpp b/include/igl/writeDMAT.cpp index 43a09760f..cd6630f5d 100644 --- a/include/igl/writeDMAT.cpp +++ b/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 #include -#ifndef IGL_NO_EIGEN -# include -#endif -template +template IGL_INLINE bool igl::writeDMAT( const std::string file_name, - const Mat & W, + const Eigen::MatrixBase & 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(); fwrite(Wd.data(),sizeof(double),Wd.size(),fp); //// Loop over columns slowly @@ -63,70 +63,29 @@ IGL_INLINE bool igl::writeDMAT( template IGL_INLINE bool igl::writeDMAT( const std::string file_name, - const std::vector > W) + const std::vector > & 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 mW; + list_to_matrix(W,mW); + return igl::writeDMAT(file_name,mW,ascii); } template IGL_INLINE bool igl::writeDMAT( const std::string file_name, - const std::vector W) + const std::vector & 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 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 >(std::basic_string, std::allocator >, Eigen::Matrix const&,bool); -template bool igl::writeDMAT(std::basic_string, std::allocator >, std::vector >, std::allocator > > >); template bool igl::writeDMAT >(std::basic_string, std::allocator >, Eigen::Matrix const&, bool); template bool igl::writeDMAT >(std::basic_string, std::allocator >, Eigen::Matrix const&, bool); template bool igl::writeDMAT >(std::basic_string, std::allocator >, Eigen::Matrix const&, bool); diff --git a/include/igl/writeDMAT.h b/include/igl/writeDMAT.h index 3855bbffa..08409ef67 100644 --- a/include/igl/writeDMAT.h +++ b/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 #include #include namespace igl @@ -23,19 +24,21 @@ namespace igl // ascii write ascii file {true} // Returns true on success, false on error // - template + template IGL_INLINE bool writeDMAT( const std::string file_name, - const Mat & W, + const Eigen::MatrixBase & W, const bool ascii=true); template IGL_INLINE bool writeDMAT( const std::string file_name, - const std::vector > W); + const std::vector > & W, + const bool ascii=true); template IGL_INLINE bool writeDMAT( const std::string file_name, - const std::vector W); + const std::vector &W, + const bool ascii=true); } #ifndef IGL_STATIC_LIBRARY