|
@@ -7,20 +7,20 @@
|
|
|
|
|
|
template <typename Scalar, typename Index>
|
|
template <typename Scalar, typename Index>
|
|
IGL_INLINE bool igl::readOBJ(
|
|
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)
|
|
|
|
|
|
+ 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
|
|
// Open file, and check for error
|
|
FILE * obj_file = fopen(obj_file_name.c_str(),"r");
|
|
FILE * obj_file = fopen(obj_file_name.c_str(),"r");
|
|
if(NULL==obj_file)
|
|
if(NULL==obj_file)
|
|
{
|
|
{
|
|
fprintf(stderr,"IOError: %s could not be opened...\n",
|
|
fprintf(stderr,"IOError: %s could not be opened...\n",
|
|
- obj_file_name.c_str());
|
|
|
|
|
|
+ obj_file_name.c_str());
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
// File open was succesfull so clear outputs
|
|
// File open was succesfull so clear outputs
|
|
@@ -30,7 +30,7 @@ IGL_INLINE bool igl::readOBJ(
|
|
F.clear();
|
|
F.clear();
|
|
FTC.clear();
|
|
FTC.clear();
|
|
FN.clear();
|
|
FN.clear();
|
|
-
|
|
|
|
|
|
+
|
|
// variables an constants to assist parsing the .obj file
|
|
// variables an constants to assist parsing the .obj file
|
|
// flag for whether vertex texture coordinates exist in file
|
|
// flag for whether vertex texture coordinates exist in file
|
|
bool has_texture = false;
|
|
bool has_texture = false;
|
|
@@ -45,7 +45,7 @@ IGL_INLINE bool igl::readOBJ(
|
|
#ifndef LINE_MAX
|
|
#ifndef LINE_MAX
|
|
# define LINE_MAX 2048
|
|
# define LINE_MAX 2048
|
|
#endif
|
|
#endif
|
|
-
|
|
|
|
|
|
+
|
|
char line[LINE_MAX];
|
|
char line[LINE_MAX];
|
|
int line_no = 1;
|
|
int line_no = 1;
|
|
while (fgets(line, LINE_MAX, obj_file) != NULL)
|
|
while (fgets(line, LINE_MAX, obj_file) != NULL)
|
|
@@ -60,12 +60,12 @@ IGL_INLINE bool igl::readOBJ(
|
|
{
|
|
{
|
|
double x[4];
|
|
double x[4];
|
|
int count =
|
|
int count =
|
|
- sscanf(l,"%lf %lf %lf %lf\n",&x[0],&x[1],&x[2],&x[3]);
|
|
|
|
|
|
+ sscanf(l,"%lf %lf %lf %lf\n",&x[0],&x[1],&x[2],&x[3]);
|
|
if(count != 3 && count != 4)
|
|
if(count != 3 && count != 4)
|
|
{
|
|
{
|
|
fprintf(stderr,
|
|
fprintf(stderr,
|
|
- "Error: readOBJ() vertex on line %d should have 3 or 4 coordinates",
|
|
|
|
- line_no);
|
|
|
|
|
|
+ "Error: readOBJ() vertex on line %d should have 3 or 4 coordinates",
|
|
|
|
+ line_no);
|
|
fclose(obj_file);
|
|
fclose(obj_file);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -80,12 +80,12 @@ IGL_INLINE bool igl::readOBJ(
|
|
has_normals = true;
|
|
has_normals = true;
|
|
double x[3];
|
|
double x[3];
|
|
int count =
|
|
int count =
|
|
- sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
|
|
|
|
|
|
+ sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
|
|
if(count != 3)
|
|
if(count != 3)
|
|
{
|
|
{
|
|
fprintf(stderr,
|
|
fprintf(stderr,
|
|
- "Error: readOBJ() normal on line %d should have 3 coordinates",
|
|
|
|
- line_no);
|
|
|
|
|
|
+ "Error: readOBJ() normal on line %d should have 3 coordinates",
|
|
|
|
+ line_no);
|
|
fclose(obj_file);
|
|
fclose(obj_file);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -100,12 +100,12 @@ IGL_INLINE bool igl::readOBJ(
|
|
has_texture = true;
|
|
has_texture = true;
|
|
double x[3];
|
|
double x[3];
|
|
int count =
|
|
int count =
|
|
- sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
|
|
|
|
|
|
+ sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
|
|
if(count != 2 && count != 3)
|
|
if(count != 2 && count != 3)
|
|
{
|
|
{
|
|
fprintf(stderr,
|
|
fprintf(stderr,
|
|
- "Error: readOBJ() vertex on line %d should have 2 or 3 coordinates",
|
|
|
|
- line_no);
|
|
|
|
|
|
+ "Error: readOBJ() vertex on line %d should have 2 or 3 coordinates",
|
|
|
|
+ line_no);
|
|
fclose(obj_file);
|
|
fclose(obj_file);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -148,17 +148,17 @@ IGL_INLINE bool igl::readOBJ(
|
|
}else
|
|
}else
|
|
{
|
|
{
|
|
fprintf(stderr,
|
|
fprintf(stderr,
|
|
- "Error: readOBJ() face on line %d has invalid element format\n",
|
|
|
|
- line_no);
|
|
|
|
|
|
+ "Error: readOBJ() face on line %d has invalid element format\n",
|
|
|
|
+ line_no);
|
|
fclose(obj_file);
|
|
fclose(obj_file);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(
|
|
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()))
|
|
|
|
|
|
+ (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
|
|
// No matter what add each type to lists so that lists are the
|
|
// correct lengths
|
|
// correct lengths
|
|
@@ -168,7 +168,7 @@ IGL_INLINE bool igl::readOBJ(
|
|
}else
|
|
}else
|
|
{
|
|
{
|
|
fprintf(stderr,
|
|
fprintf(stderr,
|
|
- "Error: readOBJ() face on line %d has invalid format\n", line_no);
|
|
|
|
|
|
+ "Error: readOBJ() face on line %d has invalid format\n", line_no);
|
|
fclose(obj_file);
|
|
fclose(obj_file);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -179,9 +179,9 @@ IGL_INLINE bool igl::readOBJ(
|
|
{
|
|
{
|
|
//ignore any other lines
|
|
//ignore any other lines
|
|
fprintf(stderr,
|
|
fprintf(stderr,
|
|
- "Warning: readOBJ() ignored non-comment line %d:\n %s",
|
|
|
|
- line_no,
|
|
|
|
- line);
|
|
|
|
|
|
+ "Warning: readOBJ() ignored non-comment line %d:\n %s",
|
|
|
|
+ line_no,
|
|
|
|
+ line);
|
|
}
|
|
}
|
|
}else
|
|
}else
|
|
{
|
|
{
|
|
@@ -190,14 +190,102 @@ IGL_INLINE bool igl::readOBJ(
|
|
line_no++;
|
|
line_no++;
|
|
}
|
|
}
|
|
fclose(obj_file);
|
|
fclose(obj_file);
|
|
-
|
|
|
|
|
|
+
|
|
assert(F.size() == FN.size());
|
|
assert(F.size() == FN.size());
|
|
assert(F.size() == FTC.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;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-IGL_INLINE bool igl::readOBJ(const std::string str, Eigen::MatrixXd& V, Eigen::MatrixXi& F)
|
|
|
|
|
|
+template <typename DerivedV, typename DerivedF, typename DerivedT>
|
|
|
|
+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<double> > vV,vTC,vN;
|
|
std::vector<std::vector<int> > vF,vFTC,vFN;
|
|
std::vector<std::vector<int> > vF,vFTC,vFN;
|
|
@@ -224,10 +312,10 @@ IGL_INLINE bool igl::readOBJ(const std::string str, Eigen::MatrixXd& V, Eigen::M
|
|
if(F.cols() != 3)
|
|
if(F.cols() != 3)
|
|
{
|
|
{
|
|
fprintf(stderr,
|
|
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());
|
|
|
|
|
|
+ "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 false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|