DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
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
49
using namespace
std
;
50
using namespace
DGtal
;
51
using namespace
LibBoard
;
53
// Functions for testing class ObjectBorder.
55
56
57
58
struct
MyObjectStyleCustom
:
public
DrawableWithBoard2D
59
{
60
virtual
void
setStyle
(
Board2D
& aboard )
const
61
{
62
aboard.
setFillColorRGBi
( 0, 169, 0 );
63
}
64
};
65
struct
MyObjectStyleCustomRed
:
public
DrawableWithBoard2D
66
{
67
virtual
void
setStyle
(
Board2D
& aboard )
const
68
{
69
aboard.
setFillColorRGBi
( 169, 0, 0 );
70
}
71
};
72
73
struct
MyDrawStyleCustomRed
:
public
DrawableWithBoard2D
74
{
75
virtual
void
setStyle
(
Board2D
& aboard )
const
76
{
77
aboard.
setFillColorRGBi
( 169, 150, 150 );
78
aboard.
setPenColorRGBi
( 0, 0, 0 );
79
aboard.
setLineStyle
(
Board2D::Shape::SolidStyle
);
80
aboard.
setLineWidth
( 1.5 );
81
}
82
};
83
84
struct
MyDrawStyleCustomBlue
:
public
DrawableWithBoard2D
85
{
86
virtual
void
setStyle
(
Board2D
& aboard )
const
87
{
88
aboard.
setFillColorRGBi
( 150, 150, 250 );
89
aboard.
setPenColorRGBi
( 0, 0, 200 );
90
aboard.
setLineStyle
(
Board2D::Shape::SolidStyle
);
91
aboard.
setLineWidth
( 1.5 );
92
}
93
};
94
95
struct
MyDrawStyleCustomGreen
:
public
DrawableWithBoard2D
96
{
97
virtual
void
setStyle
(
Board2D
& aboard )
const
98
{
99
aboard.
setFillColorRGBi
( 150, 150, 160 );
100
aboard.
setPenColorRGBi
( 150, 150, 160 );
101
aboard.
setLineStyle
(
Board2D::Shape::DashStyle
);
102
aboard.
setLineWidth
( 1.0 );
103
}
104
};
105
106
112
bool
testObjectBorder
()
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
121
typedef
HyperRectDomain< Z2 >
Domain
;
122
typedef
Domain::ConstIterator
DomainConstIterator;
123
typedef
DigitalSetSelector < Domain, BIG_DS + HIGH_BEL_DS >::Type
DigitalSet
;
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
180
typedef
Object<DT8_4::ReverseTopology, DigitalSet>
ObjectType48;
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
229
bool
testBoard2D
()
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
239
typedef
HyperRectDomain< Z2 >
Domain
;
240
typedef
Domain::ConstIterator
DomainConstIterator;
241
typedef
DigitalSetSelector < Domain, BIG_DS + HIGH_BEL_DS >::Type
DigitalSet
;
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
306
int
main
(
/* int argc, char** argv*/
)
307
{
308
bool
res =
testObjectBorder
()
309
&&
testBoard2D
();
310
return
res ? 0 : 1;
311
}
312
// //
DGtal::Board2D
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition
Board2D.h:71
DGtal::DigitalSetByAssociativeContainer::className
std::string className() const
DGtal::DigitalSetByAssociativeContainer::insertNew
void insertNew(const Point &p)
DGtal::DigitalTopology< Adj8, Adj4 >
DGtal::HyperRectDomain
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Definition
HyperRectDomain.h:100
DGtal::HyperRectDomain< Space >::ConstIterator
Iterator ConstIterator
Definition
HyperRectDomain.h:125
DGtal::MetricAdjacency
Aim: Describes digital adjacencies in digital spaces that are defined with the 1-norm and the infinit...
Definition
MetricAdjacency.h:80
DGtal::Object
Aim: An object (or digital object) represents a set in some digital space associated with a digital t...
Definition
Object.h:120
DGtal::SpaceND
Definition
SpaceND.h:96
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::info
std::ostream & info()
DGtal::Trace::endBlock
double endBlock()
DGtal::Domain
LibBoard::Board::UCentimeter
@ UCentimeter
Definition
Board.h:43
LibBoard::Board::setPenColorRGBi
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition
Board.cpp:277
LibBoard::Board::clear
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition
Board.cpp:151
LibBoard::Board::setFillColorRGBi
Board & setFillColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition
Board.cpp:304
LibBoard::Board::setLineWidth
Board & setLineWidth(double width)
Definition
Board.cpp:328
LibBoard::Board::saveSVG
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition
Board.cpp:1011
LibBoard::Board::setUnit
void setUnit(Unit unit)
Definition
Board.cpp:239
LibBoard::Board::setLineStyle
Board & setLineStyle(Shape::LineStyle style)
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::CONNECTED
@ CONNECTED
Definition
Topology.h:52
DGtal::trace
Trace trace
DGtal::JORDAN_DT
@ JORDAN_DT
Definition
DigitalTopology.h:57
LibBoard
Definition
Board.cpp:86
std
STL namespace.
DGtal::CustomStyle::CustomStyle
CustomStyle(std::string classname, DrawableWithBoard2D *style)
Definition
Board2D.h:224
DGtal::DigitalSetSelector::Type
DigitalSetByAssociativeContainer< Domain, std::unordered_set< typename Domain::Point > > Type
Definition
DigitalSetSelector.h:102
DGtal::DrawableWithBoard2D
Definition
Common.h:151
DGtal::SetMode::SetMode
SetMode(std::string classname, std::string mode)
Definition
Board2D.h:254
LibBoard::Point
Struct representing a 2D point.
Definition
Point.h:27
LibBoard::Shape::DashStyle
@ DashStyle
Definition
Board/Shapes.h:63
LibBoard::Shape::SolidStyle
@ SolidStyle
Definition
Board/Shapes.h:62
MyDrawStyleCustomBlue
Definition
testObjectBorder.cpp:85
MyDrawStyleCustomBlue::setStyle
virtual void setStyle(Board2D &aboard) const
Definition
testObjectBorder.cpp:86
MyDrawStyleCustomGreen
Definition
testBoard2DCustomStyle.cpp:52
MyDrawStyleCustomGreen::setStyle
virtual void setStyle(Board2D &aboard) const
Definition
testObjectBorder.cpp:97
MyDrawStyleCustomRed
Definition
testObject.cpp:515
MyDrawStyleCustomRed::setStyle
virtual void setStyle(Board2D &aboard) const
Definition
testObjectBorder.cpp:75
MyObjectStyleCustomRed
Definition
testObjectBorder.cpp:66
MyObjectStyleCustomRed::setStyle
virtual void setStyle(Board2D &aboard) const
Definition
testObjectBorder.cpp:67
MyObjectStyleCustom
Definition
testObjectBorder.cpp:59
MyObjectStyleCustom::setStyle
virtual void setStyle(Board2D &aboard) const
Definition
testObjectBorder.cpp:60
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
testObjectBorder
bool testObjectBorder()
Definition
testObjectBorder.cpp:112
testBoard2D
bool testBoard2D()
Definition
testObjectBorder.cpp:229
domain
Domain domain
Definition
testProjection.cpp:88
DigitalSet
Z2i::DigitalSet DigitalSet
Definition
testVoronoiMapComplete.cpp:40
tests
topology
testObjectBorder.cpp
Generated on
for DGtal by
1.18.0