DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testObjectBorder.cpp
1 
30 
31 #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/topology/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 
60 {
61  virtual void setStyle ( Board2D & aboard ) const
62  {
63  aboard.setFillColorRGBi ( 0, 169, 0 );
64  }
65 };
67 {
68  virtual void setStyle ( Board2D & aboard ) const
69  {
70  aboard.setFillColorRGBi ( 169, 0, 0 );
71  }
72 };
73 
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 
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 
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 int Integer; // choose your digital line here.
118  typedef SpaceND<2> Z2; // Z^2
119  typedef Z2::Point Point;
120  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
121  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
122  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
124  typedef Domain::ConstIterator DomainConstIterator;
126  typedef Object<DT8_4, DigitalSet> ObjectType;
127 
128 
129  Point p1 ( -20, -10 );
130  Point p2 ( 20, 10 );
131  Domain domain ( p1, p2 );
132 
133  Adj4 adj4; // instance of 4-adjacency
134  Adj8 adj8; // instance of 8-adjacency
135  DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
136 
137  Point c ( 0, 0 );
138 
139  //We construct a simple 3-bubbles set
140  DigitalSet bubble_set ( domain );
141  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
142  {
143  int x = ( *it ) [0];
144  int y = ( *it ) [1];
145  if ( ( x*x + y*y < 82 ) ||
146  ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
147  ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
148  bubble_set.insertNew ( *it );
149  }
150 
151  ObjectType bubble ( dt8_4, bubble_set );
152 
153  //Connectedness Check
154  if (bubble.computeConnectedness() == CONNECTED)
155  trace.info() << "The object is (8,4)connected." << endl;
156  else
157  trace.info() << "The object is not (8,4)connected." << endl;
158 
159  //Border Computation
160  ObjectType bubbleBorder = bubble.border();
161  if (bubbleBorder.computeConnectedness() == CONNECTED)
162  trace.info() << "The object (8,4) border is connected." << endl;
163  else
164  trace.info() << "The object (8,4) border is not connected." << endl;
165 
166  //Board Export
167  Board2D board;
168  board.setUnit ( Board::UCentimeter );
169 
170  board << SetMode( domain.className(), "Grid" ) << domain << bubble_set;
171  board.saveSVG ( "bubble-set.svg" );
172 
173  board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
174  << CustomStyle ( bubbleBorder.className(), new MyObjectStyleCustom )
175  << bubbleBorder;
176  board.saveSVG ( "bubble-object-border.svg" );
177 
178  board.clear();
179 
181  //the same with the reverse topology
182  typedef Object<DT8_4::ReverseTopology, DigitalSet> ObjectType48;
183  DT8_4::ReverseTopology dt4_8 = dt8_4.reverseTopology();
184 
185  ObjectType48 bubble2 ( dt4_8, bubble_set );
186 
187  //Border Computation
188  ObjectType48 bubbleBorder2 = bubble2.border();
189  if (bubbleBorder2.computeConnectedness() == CONNECTED)
190  trace.info() << "The object (4,8) border is connected." << endl;
191  else
192  trace.info() << "The object (4,8) border is not connected." << endl;
193 
194  board << SetMode( domain.className(), "Grid" ) << domain;
195  board << bubble_set
196  << SetMode( bubbleBorder2.className(), "DrawAdjacencies" )
197  << CustomStyle ( bubbleBorder2.className(), new MyObjectStyleCustom )
198  << bubbleBorder2;
199 
200  board.saveSVG ( "bubble-object-border-48.svg" );
201 
202  //We split the border according to its components
203  vector<ObjectType48> borders ( 30 );
204  unsigned int nbComponents;
205 
206  vector<ObjectType48>::iterator it = borders.begin();
207  nbComponents = bubbleBorder2.writeComponents ( it );
208 
209  trace.info() << "The Bubble object has " << nbComponents << " (4,8)-connected components" << endl;
210 
211  bool flag = true;
212  for ( unsigned int k = 0;k < nbComponents ; k++ )
213  {
214  if ( flag )
215  board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
216  else
217  board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
218  flag = !flag;
219  }
220 
221  board.saveSVG ( "bubble-object-color-borders-48.svg" );
222  trace.endBlock();
223 
224  return true;
225 }
226 
227 
233 bool testBoard2D()
234 {
235  trace.beginBlock ( "Testing Board2D with Object Borders in 2D ..." );
236 
237  typedef int Integer; // choose your digital line here.
238  typedef SpaceND<2> Z2; // Z^2
239  typedef Z2::Point Point;
240  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
241  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
242  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
243  typedef HyperRectDomain< Z2 > Domain;
244  typedef Domain::ConstIterator DomainConstIterator;
246  typedef Object<DT8_4, DigitalSet> ObjectType;
247 
248 
249  Point p1 ( -20, -10 );
250  Point p2 ( 20, 10 );
251  Domain domain ( p1, p2 );
252 
253  Adj4 adj4; // instance of 4-adjacency
254  Adj8 adj8; // instance of 8-adjacency
255  DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
256 
257  Point c ( 0, 0 );
258 
259  //We construct a simple 3-bubbles set
260  DigitalSet bubble_set ( domain );
261  for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
262  {
263  int x = ( *it ) [0];
264  int y = ( *it ) [1];
265  if ( ( x*x + y*y < 82 ) ||
266  ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
267  ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
268  bubble_set.insertNew ( *it );
269  }
270 
271  ObjectType bubble ( dt8_4, bubble_set );
272 
273  //Connectedness Check
274  if (bubble.computeConnectedness() == CONNECTED)
275  trace.info() << "The object is (8,4)connected." << endl;
276  else
277  trace.info() << "The object is not (8,4)connected." << endl;
278 
279  //Border Computation
280  ObjectType bubbleBorder = bubble.border();
281  if (bubbleBorder.computeConnectedness() == CONNECTED)
282  trace.info() << "The object (8,4) border is connected." << endl;
283  else
284  trace.info() << "The object (8,4) border is not connected." << endl;
285 
286  //Board Export
287  Board2D board;
288  board.setUnit ( Board::UCentimeter );
289 
290  board << SetMode( domain.className(), "Grid" )
291  << CustomStyle ( domain.className(), new MyDrawStyleCustomGreen )
292  << domain
293  << CustomStyle ( bubble_set.className(), new MyDrawStyleCustomRed )
294  << bubble_set;
295  board.saveSVG ( "bubble-set-dgtalboard.svg" );
296 
297  board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
298  << CustomStyle ( bubbleBorder.className(), new MyDrawStyleCustomBlue )
299  << bubbleBorder;
300  board.saveSVG ( "bubble-object-border-dgtalboard.svg" );
301  board.clear();
302 
303  trace.endBlock();
304  return true;
305 }
306 
308 // Standard services - public :
309 
310 int main ( /* int argc, char** argv*/ )
311 {
312  bool res = testObjectBorder()
313  && testBoard2D();
314  return res ? 0 : 1;
315 }
316 // //