VCG Library
trimesh_ransac.cpp
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 
24 #include<vcg/complex/complex.h>
25 #include<wrap/io_trimesh/import.h>
26 #include<wrap/io_trimesh/export.h>
27 #include<vcg/complex/algorithms/update/topology.h>
28 #include<vcg/complex/algorithms/update/normal.h>
29 #include<vcg/complex/algorithms/update/color.h>
30 #include<vcg/complex/algorithms/inertia.h>
31 #include<vcg/complex/algorithms/point_sampling.h>
32 #include<vcg/complex/algorithms/ransac_matching.h>
33 
34 
35 #include<vcg/space/index/kdtree/kdtree.h>
36 #include<vcg/space/point_matching.h>
37 
38 using namespace vcg;
39 
40 class MyVertex; class MyEdge; class MyFace;
41 struct MyUsedTypes : public UsedTypes<Use<MyVertex> ::AsVertexType,
42  Use<MyEdge> ::AsEdgeType,
43  Use<MyFace> ::AsFaceType>{};
44 
45 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::Color4b, vertex::Qualityf, vertex::BitFlags >{};
46 class MyFace : public Face< MyUsedTypes, face::FFAdj, face::Color4b, face::Normal3f, face::VertexRef, face::BitFlags > {};
47 class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::VEAdj, edge::EEAdj, edge::BitFlags> {};
48 
49 class MyMesh : public tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge> > {};
50 
51 
52 int main( int argc, char **argv )
53 {
54  setvbuf(stdout, NULL, _IONBF, 0);
55  if(argc<3)
56  {
57  printf("Usage alignmeshmesh <meshfilename2.ply> <meshfilename2.ply>\n");
58  return -1;
59  }
60 
61  MyMesh fixM,movM;
62 
63  tri::io::Importer<MyMesh>::Open(fixM,argv[1]);
64  printf( "Mesh0 has %i vert and %i faces\n", fixM.VN(), fixM.FN() );
65  tri::io::Importer<MyMesh>::Open(movM,argv[2]);
66  printf( "Mesh1 has %i vert and %i faces\n", movM.VN(), movM.FN() );
67 
68  math::MarsenneTwisterRNG rnd;
69 
72 
73  Point3f delta = math::GeneratePointInUnitBallUniform<float>(rnd) * fixM.bbox.Diag();
75  Point3f axis = math::GeneratePointOnUnitSphereUniform<float>(rnd);
76  float angle = rnd.generate01() * M_PI;
77  Matrix44f rot; rot.SetRotateRad(angle,axis);
79  tri::io::ExporterPLY<MyMesh>::Save(movM,"out.ply");
80  int randSeed = clock();
81 
82  RansacFramework<MyMesh,BaseFeatureSet<MyMesh> > Ran;
83  RansacFramework<MyMesh,BaseFeatureSet<MyMesh> >::Param pp;
84  BaseFeatureSet<MyMesh>::Param fpp;
85  pp.samplingRadiusPerc=0.008;
86  pp.evalSize=50;
87  pp.inlierRatioThr = 0.5;
88  pp.iterMax = 400;
89  pp.maxMatchingFeatureNum = 500;
90  fpp.featureSampleRatio=0.5;
91  std::vector<RansacFramework<MyMesh,BaseFeatureSet<MyMesh> >::Candidate> cVec;
92 
93 // RansacFramework<MyMesh,NDFeatureSet<MyMesh> > Ran;
94 // RansacFramework<MyMesh,NDFeatureSet<MyMesh> >::Param pp;
95 // NDFeatureSet<MyMesh>::Param fpp;
96 // std::vector<RansacFramework<MyMesh,NDFeatureSet<MyMesh> >::Candidate> cVec;
97 
98 // pp.samplingRadiusPerc=0.01;
99 // pp.evalSize=100;
100 // pp.inlierRatioThr = 0.4;
101 // pp.iterMax = 1000;
102 // int t0=clock();
103 
104 // fpp.levAbs[0]=pp.samplingRadiusAbs*2.0;
105 // fpp.levAbs[0]=pp.samplingRadiusAbs*4.0;
106 // fpp.levAbs[0]=pp.samplingRadiusAbs*8.0;
107 // Ran.Init(fixM,movM,pp,fpp);
108 // Ran.EvaluateFeature(30,"featureTest30.ply",pp);
109 // fpp.levAbs[0]=pp.samplingRadiusAbs*3.0;
110 // fpp.levAbs[0]=pp.samplingRadiusAbs*6.0;
111 // fpp.levAbs[0]=pp.samplingRadiusAbs*9.0;
112 // Ran.Init(fixM,movM,pp,fpp);
113 // Ran.EvaluateFeature(30,"featureTest30a.ply",pp);
114 
115 // exit(-1);
116 
117  int t0=clock();
118  Ran.Init(fixM,movM,pp,fpp);
119  int t1=clock();
120  Ran.Process_SearchEvaluateTriple(cVec,pp);
121  int t2=clock();
122 
123  printf("Completed Search (%5.2f init %5.2f search)\n",float(t1-t0)/CLOCKS_PER_SEC, float(t2-t1)/CLOCKS_PER_SEC);
124 
125 
126  MyMesh out0; tri::Append<MyMesh,MyMesh>::MeshCopy(out0,fixM);
127  //tri::UpdatePosition<MyMesh>::Matrix(out0,cVec[0].Tr);
128  tri::io::ExporterPLY<MyMesh>::Save(out0,"out0.ply");
129 
130  MyMesh inlierMesh0;
131  Ran.DumpInlier(inlierMesh0,cVec[0],pp);
132  tri::io::ExporterPLY<MyMesh>::Save(inlierMesh0,"inlier0.ply");
133 
134  MyMesh out1; tri::Append<MyMesh,MyMesh>::MeshCopy(out1,movM);
135  tri::UpdatePosition<MyMesh>::Matrix(out1,cVec[1].Tr);
136  tri::io::ExporterPLY<MyMesh>::Save(out1,"out1.ply");
137 
138  MyMesh inlierMesh1;
139  Ran.DumpInlier(inlierMesh1,cVec[1],pp);
140  tri::io::ExporterPLY<MyMesh>::Save(inlierMesh1,"inlier1.ply");
141 
142  return 0;
143 }
144