import_field.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 #ifndef __VCGLIB_IMPORTERFIELD
00024 #define __VCGLIB_IMPORTERFIELD
00025 #include <vcg/complex/algorithms/parametrization/tangent_field_operators.h>
00026 
00027 namespace vcg {
00028 namespace tri {
00029 namespace io {
00030 
00034 template <class MeshType>
00035 class ImporterFIELD
00036 {
00037     typedef typename MeshType::ScalarType ScalarType;
00038     typedef typename MeshType::FaceType FaceType;
00039     typedef typename MeshType::VertexType VertexType;
00040     typedef typename MeshType::CoordType CoordType;
00041 
00042 public:
00043 
00044     static bool LoadGrad(MeshType &mesh,
00045                          const char *path)
00046     {
00047         FILE *f = fopen(path,"rt");
00048         if (!f)
00049         {
00050             return false;
00051         }
00052         int numF;
00053         fscanf(f,"%d\n",&numF);
00054         assert(numF==mesh.fn);
00055         char skipstr[200];
00056         //int readed0;
00057         for (int i=0;i<9;i++)
00058             fscanf(f,"%s",&skipstr[0]);
00059 
00060         for (int i=0;i<mesh.fn;i++)
00061         {
00062             int i0=-1;
00063             int i1=-1;
00064             int i2=-1;
00065             double u0,v0,u1,v1,u2,v2;
00066             int readed1=fscanf(f,"%d %d %d %lf %lf %lf %lf %lf %lf",&i0,&i1,&i2,&u0,&v0,&u1,&v1,&u2,&v2);
00067             assert(readed1==9);
00068             vcg::Point2<ScalarType> UV[3];
00069             UV[0]= vcg::Point2<ScalarType>(u0,v0);
00070             UV[1]= vcg::Point2<ScalarType>(u1,v1);
00071             UV[2]= vcg::Point2<ScalarType>(u2,v2);
00072             CoordType dir1;
00073             CoordType dir2;
00074             vcg::tri::CrossField<MeshType>::GradientToCross(mesh.face[i],UV[0],UV[1],UV[2],dir1,dir2);
00075             dir1.Normalize();
00076             dir2.Normalize();
00077             mesh.face[i].PD1()=dir1;
00078             mesh.face[i].PD2()=dir2;
00079         }
00080         fclose(f);
00081         return true;
00082     }
00083 
00084     static bool LoadNDF(MeshType &mesh,
00085                        const char *path)
00086     {
00087         FILE *f = fopen(path,"rt");
00088         if (!f)
00089         {
00090             fflush(stdout);
00091             return false;
00092         }
00093 
00094         char skipstr[200];
00095         //int readed0;
00096         do{
00097 
00098             fscanf(f,"%s\n",&skipstr[0]);
00099             printf("%s\n",skipstr);
00100         }while(strcmp(skipstr,"[Pjumps]")!=0);
00101 
00102 //        fscanf(f,"%s\"",skipstr);
00103 //        printf("%s\n",skipstr);
00104         fseek(f, 7, SEEK_CUR);
00105         char final[1];
00106         do{
00107             int period;
00108             fscanf(f,"%d;",&period);
00109             printf("%d\n",period);
00110             fscanf(f,"%c",&final);
00111             fseek(f, -1, SEEK_CUR);
00112             printf("%s\n",&final[0]);
00113         }while(strcmp(final,"\"")!=0);
00114 
00115 //        printf("%s\n",skipstr);
00116         fflush(stdout);
00117 //        for (int i=0;i<mesh.fn;i++)
00118 //        {
00119 //            int i0=-1;
00120 //            int i1=-1;
00121 //            int i2=-1;
00122 //            double u0,v0,u1,v1,u2,v2;
00123 //            int readed1=fscanf(f,"%d %d %d %lf %lf %lf %lf %lf %lf",&i0,&i1,&i2,&u0,&v0,&u1,&v1,&u2,&v2);
00124 //            assert(readed1==9);
00125 //            vcg::Point2<ScalarType> UV[3];
00126 //            UV[0]= vcg::Point2<ScalarType>(u0,v0);
00127 //            UV[1]= vcg::Point2<ScalarType>(u1,v1);
00128 //            UV[2]= vcg::Point2<ScalarType>(u2,v2);
00129 //            CoordType dir1;
00130 //            CoordType dir2;
00131 //            vcg::tri::CrossField<MeshType>::GradientToCross(mesh.face[i],UV[0],UV[1],UV[2],dir1,dir2);
00132 //            dir1.Normalize();
00133 //            dir2.Normalize();
00134 //            mesh.face[i].PD1()=dir1;
00135 //            mesh.face[i].PD2()=dir2;
00136 //        }
00137 //        fclose(f);
00138 //        return true;
00139     }
00140 
00143     static bool LoadFFIELD(MeshType &mesh,
00144         const char *path,
00145         bool per_vertex=false)
00146     {
00147 
00148         FILE *f = fopen(path,"rt");
00149         if (!f)
00150         {
00151             return false;
00152         }
00153         {
00154             char word[512]; word[0]=0;
00155             fscanf(f,"%s",word);
00156             char c=0;
00157             if (word[0]=='#') {
00158                 // skip comment line
00159                 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break;
00160             }
00161             else
00162             {
00163                 return false;
00164             }
00165             while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break;
00166             int nnv = -1;
00167             if (fscanf(f,"%d",&nnv)!=1)
00168             {
00169                 while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break; // skip
00170                 fscanf(f,"%d",&nnv);
00171             }
00172             int targetnum=mesh.fn;
00173             if (per_vertex)
00174                 targetnum=mesh.vn;
00175             if (nnv != (int)targetnum)
00176             {
00177                 //if (errorMsg) sprintf(errorMsg,"Wrong element number. Found: %d. Expected: %d.",nnv,mesh->vn);
00178                 return false;
00179             }
00180 
00181             if( per_vertex && !HasPerVertexCurvatureDir(mesh)) throw vcg::MissingComponentException("PerVertexCurvatureDir");
00182             if(!per_vertex && !HasPerFaceCurvatureDir(mesh))   throw vcg::MissingComponentException("PerFaceCurvatureDir");
00183 
00184             while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break; // skip
00185             // skip strange string line
00186             while (fscanf(f,"%c",&c)!=EOF) if (c=='\n') break;
00187             for (int i=0; i<nnv; i++){
00188                 vcg::Point3<float> u,v;
00189                 float a,b;
00190                 if (fscanf(f,
00191                     "%f %f %f %f %f %f %f %f",
00192                     &a,&b,
00193                     &(v.X()),&(v.Y()),&(v.Z()),
00194                     &(u.X()),&(u.Y()),&(u.Z())
00195                     )!=8) {
00196                         //if (errorMsg) sprintf(errorMsg,"Format error reading vertex n. %d",i);
00197                         return false;
00198                 }
00199 
00200                 u.Normalize();
00201                 v.Normalize();
00202 
00203                 if (per_vertex)
00204                 {
00205                   mesh.vert[i].PD1().Import(u);
00206                   mesh.vert[i].PD2().Import(v);
00207                 }
00208                 else
00209                 {
00210                   mesh.face[i].PD1().Import(u);
00211                   mesh.face[i].PD2().Import(v);
00212                 }
00213             }
00214         }
00215         fclose(f);
00216         return true;
00217     }
00218 
00222     static void Load4ROSY(MeshType &mesh,
00223                         const char *path)
00224     {
00225          FILE *f = fopen(path,"rt");
00226         int num,symm;
00227         fscanf(f,"%d",&num);
00228         assert(num==mesh.vn);
00229         fscanf(f,"%d\n",&symm);
00230         assert(symm==4);
00231         for (unsigned int i=0;i<num;i++)
00232         {
00233             float dirX,dirY,dirZ;
00234             fscanf(f,"%f %f %f \n",&dirX,&dirY,&dirZ);
00235             mesh.vert[i].PD1()=CoordType(dirX,dirY,dirZ);
00236             mesh.vert[i].PD2()=mesh.vert[i].PD1()^mesh.vert[i].N();
00237             mesh.vert[i].PD1().Normalize();
00238             mesh.vert[i].PD2().Normalize();
00239         }
00240         fclose(f);
00241     }
00242 
00243 
00244     static bool LoadSeamsMMFromOBJ(MeshType &mesh,std::string PathOBJ)
00245     {
00247         typename  MeshType::template PerFaceAttributeHandle<vcg::Point3i> Handle_MMatch;
00249         typename  MeshType::template PerFaceAttributeHandle<vcg::Point3<bool> > Handle_Seams;
00250 
00251         bool HasHandleMMatch=vcg::tri::HasPerFaceAttribute(mesh,std::string("MissMatch"));
00252         if (!HasHandleMMatch)
00253             Handle_MMatch = vcg::tri::Allocator<MeshType>::template AddPerFaceAttribute<vcg::Point3i>(mesh,std::string("MissMatch"));
00254         else
00255             Handle_MMatch = vcg::tri::Allocator<MeshType>::template FindPerFaceAttribute<vcg::Point3i>(mesh,std::string("MissMatch"));
00256 
00257         bool HasHandleSeams=vcg::tri::HasPerFaceAttribute(mesh,std::string("Seams"));
00258         if (!HasHandleSeams)
00259             Handle_Seams=vcg::tri::Allocator<MeshType>::template AddPerFaceAttribute<vcg::Point3<bool> >(mesh,std::string("Seams"));
00260         else
00261             Handle_Seams=vcg::tri::Allocator<MeshType>::template FindPerFaceAttribute<vcg::Point3<bool> >(mesh,std::string("Seams"));
00262 
00263         FILE *f = fopen(PathOBJ.c_str(),"rt");
00264         if (!f)
00265             return false;
00266 
00267         for (unsigned int i=0;i<mesh.face.size();i++)
00268         {
00269             for (int j=0;j<3;j++)
00270             {
00271                 Handle_Seams[i][j]=false;
00272                 Handle_MMatch[i][j]=0;
00273             }
00274         }
00275 
00276         while (!feof(f))
00277         {
00278 
00279             int f_int,v_int,rot;
00280             int readed=fscanf(f,"sm %d %d %d\n",&f_int,&v_int,&rot);
00282             if (readed==0)
00283             {
00284                 char buff[200];
00285                 fscanf(f,"%s\n",&buff[0]);
00286             }
00287             else 
00288             {
00289                 VertexType *v=&mesh.vert[v_int-1];
00290                 FaceType *f0=&mesh.face[f_int-1];
00291                 int e0=-1;
00292                 if (f0->V(0)==v)e0=0;
00293                 if (f0->V(1)==v)e0=1;
00294                 if (f0->V(2)==v)e0=2;
00295                 e0=(e0+2)%3;
00296                 assert(e0!=-1);
00297                 FaceType *f1;
00298                 int e1;
00299                 f1=f0->FFp(e0);
00300                 e1=f0->FFi(e0);
00301                 Handle_Seams[f0][e0]=true;
00302                 Handle_Seams[f1][e1]=true;
00303 
00304                 Handle_MMatch[f0][e0]=rot;
00305                 int rot1;
00306                 if (rot==0)rot1=0;
00307                 if (rot==1)rot1=3;
00308                 if (rot==2)rot1=2;
00309                 if (rot==3)rot1=1;
00310                 Handle_MMatch[f1][e1]=rot1;
00311             }
00312         }
00313         //printf("NEED  %d LINES\n",i);
00314         return true;
00315     }
00316 
00317 
00318     //Load a 4 rosy format file as pair of angles
00319     static bool Load2AngleFace(MeshType &mesh,
00320                               const char *path)
00321     {
00322         FILE *f = fopen(path,"rt");
00323         if (f==NULL)return false;
00324         int num;
00325         fscanf(f,"#%d param_field\n",&num);
00326         if (num!=mesh.face.size())return false;
00327         for (unsigned int i=0;i<mesh.face.size();i++)
00328         {
00329             float alpha1,alpha2;
00330             int index;
00331             fscanf(f,"%d %f %f \n",&index,&alpha1,&alpha2);
00332             vcg::tri::CrossField<MeshType>::AnglesToCrossField(mesh.face[i],(ScalarType)alpha1,(ScalarType)alpha2,1);
00333         }
00334         fclose(f);
00335         return true;
00336     }
00337 
00338 }; // end class
00339 
00340 
00341 
00342 } // end namespace tri
00343 } // end namespace io
00344 } // end namespace vcg
00345 
00346 #endif
00347 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:57