DGtal  0.9.3beta
geometry/volumes/distance/voronoimap2D.cpp

Example of a Voronoi map construction in dimension 2 for the Euclidean metric.

See also
Digital Voronoi Diagram Computation
voronoimap-dt.png
Distance transformation for the l_2 metric.
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/kernel/BasicPointPredicates.h"
#include "DGtal/images/SimpleThresholdForegroundPredicate.h"
#include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
#include "DGtal/geometry/volumes/distance/VoronoiMap.h"
#include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
#include "DGtal/io/colormaps/HueShadeColorMap.h"
#include "DGtal/io/boards/Board2D.h"
using namespace std;
using namespace DGtal;
using namespace DGtal::functors;
int main( int /*argc*/, char** /*argv*/ )
{
trace.beginBlock ( "Example voronoimap2D" );
L2Metric l2;
Z2i::Point lower(0,0);
Z2i::Point upper(16,16);
Z2i::Domain domain(lower,upper);
Z2i::DigitalSet set(domain);
set.insertNew(Z2i::Point(2,3));
set.insertNew(Z2i::Point(7,15));
set.insertNew(Z2i::Point(12,5));
Board2D board;
board<< domain << set;
board.saveSVG("voronoimap-inputset.svg");
typedef NotPointPredicate<Z2i::DigitalSet> NotPredicate;
NotPredicate notSetPred(set);
Voronoi2D voronoimap(domain,notSetPred,l2);
board.clear();
board << domain;
for(Voronoi2D::Domain::ConstIterator it = voronoimap.domain().begin(),
itend = voronoimap.domain().end(); it != itend; ++it)
{
Voronoi2D::Value site = voronoimap( *it ); //closest site to (*it)
if (site != (*it))
Display2DFactory::draw( board, site - (*it), (*it)); //Draw an arrow
}
board.saveSVG("voronoimap-voro.svg");
board.clear();
for(Voronoi2D::Domain::ConstIterator it = voronoimap.domain().begin(),
itend = voronoimap.domain().end(); it != itend; ++it)
{
Voronoi2D::Value site = voronoimap( *it ); //closest site to (*it)
unsigned char c = (site[1]*13 + site[0] * 7) % 256; //basic hashfunction
board << CustomStyle( (*it).className(), new CustomColors(Color(c,c,c),Color(c,c,c)))
<< (*it);
}
board.saveSVG("voronoimap-cells.svg");
L8Metric l8;
Voronoi2D_l8 voronoimap_l8(domain,notSetPred,l8);
board.clear();
board << domain;
for(Voronoi2D_l8::Domain::ConstIterator it = voronoimap_l8.domain().begin(),
itend = voronoimap_l8.domain().end(); it != itend; ++it)
{
Voronoi2D::Value site = voronoimap_l8( *it ); //closest site to (*it)
unsigned char c = (site[1]*13 + site[0] * 7) % 256; //basic hashfunction
board << CustomStyle( (*it).className(), new CustomColors(Color(c,c,c),Color(c,c,c)))
<< (*it);
}
board.saveSVG("voronoimap-vorol8.svg");
DT dt(domain,notSetPred,l2);
board.clear();
board << domain;
//Fast max computation on the range value
DT::Value maxDT=0.0;
for(DT::ConstRange::ConstIterator it = dt.constRange().begin(), itend = dt.constRange().end();
it != itend ; ++it)
if ((*it)>maxDT) maxDT = (*it);
//Colormap
HueShadeColorMap<DT::Value,1> hueMap(0.0,maxDT);
//Drawing
for(DT::Domain::ConstIterator it = dt.domain().begin(),
itend = dt.domain().end(); it != itend; ++it)
{
DT::Value dist = dt( *it ); //distance to closest site to (*it)
board << CustomStyle( (*it).className(), new CustomColors( hueMap(dist), hueMap(dist)))
<< (*it);
}
board.saveSVG("voronoimap-dt.svg");
return 0;
}
// //