DGtal  1.3.beta
testSimpleExpander.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/kernel/SpaceND.h"
34 #include "DGtal/kernel/domains/DomainPredicate.h"
35 #include "DGtal/kernel/domains/HyperRectDomain.h"
36 #include "DGtal/kernel/sets/DigitalSetSelector.h"
37 #include "DGtal/kernel/sets/DigitalSetConverter.h"
38 #include "DGtal/topology/MetricAdjacency.h"
39 #include "DGtal/topology/DomainMetricAdjacency.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 #include "DGtal/io/colormaps/GradientColorMap.h"
46 #include "DGtal/io/Color.h"
47 #include "DGtal/helpers/StdDefs.h"
49 
50 using namespace std;
51 using namespace DGtal;
52 
53 
54 
55 struct MyStyleCustom : public DrawableWithBoard2D
56 {
57  virtual void setStyle(Board2D & aboard) const
58  {
59  aboard.setFillColorRGBi(0, 169, 0);
60  }
61 };
62 
63 struct MyStyleCustomRed : public DrawableWithBoard2D
64 {
65  virtual void setStyle(Board2D & aboard) const
66  {
67  aboard.setFillColorRGBi(169, 0, 0);
68  }
69 };
71 // Functions for testing class SimpleExpander.
73 
78 {
79  trace.beginBlock ( "(4,8) Filling ..." );
80 
81  //typedef int Integer; // choose your digital line here.
82  typedef SpaceND<2> Z2; // Z^2
83  typedef Z2::Point Point;
84  typedef MetricAdjacency<Z2, 1> Adj4; // 4-adjacency type
85  typedef MetricAdjacency<Z2, 2> Adj8; // 8-adjacency type
86  typedef DigitalTopology< Adj8, Adj4 > DT8_4; //8,4 topology type
88  //typedef Domain::ConstIterator DomainConstIterator;
90  typedef Object<DT8_4, DigitalSet> ObjectType;
91 
92 
93  typedef Object<DT8_4::ReverseTopology, DigitalSet> ObjectTypeReverseTopo;
94 
95  typedef Expander<ObjectTypeReverseTopo> ObjectExpanderReverseTopo;
96  typedef Expander<ObjectType> ObjectExpander;
97 
98  Point p1( -5, -5 );
99  Point p2( 5, 5 );
100  Domain domain( p1, p2 );
101 
102  Adj4 adj4; // instance of 4-adjacency
103  Adj8 adj8; // instance of 8-adjacency
104  DT8_4 dt8_4(adj8, adj4, JORDAN_DT );
105  DT8_4::ReverseTopology dt4_8(adj4, adj8, JORDAN_DT );
106 
107  //We construct a simple "house" set
108  DigitalSet houseSet( domain );
109 
110  for ( int k = -3; k < 3 ; k++)
111  {
112  houseSet.insert(Point(k, -3));
113  houseSet.insert(Point(-3, k));
114  houseSet.insert(Point(3, k));
115  houseSet.insert(Point(k, 3));
116  }
117 
118  //We compute the complement
119  DigitalSet houseSetCompl( domain);
120  houseSetCompl.assignFromComplement( houseSet );
121 
122  //We create the objects associated to the sets
123  ObjectType house8( dt8_4, houseSet );
124  ObjectType houseCompl8( dt8_4, houseSetCompl );
125  ObjectTypeReverseTopo house4(dt4_8, houseSet);
126  ObjectTypeReverseTopo houseCompl4( dt4_8, houseSetCompl );
127 
128 
129  //Board Export init
130  Board2D board;
132 
133  //Border=4 Filling=4
134  board.clear();
135  board << SetMode( domain.className(), "Grid" ) << domain;
136  board << SetMode( house4.className(), "DrawAdjacencies" ) << house4;
137  ObjectExpanderReverseTopo expander(houseCompl4, Point(0, 0));
138  while (!expander.finished())
139  {
140  for ( ObjectExpander::ConstIterator it = expander.begin();
141  it != expander.end();
142  ++it )
143  std::cout << " " << *it;
144 
145  expander.nextLayer();
146  }
147  board << CustomStyle(expander.core().className(), new MyStyleCustom) << expander.core();
148  board.saveSVG("house4-4.svg");
149 
150  //Border=4 Filling=8
151  board.clear();
152  board << SetMode( domain.className(), "Grid" ) << domain;
153  board << SetMode( house4.className(), "DrawAdjacencies" ) << house4;
154  ObjectExpander expander8(houseCompl8, Point(0, 0));
155  while (!expander8.finished())
156  {
157  for ( ObjectExpander::ConstIterator it = expander8.begin();
158  it != expander8.end();
159  ++it )
160  std::cout << " " << *it;
161 
162  expander8.nextLayer();
163  }
164  board << CustomStyle(expander8.core().className(), new MyStyleCustom) << expander8.core();
165  board.saveSVG("house4-8.svg");
166 
167  //Border=8 Filling=8
168  board.clear();
169  board << SetMode( domain.className(), "Grid" ) << domain;
170  board << SetMode( house8.className(), "DrawAdjacencies" ) << house8;
171  ObjectExpander expander88(houseCompl8, Point(0, 0));
172  while (!expander88.finished())
173  {
174  for ( ObjectExpander::ConstIterator it = expander88.begin();
175  it != expander88.end();
176  ++it )
177  std::cout << " " << *it;
178 
179  expander88.nextLayer();
180  }
181  board << CustomStyle(expander88.core().className(), new MyStyleCustom) << expander88.core();
182  board.saveSVG("house8-8.svg");
183 
184  //Border=8 Filling=4
185  board.clear();
186  board << SetMode( domain.className(), "Grid" ) << domain;
187  board << SetMode( house8.className(), "DrawAdjacencies" ) << house8;
188  ObjectExpanderReverseTopo expander84(houseCompl4, Point(0, 0));
189  while (!expander84.finished())
190  {
191  for ( ObjectExpander::ConstIterator it = expander84.begin();
192  it != expander84.end();
193  ++it )
194  std::cout << " " << *it;
195 
196  expander84.nextLayer();
197  }
198  board << CustomStyle(expander.core().className(), new MyStyleCustom) << expander84.core();
199  board.saveSVG("house8-4.svg");
200 
201 
202  trace.endBlock();
203 
204  return true;
205 }
206 
207 using namespace DGtal::Z2i;
208 
210 {
211 
212  GradientColorMap<int> cmap_grad( 0, 30 );
213  cmap_grad.addColor( Color( 128, 128, 255 ) );
214  cmap_grad.addColor( Color( 255, 255, 128 ) );
215  cmap_grad.addColor( Color( 128, 255, 128 ) );
216  cmap_grad.addColor( Color( 128, 128, 128 ) );
217  //cmap_grad.addColor( Color( 220, 130, 25 ) );
218 
219  trace.beginBlock ( "(4,8) Filling ..." );
220 
221  //typedef Domain::ConstIterator DomainConstIterator;
222  typedef Object8_4 ObjectType;
223  typedef Object4_8 ObjectTypeReverseTopo;
224  typedef Expander<ObjectTypeReverseTopo> ObjectExpanderReverseTopo;
225  typedef Expander<ObjectType> ObjectExpander;
226 
227  Point p1( -5, -5 );
228  Point p2( 5, 5 );
229  Domain domain( p1, p2 );
230 
231 
232  //We construct a simple "house" set
233  DigitalSet houseSet( domain );
234 
235  for ( int k = -3; k < 3 ; k++)
236  {
237  houseSet.insert(Point(k, -3));
238  houseSet.insert(Point(-3, k));
239  houseSet.insert(Point(3, k));
240  houseSet.insert(Point(k, 3));
241  }
242 
243  //We compute the complement
244  DigitalSet houseSetCompl( domain);
245  houseSetCompl.assignFromComplement( houseSet );
246 
247  //We create the objects associated to the sets
248  ObjectType house8( dt8_4, houseSet );
249  ObjectType houseCompl8( dt8_4, houseSetCompl );
250  ObjectTypeReverseTopo house4( dt4_8, houseSet);
251  ObjectTypeReverseTopo houseCompl4( dt4_8, houseSetCompl );
252 
253 
254  //Board Export init
255  Board2D board;
257 
258  //Border=4 Filling=4
259  board.clear();
260  board << SetMode( domain.className(), "Grid" ) << domain;
261  board << SetMode( house4.className(), "DrawAdjacencies" ) << house4;
262  ObjectExpanderReverseTopo expander(houseCompl4, Point(0, 0));
263  board << CustomStyle( expander.core().className(),
264  new CustomFillColor( cmap_grad( 0 ) ) )
265  << expander.core();
266  while (!expander.finished())
267  {
268  for ( ObjectExpander::ConstIterator it = expander.begin();
269  it != expander.end();
270  ++it )
271  std::cout << " " << *it;
272  board << CustomStyle( expander.layer().className(),
273  new CustomFillColor
274  ( cmap_grad( expander.distance() ) ) )
275  << expander.layer();
276 
277  expander.nextLayer();
278  }
279  board.saveSVG("house-layers4-4.svg");
280 
281  //Border=4 Filling=8
282  board.clear();
283  board << SetMode( domain.className(), "Grid" ) << domain;
284  board << SetMode( house4.className(), "DrawAdjacencies" ) << house4;
285  ObjectExpander expander8(houseCompl8, Point(0, 0));
286  board << CustomStyle( expander.core().className(),
287  new CustomFillColor( cmap_grad( 0 ) ) )
288  << expander8.core();
289  while (!expander8.finished())
290  {
291  for ( ObjectExpander::ConstIterator it = expander8.begin();
292  it != expander8.end();
293  ++it )
294  std::cout << " " << *it;
295 
296  board << CustomStyle( expander8.layer().className(),
297  new CustomFillColor
298  ( cmap_grad( expander8.distance() ) ) )
299  << expander8.layer();
300  expander8.nextLayer();
301  }
302  board.saveSVG("house-layers4-8.svg");
303 
304  return true;
305 }
306 
308 // Standard services - public :
309 
310 int main( int argc, char** argv )
311 {
312  trace.info() << "Args:";
313  for ( int i = 0; i < argc; ++i )
314  trace.info() << " " << argv[ i ];
315  trace.info() << endl;
316 
317  bool res = testSimpleExpander()
318  && testLayers();
319  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
320  return res ? 0 : 1;
321 }
322 // //
LibBoard::Board::setUnit
void setUnit(Unit unit)
Definition: Board.cpp:240
ConstIterator
MyDigitalSurface::ConstIterator ConstIterator
Definition: greedy-plane-segmentation-ex2.cpp:93
DGtal::DigitalTopology
Aim: Represents a digital topology as a couple of adjacency relations.
Definition: DigitalTopology.h:95
DGtal::HyperRectDomain
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Definition: HyperRectDomain.h:99
DGtal::Trace::endBlock
double endBlock()
DGtal::Z2i
Z2i this namespace gathers the standard of types for 2D imagery.
LibBoard::Board::UCentimeter
@ UCentimeter
Definition: Board.h:43
testSimpleExpander
bool testSimpleExpander()
Definition: testSimpleExpander.cpp:77
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::HyperRectDomain::className
std::string className() const
LibBoard::Board::setFillColorRGBi
Board & setFillColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition: Board.cpp:305
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
LibBoard::Board::clear
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
DGtal::SpaceND
Definition: SpaceND.h:95
DGtal::MetricAdjacency
Aim: Describes digital adjacencies in digital spaces that are defined with the 1-norm and the infinit...
Definition: MetricAdjacency.h:79
DGtal::CustomStyle
Definition: Board2D.h:217
DGtal::Trace::info
std::ostream & info()
DGtal::CustomFillColor
Custom style class redefining the fill color. You may use Board2D::Color::None for transparent color.
Definition: Board2D.h:342
DGtal::JORDAN_DT
@ JORDAN_DT
Definition: DigitalTopology.h:57
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Domain
HyperRectDomain< Space > Domain
Definition: testSimpleRandomAccessRangeFromPoint.cpp:44
DGtal::Expander
Aim: This class is useful to visit an object by adjacencies, layer by layer.
Definition: Expander.h:97
DGtal::DigitalSetByAssociativeContainer::insert
void insert(const Point &p)
LibBoard::Board::saveSVG
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
DGtal::DrawableWithBoard2D
Definition: Common.h:221
DGtal::Board2D
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:70
testLayers
bool testLayers()
Definition: testSimpleExpander.cpp:209
DGtal::GradientColorMap::addColor
void addColor(const Color &color)
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::PointVector< dim, Integer >
DGtal::DigitalSetByAssociativeContainer::assignFromComplement
void assignFromComplement(const DigitalSetByAssociativeContainer< Domain, Container > &other_set)
DGtal::SetMode
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Definition: Board2D.h:247
DGtal::Object
Aim: An object (or digital object) represents a set in some digital space associated with a digital t...
Definition: Object.h:119
main
int main(int argc, char **argv)
Definition: testSimpleExpander.cpp:310
Point
MyPointD Point
Definition: testClone2.cpp:383
DGtal::GradientColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Definition: GradientColorMap.h:119
DGtal::DigitalSetByAssociativeContainer
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Definition: DigitalSetByAssociativeContainer.h:89