DGtal  0.9.3beta
voronoimap2D.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
36 #include "DGtal/kernel/BasicPointPredicates.h"
37 #include "DGtal/images/SimpleThresholdForegroundPredicate.h"
38 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
39 #include "DGtal/geometry/volumes/distance/VoronoiMap.h"
40 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
41 
42 #include "DGtal/io/colormaps/HueShadeColorMap.h"
43 #include "DGtal/io/boards/Board2D.h"
45 
47 
48 using namespace std;
49 using namespace DGtal;
50 using namespace DGtal::functors;
51 
53 
54 int main( int /*argc*/, char** /*argv*/ )
55 {
56  trace.beginBlock ( "Example voronoimap2D" );
57 
60  L2Metric l2;
62 
64  Z2i::Point lower(0,0);
65  Z2i::Point upper(16,16);
66  Z2i::Domain domain(lower,upper);
67 
68  Z2i::DigitalSet set(domain);
69  set.insertNew(Z2i::Point(2,3));
70  set.insertNew(Z2i::Point(7,15));
71  set.insertNew(Z2i::Point(12,5));
72  Board2D board;
73 
74  board<< domain << set;
75  board.saveSVG("voronoimap-inputset.svg");
77 
79  typedef NotPointPredicate<Z2i::DigitalSet> NotPredicate;
80  NotPredicate notSetPred(set);
82 
85  Voronoi2D voronoimap(domain,notSetPred,l2);
87 
89  board.clear();
90  board << domain;
91  for(Voronoi2D::Domain::ConstIterator it = voronoimap.domain().begin(),
92  itend = voronoimap.domain().end(); it != itend; ++it)
93  {
94  Voronoi2D::Value site = voronoimap( *it ); //closest site to (*it)
95  if (site != (*it))
96  Display2DFactory::draw( board, site - (*it), (*it)); //Draw an arrow
97  }
98  board.saveSVG("voronoimap-voro.svg");
100 
102  board.clear();
103  for(Voronoi2D::Domain::ConstIterator it = voronoimap.domain().begin(),
104  itend = voronoimap.domain().end(); it != itend; ++it)
105  {
106  Voronoi2D::Value site = voronoimap( *it ); //closest site to (*it)
107  unsigned char c = (site[1]*13 + site[0] * 7) % 256; //basic hashfunction
108  board << CustomStyle( (*it).className(), new CustomColors(Color(c,c,c),Color(c,c,c)))
109  << (*it);
110  }
111  board.saveSVG("voronoimap-cells.svg");
113 
114 
117  L8Metric l8;
119  Voronoi2D_l8 voronoimap_l8(domain,notSetPred,l8);
120  board.clear();
121  board << domain;
122  for(Voronoi2D_l8::Domain::ConstIterator it = voronoimap_l8.domain().begin(),
123  itend = voronoimap_l8.domain().end(); it != itend; ++it)
124  {
125  Voronoi2D::Value site = voronoimap_l8( *it ); //closest site to (*it)
126  unsigned char c = (site[1]*13 + site[0] * 7) % 256; //basic hashfunction
127  board << CustomStyle( (*it).className(), new CustomColors(Color(c,c,c),Color(c,c,c)))
128  << (*it);
129  }
130  board.saveSVG("voronoimap-vorol8.svg");
132 
135  DT dt(domain,notSetPred,l2);
136  board.clear();
137  board << domain;
138 
139  //Fast max computation on the range value
140  DT::Value maxDT=0.0;
141  for(DT::ConstRange::ConstIterator it = dt.constRange().begin(), itend = dt.constRange().end();
142  it != itend ; ++it)
143  if ((*it)>maxDT) maxDT = (*it);
144 
145  //Colormap
146  HueShadeColorMap<DT::Value,1> hueMap(0.0,maxDT);
147 
148  //Drawing
149  for(DT::Domain::ConstIterator it = dt.domain().begin(),
150  itend = dt.domain().end(); it != itend; ++it)
151  {
152  DT::Value dist = dt( *it ); //distance to closest site to (*it)
153  board << CustomStyle( (*it).className(), new CustomColors( hueMap(dist), hueMap(dist)))
154  << (*it);
155  }
156  board.saveSVG("voronoimap-dt.svg");
158 
159 
160  trace.endBlock();
161  return 0;
162 }
163 // //
void beginBlock(const std::string &keyword="")
Aim: The predicate returns true when the point predicate given at construction return false...
Aim: Implementation of the linear in time Voronoi map construction.
Definition: VoronoiMap.h:126
Aim: Implementation of the linear in time distance transformation for separable metrics.
Trace trace
Definition: Common.h:137
ExactPredicateLpSeparableMetric< Space, 2 > L2Metric
Definition: StdDefs.h:118
STL namespace.
double endBlock()
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
functors namespace gathers all DGtal functors.
Aim: implements separable l_p metrics with exact predicates.
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...
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70