File: ransac_matching.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (634 lines) | stat: -rw-r--r-- 22,740 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef RANSAC_MATCHING_H
#define RANSAC_MATCHING_H
#include<vcg/complex/algorithms/point_sampling.h>
#include<vcg/complex/algorithms/update/color.h>
#include<vcg/complex/algorithms/smooth.h>
#include<vcg/space/index/kdtree/kdtree.h>
#include<vcg/space/point_matching.h>
namespace vcg
{
/** BaseFeature a no-feature feature 
 * 
 * Basically it serve the purpose of evaluating the ransac framework factoring out the goodness of the feature. 
 * 
 */
template <class MeshType> 
class BaseFeature
{
public:
  BaseFeature():_v(0) {}
  typename MeshType::VertexType *_v;  
  typename MeshType::CoordType P() {return _v->cP();}    
};


template <class MeshType>
class BaseFeatureSet 
{
public: 
  typedef   BaseFeature<MeshType> FeatureType;  
  typedef typename MeshType::VertexType      VertexType;
  typedef typename MeshType::ScalarType      ScalarType;  
  
  class Param 
  {
  public:
    Param()
    {
      featureSampleRatio = 0.5; // the number of feature that we choose on the total number of samples.
    }

    ScalarType featureSampleRatio;
  };

 
  std::vector<FeatureType> fixFeatureVec;
  std::vector<FeatureType> movFeatureVec;
  
  FeatureType &ff(int i) { return fixFeatureVec[i]; }
  FeatureType &mf(int i) { return movFeatureVec[i]; }
  int ffNum() const { return fixFeatureVec.size(); }
  int mfNum() const { return movFeatureVec.size(); }
  
  void Init(MeshType &fix, MeshType &mov,  
            std::vector<VertexType *> &fixSampleVec, std::vector<VertexType *> &movSampleVec,
            Param &fpp)
  {
    this->fixFeatureVec.resize(fixSampleVec.size()*fpp.featureSampleRatio);
    for(int i=0;i<fixFeatureVec.size();++i) 
      this->fixFeatureVec[i]._v = fixSampleVec[i];
      
    this->movFeatureVec.resize(movSampleVec.size()*fpp.featureSampleRatio);
    for(int i=0;i<movFeatureVec.size();++i) 
      this->movFeatureVec[i]._v = movSampleVec[i];
    
    printf("Generated %i Features on Fix\n",this->fixFeatureVec.size());
    printf("Generated %i Features on Mov\n",this->movFeatureVec.size());
  }
 
 // Returns the indexes of all the fix features matching a given one (from mov usually) 
 // remember that the idea is that 
 // we are aliging mov (that could be a single map) to fix (that could be a set of already aligned maps)
 void getMatchingFixFeatureVec(FeatureType &q, vector<int> &ffiVec, size_t maxMatchingFeature)
 {
  ffiVec.resize(std::min(fixFeatureVec.size(),maxMatchingFeature));
  
  for(int i=0;i<ffiVec.size();++i)
    ffiVec[i]=i;
 } 
};

/*******************/ 

template <class MeshType> 
class NDFeature 
{
public:
  NDFeature():_v(0) {}
  typename MeshType::VertexType *_v;  
  typename MeshType::CoordType nd; //   
  typename MeshType::CoordType P() {return _v->cP();}    
};


template <class MeshType>
class NDFeatureSet 
{
public: 
  typedef   NDFeature<MeshType> FeatureType;  
  typedef typename MeshType::VertexType      VertexType;
  typedef typename MeshType::CoordType      CoordType;
  typedef typename MeshType::ScalarType      ScalarType;
 
  class Param
  {
  public:
    Param()
    {
      levAbs=CoordType(0,0,0);
      levPerc[0] = 0.01;
      levPerc[1] = levPerc[0]*2.0;
      levPerc[2] = levPerc[1]*2.0;      
    }

    CoordType levPerc; 
    CoordType levAbs; 
  };
  
  std::vector<FeatureType> fixFeatureVec;
  std::vector<FeatureType> movFeatureVec;
  KdTree<ScalarType> *fixFeatureTree;
  
  FeatureType &ff(int i) { return fixFeatureVec[i]; }
  FeatureType &mf(int i) { return movFeatureVec[i]; }
  int ffNum() const { return fixFeatureVec.size(); }
  int mfNum() const { return movFeatureVec.size(); }
  
  void Init(MeshType &fix, MeshType &mov,  
            std::vector<VertexType *> &fixSampleVec, std::vector<VertexType *> &movSampleVec, Param &pp)
  {
    ScalarType dd = std::max(fix.bbox.Diag(),mov.bbox.Diag());
    if(pp.levAbs == CoordType(0,0,0))
      pp.levAbs= pp.levPerc * dd;
        
    BuildNDFeatureVector(fix,fixSampleVec,pp.levAbs,fixFeatureVec);
    BuildNDFeatureVector(mov,movSampleVec,pp.levAbs,movFeatureVec);
    
    ConstDataWrapper<CoordType> cdw( &(fixFeatureVec[0].nd), fixFeatureVec.size(), sizeof(FeatureType));        
    fixFeatureTree = new  KdTree<ScalarType>(cdw); 
            
    printf("Generated %i ND Features on Fix\n",this->fixFeatureVec.size());
    printf("Generated %i ND Features on Mov\n",this->movFeatureVec.size());
  }
 
  
  static void BuildNDFeatureVector(MeshType &m, std::vector<VertexType *> &sampleVec, Point3f &distLev, std::vector<FeatureType> &featureVec )
  {    
    tri::UpdateNormal<MeshType>::PerVertexNormalized(m);
    tri::Smooth<MeshType>::VertexNormalLaplacian(m,10);
    
    VertexConstDataWrapper<MeshType > ww(m);
    KdTree<ScalarType> tree(ww); 
    featureVec.resize(sampleVec.size());
    const Point3f sqDistLev(distLev[0]*distLev[0], distLev[1]*distLev[1], distLev[2]*distLev[2]);
    for(int i=0;i<sampleVec.size();++i)
    {
      featureVec[i]._v=sampleVec[i];
      std::vector<unsigned int> ptIndVec;
      std::vector<ScalarType> sqDistVec;    
      tree.doQueryDist(sampleVec[i]->P(), distLev[2], ptIndVec, sqDistVec);
      Point3f varSum(0,0,0);
      Point3i varCnt(0,0,0);
      
      for(int j=0;j<sqDistVec.size();++j)
      {
        ScalarType nDist = Distance(m.vert[i].N(),m.vert[ptIndVec[j]].N()); 
        if(sqDistVec[j]<sqDistLev[0]) {
          varSum[0] += nDist;
          ++varCnt[0];
        }
        if(sqDistVec[j]<sqDistLev[1]) {
          varSum[1] += nDist; 
          ++varCnt[1];
        } 
        {
        varSum[2] += nDist; 
        ++varCnt[2];                
        }
      }      
      featureVec[i].nd[0] = varSum[0]/ScalarType(varCnt[0]);
      featureVec[i].nd[1] = varSum[1]/ScalarType(varCnt[1]);
      featureVec[i].nd[2] = varSum[2]/ScalarType(varCnt[2]);           
    }  
  }
  
  
 // Returns the indexes of all the fix features matching a given one (from mov usually) 
void getMatchingFixFeatureVec(FeatureType &q, vector<int> &ffiVec, int maxNum)
{
  ffiVec.clear();
  typename KdTree<ScalarType>::PriorityQueue pq;
  this->fixFeatureTree->doQueryK(q.nd,maxNum,pq);
  for(int i=0;i<pq.getNofElements();++i)
  {
    ffiVec.push_back(pq.getIndex(i));
  }
} 
};


/** Ransac Framework
 *
 * A ransac framework for mesh-mesh rough alignment. 
 * Templated on the featureSet
 * 
 * A feature set must expose 
 * - A method for intializing features on a mesh
 * - A method to return up to <k> features matching a given feature
 * 
 * The framework, given two meshes (fix and mov), will search for a triplet of 
 * matching features that brings mov onto fix. 
 * 
 * Validity of a transformation is checked by mean of two poisson disk sampling of the input meshes. 
 */


template <class MeshType, class FeatureSetType>
class RansacFramework
{
  typedef typename FeatureSetType::FeatureType       FeatureType;
  typedef typename FeatureSetType::Param       FeatureParam;
  
  typedef typename MeshType::CoordType       CoordType;
  typedef typename MeshType::BoxType         BoxType;
  typedef typename MeshType::ScalarType      ScalarType;
  typedef typename MeshType::VertexType      VertexType;
  typedef typename MeshType::VertexPointer   VertexPointer;
  typedef typename MeshType::VertexIterator  VertexIterator;
  typedef typename MeshType::EdgeType        EdgeType;
  typedef typename MeshType::EdgeIterator    EdgeIterator;
  typedef typename MeshType::FaceType        FaceType;
  typedef typename MeshType::FacePointer     FacePointer;
  typedef typename MeshType::FaceIterator    FaceIterator;
  typedef typename MeshType::FaceContainer   FaceContainer;
  typedef Matrix44<ScalarType>               Matrix44Type;
  
public:
  class Param
  {
  public:
    Param()
    {
      iterMax=100;
      samplingRadiusPerc=0.005;
      samplingRadiusAbs=0;
      evalSize=1000;
      inlierRatioThr=0.3;
      inlierDistanceThrPerc = 1.5; // the distance between a transformed mov sample and the corresponding on fix should be 1.5 * sampling dist.
      congruenceThrPerc = 2.0; // the distance between two matching features must be  within 2.0 * sampling distance 
      minFeatureDistancePerc = 4.0; // the distance between two chosen features must be  at least 4.0 * sampling distance 
      maxMatchingFeatureNum = 100;
      areaThrPerc = 20.0;    // Triplets that make small triangles are discarded 
      
    }
   
    ScalarType inlierRatioThr;
    ScalarType inlierDistanceThrPerc;
    ScalarType congruenceThrPerc;
    ScalarType minFeatureDistancePerc;
    ScalarType samplingRadiusPerc;
    ScalarType samplingRadiusAbs;
    ScalarType areaThrPerc;
    int iterMax;
    int evalSize;
    int maxMatchingFeatureNum;
    
    ScalarType inlierSquareThr() const { return pow(samplingRadiusAbs* inlierDistanceThrPerc,2); }
  };
  
  class Candidate
  {
  public:
    int fixInd[3];
    int movInd[3];  
    int inlierNum;
    int evalSize;
    Matrix44Type Tr;
    ScalarType err() const {return float(inlierNum)/float(evalSize);}
    bool operator <(const Candidate &cc) const
    {
      return this->err() > cc.err();
    }
    
  };

  FeatureSetType FS;
  std::vector<Point3f> fixConsensusVec, movConsensusVec;
  KdTree<ScalarType> *consensusTree;
  
  
  // Given three pairs of sufficiently different distances (e.g. the edges of a scalene triangle)
  // it finds the permutation that brings the vertexes so that the distances match.
  // The meaning of the permutation vector nm0,nm1,nm2 is that the (N)ew index of (M)ov vertx i is the value of nmi 
  
  bool FindPermutation(int d01, int d02, int d12, int m01, int m02, int m12, int nm[], Param &pp)
  {
    ScalarType eps = pp.samplingRadiusAbs*2.0;
        
    if(fabs(d01-m01)<eps) {
      if(fabs(d02-m02)<eps) {
        if(fabs(d12-m12)<eps){ nm[0]=0;nm[1]=1;nm[2]=2; return true; }
            else return false;
      }
      if(fabs(d02-m12)<eps) {
        if(fabs(d12-m02)<eps){ nm[0]=1;nm[1]=0;nm[2]=2; return true; }
            else return false;        
      }        
    }
    
    if(fabs(d01-m02)<eps) {
      if(fabs(d02-m01)<eps) {
        if(fabs(d12-m12)<eps){ nm[0]=0;nm[1]=2;nm[2]=1; return true; }
            else return false;
      }
      if(fabs(d02-m12)<eps) {
        if(fabs(d12-m01)<eps){ nm[0]=2;nm[1]=0;nm[2]=1; return true; }
            else return false;        
      }        
    }

    if(fabs(d01-m12)<eps) {
      if(fabs(d02-m01)<eps) {
        if(fabs(d12-m02)<eps){ nm[0]=1;nm[1]=2;nm[2]=0; return true; }
            else return false;
      }
      if(fabs(d02-m02)<eps) {
        if(fabs(d12-m01)<eps){ nm[0]=2;nm[1]=1;nm[2]=0; return true; }
            else return false;        
      }        
    }
    return false;
  }
    
  
  
  // Scan the feature set of 
  void EvaluateFeature(int testSize, const char *filename, Param &pp)
  {
//    VertexConstDataWrapper<MeshType> ww(fixM);
//    KdTree<ScalarType>(ww) mTree;
    MeshType tmpM;  
    int neededSizeSum=0;
    int foundCnt=0;
    printf("Testing Feature size %i\n",testSize);
    for(int i=0;i<FS.mfNum();++i)
    {
      int neededSize = testSize;
      for(int j=1;j<neededSize;++j)
      {
        std::vector<int> closeFeatureVec; 
        FS.getMatchingFixFeatureVec(FS.mf(i), closeFeatureVec, j);        
        for(int k=0; k<closeFeatureVec.size();++k)
        {
          if(Distance(FS.mf(i).P(),FS.ff(closeFeatureVec[k]).P() )<pp.samplingRadiusAbs *3.0 )  
            neededSize = j;
        }
      }      
      tri::Allocator<MeshType>::AddVertex(tmpM, FS.mf(i).P());      
      tmpM.vert.back().Q() = neededSize;      
      neededSizeSum+=neededSize;
      if(neededSize<testSize) foundCnt++;
    }
    
    tri::UpdateColor<MeshType>::PerVertexQualityRamp(tmpM);
    tri::io::ExporterPLY<MeshType>::Save(tmpM,filename, tri::io::Mask::IOM_VERTCOLOR + tri::io::Mask::IOM_VERTQUALITY);    
    printf("Found %i / %i Average Needed Size %5.2f on %i\n",foundCnt,FS.mfNum(), float(neededSizeSum)/FS.mfNum(),testSize);
    
  }

  // The main loop. 
  // Choose three points on mov that make a scalene triangle 
  // and search on fix three other points with matchng distances 
  
  void Process_SearchEvaluateTriple (vector<Candidate> &cVec, Param &pp)
  {
    math::MarsenneTwisterRNG rnd;
//    ScalarType congruenceEps = pow(pp.samplingRadiusAbs * pp.congruenceThrPerc,2.0f);
    ScalarType congruenceEps = pp.samplingRadiusAbs * pp.congruenceThrPerc;
    ScalarType minFeatureDistEps = pp.samplingRadiusAbs * pp.minFeatureDistancePerc;
    ScalarType minAreaThr = pp.samplingRadiusAbs * pp.samplingRadiusAbs *pp.areaThrPerc;
    printf("Starting search congruenceEps = samplingRadiusAbs * 3.0 = %6.2f \n",congruenceEps);
    int iterCnt=0;
    
    while ( (iterCnt < pp.iterMax) && (cVec.size()<100) )
    {
      Candidate c;
      // Choose a random pair of features from mov 
      c.movInd[0] = rnd.generate(FS.mfNum());
      c.movInd[1] = rnd.generate(FS.mfNum());
      ScalarType d01 = Distance(FS.mf(c.movInd[0]).P(),FS.mf(c.movInd[1]).P());
      if( d01 > minFeatureDistEps )
      {
        c.movInd[2] = rnd.generate(FS.mfNum());
        ScalarType d02=Distance(FS.mf(c.movInd[0]).P(),FS.mf(c.movInd[2]).P());
        ScalarType d12=Distance(FS.mf(c.movInd[1]).P(),FS.mf(c.movInd[2]).P());
        ScalarType areaTri = DoubleArea(Triangle3<ScalarType>(FS.mf(c.movInd[0]).P(), FS.mf(c.movInd[1]).P(), FS.mf(c.movInd[2]).P() ));
        if( ( d02 > minFeatureDistEps ) &&  // Sample are sufficiently distant
            ( d12 > minFeatureDistEps ) && 
            ( areaTri > minAreaThr) && 
            ( fabs(d01-d02) > congruenceEps ) && // and they make a scalene triangle
            ( fabs(d01-d12) > congruenceEps ) && 
            ( fabs(d12-d02) > congruenceEps ) )
        {
          // Find a congruent triple on mov 
          printf("Starting search of a [%i] congruent triple for %4i %4i %4i - %6.2f %6.2f %6.2f\n",
                 iterCnt,c.movInd[0],c.movInd[1],c.movInd[2],d01,d02,d12);
          // As a first Step we ask for three vectors of matching features;
          
          std::vector<int> fixFeatureVec0; 
          FS.getMatchingFixFeatureVec(FS.mf(c.movInd[0]), fixFeatureVec0,pp.maxMatchingFeatureNum);
          std::vector<int> fixFeatureVec1; 
          FS.getMatchingFixFeatureVec(FS.mf(c.movInd[1]), fixFeatureVec1,pp.maxMatchingFeatureNum);
          std::vector<int> fixFeatureVec2; 
          FS.getMatchingFixFeatureVec(FS.mf(c.movInd[2]), fixFeatureVec2,pp.maxMatchingFeatureNum);
          
          int congrNum=0;
          int congrGoodNum=0;
          for(int i=0;i<fixFeatureVec0.size();++i)
          { 
            if(cVec.size()>100) break;
            c.fixInd[0]=fixFeatureVec0[i];
            for(int j=0;j<fixFeatureVec1.size();++j)
            {               
              if(cVec.size()>100) break;             
              c.fixInd[1]=fixFeatureVec1[j];              
              ScalarType m01 = Distance(FS.ff(c.fixInd[0]).P(),FS.ff(c.fixInd[1]).P());
              if( (fabs(m01-d01)<congruenceEps) )
              {
//                printf("- Found a congruent pair %i %i %6.2f\n", c.movInd[0],c.movInd[1], m01);                
                ++congrNum;
                for(int k=0;k<fixFeatureVec2.size();++k)
                { 
                  if(cVec.size()>100) break;                  
                  c.fixInd[2]=fixFeatureVec2[k];
                  ScalarType m02=Distance(FS.ff(c.fixInd[0]).P(),FS.ff(c.fixInd[2]).P());
                  ScalarType m12=Distance(FS.ff(c.fixInd[1]).P(),FS.ff(c.fixInd[2]).P());
                  if( (fabs(m02-d02)<congruenceEps)  && (fabs(m12-d12)<congruenceEps ) )
                  {
                    c.Tr = GenerateMatchingMatrix(c,pp);
                    
                    EvaluateMatrix(c,pp);
                    if(c.err() > pp.inlierRatioThr ){
                      printf("- - Found  %lu th good congruent triple %i %i %i -- %f / %i \n", cVec.size(), c.movInd[0],c.movInd[1],c.movInd[2],c.err(),pp.evalSize);
//                      printf("      - %4.3f %4.3f %4.3f - %4.3f %4.3f %4.3f \n",
//                             FS.ff(c.fixInd[0]).nd[0], FS.ff(c.fixInd[0]).nd[1], FS.ff(c.fixInd[0]).nd[2],
//                             FS.mf(c.movInd[0]).nd[0], FS.mf(c.movInd[0]).nd[1],FS.mf(c.movInd[0]).nd[2]);
                      
                      ++congrGoodNum;                      
                      cVec.push_back(c);
                    }
                  }
                }
              }                
            }
          }
          printf("Completed Search of congruent triple (found %i / %i good/congruent)\n",congrGoodNum,congrNum);
        }               
      }
      ++iterCnt;
    } // end While

    printf("Found %lu candidates \n",cVec.size());
    sort(cVec.begin(),cVec.end());
    printf("best candidate %f \n",cVec[0].err());
    
    pp.evalSize = FS.mfNum();
    
    for(int i=0;i<cVec.size();++i)
      EvaluateMatrix(cVec[i],pp);
    
    sort(cVec.begin(),cVec.end());
    
    printf("After re-evaluation best is %f",cVec[0].err());
        
      
    
  } // end Process
  
  
  /**
   * @brief EvaluateMatrix
   * @param c
   * @param pp
   * 
   * Evaluate the matrix resulting from a candidate.
   * Done using the poisson sampling using only evalSize samples
   * 
   *  
   */
  void EvaluateMatrix(Candidate &c, Param &pp)
  {
    c.inlierNum=0;
    c.evalSize=pp.evalSize;    
    
    ScalarType sqThr = pp.inlierSquareThr();
    int mid = pp.evalSize/2;
    uint ind;
    ScalarType squareDist;
    std::vector<Point3f>::iterator pi=movConsensusVec.begin();
    
    for(int j=0;j<2;++j)
    {
      for(int i=0;i<mid;++i)
      {
        Point3f qp = c.Tr*(*pi);
        consensusTree->doQueryClosest(qp,ind,squareDist);
        if(squareDist < sqThr)
          ++c.inlierNum;
        ++pi;
      }
      // Early bailout if after 1/2 of the test we have a very low consensus reject
      if((j==0) && (c.inlierNum < mid/10))  
      {
        c.inlierNum *=2;
        return;
      }        
    }
  }
  
  void DumpInlier(MeshType &m, Candidate &c, Param &pp)
  {
    ScalarType sqThr = pp.inlierSquareThr();
    for(int i=0;i<pp.evalSize;++i)
    {
      Point3f qp = c.Tr*movConsensusVec[i];
      uint ind;
      ScalarType squareDist;
      consensusTree->doQueryClosest(qp,ind,squareDist);
      if(squareDist < sqThr)
        tri::Allocator<MeshType>::AddVertex(m,qp);
    }
  }
  

// Find the transformation that matches the mov onto the fix
// eg M * piMov = piFix 

Matrix44f GenerateMatchingMatrix(Candidate &c, Param pp)
{
  std::vector<Point3f> pFix(3);
  pFix[0]= FS.ff(c.fixInd[0]).P();
  pFix[1]= FS.ff(c.fixInd[1]).P();
  pFix[2]= FS.ff(c.fixInd[2]).P();
  
  std::vector<Point3f> pMov(3);
  pMov[0]= FS.mf(c.movInd[0]).P();
  pMov[1]= FS.mf(c.movInd[1]).P();
  pMov[2]= FS.mf(c.movInd[2]).P();

  Point3f upFix = vcg::Normal(pFix[0],pFix[1],pFix[2]);
  Point3f upMov = vcg::Normal(pMov[0],pMov[1],pMov[2]);  
  
  upFix.Normalize(); 
  upMov.Normalize();
  
  upFix *= Distance(pFix[0],pFix[1]);
  upMov *= Distance(pMov[0],pMov[1]);
  
  for(int i=0;i<3;++i) pFix.push_back(pFix[i]+upFix);
  for(int i=0;i<3;++i) pMov.push_back(pMov[i]+upMov);
  
  Matrix44f res;
  ComputeRigidMatchMatrix(pFix,pMov,res);
  return res;  
}


void Init(MeshType &fixM, MeshType &movM, Param &pp, FeatureParam &fpp)
{
  tri::UpdateNormal<MeshType>::PerVertexNormalizedPerFaceNormalized(fixM);
  tri::UpdateNormal<MeshType>::PerVertexNormalizedPerFaceNormalized(movM);
  
  // First a bit of Sampling
  typedef tri::TrivialPointerSampler<MeshType> BaseSampler;
  typename tri::SurfaceSampling<MeshType, BaseSampler>::PoissonDiskParam pdp;
  pdp.randomSeed = 0;
  pdp.bestSampleChoiceFlag = true;
  pdp.bestSamplePoolSize = 20;
  int t0=clock();
  pp.samplingRadiusAbs = pp.samplingRadiusPerc *fixM.bbox.Diag();
  BaseSampler pdSampler;
  std::vector<VertexType *> fixSampleVec;
  tri::SurfaceSampling<MeshType,BaseSampler>::PoissonDiskPruning(pdSampler, fixM, pp.samplingRadiusAbs,pdp);
  std::swap(pdSampler.sampleVec,fixSampleVec);
  std::vector<VertexType *> movSampleVec;  
  tri::SurfaceSampling<MeshType,BaseSampler>::PoissonDiskPruning(pdSampler, movM, pp.samplingRadiusAbs,pdp);
  std::swap(pdSampler.sampleVec,movSampleVec);
  int t1=clock();
  printf("Poisson Sampling of surfaces %5.2f ( %iv and %iv) \n",float(t1-t0)/CLOCKS_PER_SEC,fixSampleVec.size(),movSampleVec.size());
  printf("Sampling Radius %f \n",pp.samplingRadiusAbs);
  
  for(int i=0;i<fixSampleVec.size();++i) 
    this->fixConsensusVec.push_back(fixSampleVec[i]->P());    

  for(int i=0;i<movSampleVec.size();++i) 
    this->movConsensusVec.push_back(movSampleVec[i]->P());
  
  FS.Init(fixM, movM, fixSampleVec, movSampleVec, fpp);
    
  std::random_shuffle(movConsensusVec.begin(),movConsensusVec.end());
  
  VectorConstDataWrapper<std::vector<CoordType> > ww(fixConsensusVec);
  consensusTree = new  KdTree<ScalarType>(ww); 
}


};

} //end namespace vcg


#endif // RANSAC_MATCHING_H