Debian Codegraph @

src/vcglib/wrap/io_trimesh/import_field.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #ifndef __VCGLIB_IMPORTERFIELD
24 #define __VCGLIB_IMPORTERFIELD
25 #include <vcg/complex/algorithms/parametrization/tangent_field_operators.h>
26
27 namespace vcg {
28 namespace tri {
29 namespace io {
30
31 /**
32 This class encapsulate a filter for opening field formats
33 */
34 template <class MeshType>
35 class ImporterFIELD
36 {
37 typedef typename MeshType::ScalarType ScalarType;
38 typedef typename MeshType::FaceType FaceType;
39 typedef typename MeshType::VertexType VertexType;
40 typedef typename MeshType::CoordType CoordType;
41
42 public:
43
44 static bool LoadGrad(MeshType &mesh,
45 const char *path)
46 {
47 FILE *f = fopen(path,"rt");
48 if (!f)
49 {
50 return false;
51 }
52 int numF;
53 fscanf(f,"%d\n",&numF);
54 assert(numF==mesh.fn);
55 char skipstr[200];
56 //int readed0;
57 for (int i=0;i<9;i++)
58 fscanf(f,"%s",&skipstr[0]);
59
60 for (int i=0;i<mesh.fn;i++)
61 {
62 int i0=-1;
63 int i1=-1;
64 int i2=-1;
65 double u0,v0,u1,v1,u2,v2;
66 int readed1=fscanf(f,"%d %d %d %lf %lf %lf %lf %lf %lf",&i0,&i1,&i2,&u0,&v0,&u1,&v1,&u2,&v2);
67 assert(readed1==9);
68 vcg::Point2<ScalarType> UV[3];
69 UV[0]= vcg::Point2<ScalarType>(u0,v0);
70 UV[1]= vcg::Point2<ScalarType>(u1,v1);
71 UV[2]= vcg::Point2<ScalarType>(u2,v2);
72 CoordType dir1;
73 CoordType dir2;
74 vcg::tri::CrossField<MeshType>::GradientToCross(mesh.face[i],UV[0],UV[1],UV[2],dir1,dir2);
75 dir1.Normalize();
76 dir2.Normalize();
77 mesh.face[i].PD1()=dir1;
78 mesh.face[i].PD2()=dir2;
79 }
80 fclose(f);
81 return true;
82 }
83
84 static bool LoadNDF(MeshType &mesh,
85 const char *path)
86 {
87 FILE *f = fopen(path,"rt");
88 if (!f)
89 {
90 fflush(stdout);
91 return false;
92 }
93
94 char skipstr[200];
95 //int readed0;
96 do{
97
98 fscanf(f,"%s\n",&skipstr[0]);
99 printf("%s\n",skipstr);
100 }while(strcmp(skipstr,"[Pjumps]")!=0);
101
102 // fscanf(f,"%s\"",skipstr);
103 // printf("%s\n",skipstr);
104 fseek(f, 7, SEEK_CUR);
105 char final[1];
106 do{
107 int period;
108 fscanf(f,"%d;",&period);
109 printf("%d\n",period);
110 fscanf(f,"%c",&final);
111 fseek(f, -1, SEEK_CUR);
112 //printf("%s\n",&final[0]);
113 }while(strcmp(final,"\"")!=0);
114
115 // printf("%s\n",skipstr);
116 fflush(stdout);
117 for (int i=0;i<mesh.fn;i++)
118 {
119 int i0=-1;
120 int i1=-1;
121 int i2=-1;
122 double u0,v0,u1,v1,u2,v2;
123 int readed1=fscanf(f,"%d %d %d %lf %lf %lf %lf %lf %lf",&i0,&i1,&i2,&u0,&v0,&u1,&v1,&u2,&v2);
124 assert(readed1==9);
125 vcg::Point2<ScalarType> UV[3];
126 UV[0]= vcg::Point2<ScalarType>(u0,v0);
127 UV[1]= vcg::Point2<ScalarType>(u1,v1);
128 UV[2]= vcg::Point2<ScalarType>(u2,v2);
129 CoordType dir1;
130 CoordType dir2;
131 vcg::tri::CrossField<MeshType>::GradientToCross(mesh.face[i],UV[0],UV[1],UV[2],dir1,dir2);
132 dir1.Normalize();
133 dir2.Normalize();
134 mesh.face[i].PD1()=dir1;
135 mesh.face[i].PD2()=dir2;
136 }
137 fclose(f);
138 return true;
139 }
140
141 ///load a field on the mesh, it could be a vfield file (per vertex)
142 ///or an ffield file (per face)
143 static bool LoadFFIELD(MeshType &mesh,
144 const char *path,
145 bool per_vertex=false)
146 {
147
148 FILE *f = fopen(path,"rt");
149 if (!f)
150 {
151 return false;
152 }
153 {
154 char word[512]; word[0]=0;
155 fscanf(f,"%s",word);
156 char c=0;
157 if (word[0]=='#') {
158 // skip comment line
159 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break;
160 }
161 else
162 {
163 return false;
164 }
165 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break;
166 int nnv = -1;
167 if (fscanf(f,"%d",&nnv)!=1)
168 {
169 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break; // skip
170 fscanf(f,"%d",&nnv);
171 }
172 int targetnum=mesh.fn;
173 if (per_vertex)
174 targetnum=mesh.vn;
175 if (nnv != (int)targetnum)
176 {
177 //if (errorMsg) sprintf(errorMsg,"Wrong element number. Found: %d. Expected: %d.",nnv,mesh->vn);
178 return false;
179 }
180
181 if( per_vertex && !HasPerVertexCurvatureDir(mesh)) throw vcg::MissingComponentException("PerVertexCurvatureDir");
182 if(!per_vertex && !HasPerFaceCurvatureDir(mesh)) throw vcg::MissingComponentException("PerFaceCurvatureDir");
183
184 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break; // skip
185 // skip strange string line
186 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break;
187 for (int i=0; i<nnv; i++){
188 vcg::Point3<float> u,v;
189 float a,b;
190 if (fscanf(f,
191 "%f %f %f %f %f %f %f %f",
192 &a,&b,
193 &(v.X()),&(v.Y()),&(v.Z()),
194 &(u.X()),&(u.Y()),&(u.Z())
195 )!=8) {
196 //if (errorMsg) sprintf(errorMsg,"Format error reading vertex n. %d",i);
197 return false;
198 }
199
200 u.Normalize();
201 v.Normalize();
202
203 if (per_vertex)
204 {
205 mesh.vert[i].PD1().Import(u);
206 mesh.vert[i].PD2().Import(v);
207 }
208 else
209 {
210 mesh.face[i].PD1().Import(u);
211 mesh.face[i].PD2().Import(v);
212 }
213 }
214 }
215 fclose(f);
216 return true;
217 }
218
219 ///Load a 4 rosy format file as used by
220 ///Interactive Visualization of Rotational Symmetry Fields on Surfaces
221 ///Jonathan Palacios and Eugene Zhang
222 static bool Load4ROSY(MeshType &mesh,
223 const char *path)
224 {
225 FILE *f = fopen(path,"rt");
226 if (!f)
227 {
228 return false;
229 }
230 int num,symm;
231 fscanf(f,"%d",&num);
232 assert(num==mesh.fn);
233 fscanf(f,"%d\n",&symm);
234 assert(symm==4);
235 for (int i=0;i<num;i++)
236 {
237 float dirX,dirY,dirZ;
238 fscanf(f,"%f %f %f \n",&dirX,&dirY,&dirZ);
239 mesh.face[i].PD1()=CoordType(dirX,dirY,dirZ);
240 mesh.face[i].PD2()=mesh.face[i].PD1()^mesh.face[i].N();
241 mesh.face[i].PD1().Normalize();
242 mesh.face[i].PD2().Normalize();
243 }
244 fclose(f);
245 return true;
246 }
247
248
249 static bool LoadSeamsMMFromOBJ(MeshType &mesh,std::string PathOBJ)
250 {
251 ///per face per edge of mmatch in the solver
252 typename MeshType::template PerFaceAttributeHandle<vcg::Point3i> Handle_MMatch;
253 ///seam per face
254 typename MeshType::template PerFaceAttributeHandle<vcg::Point3<bool> > Handle_Seams;
255
256 bool HasHandleMMatch=vcg::tri::HasPerFaceAttribute(mesh,std::string("MissMatch"));
257 if (!HasHandleMMatch)
258 Handle_MMatch = vcg::tri::Allocator<MeshType>::template AddPerFaceAttribute<vcg::Point3i>(mesh,std::string("MissMatch"));
259 else
260 Handle_MMatch = vcg::tri::Allocator<MeshType>::template FindPerFaceAttribute<vcg::Point3i>(mesh,std::string("MissMatch"));
261
262 bool HasHandleSeams=vcg::tri::HasPerFaceAttribute(mesh,std::string("Seams"));
263 if (!HasHandleSeams)
264 Handle_Seams=vcg::tri::Allocator<MeshType>::template AddPerFaceAttribute<vcg::Point3<bool> >(mesh,std::string("Seams"));
265 else
266 Handle_Seams=vcg::tri::Allocator<MeshType>::template FindPerFaceAttribute<vcg::Point3<bool> >(mesh,std::string("Seams"));
267
268 FILE *f = fopen(PathOBJ.c_str(),"rt");
269 if (!f)
270 return false;
271
272 for (unsigned int i=0;i<mesh.face.size();i++)
273 {
274 for (int j=0;j<3;j++)
275 {
276 Handle_Seams[i][j]=false;
277 Handle_MMatch[i][j]=0;
278 }
279 }
280
281 while (!feof(f))
282 {
283
284 int f_int,v_int,rot;
285 int readed=fscanf(f,"sm %d %d %d\n",&f_int,&v_int,&rot);
286 ///skip lines
287 if (readed==0)
288 {
289 char buff[200];
290 fscanf(f,"%s\n",&buff[0]);
291 }
292 else ///add the actual seams
293 {
294 VertexType *v=&mesh.vert[v_int-1];
295 FaceType *f0=&mesh.face[f_int-1];
296 int e0=-1;
297 if (f0->V(0)==v)e0=0;
298 if (f0->V(1)==v)e0=1;
299 if (f0->V(2)==v)e0=2;
300 e0=(e0+2)%3;
301 assert(e0!=-1);
302 FaceType *f1;
303 int e1;
304 f1=f0->FFp(e0);
305 e1=f0->FFi(e0);
306 Handle_Seams[f0][e0]=true;
307 Handle_Seams[f1][e1]=true;
308
309 Handle_MMatch[f0][e0]=rot;
310 int rot1;
311 if (rot==0)rot1=0;
312 if (rot==1)rot1=3;
313 if (rot==2)rot1=2;
314 if (rot==3)rot1=1;
315 Handle_MMatch[f1][e1]=rot1;
316 }
317 }
318 //printf("NEED %d LINES\n",i);
319 return true;
320 }
321
322
323 //Load a 4 rosy format file as pair of angles
324 static bool Load2AngleFace(MeshType &mesh,
325 const char *path)
326 {
327 FILE *f = fopen(path,"rt");
328 if (f==NULL)return false;
329 int num;
330 fscanf(f,"#%d param_field\n",&num);
331 if (num!=mesh.face.size())return false;
332 for (unsigned int i=0;i<mesh.face.size();i++)
333 {
334 float alpha1,alpha2;
335 int index;
336 fscanf(f,"%d %f %f \n",&index,&alpha1,&alpha2);
337 vcg::tri::CrossField<MeshType>::AnglesToCrossField(mesh.face[i],(ScalarType)alpha1,(ScalarType)alpha2,1);
338 }
339 fclose(f);
340 return true;
341 }
342
343 static bool LoadCSVField(MeshType &mesh,
344 const std::string &field_1,
345 const std::string &field_2,
346 bool Normalize=true)
347 {
348 FILE *f1=NULL;
349 FILE *f2=NULL;
350 f1=fopen(field_1.c_str(),"rt");
351 if(f1==NULL)return false;
352 f2=fopen(field_2.c_str(),"rt");
353 if(f2==NULL)return false;
354 for (size_t i=0;i<mesh.face.size();i++)
355 {
356 float Xdir,Ydir,Zdir;
357 fscanf(f1,"%f,%f,%f\n",&Xdir,&Ydir,&Zdir);
358 mesh.face[i].PD1()=CoordType(Xdir,Ydir,Zdir);
359 //std::cout<<Xdir<<" "<<Ydir<<std::endl;
360 fscanf(f2,"%f,%f,%f\n",&Xdir,&Ydir,&Zdir);
361 mesh.face[i].PD2()=CoordType(Xdir,Ydir,Zdir);
362 //set the quality as force Value
363 if (Normalize)
364 {
365 mesh.face[i].PD1().Normalize();
366 mesh.face[i].PD2().Normalize();
367 }
368 }
369
370 //then color by quality
371 fclose(f1);
372 fclose(f2);
373 return true;
374 }
375
376 }; // end class
377
378
379
380 } // end namespace tri
381 } // end namespace io
382 } // end namespace vcg
383
384 #endif
385