Ejemplo n.º 1
0
void
estimateGeometry(Shape& s, 
                 const double& h,
                 const Range& r, 
                 vector<Point>& points, 
                 vector<Point>& tangents, 
                 vector<Quantity>& curvatures) {

  typedef typename Range::ConstIterator ConstIterator; 
  typedef ParametricShapeTangentFunctor< Shape > TangentFunctor;
  typedef ParametricShapeCurvatureFunctor< Shape > CurvatureFunctor;

  for (ConstIterator i = r.begin(); i != r.end(); ++i) {
    Point p( *i ); 
    p *= h; 
    points.push_back(p); 
  } 

  TrueLocalEstimatorOnPoints< ConstIterator, Shape, TangentFunctor >  
    trueTangentEstimator;
  TrueLocalEstimatorOnPoints< ConstIterator, Shape, CurvatureFunctor >  
    trueCurvatureEstimator;
  trueTangentEstimator.init( h, r.begin(), r.end(), &s, false);
  tangents = 
    estimateQuantity( trueTangentEstimator, r.begin(), r.end() );
  trueCurvatureEstimator.init( h, r.begin(), r.end(), &s, false);
  curvatures = 
    estimateQuantity( trueCurvatureEstimator, r.begin(), r.end() );

}
Ejemplo n.º 2
0
void
estimateGeometry(Shape& s,
                 const double& h,
                 const Range& r,
                 std::vector<Point>& points,
                 std::vector<Point>& tangents,
                 std::vector<Quantity>& curvatures) {
  
  typedef typename Range::ConstIterator ConstIterator;
  for (ConstIterator i = r.begin(); i != r.end(); ++i) {
    Point p( *i );
    p *= h;
    points.push_back(p);
  }
  
  typedef typename Range::ConstCirculator ConstCirculator;
  
  typedef ParametricShapeTangentFunctor< Shape > TangentFunctor;
  TrueLocalEstimatorOnPoints< ConstCirculator, Shape, TangentFunctor >
  trueTangentEstimator;
  trueTangentEstimator.attach(&s);
  trueTangentEstimator.init( h, r.c(), r.c());
  trueTangentEstimator.eval(r.c(), r.c(), std::back_inserter(tangents) );
  
  typedef ParametricShapeCurvatureFunctor< Shape > CurvatureFunctor;
  TrueLocalEstimatorOnPoints< ConstCirculator, Shape, CurvatureFunctor >
  trueCurvatureEstimator;
  trueCurvatureEstimator.attach(&s);
  trueCurvatureEstimator.init( h, r.c(), r.c());
  trueCurvatureEstimator.eval(r.c(), r.c(), std::back_inserter(curvatures) );
  
}
/**
 * Example of a test. To be completed.
 *
 */
bool testTrueLocalEstimator(const std::string &filename)
{
  trace.info() << "Reading GridCurve " << endl;
  ifstream instream; // input stream
  instream.open (filename.c_str(), ifstream::in);
  typedef KhalimskySpaceND<2> Kspace; //space
  GridCurve<Kspace> c; 
  c.initFromVectorStream(instream); //building grid curve
  typedef GridCurve<Kspace >::PointsRange Range;//range
  Range r = c.getPointsRange();//building range

  
  typedef Ball2D<Z2i::Space> Shape;
  typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
  typedef Range::ConstIterator ConstIteratorOnPoints;
  typedef ParametricShapeCurvatureFunctor< Shape > Curvature;
  typedef ParametricShapeTangentFunctor< Shape > Tangent;
  typedef ParametricShapeArcLengthFunctor< Shape > Length;

  Shape ball(Z2i::Point(0,0), 30);

   
  TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, Curvature  >  curvatureEstimator;
  TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, Tangent  >  tangentEstimator;
  TrueGlobalEstimatorOnPoints< ConstIteratorOnPoints, Shape, Length  >  lengthEstimator;

  curvatureEstimator.init( 1, r.begin(), r.end(), &ball, true);
  tangentEstimator.init( 1, r.begin(), r.end(), &ball, true);
 

  ConstIteratorOnPoints it = r.begin();
  //  ConstIteratorOnPoints it2 = r.begin()+15;
  ConstIteratorOnPoints it2 = it;
  for (  int compteur = 0; compteur < 15; ++compteur ) ++it2;
  lengthEstimator.init( 1, it, it2, &ball, true);
  
  
  trace.info() << "Current point = "<<*it<<std::endl;
  trace.info() << "Current point+15 = "<<*it2<<std::endl;
  trace.info() << "Eval curvature (begin, h=1) = "<< curvatureEstimator.eval(it2)<<std::endl;
  trace.info() << "Eval tangent (begin, h=1) = "<< tangentEstimator.eval(it2)<<std::endl;
  trace.info() << "Eval length ( h=1) = "<< lengthEstimator.eval(it,it2)<<std::endl;
  
  return true;

}
Ejemplo n.º 4
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 
testTrueLocalEstimatorOnShapeDigitization( const string & name,
             Shape & aShape, double h )
{
  using namespace Z2i;

  trace.beginBlock ( ( "Testing TrueLocalEstimator on digitization of "
           + name ). c_str() );
  
  // Creates a digitizer on the window (xLow, xUp).
  typedef Space::RealPoint RealPoint;
  RealPoint xLow( -10.0, -10.0 );
  RealPoint xUp( 10.0, 10.0 );
  GaussDigitizer<Space,Shape> dig;  
  dig.attach( aShape ); // attaches the shape.
  dig.init( xLow, xUp, h ); 
  
  // The domain size is given by the digitizer according to the window
  // and the step.
  Domain domain = dig.getDomain();

  // Create cellular space
  KSpace K;
  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
  if ( ! ok )
    {
      std::cerr << "[testTrueLocalEstimatorOnShapeDigitization]"
    << " error in creating KSpace." << std::endl;
    }
  else
    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 );
      typedef GridCurve<KhalimskySpaceND<2> >::PointsRange Range;
      typedef Range::ConstIterator ConstIteratorOnPoints;
      typedef ParametricShapeCurvatureFunctor< Shape > Curvature;
      TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, Curvature  >  curvatureEstimator;
      Range r = gridcurve.getPointsRange();//building range
      curvatureEstimator.init( h, r.begin(), r.end(), &aShape, true);
      std::cout << "# idx x y kappa" << endl;
      unsigned int i = 0;
      for ( ConstIteratorOnPoints it = r.begin(), ite = r.end();
      it != ite; ++it, ++i )
  {
    RealPoint x = *it;
    double kappa = curvatureEstimator.eval( it );
    std::cout << i << " " << x.at( 0 ) << " " << x.at( 1 ) 
        << " " << kappa << std::endl;
  }
    }    
    catch ( InputException e )
      {
  std::cerr << "[testTrueLocalEstimatorOnShapeDigitization]"
      << " error in finding a bel." << std::endl;
  ok = false;
      }
  trace.emphase() << ( ok ? "Passed." : "Error." ) << endl;
  trace.endBlock();
  return ok;
}