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

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

See also
Digital Voronoi Diagram Computation
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" );
typedef ExactPredicateLpSeparableMetric<Z2i::Space, 2> L2Metric;
Z2i::Point upper(16,16);
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);
typedef VoronoiMap<Z2i::Space, NotPredicate, L2Metric > Voronoi2D;
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");
typedef ExactPredicateLpSeparableMetric<Z2i::Space, 8> L8Metric;
L8Metric l8;
typedef VoronoiMap<Z2i::Space, NotPredicate, L8Metric > Voronoi2D_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");
typedef DistanceTransformation<Z2i::Space, NotPredicate, L2Metric > DT;
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;
}
// //
void beginBlock(const std::string &keyword="")
double endBlock()
MyDigitalSurface::ConstIterator ConstIterator
ExactPredicateLpSeparableMetric< Space, 2 > L2Metric
Definition: StdDefs.h:118
functors namespace gathers all DGtal functors.
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
int main(int argc, char **argv)
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
void draw(const Iterator &itb, const Iterator &ite, Board &aBoard)
HyperRectDomain< Space > Domain
Z2i::DigitalSet DigitalSet
Vector lower(const Vector &z, unsigned int k)
Vector upper(const Vector &z, unsigned int k)