Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
testObjectBorder.cpp
Go to the documentation of this file.
1
16
29
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/DomainAdjacency.h"
41#include "DGtal/topology/DigitalTopology.h"
42#include "DGtal/topology/Object.h"
43#include "DGtal/graph/Expander.h"
44#include "DGtal/io/boards/Board2D.h"
45
46//#include "Board/Board.h"
48
49using namespace std;
50using namespace DGtal;
51using namespace LibBoard;
53// Functions for testing class ObjectBorder.
55
56
57
59{
60 virtual void setStyle ( Board2D & aboard ) const
61 {
62 aboard.setFillColorRGBi ( 0, 169, 0 );
63 }
64};
66{
67 virtual void setStyle ( Board2D & aboard ) const
68 {
69 aboard.setFillColorRGBi ( 169, 0, 0 );
70 }
71};
72
74{
75 virtual void setStyle ( Board2D & aboard ) const
76 {
77 aboard.setFillColorRGBi ( 169, 150, 150 );
78 aboard.setPenColorRGBi ( 0, 0, 0 );
80 aboard.setLineWidth ( 1.5 );
81 }
82};
83
85{
86 virtual void setStyle ( Board2D & aboard ) const
87 {
88 aboard.setFillColorRGBi ( 150, 150, 250 );
89 aboard.setPenColorRGBi ( 0, 0, 200 );
91 aboard.setLineWidth ( 1.5 );
92 }
93};
94
96{
97 virtual void setStyle ( Board2D & aboard ) const
98 {
99 aboard.setFillColorRGBi ( 150, 150, 160 );
100 aboard.setPenColorRGBi ( 150, 150, 160 );
102 aboard.setLineWidth ( 1.0 );
103 }
104};
105
106
113{
114 trace.beginBlock ( "Testing Object Borders in 2D ..." );
115
116 typedef SpaceND<2> Z2; // Z^2
117 typedef Z2::Point Point;
118 typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
119 typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
120 typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
122 typedef Domain::ConstIterator DomainConstIterator;
124 typedef Object<DT8_4, DigitalSet> ObjectType;
125
126
127 Point p1 ( -20, -10 );
128 Point p2 ( 20, 10 );
129 Domain domain ( p1, p2 );
130
131 Adj4 adj4; // instance of 4-adjacency
132 Adj8 adj8; // instance of 8-adjacency
133 DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
134
135 Point c ( 0, 0 );
136
137 //We construct a simple 3-bubbles set
138 DigitalSet bubble_set ( domain );
139 for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
140 {
141 int x = ( *it ) [0];
142 int y = ( *it ) [1];
143 if ( ( x*x + y*y < 82 ) ||
144 ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
145 ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
146 bubble_set.insertNew ( *it );
147 }
148
149 ObjectType bubble ( dt8_4, bubble_set );
150
151 //Connectedness Check
152 if (bubble.computeConnectedness() == CONNECTED)
153 trace.info() << "The object is (8,4)connected." << endl;
154 else
155 trace.info() << "The object is not (8,4)connected." << endl;
156
157 //Border Computation
158 ObjectType bubbleBorder = bubble.border();
159 if (bubbleBorder.computeConnectedness() == CONNECTED)
160 trace.info() << "The object (8,4) border is connected." << endl;
161 else
162 trace.info() << "The object (8,4) border is not connected." << endl;
163
164 //Board Export
165 Board2D board;
166 board.setUnit ( Board::UCentimeter );
167
168 board << SetMode( domain.className(), "Grid" ) << domain << bubble_set;
169 board.saveSVG ( "bubble-set.svg" );
170
171 board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
172 << CustomStyle ( bubbleBorder.className(), new MyObjectStyleCustom )
173 << bubbleBorder;
174 board.saveSVG ( "bubble-object-border.svg" );
175
176 board.clear();
177
179 //the same with the reverse topology
181 DT8_4::ReverseTopology dt4_8 = dt8_4.reverseTopology();
182
183 ObjectType48 bubble2 ( dt4_8, bubble_set );
184
185 //Border Computation
186 ObjectType48 bubbleBorder2 = bubble2.border();
187 if (bubbleBorder2.computeConnectedness() == CONNECTED)
188 trace.info() << "The object (4,8) border is connected." << endl;
189 else
190 trace.info() << "The object (4,8) border is not connected." << endl;
191
192 board << SetMode( domain.className(), "Grid" ) << domain;
193 board << bubble_set
194 << SetMode( bubbleBorder2.className(), "DrawAdjacencies" )
195 << CustomStyle ( bubbleBorder2.className(), new MyObjectStyleCustom )
196 << bubbleBorder2;
197
198 board.saveSVG ( "bubble-object-border-48.svg" );
199
200 //We split the border according to its components
201 vector<ObjectType48> borders ( 30 );
202 vector<ObjectType48>::iterator it = borders.begin();
203 auto nbComponents = bubbleBorder2.writeComponents ( it );
204
205 trace.info() << "The Bubble object has " << nbComponents << " (4,8)-connected components" << endl;
206
207 bool flag = true;
208 for ( unsigned int k = 0;k < nbComponents ; k++ )
209 {
210 if ( flag )
211 board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
212 else
213 board << SetMode( borders[k].className(), "DrawAdjacencies" ) << CustomStyle ( borders[k].className(), new MyObjectStyleCustom ) << borders[k];
214 flag = !flag;
215 }
216
217 board.saveSVG ( "bubble-object-color-borders-48.svg" );
218 trace.endBlock();
219
220 return true;
221}
222
223
230{
231 trace.beginBlock ( "Testing Board2D with Object Borders in 2D ..." );
232
233 //typedef int Integer; // choose your digital line here.
234 typedef SpaceND<2> Z2; // Z^2
235 typedef Z2::Point Point;
236 typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
237 typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
238 typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
240 typedef Domain::ConstIterator DomainConstIterator;
242 typedef Object<DT8_4, DigitalSet> ObjectType;
243
244
245 Point p1 ( -20, -10 );
246 Point p2 ( 20, 10 );
247 Domain domain ( p1, p2 );
248
249 Adj4 adj4; // instance of 4-adjacency
250 Adj8 adj8; // instance of 8-adjacency
251 DT8_4 dt8_4 ( adj8, adj4, JORDAN_DT );
252
253 Point c ( 0, 0 );
254
255 //We construct a simple 3-bubbles set
256 DigitalSet bubble_set ( domain );
257 for ( DomainConstIterator it = domain.begin(); it != domain.end(); ++it )
258 {
259 int x = ( *it ) [0];
260 int y = ( *it ) [1];
261 if ( ( x*x + y*y < 82 ) ||
262 ( ( x - 14 ) * ( x - 14 ) + ( y + 1 ) * ( y + 1 ) < 17 ) ||
263 ( ( x + 14 ) * ( x + 14 ) + ( y - 1 ) * ( y - 1 ) < 17 ) )
264 bubble_set.insertNew ( *it );
265 }
266
267 ObjectType bubble ( dt8_4, bubble_set );
268
269 //Connectedness Check
270 if (bubble.computeConnectedness() == CONNECTED)
271 trace.info() << "The object is (8,4)connected." << endl;
272 else
273 trace.info() << "The object is not (8,4)connected." << endl;
274
275 //Border Computation
276 ObjectType bubbleBorder = bubble.border();
277 if (bubbleBorder.computeConnectedness() == CONNECTED)
278 trace.info() << "The object (8,4) border is connected." << endl;
279 else
280 trace.info() << "The object (8,4) border is not connected." << endl;
281
282 //Board Export
283 Board2D board;
284 board.setUnit ( Board::UCentimeter );
285
286 board << SetMode( domain.className(), "Grid" )
287 << CustomStyle ( domain.className(), new MyDrawStyleCustomGreen )
288 << domain
289 << CustomStyle ( bubble_set.className(), new MyDrawStyleCustomRed )
290 << bubble_set;
291 board.saveSVG ( "bubble-set-dgtalboard.svg" );
292
293 board << SetMode( bubbleBorder.className(), "DrawAdjacencies" )
294 << CustomStyle ( bubbleBorder.className(), new MyDrawStyleCustomBlue )
295 << bubbleBorder;
296 board.saveSVG ( "bubble-object-border-dgtalboard.svg" );
297 board.clear();
298
299 trace.endBlock();
300 return true;
301}
302
304// Standard services - public :
305
306int main ( /* int argc, char** argv*/ )
307{
308 bool res = testObjectBorder()
309 && testBoard2D();
310 return res ? 0 : 1;
311}
312// //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition Board2D.h:71
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
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
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition Board.cpp:277
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition Board.cpp:151
Board & setFillColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition Board.cpp:304
Board & setLineWidth(double width)
Definition Board.cpp:328
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
Board & setLineStyle(Shape::LineStyle style)
DGtal is the top-level namespace which contains all DGtal functions and types.
@ CONNECTED
Definition Topology.h:52
Trace trace
STL namespace.
DigitalSetByAssociativeContainer< Domain, std::unordered_set< typename Domain::Point > > Type
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
virtual void setStyle(Board2D &aboard) const
virtual void setStyle(Board2D &aboard) const
virtual void setStyle(Board2D &aboard) const
virtual void setStyle(Board2D &aboard) const
virtual void setStyle(Board2D &aboard) const
bool testObjectBorder()
bool testBoard2D()
int main()
Domain domain
Z2i::DigitalSet DigitalSet