trimesh_ransac.cpp
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-2012                                           \/)\/    *
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 
00024 #include<vcg/complex/complex.h>
00025 #include<wrap/io_trimesh/import.h>
00026 #include<wrap/io_trimesh/export.h>
00027 #include<vcg/complex/algorithms/update/topology.h>
00028 #include<vcg/complex/algorithms/update/normal.h>
00029 #include<vcg/complex/algorithms/update/color.h>
00030 #include<vcg/complex/algorithms/inertia.h>
00031 #include<vcg/complex/algorithms/point_sampling.h>
00032 #include<vcg/complex/algorithms/ransac_matching.h>
00033 
00034 
00035 #include<vcg/space/index/kdtree/kdtree.h>
00036 #include<vcg/space/point_matching.h>
00037 
00038 using namespace vcg;
00039 
00040 class MyVertex; class MyEdge; class MyFace;
00041 struct MyUsedTypes : public UsedTypes<Use<MyVertex>   ::AsVertexType,
00042                                            Use<MyEdge>     ::AsEdgeType,
00043                                            Use<MyFace>     ::AsFaceType>{};
00044 
00045 class MyVertex  : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::Color4b, vertex::Qualityf, vertex::BitFlags  >{};
00046 class MyFace    : public Face<   MyUsedTypes, face::FFAdj,  face::Color4b, face::Normal3f, face::VertexRef, face::BitFlags > {};
00047 class MyEdge    : public Edge<   MyUsedTypes, edge::VertexRef, edge::VEAdj, edge::EEAdj, edge::BitFlags> {};
00048 
00049 class MyMesh    : public tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge>  > {};
00050 
00051 
00052 int main( int argc, char **argv )
00053 {
00054   setvbuf(stdout, NULL, _IONBF, 0);
00055   if(argc<3)
00056   {
00057     printf("Usage alignmeshmesh <meshfilename2.ply> <meshfilename2.ply>\n");
00058     return -1;
00059   }
00060   
00061   MyMesh fixM,movM;
00062 
00063   tri::io::Importer<MyMesh>::Open(fixM,argv[1]);
00064   printf( "Mesh0 has %i vert and %i faces\n", fixM.VN(), fixM.FN() );
00065   tri::io::Importer<MyMesh>::Open(movM,argv[2]);
00066   printf( "Mesh1 has %i vert and %i faces\n", movM.VN(), movM.FN() );
00067 
00068   math::MarsenneTwisterRNG rnd;
00069   
00070   tri::UpdateBounding<MyMesh>::Box(fixM);
00071   tri::UpdateBounding<MyMesh>::Box(movM);
00072   
00073   Point3f delta = math::GeneratePointInUnitBallUniform<float>(rnd) * fixM.bbox.Diag();
00074   tri::UpdatePosition<MyMesh>::Translate(movM,delta);
00075   Point3f axis = math::GeneratePointOnUnitSphereUniform<float>(rnd);
00076   float angle = rnd.generate01() * M_PI;
00077   Matrix44f rot; rot.SetRotateRad(angle,axis);
00078   tri::UpdatePosition<MyMesh>::Matrix(movM,rot);    
00079   tri::io::ExporterPLY<MyMesh>::Save(movM,"out.ply");
00080   int randSeed = clock();
00081   
00082   RansacFramework<MyMesh,BaseFeatureSet<MyMesh> > Ran;
00083   RansacFramework<MyMesh,BaseFeatureSet<MyMesh> >::Param pp;
00084   BaseFeatureSet<MyMesh>::Param fpp;
00085   pp.samplingRadiusPerc=0.008;
00086   pp.evalSize=50;
00087   pp.inlierRatioThr = 0.5;
00088   pp.iterMax = 100;
00089   pp.maxMatchingFeatureNum = 500;
00090   fpp.featureSampleRatio=0.5;
00091   std::vector<RansacFramework<MyMesh,BaseFeatureSet<MyMesh> >::Candidate> cVec;
00092   
00093 //  RansacFramework<MyMesh,NDFeatureSet<MyMesh> > Ran;
00094 //  RansacFramework<MyMesh,NDFeatureSet<MyMesh> >::Param pp;
00095 //  NDFeatureSet<MyMesh>::Param fpp;
00096 //  std::vector<RansacFramework<MyMesh,NDFeatureSet<MyMesh> >::Candidate> cVec;
00097 
00098 //  pp.samplingRadiusPerc=0.01;
00099 //  pp.evalSize=100;
00100 //  pp.inlierRatioThr = 0.4;
00101 //  pp.iterMax = 1000;
00102 //  int t0=clock();
00103   
00104 //  fpp.levAbs[0]=pp.samplingRadiusAbs*2.0;
00105 //  fpp.levAbs[0]=pp.samplingRadiusAbs*4.0;
00106 //  fpp.levAbs[0]=pp.samplingRadiusAbs*8.0;
00107 //  Ran.Init(fixM,movM,pp,fpp);
00108 //  Ran.EvaluateFeature(30,"featureTest30.ply",pp);
00109 //  fpp.levAbs[0]=pp.samplingRadiusAbs*3.0;
00110 //  fpp.levAbs[0]=pp.samplingRadiusAbs*6.0;
00111 //  fpp.levAbs[0]=pp.samplingRadiusAbs*9.0;
00112 //  Ran.Init(fixM,movM,pp,fpp);  
00113 //  Ran.EvaluateFeature(30,"featureTest30a.ply",pp);
00114   
00115 //  exit(-1);
00116   
00117   int t0=clock();
00118   Ran.Init(fixM,movM,pp,fpp);  
00119   int t1=clock(); 
00120   Ran.Process_SearchEvaluateTriple(cVec,pp);
00121   int t2=clock();
00122   
00123   printf("Completed Search (%5.2f init %5.2f search)\n",float(t1-t0)/CLOCKS_PER_SEC, float(t2-t1)/CLOCKS_PER_SEC);
00124   
00125   
00126   MyMesh out0; tri::Append<MyMesh,MyMesh>::MeshCopy(out0,movM);   
00127   tri::UpdatePosition<MyMesh>::Matrix(out0,cVec[0].Tr);
00128   tri::io::ExporterPLY<MyMesh>::Save(out0,"out0.ply");  
00129   
00130   MyMesh inlierMesh0;
00131   Ran.DumpInlier(inlierMesh0,cVec[0],pp);
00132   tri::io::ExporterPLY<MyMesh>::Save(inlierMesh0,"inlier0.ply");
00133   
00134   MyMesh out1; tri::Append<MyMesh,MyMesh>::MeshCopy(out1,movM);
00135   tri::UpdatePosition<MyMesh>::Matrix(out1,cVec[1].Tr);
00136   tri::io::ExporterPLY<MyMesh>::Save(out1,"out1.ply");  
00137   
00138   MyMesh inlierMesh1;
00139   Ran.DumpInlier(inlierMesh1,cVec[1],pp);
00140   tri::io::ExporterPLY<MyMesh>::Save(inlierMesh1,"inlier1.ply");
00141 
00142   return 0;
00143 }
00144 


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