ptx2ply.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <vector>
00003 #include<vcg/math/base.h>
00004 #include<vcg/space/point3.h>
00005 #include<vcg/space/point4.h>
00006 #include<vcg/space/color4.h>
00007 #include<vcg/math/matrix44.h>
00008 
00009 #include <vcg/simplex/vertex/base.h>
00010 #include <vcg/simplex/vertex/component.h>
00011 #include <vcg/simplex/face/base.h>
00012 #include <vcg/simplex/face/component.h>
00013 #include <vcg/simplex/face/component_rt.h>
00014 
00015 #include<vcg/complex/complex.h>
00016 #include<vcg/complex/algorithms/create/platonic.h>
00017 #include<vcg/complex/algorithms/update/flag.h>
00018 #include<vcg/complex/algorithms/update/normal.h>
00019 #include<vcg/complex/algorithms/update/color.h>
00020 #include<vcg/complex/algorithms/clean.h>
00021 
00022 #include<wrap/io_trimesh/import_ply.h>
00023 #include<wrap/io_trimesh/export_ply.h>
00024 
00025 using namespace vcg;
00026 
00027 class MyEdge;
00028 class MyFaceC;
00029 class MyFace;
00030 class MyVertexC   : public VertexSimp2<MyVertexC,MyEdge,MyFaceC,vert::Coord3f,vert::Color4b,vert::Qualityf,vert::Normal3f,vert::BitFlags> {};
00031 class MyFaceC     : public FaceSimp2< MyVertexC,MyEdge,MyFaceC,face::VertexRef, face::Normal3f,face::BitFlags> {};
00032 class MyMeshC     : public tri::TriMesh< std::vector<MyVertexC>, std::vector<MyFaceC> > {};
00033 
00034 class MyVertex   : public VertexSimp2<MyVertex,MyEdge,MyFace,vert::Coord3f,vert::Normal3f,vert::BitFlags> {};
00035 class MyFace     : public FaceSimp2<  MyVertex,MyEdge,MyFace,face::VertexRef, face::Normal3f,face::BitFlags> {};
00036 class MyMesh     : public tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
00037 
00038 using namespace std;
00039 using namespace tri;
00040 
00041 /*
00042   class MyEdge;
00043   class MyFace;
00044   class MyEdgeC;
00045   class MyFaceC;
00046 
00047   class MyVertexC:public VertexVCVN<float,MyEdgeC,MyFace>{};
00048   class MyFaceC :public FaceFN<MyVertexC,MyEdgeC,MyFaceC>{};
00049   class MyMeshC: public tri::TriMesh< std::vector<MyVertexC>, std::vector<MyFaceC > >{};
00050 
00051   class MyVertex:public VertexVN<float,MyEdge,MyFace>{};
00052   class MyFace :public FaceFN<MyVertex,MyEdge,MyFace>{};
00053   class MyMesh: public tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace > >{};
00054 */
00055 
00056 //-------------------------------------------------------
00057 
00058   int                   nummeshes;                      // number of meshes extracted so far
00059   MyMesh                currentmesh;            // current mesh, read from stream and saved one completed
00060   MyMeshC               currentmeshC;           // current mesh, read from stream and saved one completed
00061   Matrix44f             currtrasf;
00062   float                 angle;                          // angle treshold for face deletion
00063   int                   singlemap;                      // single map mode, which map is to be saved. if -1 then all map are saved
00064   int                   frommap;                        // skip all maps BEFORE this index
00065   int                   tomap;                          // skip all maps AFTER this index
00066   bool                  savecolor;                      // if has color, save it on 3dmesh
00067   bool                  hascolor;                       // true if the current mesh has color
00068   bool          saveall;                        // all elements are keeped (even invalids)
00069   bool          flipfaces;                      // flip all faces
00070   int                   todump;
00071   bool                  dumpit;
00072   bool          unpack;
00073   bool                  onlypoints;                     // store only points
00074   bool                  switchside;                     // inverse triangulation order (swaping row->cols)
00075 
00076 
00077 
00078 
00079 // read the current mesh from the stream
00080 int readmesh(FILE* fp)
00081 {
00082         int colnum;
00083         int rownum;
00084         int trinum;
00085         int numtokens;
00086         int rit,cit;
00087         char linebuf[256];
00088         int ii;
00089         float xx,yy,zz; // position
00090         float rr,gg,bb; // color
00091         float rf;               // reflectance
00092         MyMesh::FaceIterator fi;
00093         MyMesh::VertexIterator vi;
00094         MyMeshC::FaceIterator fiC;
00095         MyMeshC::VertexIterator viC;
00096 
00097         // cleaning mesh
00098         currentmesh.Clear();
00099         currentmeshC.Clear();
00100 
00101         // getting mesh size;
00102         fscanf(fp,"%i\n",&colnum);
00103         fscanf(fp,"%i\n",&rownum);
00104 
00105         // initial 4 lines [still don't know what is this :) :)]
00106         fscanf(fp,"%f %f %f\n", &xx, &yy, &zz);
00107         fscanf(fp,"%f %f %f\n", &xx, &yy, &zz);
00108         fscanf(fp,"%f %f %f\n", &xx, &yy, &zz);
00109         fscanf(fp,"%f %f %f\n", &xx, &yy, &zz);
00110 
00111         // now the transformation matrix
00112         fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(0,0)), &(currtrasf.ElementAt(0,1)), &(currtrasf.ElementAt(0,2)), &(currtrasf.ElementAt(0,3)));
00113         fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(1,0)), &(currtrasf.ElementAt(1,1)), &(currtrasf.ElementAt(1,2)), &(currtrasf.ElementAt(1,3)));
00114         fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(2,0)), &(currtrasf.ElementAt(2,1)), &(currtrasf.ElementAt(2,2)), &(currtrasf.ElementAt(2,3)));
00115         fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(3,0)), &(currtrasf.ElementAt(3,1)), &(currtrasf.ElementAt(3,2)), &(currtrasf.ElementAt(3,3)));
00116 
00117         // now the real data begins
00118 
00119         // first line, we should know if the format is
00120         // XX YY ZZ RF
00121         // or it is
00122         // XX YY ZZ RF RR GG BB
00123 
00124         // read the entire first line and then count the spaces. it's rude but it works :)
00125         ii=0;
00126         fread(&(linebuf[ii++]),1,1,fp);
00127         while(linebuf[ii-1] != '\n')
00128        fread(&(linebuf[ii++]),1,1,fp);
00129     linebuf[ii-1] = '\0'; // terminate the string
00130 
00131     numtokens=1;
00132         for(ii=0; ii<strlen(linebuf); ii++)
00133         {
00134                 if(linebuf[ii] == ' ')
00135                         numtokens++;
00136         }
00137 
00138         if(numtokens == 4)
00139           hascolor = false;
00140         else if(numtokens == 7)
00141           hascolor = true;
00142         else
00143           return -1;
00144 
00145         // allocate all points
00146         printf("\n %i x %i \n", rownum, colnum);
00147         printf(" expect V %i F %i",(rownum*colnum),((rownum-1)*(colnum-1)*2));
00148 
00149         if(hascolor && savecolor)
00150         {
00151          viC = Allocator<MyMeshC>::AddVertices(currentmeshC,(rownum*colnum));
00152         }
00153         else
00154         {
00155          vi = Allocator<MyMesh>::AddVertices(currentmesh,(rownum*colnum));
00156         }
00157 
00158         // parse the first line....
00159         if(hascolor)
00160         {
00161           printf("\n hascolor ");
00162           sscanf(linebuf,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
00163         }
00164         else
00165         {
00166           printf("\n no color ");
00167           sscanf(linebuf,"%f %f %f %f", &xx, &yy, &zz, &rf);
00168         }
00169 
00170         //addthefirstpoint
00171         if(hascolor && savecolor)
00172         {
00173          (*viC).P()[0]=xx;
00174          (*viC).P()[1]=yy;
00175          (*viC).P()[2]=zz;
00176          (*viC).Q()=rf;
00177          (*viC).C()[0]=rr;
00178          (*viC).C()[1]=gg;
00179          (*viC).C()[2]=bb;
00180          viC++;
00181         }
00182         else
00183         {
00184          (*vi).P()[0]=xx;
00185          (*vi).P()[1]=yy;
00186          (*vi).P()[2]=zz;
00187          vi++;
00188         }
00189 
00190         // now for each line until end of mesh (row*col)-1
00191         for(ii=0; ii<((rownum*colnum)-1); ii++)
00192         {
00193          // read the stream
00194          if(hascolor)
00195            fscanf(fp,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
00196          else
00197            fscanf(fp,"%f %f %f %f", &xx, &yy, &zz, &rf);
00198 
00199          // add the point
00200          if(hascolor && savecolor)
00201          {
00202           (*viC).P()[0]=xx;
00203           (*viC).P()[1]=yy;
00204           (*viC).P()[2]=zz;
00205           (*viC).Q()=rf;
00206                 (*viC).C()[0]=rr;
00207           (*viC).C()[1]=gg;
00208           (*viC).C()[2]=bb;
00209           viC++;
00210          }
00211          else
00212          {
00213           (*vi).P()[0]=xx;
00214           (*vi).P()[1]=yy;
00215           (*vi).P()[2]=zz;
00216           vi++;
00217          }
00218 
00219         }
00220 
00221         currentmesh.vn = currentmesh.vert.size();
00222 
00223 
00224         if(! onlypoints)
00225         {
00226 
00227                 // now i can triangulate
00228                 trinum = (rownum-1) * (colnum-1) * 2;
00229 
00230                 if(hascolor && savecolor)
00231                 {
00232                 fiC= Allocator<MyMeshC>::AddFaces(currentmeshC,trinum);
00233                 }
00234                 else
00235                 {
00236                 fi= Allocator<MyMesh>::AddFaces(currentmesh,trinum);
00237                 }
00238 
00239 
00240                 currentmesh.fn = 0;
00241                 currentmeshC.fn = 0;
00242                 int v0i,v1i,v2i;
00243                 for(rit=0; rit<rownum-1; rit++)
00244                 for(cit=0; cit<colnum-1; cit++)
00245                 if(hascolor && savecolor)
00246                 {
00247 
00248                         if(!switchside)
00249                         {
00250                                 v0i = (rit  ) + ((cit  ) * rownum);
00251                                 v1i = (rit+1) + ((cit  ) * rownum);
00252                                 v2i = (rit  ) + ((cit+1) * rownum);
00253                         }
00254                         else
00255                         {
00256                                 v0i = (cit  ) + ((rit  ) * colnum);
00257                                 v1i = (cit+1) + ((rit  ) * colnum);
00258                                 v2i = (cit  ) + ((rit+1) * colnum);
00259                         }
00260 
00261 
00262                         // upper tri
00263                         (*fiC).V(2) = &(currentmeshC.vert[v0i]);
00264                         (*fiC).V(1) = &(currentmeshC.vert[v1i]);
00265                         (*fiC).V(0) = &(currentmeshC.vert[v2i]);
00266 
00267                         if(flipfaces)
00268                         {
00269                         (*fiC).V(2) = &(currentmeshC.vert[v1i]);
00270                         (*fiC).V(1) = &(currentmeshC.vert[v0i]);
00271                         }
00272 
00273                         currentmeshC.fn++;
00274                         fiC++;
00275 
00276                         if(!switchside)
00277                         {
00278                                 v0i = (rit+1) + ((cit  ) * rownum);
00279                                 v1i = (rit+1) + ((cit+1) * rownum);
00280                                 v2i = (rit  ) + ((cit+1) * rownum);
00281                         }
00282                         else
00283                         {
00284                                 v0i = (cit+1) + ((rit  ) * colnum);
00285                                 v1i = (cit+1) + ((rit+1) * colnum);
00286                                 v2i = (cit  ) + ((rit+1) * colnum);
00287                         }
00288 
00289                         // lower tri
00290                         (*fiC).V(2) = &(currentmeshC.vert[v0i]);
00291                         (*fiC).V(1) = &(currentmeshC.vert[v1i]);
00292                         (*fiC).V(0) = &(currentmeshC.vert[v2i]);
00293 
00294                         if(flipfaces)
00295                         {
00296                         (*fiC).V(2) = &(currentmeshC.vert[v1i]);
00297                         (*fiC).V(1) = &(currentmeshC.vert[v0i]);
00298                         }
00299 
00300                         currentmeshC.fn++;
00301                         fiC++;
00302                 }
00303                 else
00304                 {
00305                         // upper tri
00306                         if(!switchside)
00307                         {
00308                                 v0i = (rit  ) + ((cit  ) * rownum);
00309                                 v1i = (rit+1) + ((cit  ) * rownum);
00310                                 v2i = (rit  ) + ((cit+1) * rownum);
00311                         }
00312                         else
00313                         {
00314                                 v0i = (cit  ) + ((rit  ) * colnum);
00315                                 v1i = (cit+1) + ((rit  ) * colnum);
00316                                 v2i = (cit  ) + ((rit+1) * colnum);
00317                         }
00318 
00319                         (*fi).V(2) = &(currentmesh.vert[v0i]);
00320                         (*fi).V(1) = &(currentmesh.vert[v1i]);
00321                         (*fi).V(0) = &(currentmesh.vert[v2i]);
00322 
00323                         if(flipfaces)
00324                         {
00325                         (*fi).V(2) = &(currentmesh.vert[v1i]);
00326                         (*fi).V(1) = &(currentmesh.vert[v0i]);
00327                         }
00328 
00329                         currentmesh.fn++;
00330                         fi++;
00331 
00332                         // lower tri
00333                         if(!switchside)
00334                         {
00335                                 v0i = (rit+1) + ((cit  ) * rownum);
00336                                 v1i = (rit+1) + ((cit+1) * rownum);
00337                                 v2i = (rit  ) + ((cit+1) * rownum);
00338                         }
00339                         else
00340                         {
00341                                 v0i = (cit+1) + ((rit  ) * colnum);
00342                                 v1i = (cit+1) + ((rit+1) * colnum);
00343                                 v2i = (cit  ) + ((rit+1) * colnum);
00344                         }
00345 
00346                         (*fi).V(2) = &(currentmesh.vert[v0i]);
00347                         (*fi).V(1) = &(currentmesh.vert[v1i]);
00348                         (*fi).V(0) = &(currentmesh.vert[v2i]);
00349 
00350                         if(flipfaces)
00351                         {
00352                         (*fi).V(2) = &(currentmesh.vert[v1i]);
00353                         (*fi).V(1) = &(currentmesh.vert[v0i]);
00354                         }
00355 
00356                         currentmesh.fn++;
00357                         fi++;
00358                 }
00359 
00360 
00361                 if(hascolor && savecolor)
00362                 printf("\nV: %8i F: %8i \n", currentmeshC.vn, currentmeshC.fn);
00363                 else
00364                 printf("\nV: %8i F: %8i \n", currentmesh.vn, currentmesh.fn);
00365         }
00366 
00367 
00368         if(! saveall)
00369         {
00370         printf("deleting unsampled points \n");
00371         // now i delete all points in (0,0,0) that are unsampled points
00372         if(hascolor && savecolor)
00373         for(viC = currentmeshC.vert.begin(); viC != currentmeshC.vert.end(); viC++)
00374         {
00375                 if((*viC).P() == Point3f(0.0, 0.0, 0.0))
00376                 {
00377                         (*viC).SetD();
00378                         currentmeshC.vn--;
00379                 }
00380         }
00381         else
00382         for(vi = currentmesh.vert.begin(); vi != currentmesh.vert.end(); vi++)
00383         {
00384                 if((*vi).P() == Point3f(0.0, 0.0, 0.0))
00385                 {
00386                         (*vi).SetD();
00387                         currentmesh.vn--;
00388                 }
00389         }
00390         }
00391 
00392 
00393         if(! onlypoints)
00394         {
00395 
00396                 if(! saveall)
00397                 {
00398                 printf("deleting invalid faces \n");
00399                 // and then i delete all faces with null vertices
00400                 if(hascolor && savecolor)
00401                 for(fiC = currentmeshC.face.begin(); fiC != currentmeshC.face.end(); fiC++)
00402                 {
00403                         if( ((*fiC).V(0)->IsD()) || ((*fiC).V(1)->IsD()) || ((*fiC).V(2)->IsD()) )
00404                         {
00405                                 (*fiC).SetD();
00406                                 currentmeshC.fn--;
00407                         }
00408                 }
00409                 else
00410                 for(fi = currentmesh.face.begin(); fi != currentmesh.face.end(); fi++)
00411                 {
00412                         if( ((*fi).V(0)->IsD()) || ((*fi).V(1)->IsD()) || ((*fi).V(2)->IsD()) )
00413                         {
00414                                 (*fi).SetD();
00415                                 currentmesh.fn--;
00416                         }
00417                 }
00418                 }
00419 
00420                 if(hascolor && savecolor)
00421                 printf("V: %8i F: %8i \n", currentmeshC.vn, currentmeshC.fn);
00422                 else
00423                 printf("V: %8i F: %8i \n", currentmesh.vn, currentmesh.fn);
00424 
00425 
00426                 // eliminate high angle triangles
00427                 if((angle != 90)&&(!saveall))
00428                 {
00429                 printf(" culling by angle \n");
00430                 float limit = cos( angle*3.14159265358979323846/180.0 );
00431                 Point3f raggio;
00432 
00433                 if(hascolor && savecolor)
00434                 {
00435                 tri::UpdateNormals<MyMeshC>::PerFaceNormalized(currentmeshC);
00436                 for(fiC = currentmeshC.face.begin(); fiC != currentmeshC.face.end(); fiC++)
00437                 if(!(*fiC).IsD())
00438                         {
00439                                 raggio = -((*fiC).V(0)->P() + (*fiC).V(1)->P() + (*fiC).V(2)->P()) / 3.0;
00440                                 raggio.Normalize();
00441                                 if(((*fiC).N() * raggio) < limit)
00442                                 {
00443                                 (*fiC).SetD();
00444                                 currentmeshC.fn--;
00445                                 }
00446                         }
00447                 }
00448                 else
00449                 {
00450                 vcg::tri::UpdateNormals<MyMesh>::PerFaceNormalized(currentmesh);
00451                 for(fi = currentmesh.face.begin(); fi != currentmesh.face.end(); fi++)
00452                 if(!(*fi).IsD())
00453                         {
00454                                 raggio = -((*fi).V(0)->P() + (*fi).V(1)->P() + (*fi).V(2)->P()) / 3.0;
00455                                 raggio.Normalize();
00456                                 if((raggio * (*fi).N()) < limit)
00457                                 {
00458                                 (*fi).SetD();
00459                                 currentmesh.fn--;
00460                                 }
00461                         }
00462                 }
00463 
00464                 }
00465         }
00466 
00467         currtrasf.transposeInPlace();
00468 
00469         // apply tranformation
00470         if(hascolor && savecolor)
00471         {
00472          for(viC = currentmeshC.vert.begin(); viC != currentmeshC.vert.end(); viC++)
00473           if(!(*viC).IsD())
00474                 {
00475                   (*viC).P() = currtrasf * (*viC).P();
00476                 }
00477         }
00478         else
00479         {
00480          for(vi = currentmesh.vert.begin(); vi != currentmesh.vert.end(); vi++)
00481           if(!(*vi).IsD())
00482                 {
00483                   (*vi).P() = currtrasf * (*vi).P();
00484                 }
00485         }
00486 
00487         if(hascolor && savecolor)
00488         {
00489      int dup = tri::Clean<MyMeshC>::RemoveDuplicateVertex(currentmeshC);
00490          if(! onlypoints)
00491        int unref =  tri::Clean<MyMeshC>::RemoveUnreferencedVertex(currentmeshC);
00492         }
00493         else
00494         {
00495      int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(currentmesh);
00496          if(! onlypoints)
00497                 int unref =  tri::Clean<MyMesh>::RemoveUnreferencedVertex(currentmesh);
00498         }
00499 
00500         return 0;
00501 }
00502 
00503 // save each mesh in a separate file
00504 void dounpack(FILE* fp)
00505 {
00506  FILE* outf;
00507  char namef[128];
00508  int rnum;
00509  char linebuf[256];
00510  int ii;
00511  bool trovato;
00512 
00513  rnum=1;
00514 
00515          trovato = false;
00516 
00517          // search for the first integer
00518          while(!trovato)
00519          {
00520           // read the entire first line and then count the spaces. it's rude but it works :)
00521           ii=0;
00522           fread(&(linebuf[ii++]),1,1,fp);
00523           while(linebuf[ii-1] != '\n')
00524         fread(&(linebuf[ii++]),1,1,fp);
00525       linebuf[ii-1] = '\0'; // terminate the string
00526 
00527           //check the string
00528           if(strchr(linebuf,' ') == NULL)
00529                   trovato = true;
00530          }
00531 
00532  while(!feof(fp))
00533  {
00534 
00535      sprintf(namef,"range%03i.ptx",rnum++);
00536          outf = fopen(namef,"w");
00537 
00538          // write first integer
00539          fprintf(outf,"%s\n",linebuf);
00540 
00541          // read and write next int
00542          ii=0;
00543          fread(&(linebuf[ii++]),1,1,fp);
00544          while(linebuf[ii-1] != '\n')
00545        fread(&(linebuf[ii++]),1,1,fp);
00546      linebuf[ii-1] = '\0'; // terminate the string
00547          fprintf(outf,"%s\n",linebuf);
00548 
00549          // search for the next integer
00550          while(!trovato)
00551          {
00552           // read the entire first line and then count the spaces. it's rude but it works :)
00553           ii=0;
00554           fread(&(linebuf[ii++]),1,1,fp);
00555           while(linebuf[ii-1] != '\n')
00556         fread(&(linebuf[ii++]),1,1,fp);
00557       linebuf[ii-1] = '\0'; // terminate the string
00558 
00559           //if not an integer then write it, otherwise close and remember for next step
00560           if(strchr(linebuf,' ') == NULL)
00561               trovato = true;
00562           else
00563                   fprintf(outf,"%s\n",linebuf);
00564          }
00565 
00566          fclose(outf);
00567  }
00568 
00569 }
00570 
00571 // skip a mesh
00572 int skipmesh(FILE* fp)
00573 {
00574         int colnum;
00575         int rownum;
00576         int skiplines;
00577         char linebuf;
00578 
00579         if(feof(fp))
00580                 return -1;
00581 
00582         // getting mesh size;
00583         fscanf(fp,"%i\n",&colnum);
00584         fscanf(fp,"%i\n",&rownum);
00585 
00586         printf("\n %i x %i \n", rownum, colnum);
00587         printf(" expect V %i F %i\n",(rownum*colnum),((rownum-1)*(colnum-1)*2));
00588 
00589         if(feof(fp))
00590                 return -1;
00591 
00592         skiplines = (colnum * rownum) + 8; // have to skip (col * row) lines plus 8 lines for the header
00593         for(int ii=0; ii<skiplines; ii++)
00594         {
00595          fread(&linebuf,1,1,fp);
00596          while(linebuf != '\n')
00597        fread(&linebuf,1,1,fp);
00598         }
00599 
00600         return 0;
00601 }
00602 
00603 
00604 void parseparams(int argn, char** argvect)
00605 {
00606  int pit;
00607 
00608  for(pit = 2; pit<argn; pit++)
00609  {
00610   if(argvect[pit][0] != '-')    // invalid param
00611   {
00612           printf("invalid parameter\n");
00613   }
00614   else                                          // valid param
00615   {
00616           if(argvect[pit][1] == 'a')    // angle
00617           {
00618                 angle = atof(&(argvect[pit][2]));
00619                 printf("cutoff angle = %f \n",angle);
00620           }
00621           if(argvect[pit][1] == 'm')    // single map
00622           {
00623                 singlemap = atoi(&(argvect[pit][2]));
00624                 frommap = 0;
00625                 tomap = singlemap;
00626                 printf("single map # %i \n",singlemap);
00627           }
00628           if(argvect[pit][1] == 'f')    // from map
00629           {
00630                 frommap = atoi(&(argvect[pit][2]));
00631                 singlemap = -1;
00632                 printf("start from map # %i \n",frommap);
00633           }
00634           if(argvect[pit][1] == 't')    // from map
00635           {
00636                 tomap = atoi(&(argvect[pit][2]));
00637                 singlemap = -1;
00638                 printf("end with map # %i \n",tomap);
00639           }
00640           if(argvect[pit][1] == 'd')    // dump to file
00641           {
00642                 todump = atoi(&(argvect[pit][2]));
00643                 dumpit = true;
00644                 printf("dumping # %i chars\n",todump);
00645           }
00646           if(argvect[pit][1] == 'u')    // unpack the file in different
00647           {
00648                 unpack = true;
00649                 printf("UNPACKING \n");
00650           }
00651           if(argvect[pit][1] == 'c')    // save color if present
00652           {
00653                 savecolor = true;
00654                 printf("saving color \n");
00655           }
00656           if(argvect[pit][1] == 'k')    // keep all
00657           {
00658                 saveall = true;
00659                 printf("keeping all elements \n");
00660           }
00661           if(argvect[pit][1] == 'f')    // flip all tris
00662           {
00663                 flipfaces = true;
00664                 printf("keeping all elements \n");
00665           }
00666           if(argvect[pit][1] == 'p')    // points only, do not triangulate
00667           {
00668                 onlypoints = true;
00669                 printf("points only, do not triangulate \n");
00670           }
00671           if(argvect[pit][1] == 'r')    // points only, do not triangulate
00672           {
00673                 switchside = true;
00674                 printf("swapped triangulation \n");
00675           }
00676 
00677 
00678   }
00679  }
00680 
00681 }
00682 
00683 void printhelp()
00684 {
00685   printf("-------------------------------------------------------------\n");
00686   printf("multiple PLY files will be extracted from the PTX \n");
00687   printf("\n");
00688   printf("USAGE:    ptx2ply filename.ptx [options]");
00689   printf("\n");
00690   printf("each map contained in the file will be saved in a PLY \n");
00691   printf("\n");
00692   printf("-aAA angle threshold for face elimination, glazing faces with\n");
00693   printf("     angle between normal and viewdirection > AA are removed \n");
00694   printf("     default is no cut.                                      \n");
00695   printf("     beware! this only works for range maps that still need  \n");
00696   printf("     to be tranformed in the final reference system          \n");
00697   printf("     (in this case the viewpoint is the origin)              \n");
00698   printf("\n");
00699   printf("-c   save color if present \n");
00700   printf("\n");
00701   printf("-mNN extract just the map NN, skip all the rest of the file  \n");
00702   printf("\n");
00703   printf("-fNN extract maps starting FROM index NN  \n");
00704   printf("\n");
00705   printf("-tNN extract maps UP TO index NN  \n");
00706   printf("\n");
00707   printf("-u   unpack the file generating a ptx for each map  \n");
00708   printf("\n");
00709   printf("-k   keep all elements (no points/tris discarded)  \n");
00710   printf("     only useful for debug  \n");
00711   printf("\n");
00712   printf("-f   flip all faces  \n");
00713   printf("\n");
00714   printf("-p   store points only  \n");
00715   printf("\n");
00716   printf("-r   during triangulation, swap rows->columns  \n");
00717   printf("\n");
00718   printf("MESH INDICES STARTS FROM 1                                   \n");
00719   printf("parameters -f and -t can be used together to specify an index\n");
00720   printf("range to be processed. \n");
00721   printf("-------------------------------------------------------------\n");
00722   exit(0);
00723 }
00724 
00725 
00726 
00731 int main(int argc, char *argv[])
00732 {
00733   FILE *fp;
00734   char filename[256];
00735   char modelname[128];
00736 
00737 
00738   //---------------------------------------------------------------
00739   printf("PTX to PLY conversion\n");
00740   //---------------------------------------------------------------
00741 
00742   //-- init params
00743   nummeshes = 1;
00744   currentmesh.Clear();
00745   angle = 90.0;
00746   singlemap = -1;
00747   frommap = 0;
00748   tomap = 99999;
00749   todump = 1024;
00750   dumpit = false;
00751   unpack = false;
00752   savecolor = false;
00753   saveall = false;
00754   flipfaces = false;
00755   onlypoints = false;
00756   switchside = false;
00757 
00758 
00759   if(argc < 2)
00760    printhelp();
00761 
00762 
00763   //--
00764   parseparams(argc, argv);
00765 
00766   strcpy(modelname,argv[1]);
00767   modelname[strlen(argv[1])-4] = '\0';
00768 
00769   fp = fopen(argv[1],"r");
00770 
00771   if(unpack)
00772           dounpack(fp);
00773 
00774   while((!feof(fp)) && (nummeshes <= tomap))
00775   {
00776    printf("mesh %3i ",nummeshes);
00777 
00778    if((nummeshes >= frommap) && (nummeshes <= tomap) && ((singlemap == -1) || (singlemap == nummeshes)))
00779    {
00780      if(dumpit)
00781          {
00782           FILE* outf;
00783           char cbuf;
00784 
00785           outf = fopen("dump.txt","w");
00786 
00787           for(int dit=0; dit<todump; dit++)
00788           {
00789                   fread(&cbuf,1,1,fp);
00790                   fwrite(&cbuf,1,1,outf);
00791           }
00792 
00793           fclose(outf);
00794           fclose(fp);
00795           exit(0);
00796          }
00797 
00798          printf("reading ");
00799      readmesh(fp);
00800 
00801      sprintf(filename,"%s_%03i.ply",modelname,nummeshes);
00802      if(!feof(fp))
00803          {
00804           if(hascolor && savecolor)
00805           {
00806            int plyMask=tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY;
00807        tri::io::ExporterPLY<MyMeshC>::Save(currentmeshC,filename, plyMask);
00808           }
00809           else
00810        tri::io::ExporterPLY<MyMesh>::Save(currentmesh,filename);
00811          }
00812 
00813      printf("ok! \n");
00814 
00815     }
00816         else
00817         {
00818      printf("skipping ");
00819          skipmesh(fp);
00820      printf("ok! \n");
00821         }
00822 
00823    nummeshes++;
00824   }
00825 
00826   fclose(fp);
00827 
00828   return 0;
00829 }
00830 


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