Ejemplo n.º 1
0
/**
 * Simple test of Board2D. Illustrates the border extraction of a
 * simple 2D object considering different topologies.
 *
 */
bool testBoard2D()
{
  trace.beginBlock ( "Testing Board2D with Object Borders in 2D ..." );

  typedef int Integer;                // choose your digital line here.
  typedef SpaceND<2> Z2;          // Z^2
  typedef Z2::Point Point;
  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
  typedef HyperRectDomain< Z2 > Domain;
  typedef Domain::ConstIterator DomainConstIterator;
  typedef DigitalSetSelector < Domain, BIG_DS + HIGH_BEL_DS >::Type DigitalSet;
  typedef Object<DT8_4, DigitalSet> ObjectType;


  Point p1( -20, -10 );
  Point p2( 20, 10 );
  Domain domain( p1, p2 );

  Adj4 adj4;                          // instance of 4-adjacency
  Adj8 adj8;                          // instance of 8-adjacency
  DT8_4 dt8_4(adj8, adj4, JORDAN_DT );

  Point c( 0, 0 );

  //We construct a simple 3-bubbles set
  DigitalSet bubble_set( domain );
  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
  {
    int x = (*it)[0];
    int y = (*it)[1];
    if (( x*x + y*y < 82) ||
        (  (x - 14)*(x - 14) + (y + 1)*(y + 1) < 17) ||
        (  (x + 14)*(x + 14) + (y - 1)*(y - 1) < 17) )
      bubble_set.insertNew( *it);
  }

  ObjectType bubble( dt8_4, bubble_set );

  //Connectedness Check
  if (bubble.computeConnectedness() == ObjectType::CONNECTED)
    trace.info() << "The object is (8,4)connected." << endl;
  else
    trace.info() << "The object is not (8,4)connected." << endl;

  //Border Computation
  ObjectType bubbleBorder = bubble.border();
  if (bubbleBorder.computeConnectedness() == ObjectType::CONNECTED)
    trace.info() << "The object (8,4) border is connected." << endl;
  else
    trace.info() << "The object (8,4) border is not connected." << endl;

  //Board Export
  Board2D board;
  board.setUnit(Board::UCentimeter);

  board << DrawDomainGrid()
  << CustomStyle( domain.styleName(), new MyDrawStyleCustomGreen )
  << domain
  << CustomStyle( bubble_set.styleName(), new MyDrawStyleCustomRed )
  << bubble_set;
  board.saveSVG("bubble-set-dgtalboard.svg");

  board << DrawObjectAdjacencies( true )
  << CustomStyle( bubbleBorder.styleName(), new MyDrawStyleCustomBlue )
  << bubbleBorder;
  board.saveSVG("bubble-object-border-dgtalboard.svg");
  board.clear();

  trace.endBlock();
  return true;
}
Ejemplo n.º 2
0
/**
 * Simple test to illustrate the border extraction of a simple 2D
 * object considering different topologies.
 *
 */
bool testObjectBorder()
{
  trace.beginBlock ( "Testing Object Borders in 2D ..." );

  typedef int Integer;                // choose your digital line here.
  typedef SpaceND<2> Z2;          // Z^2
  typedef Z2::Point Point;
  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
  typedef HyperRectDomain< Z2 > Domain;
  typedef Domain::ConstIterator DomainConstIterator;
  typedef DigitalSetSelector < Domain, BIG_DS + HIGH_BEL_DS >::Type DigitalSet;
  typedef Object<DT8_4, DigitalSet> ObjectType;


  Point p1( -20, -10 );
  Point p2( 20, 10 );
  Domain domain( p1, p2 );

  Adj4 adj4;                          // instance of 4-adjacency
  Adj8 adj8;                          // instance of 8-adjacency
  DT8_4 dt8_4(adj8, adj4, JORDAN_DT );

  Point c( 0, 0 );

  //We construct a simple 3-bubbles set
  DigitalSet bubble_set( domain );
  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
  {
    int x = (*it)[0];
    int y = (*it)[1];
    if (( x*x + y*y < 82) ||
        (  (x - 14)*(x - 14) + (y + 1)*(y + 1) < 17) ||
        (  (x + 14)*(x + 14) + (y - 1)*(y - 1) < 17) )
      bubble_set.insertNew( *it);
  }

  ObjectType bubble( dt8_4, bubble_set );

  //Connectedness Check
  if (bubble.computeConnectedness() == ObjectType::CONNECTED)
    trace.info() << "The object is (8,4)connected." << endl;
  else
    trace.info() << "The object is not (8,4)connected." << endl;

  //Border Computation
  ObjectType bubbleBorder = bubble.border();
  if (bubbleBorder.computeConnectedness() == ObjectType::CONNECTED)
    trace.info() << "The object (8,4) border is connected." << endl;
  else
    trace.info() << "The object (8,4) border is not connected." << endl;

  //Board Export
  Board2D board;
  board.setUnit(Board::UCentimeter);

  board << DrawDomainGrid() <<  domain << bubble_set;
  board.saveSVG("bubble-set.svg");

  board << DrawObjectAdjacencies()
  //  << DrawWithCustomStyle<SelfDrawStyleCustom>()
  << CustomStyle( "Object", new MyObjectStyleCustom )
  << bubbleBorder;
  board.saveSVG("bubble-object-border.svg");

  board.clear();

  //////////////////////:
  //the same with the reverse topology
  typedef Object<DT8_4::ReverseTopology, DigitalSet> ObjectType48;
  DT8_4::ReverseTopology dt4_8 = dt8_4.reverseTopology();

  ObjectType48 bubble2( dt4_8, bubble_set );

  //Border Computation
  ObjectType48 bubbleBorder2 = bubble2.border();
  if (bubbleBorder2.computeConnectedness() == ObjectType48::CONNECTED)
    trace.info() << "The object (4,8) border is connected." << endl;
  else
    trace.info() << "The object (4,8) border is not connected." << endl;

  domain.selfDrawAsGrid(board);
  bubble_set.selfDraw(board);
  board << DrawObjectAdjacencies()
  << CustomStyle( "Object", new MyObjectStyleCustom )
  << bubbleBorder2;

  board.saveSVG("bubble-object-border-48.svg");

  //We split the border according to its components
  vector<ObjectType48> borders(30);
  unsigned int nbComponents;

  vector<ObjectType48>::iterator it = borders.begin();
  nbComponents = bubbleBorder2.writeComponents( it );

  trace.info() << "The Bubble object has " << nbComponents << " (4,8)-connected components" << endl;

  bool flag = true;
  for (unsigned int k = 0;k < nbComponents ; k++)
  {
    if (flag)
      board <<  DrawObjectAdjacencies() << CustomStyle( "Object", new MyObjectStyleCustom ) << borders[k];
    else
      board <<  DrawObjectAdjacencies() << CustomStyle( "Object", new MyObjectStyleCustom ) << borders[k];
    flag = !flag;
  }

  board.saveSVG("bubble-object-color-borders-48.svg");
  trace.endBlock();

  return true;
}