DGtal  1.5.beta
testObjectBorder.cpp File Reference
#include <iostream>
#include <iterator>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/SpaceND.h"
#include "DGtal/kernel/domains/DomainPredicate.h"
#include "DGtal/kernel/domains/HyperRectDomain.h"
#include "DGtal/kernel/sets/DigitalSetSelector.h"
#include "DGtal/kernel/sets/DigitalSetConverter.h"
#include "DGtal/topology/MetricAdjacency.h"
#include "DGtal/topology/DomainMetricAdjacency.h"
#include "DGtal/topology/DomainAdjacency.h"
#include "DGtal/topology/DigitalTopology.h"
#include "DGtal/topology/Object.h"
#include "DGtal/graph/Expander.h"
#include "DGtal/io/boards/Board2D.h"
Include dependency graph for testObjectBorder.cpp:

Go to the source code of this file.

Functions

bool testObjectBorder ()
 
bool testBoard2D ()
 
int main ()
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 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 for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2010/09/17

Functions for testing class ObjectBorder.

This file is part of the DGtal library.

Definition in file testObjectBorder.cpp.

Function Documentation

◆ main()

int main ( void  )

Definition at line 307 of file testObjectBorder.cpp.

308 {
309  bool res = testObjectBorder()
310  && testBoard2D();
311  return res ? 0 : 1;
312 }
bool testObjectBorder()
bool testBoard2D()

References testBoard2D(), and testObjectBorder().

◆ testBoard2D()

bool testBoard2D ( )

Simple test of Board2D. Illustrates the border extraction of a simple 2D object considering different topologies.

Definition at line 230 of file testObjectBorder.cpp.

231 {
232  trace.beginBlock ( "Testing Board2D with Object Borders in 2D ..." );
233 
234  //typedef int Integer; // choose your digital line here.
235  typedef SpaceND<2> Z2; // Z^2
236  typedef Z2::Point Point;
237  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
238  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
239  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
241  typedef Domain::ConstIterator DomainConstIterator;
243  typedef Object<DT8_4, DigitalSet> ObjectType;
244 
245 
246  Point p1 ( -20, -10 );
247  Point p2 ( 20, 10 );
248  Domain domain ( p1, p2 );
249 
250  Adj4 adj4; // instance of 4-adjacency
251  Adj8 adj8; // instance of 8-adjacency
252  DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
253 
254  Point c ( 0, 0 );
255 
256  //We construct a simple 3-bubbles set
257  DigitalSet bubble_set ( domain );
258  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
259  {
260  int x = ( *it ) [0];
261  int y = ( *it ) [1];
262  if ( ( x*x + y*y < 82 ) ||
263  ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
264  ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
265  bubble_set.insertNew ( *it );
266  }
267 
268  ObjectType bubble ( dt8_4, bubble_set );
269 
270  //Connectedness Check
271  if (bubble.computeConnectedness() == CONNECTED)
272  trace.info() << "The object is (8,4)connected." << endl;
273  else
274  trace.info() << "The object is not (8,4)connected." << endl;
275 
276  //Border Computation
277  ObjectType bubbleBorder = bubble.border();
278  if (bubbleBorder.computeConnectedness() == CONNECTED)
279  trace.info() << "The object (8,4) border is connected." << endl;
280  else
281  trace.info() << "The object (8,4) border is not connected." << endl;
282 
283  //Board Export
284  Board2D board;
285  board.setUnit ( Board::UCentimeter );
286 
287  board << SetMode( domain.className(), "Grid" )
288  << CustomStyle ( domain.className(), new MyDrawStyleCustomGreen )
289  << domain
290  << CustomStyle ( bubble_set.className(), new MyDrawStyleCustomRed )
291  << bubble_set;
292  board.saveSVG ( "bubble-set-dgtalboard.svg" );
293 
294  board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
295  << CustomStyle ( bubbleBorder.className(), new MyDrawStyleCustomBlue )
296  << bubbleBorder;
297  board.saveSVG ( "bubble-object-border-dgtalboard.svg" );
298  board.clear();
299 
300  trace.endBlock();
301  return true;
302 }
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Aim: Represents a digital topology as a couple of adjacency relations.
Iterator for HyperRectDomain.
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
const ConstIterator & end() const
const ConstIterator & begin() const
std::string className() const
Aim: Describes digital adjacencies in digital spaces that are defined with the 1-norm and the infinit...
Aim: An object (or digital object) represents a set in some digital space associated with a digital t...
Definition: Object.h:120
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:151
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1011
void setUnit(Unit unit)
Definition: Board.cpp:239
DigitalTopology< Adj8, Adj4 > DT8_4
Definition: StdDefs.h:94
MetricAdjacency< Space, 1 > Adj4
Definition: StdDefs.h:90
MetricAdjacency< Space, 2 > Adj8
Definition: StdDefs.h:92
Space Z2
Definition: StdDefs.h:76
Trace trace
Definition: Common.h:153
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
Struct representing a 2D point.
Definition: Point.h:27
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
HyperRectDomain< Space > Domain
Z2i::DigitalSet DigitalSet

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::Trace::beginBlock(), DGtal::HyperRectDomain< TSpace >::className(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::className(), LibBoard::Board::clear(), DGtal::CONNECTED, domain, DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::insertNew(), DGtal::JORDAN_DT, LibBoard::Board::saveSVG(), LibBoard::Board::setUnit(), and DGtal::trace.

Referenced by main().

◆ testObjectBorder()

bool testObjectBorder ( )

Simple test to illustrate the border extraction of a simple 2D object considering different topologies.

Definition at line 113 of file testObjectBorder.cpp.

114 {
115  trace.beginBlock ( "Testing Object Borders in 2D ..." );
116 
117  typedef SpaceND<2> Z2; // Z^2
118  typedef Z2::Point Point;
119  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
120  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
121  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
123  typedef Domain::ConstIterator DomainConstIterator;
125  typedef Object<DT8_4, DigitalSet> ObjectType;
126 
127 
128  Point p1 ( -20, -10 );
129  Point p2 ( 20, 10 );
130  Domain domain ( p1, p2 );
131 
132  Adj4 adj4; // instance of 4-adjacency
133  Adj8 adj8; // instance of 8-adjacency
134  DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
135 
136  Point c ( 0, 0 );
137 
138  //We construct a simple 3-bubbles set
139  DigitalSet bubble_set ( domain );
140  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
141  {
142  int x = ( *it ) [0];
143  int y = ( *it ) [1];
144  if ( ( x*x + y*y < 82 ) ||
145  ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
146  ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
147  bubble_set.insertNew ( *it );
148  }
149 
150  ObjectType bubble ( dt8_4, bubble_set );
151 
152  //Connectedness Check
153  if (bubble.computeConnectedness() == CONNECTED)
154  trace.info() << "The object is (8,4)connected." << endl;
155  else
156  trace.info() << "The object is not (8,4)connected." << endl;
157 
158  //Border Computation
159  ObjectType bubbleBorder = bubble.border();
160  if (bubbleBorder.computeConnectedness() == CONNECTED)
161  trace.info() << "The object (8,4) border is connected." << endl;
162  else
163  trace.info() << "The object (8,4) border is not connected." << endl;
164 
165  //Board Export
166  Board2D board;
167  board.setUnit ( Board::UCentimeter );
168 
169  board << SetMode( domain.className(), "Grid" ) << domain << bubble_set;
170  board.saveSVG ( "bubble-set.svg" );
171 
172  board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
173  << CustomStyle ( bubbleBorder.className(), new MyObjectStyleCustom )
174  << bubbleBorder;
175  board.saveSVG ( "bubble-object-border.svg" );
176 
177  board.clear();
178 
180  //the same with the reverse topology
181  typedef Object<DT8_4::ReverseTopology, DigitalSet> ObjectType48;
182  DT8_4::ReverseTopology dt4_8 = dt8_4.reverseTopology();
183 
184  ObjectType48 bubble2 ( dt4_8, bubble_set );
185 
186  //Border Computation
187  ObjectType48 bubbleBorder2 = bubble2.border();
188  if (bubbleBorder2.computeConnectedness() == CONNECTED)
189  trace.info() << "The object (4,8) border is connected." << endl;
190  else
191  trace.info() << "The object (4,8) border is not connected." << endl;
192 
193  board << SetMode( domain.className(), "Grid" ) << domain;
194  board << bubble_set
195  << SetMode( bubbleBorder2.className(), "DrawAdjacencies" )
196  << CustomStyle ( bubbleBorder2.className(), new MyObjectStyleCustom )
197  << bubbleBorder2;
198 
199  board.saveSVG ( "bubble-object-border-48.svg" );
200 
201  //We split the border according to its components
202  vector<ObjectType48> borders ( 30 );
203  vector<ObjectType48>::iterator it = borders.begin();
204  auto nbComponents = bubbleBorder2.writeComponents ( it );
205 
206  trace.info() << "The Bubble object has " << nbComponents << " (4,8)-connected components" << endl;
207 
208  bool flag = true;
209  for ( unsigned int k = 0;k < nbComponents ; k++ )
210  {
211  if ( flag )
212  board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
213  else
214  board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
215  flag = !flag;
216  }
217 
218  board.saveSVG ( "bubble-object-color-borders-48.svg" );
219  trace.endBlock();
220 
221  return true;
222 }

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::Trace::beginBlock(), DGtal::HyperRectDomain< TSpace >::className(), LibBoard::Board::clear(), DGtal::CONNECTED, domain, DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::DigitalSetByAssociativeContainer< TDomain, TContainer >::insertNew(), DGtal::JORDAN_DT, LibBoard::Board::saveSVG(), LibBoard::Board::setUnit(), and DGtal::trace.

Referenced by main().