Example #1
0
bool
compareShapeEstimators( const string & name,
      Shape & aShape, 
      double h )
{
  // Types
  typedef typename Space::Point Point;
  typedef typename Space::Vector Vector;
  typedef typename Space::RealPoint RealPoint;
  typedef typename Space::Integer Integer;
  typedef HyperRectDomain<Space> Domain;
  typedef KhalimskySpaceND<Space::dimension,Integer> KSpace;
  typedef typename KSpace::SCell SCell;
  typedef typename GridCurve<KSpace>::PointsRange PointsRange;
  typedef typename GridCurve<KSpace>::ArrowsRange ArrowsRange;
  typedef typename PointsRange::ConstIterator ConstIteratorOnPoints;


  // Digitizer
  GaussDigitizer<Space,Shape> dig;  
  dig.attach( aShape ); // attaches the shape.
  Vector vlow(-1,-1); Vector vup(1,1);
  dig.init( aShape.getLowerBound()+vlow, aShape.getUpperBound()+vup, h ); 
  Domain domain = dig.getDomain();

  // Create cellular space
  KSpace K;
  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
  if ( ! ok )
    {
      std::cerr << "[compareShapeEstimators]"
    << " error in creating KSpace." << std::endl;
      return false;
    }
  try {
    // Extracts shape boundary
    SurfelAdjacency<KSpace::dimension> SAdj( true );
    SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
    // Getting the consecutive surfels of the 2D boundary
    std::vector<Point> points;
    Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
    // Create GridCurve
    GridCurve<KSpace> gridcurve;
    gridcurve.initFromVector( points );
    // Ranges
    PointsRange r = gridcurve.getPointsRange(); 
    std::cout << "# range size = " << r.size() << std::endl;  

    // Estimations
    // True values
    std::cout << "# True values computation" << std::endl;  
    typedef ParametricShapeTangentFunctor< Shape > TangentFunctor;
    typedef ParametricShapeCurvatureFunctor< Shape > CurvatureFunctor;
  
    TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, TangentFunctor >  
      trueTangentEstimator;
    TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, CurvatureFunctor >  
      trueCurvatureEstimator;
  
    trueTangentEstimator.init( h, r.begin(), r.end(), &aShape, gridcurve.isClosed());
    std::vector<RealPoint> trueTangents = 
      estimateQuantity( trueTangentEstimator, r.begin(), r.end() );
    trueCurvatureEstimator.init( h, r.begin(), r.end(), &aShape, gridcurve.isClosed());
    std::vector<double> trueCurvatures = 
      estimateQuantity( trueCurvatureEstimator, r.begin(), r.end() );
  
    // Maximal Segments
    std::cout << "# Maximal DSS tangent estimation" << std::endl;  
    typedef ArithmeticalDSS<ConstIteratorOnPoints,Integer,4> SegmentComputer;
    typedef TangentFromDSSFunctor<SegmentComputer> SCFunctor;
    SegmentComputer sc;
    SCFunctor f; 
    MostCenteredMaximalSegmentEstimator<SegmentComputer,SCFunctor> MSTangentEstimator(sc, f); 
   
    Clock c;
    
    c.startClock();
    MSTangentEstimator.init( h, r.begin(), r.end(), gridcurve.isClosed() );
    std::vector<typename SCFunctor::Value> MSTangents = 
      estimateQuantity( MSTangentEstimator, r.begin(), r.end() );
    double TMST = c.stopClock();


    // Binomial
    std::cout << "# Tangent and curvature estimation from binomial convolution" << std::endl;
    typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
    std::cout << "# mask size = " << 
      MyBinomialConvolver::suggestedSize( h, r.begin(), r.end() ) << std::endl;
    typedef TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
      TangentBCFct;
    typedef CurvatureFromBinomialConvolverFunctor< MyBinomialConvolver, double >
      CurvatureBCFct;
    BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct> BCTangentEstimator;
    BinomialConvolverEstimator< MyBinomialConvolver, CurvatureBCFct> BCCurvatureEstimator;
    
    c.startClock();
    BCTangentEstimator.init( h, r.begin(), r.end(), gridcurve.isClosed() );
    std::vector<RealPoint> BCTangents = 
      estimateQuantity( BCTangentEstimator, r.begin(), r.end() );
    double TBCTan = c.stopClock();

    c.startClock();
    BCCurvatureEstimator.init( h, r.begin(), r.end(), gridcurve.isClosed() );
    std::vector<double> BCCurvatures =
      estimateQuantity( BCCurvatureEstimator, r.begin(), r.end() );
    double TBCCurv = c.stopClock();

    // Output
    std::cout << "# Shape = "<< name <<std::endl
        << "# Time-BCtangent = "<<TBCTan <<std::endl
        << "# Time-BCcurvature = "<<TBCCurv<<std::endl
        << "# Time-MStangent = "<<TMST<<std::endl
        << "# id x y tangentx tangenty curvature"
        << " BCtangentx BCtangenty BCcurvature"
        << " MStangentx MStangenty"
        << std::endl;  
    unsigned int i = 0;
    for ( ConstIteratorOnPoints it = r.begin(), it_end = r.end();
    it != it_end; ++it, ++i )
      {
  Point p = *it;
  std::cout << i << setprecision( 15 )
      << " " << p[ 0 ] << " " << p[ 1 ] 
      << " " << trueTangents[ i ][ 0 ]
      << " " << trueTangents[ i ][ 1 ]
      << " " << trueCurvatures[ i ]
      << " " << BCTangents[ i ][ 0 ]
      << " " << BCTangents[ i ][ 1 ]
      << " " << BCCurvatures[ i ]
      << " " << MSTangents[ i ][ 0 ]
      << " " << MSTangents[ i ][ 1 ]
      << std::endl;
      }
    return true;
  }    
  catch ( InputException e )
    {
      std::cerr << "[compareShapeEstimators]"
    << " error in finding a bel." << std::endl;
      return false;
    }
}
bool
lengthEstimators( const std::string & /*name*/,
      Shape & aShape, 
      double h )
{
  // Types
  typedef typename Space::Point Point;
  typedef typename Space::Vector Vector;
  typedef typename Space::RealPoint RealPoint;
  typedef typename Space::Integer Integer;
  typedef HyperRectDomain<Space> Domain;
  typedef KhalimskySpaceND<Space::dimension,Integer> KSpace;
  typedef typename KSpace::SCell SCell;
  typedef typename GridCurve<KSpace>::PointsRange PointsRange;
  typedef typename GridCurve<KSpace>::ArrowsRange ArrowsRange;

  // Digitizer
  GaussDigitizer<Space,Shape> dig;  
  dig.attach( aShape ); // attaches the shape.
  Vector vlow(-1,-1); Vector vup(1,1);
  dig.init( aShape.getLowerBound()+vlow, aShape.getUpperBound()+vup, h ); 
  Domain domain = dig.getDomain();

  // Create cellular space
  KSpace K;
  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
  if ( ! ok )
    {
      std::cerr << "[lengthEstimators]"
    << " error in creating KSpace." << std::endl;
      return false;
    }
  try {
    // Extracts shape boundary
    SurfelAdjacency<KSpace::dimension> SAdj( true );
    SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
    // Getting the consecutive surfels of the 2D boundary
    std::vector<Point> points;
    Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
    // Create GridCurve
    GridCurve<KSpace> gridcurve;
    gridcurve.initFromVector( points );
    // Ranges
    ArrowsRange ra = gridcurve.getArrowsRange(); 
    PointsRange rp = gridcurve.getPointsRange(); 


    // Estimations
    typedef typename PointsRange::ConstIterator ConstIteratorOnPoints; 
    typedef ParametricShapeArcLengthFunctor< Shape > Length;
    TrueGlobalEstimatorOnPoints< ConstIteratorOnPoints, Shape, Length  >  trueLengthEstimator;
    trueLengthEstimator.init( h, rp.begin(), rp.end(), &aShape, gridcurve.isClosed());

    L1LengthEstimator< typename ArrowsRange::ConstCirculator > l1length;
    DSSLengthEstimator< typename PointsRange::ConstCirculator > DSSlength;
    MLPLengthEstimator< typename PointsRange::ConstIterator > MLPlength;
    FPLengthEstimator< typename PointsRange::ConstIterator > FPlength;
    BLUELocalLengthEstimator< typename ArrowsRange::ConstIterator > BLUElength;
    RosenProffittLocalLengthEstimator< typename ArrowsRange::ConstIterator > RosenProffittlength;
  
    // Output
    double trueValue = trueLengthEstimator.eval();
    double l1, blue, rosen,dss,mlp,fp;
    double Tl1, Tblue, Trosen,Tdss,Tmlp,Tfp;
    
    Clock c;

    //Length evaluation & timing
    c.startClock();
    l1length.init(h, ra.c(), ra.c());
    l1 = l1length.eval();
    Tl1 = c.stopClock();
    
    c.startClock();
    BLUElength.init(h, ra.begin(), ra.end(), gridcurve.isClosed());
    blue = BLUElength.eval();
    Tblue = c.stopClock();
    
    c.startClock();
    RosenProffittlength.init(h, ra.begin(), ra.end(), gridcurve.isClosed());
    rosen = RosenProffittlength.eval();
    Trosen = c.stopClock();
    
    c.startClock();
    DSSlength.init(h, rp.c(), rp.c());
    dss = DSSlength.eval();
    Tdss = c.stopClock();
    
    c.startClock();
    MLPlength.init(h, rp.begin(), rp.end(), gridcurve.isClosed());
    mlp = MLPlength.eval();
    Tmlp = c.stopClock();

    c.startClock();
    FPlength.init(h, rp.begin(), rp.end(), gridcurve.isClosed());
    fp = FPlength.eval();
    Tfp = c.stopClock();

    std::cout << std::setprecision( 15 ) << h << " " << rp.size() << " " << trueValue 
   << " " << l1
   << " " << blue
   << " " << rosen
   << " " << dss
   << " " << mlp   
   << " " << fp
         << " " << Tl1
   << " " << Tblue
   << " " << Trosen
   << " " << Tdss
   << " " << Tmlp
   << " " << Tfp     
   << std::endl;
    return true;
  }    
  catch ( InputException e )
    {
      std::cerr << "[lengthEstimators]"
    << " error in finding a bel." << std::endl;
      return false;
    }
}