Esempio n. 1
0
/**
 * Example of a test. To be completed.
 *
 */
bool testFitting()
{
  unsigned int nbok = 0;
  unsigned int nb = 0;
  trace.beginBlock ( "Testing init ..." );

  using namespace Z3i;

  trace.beginBlock("Creating Surface");
  Point p1( -20, -20, -20 );
  Point p2( 20, 20, 20 );
   
  ImplicitBall<Z3i::Space> shape( RealPoint(6.0,0,0), 4);
  typedef GaussDigitizer<Z3i::Space, ImplicitBall<Z3i::Space> > Gauss;
  Gauss gauss;
  gauss.attach(shape);
  gauss.init(p1, p2, 1);
  
  typedef LightImplicitDigitalSurface<KSpace,  Gauss > SurfaceContainer;
  typedef DigitalSurface<SurfaceContainer> Surface;
  typedef Surface::Surfel Surfel;


  KSpace K;
  nbok += K.init( p1, p2, true ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") "
               << "K.init() is ok" << std::endl;
  Surfel bel = Surfaces<KSpace>::findABel( K, gauss, 10000 );
  SurfaceContainer* surfaceContainer = new SurfaceContainer
    ( K, gauss, SurfelAdjacency<KSpace::dimension>( true ), bel );
  Surface surface( surfaceContainer ); // acquired
  CanonicSCellEmbedder<KSpace> embedder(surface.container().space());
  trace.endBlock();

  trace.beginBlock("Normal vector field computation");
  typedef functors::ElementaryConvolutionNormalVectorEstimator<Surfel, CanonicSCellEmbedder<KSpace> > FunctorNormal;
  typedef LocalEstimatorFromSurfelFunctorAdapter<SurfaceContainer, Z3i::L2Metric,
                                                 FunctorNormal,
                                                 DGtal::functors::GaussianKernel> ReporterNormal;
  typedef EstimatorCache<ReporterNormal> NormalCache;

  //estimator
  DGtal::functors::GaussianKernel gaussKernelFunc(5.0);
  FunctorNormal functorNormal(embedder, 1.0);
  ReporterNormal reporterNormal;
  reporterNormal.attach(surface);
  reporterNormal.setParams(l2Metric, functorNormal, gaussKernelFunc, 5.0);

  //caching normal field
  NormalCache normalCache(reporterNormal);
  normalCache.init( 1, surface.begin(), surface.end());
  trace.info() << "Normal vector field cached... "<< normalCache << std::endl;
  trace.endBlock();

  trace.beginBlock("Creating  sphere fitting adapter from normal vector field");
  typedef functors::SphereFittingEstimator<Surfel, CanonicSCellEmbedder<KSpace> , NormalCache> Functor;
  typedef functors::ConstValue< double > ConvFunctor;
  typedef LocalEstimatorFromSurfelFunctorAdapter<SurfaceContainer, Z3i::L2Metric, Functor, ConvFunctor> Reporter;

  Functor fitter(embedder,1.0, 5.0, normalCache);
  ConvFunctor convFunc(1.0);
  Reporter reporter;
  reporter.attach(surface);
  reporter.setParams(l2Metric, fitter , convFunc, 15.0);
  
  reporter.init(1, surface.begin(), surface.end());
  for(Surface::ConstIterator it = surface.begin(), ite=surface.end(); it!=ite; ++it)
    {
      Functor::Quantity val = reporter.eval( it );
      trace.info() << "Fitting = "<<val.center <<" rad="<<val.radius<<std::endl;
    }
  trace.endBlock();


  trace.endBlock();

  nbok += true ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") "
	       << "true == true" << std::endl;

  return nbok == nb;
}
/**
 * Example of a test. To be completed.
 *
 */
bool testLocalEstimatorFromFunctorAdapter()
{
  unsigned int nbok = 0;
  unsigned int nb = 0;
  trace.beginBlock ( "Testing init ..." );

  using namespace Z3i;
  typedef ImplicitDigitalEllipse3<Point> ImplicitDigitalEllipse;
  typedef LightImplicitDigitalSurface<KSpace,ImplicitDigitalEllipse> SurfaceContainer;
  typedef DigitalSurface< SurfaceContainer > Surface;
  typedef SurfaceContainer::SurfelConstIterator ConstIterator;
  typedef SurfaceContainer::Surfel Surfel;


  trace.beginBlock("Creating Surface");
  Point p1( -10, -10, -10 );
  Point p2( 10, 10, 10 );
  KSpace K;
  nbok += K.init( p1, p2, true ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") "
               << "K.init() is ok" << std::endl;
  ImplicitDigitalEllipse ellipse( 6.0, 4.5, 3.4 );
  Surfel bel = Surfaces<KSpace>::findABel( K, ellipse, 10000 );
  SurfaceContainer* surfaceContainer = new SurfaceContainer
    ( K, ellipse, SurfelAdjacency<KSpace::dimension>( true ), bel );
  Surface surface( surfaceContainer ); // acquired
  unsigned int nbsurfels = 0;
  for ( ConstIterator it = surface.begin(), it_end = surface.end();
       it != it_end; ++it )
  {
    ++nbsurfels;
  }
  trace.info() << nbsurfels << " surfels found." << std::endl;
  trace.endBlock();

  trace.beginBlock("Creating  adapter");
  typedef DGtal::functors::DummyEstimatorFromSurfels<Surfel, CanonicSCellEmbedder<KSpace> > Functor;
  typedef DGtal::functors::ConstValue< double > ConvFunctor;
  typedef LocalEstimatorFromSurfelFunctorAdapter<SurfaceContainer, LpMetric<Z3i::Space>,
                                                 Functor, ConvFunctor> Reporter;

  typedef LocalEstimatorFromSurfelFunctorAdapter<SurfaceContainer, LpMetric<Z3i::Space>,
                                                 Functor, DGtal::functors::GaussianKernel> ReporterGaussian;

  LpMetric<Z3i::Space> l2(2.0);
  CanonicSCellEmbedder<KSpace> embedder(surface.container().space());
  Functor estimator(embedder, 1);

  ConvFunctor convFunc(1.0);
  Reporter reporter;
  reporter.attach(surface);
  reporter.setParams(l2,estimator,convFunc, 5);

  //We just test the init for Gaussian
  DGtal::functors::GaussianKernel gaussKernelFunc(1.0);
  ReporterGaussian reporterGaussian;
  reporterGaussian.attach(surface);
  reporterGaussian.setParams(l2,estimator,gaussKernelFunc, 5.0);
  reporterGaussian.init(1, surface.begin(), surface.end());

  reporter.init(1.0, surface.begin(), surface.end());
  Functor::Quantity val = reporter.eval( surface.begin() );
  trace.info() <<  "Value with radius 5= "<<val << std::endl;
  nbok += ((fabs((double)val - 124.0)) < 40) ? 1 : 0;
  nb++;

  reporter.setParams(l2,estimator,convFunc, 20.0);
  reporter.init(1, surface.begin(), surface.end());
  Functor::Quantity val2 = reporter.eval( surface.begin() );
  trace.info() <<  "Value with radius 20= "<<val2 << std::endl;

  nbok += ((fabs((double)val2 - 398.0)) < 120) ? 1 : 0;
  nb++;

  trace.endBlock();
  trace.endBlock();

  trace.info() << "(" << nbok << "/" << nb << ") "
	       << "true == true" << std::endl;

  return nbok == nb;
}