123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- #include "readOBJ.h"
- #include "list_to_matrix.h"
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- template <typename Scalar, typename Index>
- IGL_INLINE bool igl::readOBJ(
- const std::string obj_file_name,
- std::vector<std::vector<Scalar > > & V,
- std::vector<std::vector<Scalar > > & TC,
- std::vector<std::vector<Scalar > > & N,
- std::vector<std::vector<Index > > & F,
- std::vector<std::vector<Index > > & FTC,
- std::vector<std::vector<Index > > & FN)
- {
- // Open file, and check for error
- FILE * obj_file = fopen(obj_file_name.c_str(),"r");
- if(NULL==obj_file)
- {
- fprintf(stderr,"IOError: %s could not be opened...\n",
- obj_file_name.c_str());
- return false;
- }
- // File open was succesfull so clear outputs
- V.clear();
- TC.clear();
- N.clear();
- F.clear();
- FTC.clear();
- FN.clear();
-
- // variables an constants to assist parsing the .obj file
- // Constant strings to compare against
- std::string v("v");
- std::string vn("vn");
- std::string vt("vt");
- std::string f("f");
- std::string tic_tac_toe("#");
- #ifndef LINE_MAX
- # define LINE_MAX 2048
- #endif
-
- char line[LINE_MAX];
- int line_no = 1;
- while (fgets(line, LINE_MAX, obj_file) != NULL)
- {
- char type[LINE_MAX];
- // Read first word containing type
- if(sscanf(line, "%s",type) == 1)
- {
- // Get pointer to rest of line right after type
- char * l = &line[strlen(type)];
- if(type == v)
- {
- double x[4];
- int count =
- sscanf(l,"%lf %lf %lf %lf\n",&x[0],&x[1],&x[2],&x[3]);
- if(count != 3 && count != 4)
- {
- fprintf(stderr,
- "Error: readOBJ() vertex on line %d should have 3 or 4 coordinates",
- line_no);
- fclose(obj_file);
- return false;
- }
- std::vector<Scalar > vertex(count);
- for(int i = 0;i<count;i++)
- {
- vertex[i] = x[i];
- }
- V.push_back(vertex);
- }else if(type == vn)
- {
- double x[3];
- int count =
- sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
- if(count != 3)
- {
- fprintf(stderr,
- "Error: readOBJ() normal on line %d should have 3 coordinates",
- line_no);
- fclose(obj_file);
- return false;
- }
- std::vector<Scalar > normal(count);
- for(int i = 0;i<count;i++)
- {
- normal[i] = x[i];
- }
- N.push_back(normal);
- }else if(type == vt)
- {
- double x[3];
- int count =
- sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
- if(count != 2 && count != 3)
- {
- fprintf(stderr,
- "Error: readOBJ() vertex on line %d should have 2 or 3 coordinates (%d)",
- line_no,count);
- fclose(obj_file);
- return false;
- }
- std::vector<Scalar > tex(count);
- for(int i = 0;i<count;i++)
- {
- tex[i] = x[i];
- }
- TC.push_back(tex);
- }else if(type == f)
- {
- std::vector<Index > f;
- std::vector<Index > ftc;
- std::vector<Index > fn;
- // Read each "word" after type
- char word[LINE_MAX];
- int offset;
- while(sscanf(l,"%s%n",word,&offset) == 1)
- {
- // adjust offset
- l += offset;
- // Process word
- unsigned int i,it,in;
- if(sscanf(word,"%u/%u/%u",&i,&it,&in) == 3)
- {
- f.push_back(i-1);
- ftc.push_back(it-1);
- fn.push_back(in-1);
- }else if(sscanf(word,"%u/%u",&i,&it) == 2)
- {
- f.push_back(i-1);
- ftc.push_back(it-1);
- }else if(sscanf(word,"%u//%u",&i,&in) == 2)
- {
- f.push_back(i-1);
- fn.push_back(in-1);
- }else if(sscanf(word,"%u",&i) == 1)
- {
- f.push_back(i-1);
- }else
- {
- fprintf(stderr,
- "Error: readOBJ() face on line %d has invalid element format\n",
- line_no);
- fclose(obj_file);
- return false;
- }
- }
- if(
- (f.size()>0 && fn.size() == 0 && ftc.size() == 0) ||
- (f.size()>0 && fn.size() == f.size() && ftc.size() == 0) ||
- (f.size()>0 && fn.size() == 0 && ftc.size() == f.size()) ||
- (f.size()>0 && fn.size() == f.size() && ftc.size() == f.size()))
- {
- // No matter what add each type to lists so that lists are the
- // correct lengths
- F.push_back(f);
- FTC.push_back(ftc);
- FN.push_back(fn);
- }else
- {
- fprintf(stderr,
- "Error: readOBJ() face on line %d has invalid format\n", line_no);
- fclose(obj_file);
- return false;
- }
- }else if(strlen(type) >= 1 && (type[0] == '#' ||
- type[0] == 'g' ||
- type[0] == 's' ||
- strcmp("usemtl",type)==0 ||
- strcmp("mtllib",type)==0))
- {
- //ignore comments or other shit
- }else
- {
- //ignore any other lines
- fprintf(stderr,
- "Warning: readOBJ() ignored non-comment line %d:\n %s",
- line_no,
- line);
- }
- }else
- {
- // ignore empty line
- }
- line_no++;
- }
- fclose(obj_file);
-
- assert(F.size() == FN.size());
- assert(F.size() == FTC.size());
-
- return true;
- }
- template <typename DerivedV, typename DerivedF, typename DerivedT>
- IGL_INLINE bool igl::readOBJ(
- const std::string str,
- Eigen::PlainObjectBase<DerivedV>& V,
- Eigen::PlainObjectBase<DerivedF>& F,
- Eigen::PlainObjectBase<DerivedV>& CN,
- Eigen::PlainObjectBase<DerivedF>& FN,
- Eigen::PlainObjectBase<DerivedT>& TC,
- Eigen::PlainObjectBase<DerivedF>& FTC)
- {
- std::vector<std::vector<double> > vV,vTC,vN;
- std::vector<std::vector<int> > vF,vFTC,vFN;
- bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
- if(!success)
- {
- // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
- // message to stderr
- return false;
- }
- bool V_rect = igl::list_to_matrix(vV,V);
- if(!V_rect)
- {
- // igl::list_to_matrix(vV,V) already printed error message to std err
- return false;
- }
- bool F_rect = igl::list_to_matrix(vF,F);
- if(!F_rect)
- {
- // igl::list_to_matrix(vF,F) already printed error message to std err
- return false;
- }
- if(!vN.empty())
- {
- bool VN_rect = igl::list_to_matrix(vN,CN);
- if(!VN_rect)
- {
- // igl::list_to_matrix(vV,V) already printed error message to std err
- return false;
- }
- }
-
- if(!vFN.empty())
- {
- bool FN_rect = igl::list_to_matrix(vFN,FN);
- if(!FN_rect)
- {
- // igl::list_to_matrix(vV,V) already printed error message to std err
- return false;
- }
- }
-
- if(!vTC.empty())
- {
-
- bool T_rect = igl::list_to_matrix(vTC,TC);
- if(!T_rect)
- {
- // igl::list_to_matrix(vTC,T) already printed error message to std err
- return false;
- }
- }
- if(!vFTC.empty())
- {
-
- bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
- if(!FTC_rect)
- {
- // igl::list_to_matrix(vF,F) already printed error message to std err
- return false;
- }
- }
- // Legacy
- if(F.cols() != 3)
- {
- fprintf(stderr,
- "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
- " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
- " options.\n",
- (int)F.cols());
- return false;
- }
- return true;
- }
- template <typename DerivedV, typename DerivedF, typename DerivedT, typename Index>
- IGL_INLINE bool igl::readOBJPoly(
- const std::string str,
- Eigen::PlainObjectBase<DerivedV>& V,
- std::vector<std::vector< Index > >& F,
- Eigen::PlainObjectBase<DerivedV>& CN,
- Eigen::PlainObjectBase<DerivedF>& FN,
- Eigen::PlainObjectBase<DerivedT>& TC,
- Eigen::PlainObjectBase<DerivedF>& FTC)
- {
- std::vector<std::vector<double> > vV,vTC,vN;
- std::vector<std::vector<Index> > vF,vFTC,vFN;
- bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
- if(!success)
- return false;
- bool V_rect = igl::list_to_matrix(vV,V);
- if(!V_rect)
- return false;
- F = vF;
-
- if(!vN.empty())
- {
- bool VN_rect = igl::list_to_matrix(vN,CN);
- if(!VN_rect)
- return false;
- }
-
- if(!vFN.empty())
- {
- bool FN_rect = igl::list_to_matrix(vFN,FN);
- if(!FN_rect)
- return false;
- }
-
- if(!vTC.empty())
- {
-
- bool T_rect = igl::list_to_matrix(vTC,TC);
- if(!T_rect)
- return false;
- }
- if(!vFTC.empty())
- {
-
- bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
- if(!FTC_rect)
- return false;
- }
- return true;
- }
- template <typename DerivedV, typename DerivedF>
- IGL_INLINE bool igl::readOBJ(
- const std::string str,
- Eigen::PlainObjectBase<DerivedV>& V,
- Eigen::PlainObjectBase<DerivedF>& F)
- {
- std::vector<std::vector<double> > vV,vTC,vN;
- std::vector<std::vector<int> > vF,vFTC,vFN;
- bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
- if(!success)
- {
- // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
- // message to stderr
- return false;
- }
- bool V_rect = igl::list_to_matrix(vV,V);
- if(!V_rect)
- {
- // igl::list_to_matrix(vV,V) already printed error message to std err
- return false;
- }
- bool F_rect = igl::list_to_matrix(vF,F);
- if(!F_rect)
- {
- // igl::list_to_matrix(vF,F) already printed error message to std err
- return false;
- }
- // Legacy
- if(F.cols() != 3)
- {
- fprintf(stderr,
- "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
- " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
- " options.\n",
- (int)F.cols());
- return false;
- }
- return true;
- }
- #ifndef IGL_HEADER_ONLY
- // Explicit template specialization
- // generated by autoexplicit.sh
- template bool igl::readOBJ<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
- template bool igl::readOBJ<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, 2, 1, -1, 2> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 1, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >&);
- template bool igl::readOBJ<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
- #endif
|