// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 Alec Jacobson // // This Source Code Form is subject to the terms of the Mozilla Public License // 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 "writeOBJ.h" #include #include #include #include #include #include template < typename DerivedV, typename DerivedF, typename DerivedCN, typename DerivedFN, typename DerivedTC, typename DerivedFTC> IGL_INLINE bool igl::writeOBJ( const std::string str, const Eigen::MatrixBase& V, const Eigen::MatrixBase& F, const Eigen::MatrixBase& CN, const Eigen::MatrixBase& FN, const Eigen::MatrixBase& TC, const Eigen::MatrixBase& FTC) { FILE * obj_file = fopen(str.c_str(),"w"); if(NULL==obj_file) { printf("IOError: %s could not be opened for writing...",str.c_str()); return false; } // Loop over V for(int i = 0;i<(int)V.rows();i++) { fprintf(obj_file,"v"); for(int j = 0;j<(int)V.cols();++j) { fprintf(obj_file," %0.17g", V(i,j)); } fprintf(obj_file,"\n"); } bool write_N = CN.rows() >0; if(write_N) { for(int i = 0;i<(int)CN.rows();i++) { fprintf(obj_file,"vn %0.17g %0.17g %0.17g\n", CN(i,0), CN(i,1), CN(i,2) ); } fprintf(obj_file,"\n"); } bool write_texture_coords = TC.rows() >0; if(write_texture_coords) { for(int i = 0;i<(int)TC.rows();i++) { fprintf(obj_file, "vt %0.17g %0.17g\n",TC(i,0),TC(i,1)); } fprintf(obj_file,"\n"); } // loop over F for(int i = 0;i<(int)F.rows();++i) { fprintf(obj_file,"f"); for(int j = 0; j<(int)F.cols();++j) { // OBJ is 1-indexed fprintf(obj_file," %u",F(i,j)+1); if(write_texture_coords) fprintf(obj_file,"/%u",FTC(i,j)+1); if(write_N) { if (write_texture_coords) fprintf(obj_file,"/%u",FN(i,j)+1); else fprintf(obj_file,"//%u",FN(i,j)+1); } } fprintf(obj_file,"\n"); } fclose(obj_file); return true; } template IGL_INLINE bool igl::writeOBJ( const std::string str, const Eigen::MatrixBase& V, const Eigen::MatrixBase& F) { using namespace std; using namespace Eigen; assert(V.cols() == 3 && "V should have 3 columns"); ofstream s(str); if(!s.is_open()) { fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str()); return false; } s<< V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","v ","","","\n"))<< (F.array()+1).format(IOFormat(FullPrecision,DontAlignCols," ","\n","f ","","","\n")); return true; } template IGL_INLINE bool igl::writeOBJ( const std::string &str, const Eigen::MatrixBase& V, const std::vector >& F) { using namespace std; using namespace Eigen; assert(V.cols() == 3 && "V should have 3 columns"); ofstream s(str); if(!s.is_open()) { fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str()); return false; } s<, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); // generated by autoexplicit.sh template bool igl::writeOBJ, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); // generated by autoexplicit.sh template bool igl::writeOBJ, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); // generated by autoexplicit.sh template bool igl::writeOBJ, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); template bool igl::writeOBJ, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); template bool igl::writeOBJ, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); template bool igl::writeOBJ, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); template bool igl::writeOBJ, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); template bool igl::writeOBJ, Eigen::Matrix >(std::basic_string, std::allocator >, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); #endif