// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2014 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 "readSTL.h" #include "list_to_matrix.h" #include template IGL_INLINE bool igl::readSTL( const std::string & filename, Eigen::PlainObjectBase & V, Eigen::PlainObjectBase & F, Eigen::PlainObjectBase & N) { using namespace std; vector > vV; vector > vN; vector > vF; if(!readSTL(filename,vV,vF,vN)) { return false; } if(!list_to_matrix(vV,V)) { return false; } if(!list_to_matrix(vF,F)) { return false; } if(!list_to_matrix(vN,N)) { return false; } return true; } template IGL_INLINE bool igl::readSTL( const std::string & filename, std::vector > & V, std::vector > & F, std::vector > & N) { using namespace std; // Should test for ascii // Open file, and check for error FILE * stl_file = fopen(filename.c_str(),"rb"); if(NULL==stl_file) { fprintf(stderr,"IOError: %s could not be opened...\n", filename.c_str()); return false; } return readSTL(stl_file,V,F,N); } template IGL_INLINE bool igl::readSTL( FILE * stl_file, std::vector > & V, std::vector > & F, std::vector > & N) { using namespace std; //stl_file = freopen(NULL,"rb",stl_file); if(NULL==stl_file) { fprintf(stderr,"IOError: stl file could not be reopened as binary (1) ...\n"); return false; } V.clear(); F.clear(); N.clear(); // Specifically 80 character header char header[80]; char solid[80]; bool is_ascii = true; if(fread(header,1,80,stl_file) != 80) { cerr<<"IOError: too short (1)."<(buf); fseek(stl_file,0,SEEK_END); int file_size = ftell(stl_file); if(file_size == 80 + 4 + (4*12 + 2) * num_faces) { is_ascii = false; }else { is_ascii = true; } } if(is_ascii) { // Rewind to end of header //stl_file = fopen(filename.c_str(),"r"); //stl_file = freopen(NULL,"r",stl_file); fseek(stl_file, 0, SEEK_SET); if(NULL==stl_file) { fprintf(stderr,"IOError: stl file could not be reopened as ascii ...\n"); return false; } // Read 80 header // Eat file name #ifndef IGL_LINE_MAX # define IGL_LINE_MAX 2048 #endif char name[IGL_LINE_MAX]; if(NULL==fgets(name,IGL_LINE_MAX,stl_file)) { cerr<<"IOError: ascii too short (2)."< n(3); double nd[3]; ret = fscanf(stl_file,"%s %s %lg %lg %lg",facet,normal,nd,nd+1,nd+2); if(string("endsolid") == facet) { break; } if(ret != 5 || !(string("facet") == facet || string("faced") == facet) || string("normal") != normal) { cout<<"facet: "< f; while(true) { char word[IGL_LINE_MAX]; int ret = fscanf(stl_file,"%s",word); if(ret == 1 && string("endloop") == word) { break; }else if(ret == 1 && string("vertex") == word) { vector v(3); double vd[3]; int ret = fscanf(stl_file,"%lg %lg %lg",vd,vd+1,vd+2); if(ret != 3) { cerr<<"IOError: bad format (3)."<(3,0)); N.resize(num_tri,vector(3,0)); F.resize(num_tri,vector(3,0)); for(int t = 0;t<(int)num_tri;t++) { // Read normal float n[3]; if(fread(n,sizeof(float),3,stl_file)!=3) { cerr<<"IOError: bad format (8)."<, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); // generated by autoexplicit.sh template bool igl::readSTL, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); // generated by autoexplicit.sh template bool igl::readSTL, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); // generated by autoexplicit.sh template bool igl::readSTL, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); template bool igl::readSTL, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); template bool igl::readSTL, Eigen::Matrix, Eigen::Matrix >(std::basic_string, std::allocator > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); #endif