1 // This file is part of libigl, a simple c++ geometry processing library.
2 //
3 // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public License
6 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
7 // obtain one at http://mozilla.org/MPL/2.0/.
8 #include "readOFF.h"
9 #include "list_to_matrix.h"
10
11 template <typename Scalar, typename Index>
readOFF(const std::string off_file_name,std::vector<std::vector<Scalar>> & V,std::vector<std::vector<Index>> & F,std::vector<std::vector<Scalar>> & N,std::vector<std::vector<Scalar>> & C)12 IGL_INLINE bool igl::readOFF(
13 const std::string off_file_name,
14 std::vector<std::vector<Scalar > > & V,
15 std::vector<std::vector<Index > > & F,
16 std::vector<std::vector<Scalar > > & N,
17 std::vector<std::vector<Scalar > > & C)
18 {
19 using namespace std;
20 FILE * off_file = fopen(off_file_name.c_str(),"r");
21 if(NULL==off_file)
22 {
23 printf("IOError: %s could not be opened...\n",off_file_name.c_str());
24 return false;
25 }
26 return readOFF(off_file,V,F,N,C);
27 }
28
29 template <typename Scalar, typename Index>
readOFF(FILE * off_file,std::vector<std::vector<Scalar>> & V,std::vector<std::vector<Index>> & F,std::vector<std::vector<Scalar>> & N,std::vector<std::vector<Scalar>> & C)30 IGL_INLINE bool igl::readOFF(
31 FILE * off_file,
32 std::vector<std::vector<Scalar > > & V,
33 std::vector<std::vector<Index > > & F,
34 std::vector<std::vector<Scalar > > & N,
35 std::vector<std::vector<Scalar > > & C)
36 {
37 using namespace std;
38 V.clear();
39 F.clear();
40 N.clear();
41 C.clear();
42
43 // First line is always OFF
44 char header[1000];
45 const std::string OFF("OFF");
46 const std::string NOFF("NOFF");
47 const std::string COFF("COFF");
48 if(fscanf(off_file,"%s\n",header)!=1
49 || !(
50 string(header).compare(0, OFF.length(), OFF)==0 ||
51 string(header).compare(0, COFF.length(), COFF)==0 ||
52 string(header).compare(0,NOFF.length(),NOFF)==0))
53 {
54 printf("Error: readOFF() first line should be OFF or NOFF or COFF, not %s...",header);
55 fclose(off_file);
56 return false;
57 }
58 bool has_normals = string(header).compare(0,NOFF.length(),NOFF)==0;
59 bool has_vertexColors = string(header).compare(0,COFF.length(),COFF)==0;
60 // Second line is #vertices #faces #edges
61 int number_of_vertices;
62 int number_of_faces;
63 int number_of_edges;
64 char tic_tac_toe;
65 char line[1000];
66 bool still_comments = true;
67 while(still_comments)
68 {
69 fgets(line,1000,off_file);
70 still_comments = (line[0] == '#' || line[0] == '\n');
71 }
72 sscanf(line,"%d %d %d",&number_of_vertices,&number_of_faces,&number_of_edges);
73 V.resize(number_of_vertices);
74 if (has_normals)
75 N.resize(number_of_vertices);
76 if (has_vertexColors)
77 C.resize(number_of_vertices);
78 F.resize(number_of_faces);
79 //printf("%s %d %d %d\n",(has_normals ? "NOFF" : "OFF"),number_of_vertices,number_of_faces,number_of_edges);
80 // Read vertices
81 for(int i = 0;i<number_of_vertices;)
82 {
83 fgets(line, 1000, off_file);
84 double x,y,z,nx,ny,nz;
85 if(sscanf(line, "%lg %lg %lg %lg %lg %lg",&x,&y,&z,&nx,&ny,&nz)>= 3)
86 {
87 std::vector<Scalar > vertex;
88 vertex.resize(3);
89 vertex[0] = x;
90 vertex[1] = y;
91 vertex[2] = z;
92 V[i] = vertex;
93
94 if (has_normals)
95 {
96 std::vector<Scalar > normal;
97 normal.resize(3);
98 normal[0] = nx;
99 normal[1] = ny;
100 normal[2] = nz;
101 N[i] = normal;
102 }
103
104 if (has_vertexColors)
105 {
106 C[i].resize(3);
107 C[i][0] = nx / 255.0;
108 C[i][1] = ny / 255.0;
109 C[i][2] = nz / 255.0;
110 }
111 i++;
112 }else if(
113 fscanf(off_file,"%[#]",&tic_tac_toe)==1)
114 {
115 char comment[1000];
116 fscanf(off_file,"%[^\n]",comment);
117 }else
118 {
119 printf("Error: bad line (%d)\n",i);
120 if(feof(off_file))
121 {
122 fclose(off_file);
123 return false;
124 }
125 }
126 }
127 // Read faces
128 for(int i = 0;i<number_of_faces;)
129 {
130 std::vector<Index > face;
131 int valence;
132 if(fscanf(off_file,"%d",&valence)==1)
133 {
134 face.resize(valence);
135 for(int j = 0;j<valence;j++)
136 {
137 int index;
138 if(j<valence-1)
139 {
140 fscanf(off_file,"%d",&index);
141 }else{
142 fscanf(off_file,"%d%*[^\n]",&index);
143 }
144
145 face[j] = index;
146 }
147 F[i] = face;
148 i++;
149 }else if(
150 fscanf(off_file,"%[#]",&tic_tac_toe)==1)
151 {
152 char comment[1000];
153 fscanf(off_file,"%[^\n]",comment);
154 }else
155 {
156 printf("Error: bad line\n");
157 fclose(off_file);
158 return false;
159 }
160 }
161 fclose(off_file);
162 return true;
163 }
164
165
166 #ifndef IGL_NO_EIGEN
167 template <typename DerivedV, typename DerivedF>
readOFF(const std::string str,Eigen::PlainObjectBase<DerivedV> & V,Eigen::PlainObjectBase<DerivedF> & F)168 IGL_INLINE bool igl::readOFF(
169 const std::string str,
170 Eigen::PlainObjectBase<DerivedV>& V,
171 Eigen::PlainObjectBase<DerivedF>& F)
172 {
173 std::vector<std::vector<double> > vV;
174 std::vector<std::vector<double> > vN;
175 std::vector<std::vector<int> > vF;
176 std::vector<std::vector<double> > vC;
177 bool success = igl::readOFF(str,vV,vF,vN,vC);
178 if(!success)
179 {
180 // readOFF(str,vV,vF,vN,vC) should have already printed an error
181 // message to stderr
182 return false;
183 }
184 bool V_rect = igl::list_to_matrix(vV,V);
185 if(!V_rect)
186 {
187 // igl::list_to_matrix(vV,V) already printed error message to std err
188 return false;
189 }
190 bool F_rect = igl::list_to_matrix(vF,F);
191 if(!F_rect)
192 {
193 // igl::list_to_matrix(vF,F) already printed error message to std err
194 return false;
195 }
196 return true;
197 }
198
199
200 template <typename DerivedV, typename DerivedF>
readOFF(const std::string str,Eigen::PlainObjectBase<DerivedV> & V,Eigen::PlainObjectBase<DerivedF> & F,Eigen::PlainObjectBase<DerivedV> & N)201 IGL_INLINE bool igl::readOFF(
202 const std::string str,
203 Eigen::PlainObjectBase<DerivedV>& V,
204 Eigen::PlainObjectBase<DerivedF>& F,
205 Eigen::PlainObjectBase<DerivedV>& N)
206 {
207 std::vector<std::vector<double> > vV;
208 std::vector<std::vector<double> > vN;
209 std::vector<std::vector<int> > vF;
210 std::vector<std::vector<double> > vC;
211 bool success = igl::readOFF(str,vV,vF,vN,vC);
212 if(!success)
213 {
214 // readOFF(str,vV,vF,vC) should have already printed an error
215 // message to stderr
216 return false;
217 }
218 bool V_rect = igl::list_to_matrix(vV,V);
219 if(!V_rect)
220 {
221 // igl::list_to_matrix(vV,V) already printed error message to std err
222 return false;
223 }
224 bool F_rect = igl::list_to_matrix(vF,F);
225 if(!F_rect)
226 {
227 // igl::list_to_matrix(vF,F) already printed error message to std err
228 return false;
229 }
230
231 if (vN.size())
232 {
233 bool N_rect = igl::list_to_matrix(vN,N);
234 if(!N_rect)
235 {
236 // igl::list_to_matrix(vN,N) already printed error message to std err
237 return false;
238 }
239 }
240
241 //Warning: RGB colors will be returned in the N matrix
242 if (vC.size())
243 {
244 bool C_rect = igl::list_to_matrix(vC,N);
245 if(!C_rect)
246 {
247 // igl::list_to_matrix(vC,N) already printed error message to std err
248 return false;
249 }
250 }
251
252 return true;
253 }
254 #endif
255
256
257 #ifdef IGL_STATIC_LIBRARY
258 // Explicit template instantiation
259 // generated by autoexplicit.sh
260 template bool igl::readOFF<double, int>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&);
261 // generated by autoexplicit.sh
262 template bool igl::readOFF<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 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, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
263 // generated by autoexplicit.sh
264 template bool igl::readOFF<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> >&);
265 template bool igl::readOFF<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(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> >&);
266 template bool igl::readOFF<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
267 template bool igl::readOFF<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> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
268 #endif
269