DGtal  0.9.2
testObjectBorder.cpp
1 
30 #include <iostream>
32 #include <iterator>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/kernel/SpaceND.h"
35 #include "DGtal/kernel/domains/DomainPredicate.h"
36 #include "DGtal/kernel/domains/HyperRectDomain.h"
37 #include "DGtal/kernel/sets/DigitalSetSelector.h"
38 #include "DGtal/kernel/sets/DigitalSetConverter.h"
39 #include "DGtal/topology/MetricAdjacency.h"
40 #include "DGtal/topology/DomainMetricAdjacency.h"
41 #include "DGtal/topology/DomainAdjacency.h"
42 #include "DGtal/topology/DigitalTopology.h"
43 #include "DGtal/topology/Object.h"
44 #include "DGtal/graph/Expander.h"
45 #include "DGtal/io/boards/Board2D.h"
46 
47 //#include "Board/Board.h"
49 
50 using namespace std;
51 using namespace DGtal;
52 using namespace LibBoard;
54 // Functions for testing class ObjectBorder.
56 
57 
58 
59 struct MyObjectStyleCustom : public DrawableWithBoard2D
60 {
61  virtual void setStyle ( Board2D & aboard ) const
62  {
63  aboard.setFillColorRGBi ( 0, 169, 0 );
64  }
65 };
66 struct MyObjectStyleCustomRed : public DrawableWithBoard2D
67 {
68  virtual void setStyle ( Board2D & aboard ) const
69  {
70  aboard.setFillColorRGBi ( 169, 0, 0 );
71  }
72 };
73 
74 struct MyDrawStyleCustomRed : public DrawableWithBoard2D
75 {
76  virtual void setStyle ( Board2D & aboard ) const
77  {
78  aboard.setFillColorRGBi ( 169, 150, 150 );
79  aboard.setPenColorRGBi ( 0, 0, 0 );
80  aboard.setLineStyle ( Board2D::Shape::SolidStyle );
81  aboard.setLineWidth ( 1.5 );
82  }
83 };
84 
85 struct MyDrawStyleCustomBlue : public DrawableWithBoard2D
86 {
87  virtual void setStyle ( Board2D & aboard ) const
88  {
89  aboard.setFillColorRGBi ( 150, 150, 250 );
90  aboard.setPenColorRGBi ( 0, 0, 200 );
91  aboard.setLineStyle ( Board2D::Shape::SolidStyle );
92  aboard.setLineWidth ( 1.5 );
93  }
94 };
95 
96 struct MyDrawStyleCustomGreen : public DrawableWithBoard2D
97 {
98  virtual void setStyle ( Board2D & aboard ) const
99  {
100  aboard.setFillColorRGBi ( 150, 150, 160 );
101  aboard.setPenColorRGBi ( 150, 150, 160 );
102  aboard.setLineStyle ( Board2D::Shape::DashStyle );
103  aboard.setLineWidth ( 1.0 );
104  }
105 };
106 
107 
113 bool testObjectBorder()
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  unsigned int nbComponents;
204 
205  vector<ObjectType48>::iterator it = borders.begin();
206  nbComponents = bubbleBorder2.writeComponents ( it );
207 
208  trace.info() << "The Bubble object has " << nbComponents << " (4,8)-connected components" << endl;
209 
210  bool flag = true;
211  for ( unsigned int k = 0;k < nbComponents ; k++ )
212  {
213  if ( flag )
214  board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
215  else
216  board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
217  flag = !flag;
218  }
219 
220  board.saveSVG ( "bubble-object-color-borders-48.svg" );
221  trace.endBlock();
222 
223  return true;
224 }
225 
226 
232 bool testBoard2D()
233 {
234  trace.beginBlock ( "Testing Board2D with Object Borders in 2D ..." );
235 
236  //typedef int Integer; // choose your digital line here.
237  typedef SpaceND<2> Z2; // Z^2
238  typedef Z2::Point Point;
239  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
240  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
241  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
242  typedef HyperRectDomain< Z2 > Domain;
243  typedef Domain::ConstIterator DomainConstIterator;
245  typedef Object<DT8_4, DigitalSet> ObjectType;
246 
247 
248  Point p1 ( -20, -10 );
249  Point p2 ( 20, 10 );
250  Domain domain ( p1, p2 );
251 
252  Adj4 adj4; // instance of 4-adjacency
253  Adj8 adj8; // instance of 8-adjacency
254  DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
255 
256  Point c ( 0, 0 );
257 
258  //We construct a simple 3-bubbles set
259  DigitalSet bubble_set ( domain );
260  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
261  {
262  int x = ( *it ) [0];
263  int y = ( *it ) [1];
264  if ( ( x*x + y*y < 82 ) ||
265  ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
266  ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
267  bubble_set.insertNew ( *it );
268  }
269 
270  ObjectType bubble ( dt8_4, bubble_set );
271 
272  //Connectedness Check
273  if (bubble.computeConnectedness() == CONNECTED)
274  trace.info() << "The object is (8,4)connected." << endl;
275  else
276  trace.info() << "The object is not (8,4)connected." << endl;
277 
278  //Border Computation
279  ObjectType bubbleBorder = bubble.border();
280  if (bubbleBorder.computeConnectedness() == CONNECTED)
281  trace.info() << "The object (8,4) border is connected." << endl;
282  else
283  trace.info() << "The object (8,4) border is not connected." << endl;
284 
285  //Board Export
286  Board2D board;
287  board.setUnit ( Board::UCentimeter );
288 
289  board << SetMode( domain.className(), "Grid" )
290  << CustomStyle ( domain.className(), new MyDrawStyleCustomGreen )
291  << domain
292  << CustomStyle ( bubble_set.className(), new MyDrawStyleCustomRed )
293  << bubble_set;
294  board.saveSVG ( "bubble-set-dgtalboard.svg" );
295 
296  board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
297  << CustomStyle ( bubbleBorder.className(), new MyDrawStyleCustomBlue )
298  << bubbleBorder;
299  board.saveSVG ( "bubble-object-border-dgtalboard.svg" );
300  board.clear();
301 
302  trace.endBlock();
303  return true;
304 }
305 
307 // Standard services - public :
308 
309 int main ( /* int argc, char** argv*/ )
310 {
311  bool res = testObjectBorder()
312  && testBoard2D();
313  return res ? 0 : 1;
314 }
315 // //
Aim: An object (or digital object) represents a set in some digital space associated with a digital t...
Definition: Object.h:119
void beginBlock(const std::string &keyword="")
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100
Board & setLineWidth(double width)
Definition: Board.cpp:329
const ConstIterator & begin() const
const ConstIterator & end() const
Board & setFillColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition: Board.cpp:305
MetricAdjacency< Space, 2 > Adj8
Definition: StdDefs.h:92
Trace trace
Definition: Common.h:130
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
STL namespace.
double endBlock()
MetricAdjacency< Space, 1 > Adj4
Definition: StdDefs.h:90
Aim: Describes digital adjacencies in digital spaces that are defined with the 1-norm and the infinit...
Struct representing a 2D point.
Definition: Point.h:27
std::string className() const
Aim: Represents a digital topology as a couple of adjacency relations.
Board & setLineStyle(Shape::LineStyle style)
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Space Z2
Definition: StdDefs.h:76
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
std::ostream & info()
Modifier class in a Board2D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithBoard2D.
Definition: Board2D.h:247
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition: Board.cpp:278
void setUnit(Unit unit)
Definition: Board.cpp:240
DigitalTopology< Adj8, Adj4 > DT8_4
Definition: StdDefs.h:94
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70