35#include "DGtal/base/Common.h"
36#include "DGtal/helpers/StdDefs.h"
37#include "DGtal/images/CConstImage.h"
38#include "DGtal/geometry/volumes/distance/VoronoiMap.h"
39#include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
40#include "DGtal/geometry/volumes/distance/InexactPredicateLpSeparableMetric.h"
41#include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
42#include "DGtal/kernel/BasicPointPredicates.h"
43#include "DGtal/io/boards/Board2D.h"
44#include "DGtal/io/colormaps/HueShadeColorMap.h"
54template<
typename Po
int>
58 for(
unsigned int i=0; i< Point::dimension; i++)
59 res += std::pow ( (
double)abs(point[i]) , p);
61 return std::pow(res, 1.0/p);
64template <
typename VoroMap>
65void saveVoroMap(
const std::string &filename,
const VoroMap &output,
const double p)
69 const double maxdt =
mynorm( output.domain().upperBound() - output.domain().lowerBound(), p );
74 for(
typename VoroMap::Domain::ConstIterator it = output.domain().begin(),
75 itend = output.domain().end();
78 typename VoroMap::Value point = output(*it);
80 hue(
mynorm(point- (*it),p))))
84 board.
saveSVG(filename.c_str());
95template <
typename Po
int,
typename Domain >
98 auto const & lowerBound = aDomain.
lowerBound();
99 auto const & upperBound = aDomain.
upperBound();
101 for ( std::size_t i = 0; i < Domain::dimension; ++i )
102 aPoint[i] = (
aPoint[i] - 2*lowerBound[i] + upperBound[i] + 1 ) % ( upperBound[i] - lowerBound[i] + 1 ) + lowerBound[i];
117template < std::
size_t N >
120 std::array<bool, N> periodicity;
121 for ( std::size_t i = 0, mask = 1 ; i < N ; ++i, mask *= 2 )
122 periodicity[i] = anInteger & mask;
133template < std::
size_t N >
137 for ( std::size_t i = 0; i < N; ++i )
138 str += aPeriodicity[i] ?
'1' :
'0';
145template <
typename Set,
typename Voro>
148 typedef typename Voro::Point
Point;
151 Point const extent = voro.domain().upperBound() - voro.domain().lowerBound() + Point::diagonal();
154 std::vector< typename Voro::PeriodicitySpec > periodicShift;
155 for ( std::size_t i = 0; i < ( 1u << Voro::Space::dimension ); ++i )
157 const auto periodicity = getPeriodicityFromInteger< Voro::Space::dimension >( i );
161 for (
size_t j = 0; j < periodicity.size(); ++j )
162 if ( periodicity[j] && ! voro.isPeriodic(j) )
169 periodicShift.push_back( periodicity );
173 for (
auto const& pt : voro.domain() )
176 const Point psite = voro(pt);
177 const auto dist = voro.metric()->rawDistance( pt, psite );
181 if ( std::find( aSet.begin(), aSet.end(), voro.projectPoint( psite ) ) == aSet.end() )
184 <<
" projected to " << voro.projectPoint( psite )
185 <<
" is not a valid site." << std::endl;
190 for (
auto site : aSet )
193 for (
auto const & periodicity : periodicShift )
195 auto currSite = site;
198 for ( std::size_t
dim = 0;
dim < Voro::Space::dimension ; ++
dim )
199 if ( periodicity[
dim] )
200 currSite[
dim] += ( pt[
dim] < currSite[
dim] ? -1 : 1 ) * extent[
dim];
203 const auto dbis = voro.metric()->rawDistance( pt, currSite );
207 <<
" Voro:" << psite <<
" (" << dist <<
")"
208 <<
" from set:" << site <<
"(" << dbis <<
")"
209 <<
" projected from " << currSite <<
"." << std::endl;
232bool testVoronoiMap( std::array<bool, 2>
const& periodicity = { {
false,
false} } )
234 unsigned int nbok = 0;
248 mySet.insertNew( *it );
266 Voro2 voro(&
domain, &mySet, &l2, periodicity);
268 for(
int j=-10; j <= 10; j++)
270 for(
int i=-10; i<=10; i++)
275 trace.
info()<<
"Exporting to SVG"<<std::endl;
278 for(Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(),
279 itend = voro.domain().end();
283 const unsigned char c = ( p[1]*13 + p[0] * 7) % 256;
306template<
typename Set>
308 const std::string &name,
309 std::array<bool, 2>
const& periodicity = { {
false,
false} } )
311 unsigned int nbok = 0;
314 Set mySet(aSet.domain());
316 for(
typename Set::Domain::ConstIterator it = aSet.domain().begin(), itend = aSet.domain().end();
319 mySet.insertNew( *it );
322 for(
typename Set::ConstIterator it = aSet.begin(), itend = aSet.end();
331 Voro2 voro(aSet.domain(), mySet, l2, periodicity );
339 Voro6 voro6( aSet.domain(), mySet, l6, periodicity );
346 DT dt( aSet.domain(), mySet, l2, periodicity );
350 if ( (aSet.domain().upperBound()[1] - aSet.domain().lowerBound()[1]) <20)
352 for(
int j= aSet.domain().lowerBound()[1]; j <= aSet.domain().upperBound()[1]; j++)
354 for(
int i=aSet.domain().lowerBound()[0]; i<=aSet.domain().upperBound()[0]; i++)
355 if ( aSet.find(
Z2i::Point(i,j) ) != aSet.end() )
364 for(
int j= aSet.domain().lowerBound()[1]; j <= aSet.domain().upperBound()[1]; j++)
366 for(
int i=aSet.domain().lowerBound()[0]; i<=aSet.domain().upperBound()[0]; i++)
373 board << voro.domain();
374 for(
typename Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(), itend = voro.domain().end();
380 std::string filename =
"Voromap-" + name +
"-orig." +
formatPeriodicity(periodicity) +
".svg";
381 board.
saveSVG(filename.c_str());
384 board << voro.domain();
386 for(
typename Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(), itend = voro.domain().end();
391 && (p != voro.domain().lowerBound()))
395 filename =
"Voromap-" + name +
"-diag." +
formatPeriodicity(periodicity) +
".svg";
396 board.
saveSVG(filename.c_str());
400 board << voro.domain();
401 for(
typename Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(), itend = voro.domain().end();
405 unsigned char c = (p[1]*13 + p[0] * 7) % 256;
411 board.
saveSVG(filename.c_str());
413 filename =
"Voromap-" + name +
"-hue." +
formatPeriodicity(periodicity) +
".svg";
418 for(
typename Voro6::OutputImage::Domain::ConstIterator it = voro6.domain().begin(),
419 itend = voro6.domain().end();
427 filename =
"Voromap-diag-l6-" + name +
"." +
formatPeriodicity(periodicity) +
".svg";
428 board.
saveSVG(filename.c_str());
431 for(
typename Voro6::OutputImage::Domain::ConstIterator it = voro6.domain().begin(), itend = voro6.domain().end();
435 unsigned char c = (p[1]*13 + p[0] * 7) % 256;
441 board.
saveSVG(filename.c_str());
443 filename =
"Voromap-hue-l6-" + name +
"." +
formatPeriodicity(periodicity) +
".svg";
464template<
typename Set>
467 std::array<bool, Set::dimension> periodicity;
468 periodicity.fill(
false );
472template<
typename Set>
475 unsigned int nbok = 0;
478 Set mySet(aSet.domain());
480 for(
typename Set::Domain::ConstIterator it = aSet.domain().begin(),
481 itend = aSet.domain().end();
484 mySet.insertNew( *it );
487 for(
typename Set::ConstIterator it = aSet.begin(), itend = aSet.end();
497 Voro2 voro(aSet.domain(), mySet, l2, periodicity);
509 Voro3 voro3(aSet.domain(), mySet, l3, periodicity);
519 DT dt(aSet.domain(), mySet, l2, periodicity);
540 for ( std::size_t i = 0; i < 4; ++i )
542 auto const periodicity = getPeriodicityFromInteger<2>(i);
562 for(
unsigned int i = 0 ; i < size; ++i)
564 Z2i::Point p( rand() % (b[0]) - a[0], rand() % (b[1]) + a[1] );
568 for ( std::size_t i = 0; i < 4; ++i )
570 auto const periodicity = getPeriodicityFromInteger<2>(i);
576 for(
unsigned int i = 0 ; i < size*size-size; ++i)
578 Z2i::Point p( rand() % (b[0]) - a[0], rand() % (b[1]) + a[1] );
582 for ( std::size_t i = 0; i < 4; ++i )
584 auto const periodicity = getPeriodicityFromInteger<2>(i);
608 for (
auto i = 0; i < 8; ++i )
610 auto const periodicity = getPeriodicityFromInteger<3>(i);
621 std::size_t
const N = 16;
630 for(
unsigned int i = 0 ; i < N; ++i)
633 rand() % (b[1]) + a[1],
634 rand() % (b[2]) + a[2] );
638 for ( std::size_t i = 0; i < 8; ++i )
640 auto const periodicity = getPeriodicityFromInteger<3>(i);
655 Space4::Point a(0,0,0,0);
656 Space4::Point b(5,5,5,5);
662 sites.
insertNew( Space4::Point(1,4,1,1));
663 sites.
insertNew( Space4::Point(3,1,3,1));
664 sites.
insertNew( Space4::Point(0,0,0,0));
666 for ( std::size_t i = 0; i < 16; ++i )
668 auto const periodicity = getPeriodicityFromInteger<4>(i);
681int main(
int argc,
char** argv )
685 for (
int i = 0; i < argc; ++i )
701 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Structure representing an RGB triple with alpha component.
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
void insertNew(const Point &p)
void insert(const Point &p)
Container::const_iterator ConstIterator
ConstIterator type of the container;.
Aim: A container class for storing sets of digital points within some given domain.
void insertNew(const Point &p)
Aim: implements separable l_p metrics with exact predicates.
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Iterator for HyperRectDomain.
const ConstIterator & begin() const
const Point & lowerBound() const
const Point & upperBound() const
const ConstIterator & end() const
static Self diagonal(Component val=1)
void beginBlock(const std::string &keyword="")
Aim: Implementation of the linear in time Voronoi map construction.
Board & setPenColor(const DGtal::Color &color)
void clear(const DGtal::Color &color=DGtal::Color::None)
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
ExactPredicateLpSeparableMetric< Space, 2 > L2Metric
DGtal is the top-level namespace which contains all DGtal functions and types.
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
static void draw(DGtal::Board2D &board, const DGtal::DiscreteExteriorCalculus< dimEmbedded, dimAmbient, TLinearAlgebraBackend, TInteger > &calculus)
Aim: Defines the concept describing a read-only image, which is a refinement of CPointFunctor.
double mynorm(const Point &point, const double p)
Point calcPointModuloDomain(Point aPoint, Domain const &aDomain)
bool testVoronoiMap(std::array< bool, 2 > const &periodicity={ {false, false} })
bool testSimpleRandom3D()
std::string formatPeriodicity(std::array< bool, N > const &aPeriodicity)
std::array< bool, N > getPeriodicityFromInteger(std::size_t anInteger)
void saveVoroMap(const std::string &filename, const VoroMap &output, const double p)
bool testVoronoiMapFromSites(const Set &aSet)
bool testVoronoiMapFromSites2D(const Set &aSet, const std::string &name, std::array< bool, 2 > const &periodicity={ {false, false} })
bool testSimpleRandom2D()
bool checkVoronoi(const Set &aSet, const Voro &voro)