34#include "DGtal/base/Common.h"
35#include "DGtal/helpers/StdDefs.h"
36#include "DGtal/images/CConstImage.h"
37#include "DGtal/geometry/volumes/distance/VoronoiMap.h"
38#include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
39#include "DGtal/geometry/volumes/distance/InexactPredicateLpSeparableMetric.h"
40#include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
41#include "DGtal/kernel/BasicPointPredicates.h"
42#include "DGtal/io/boards/Board2D.h"
43#include "DGtal/io/colormaps/HueShadeColorMap.h"
53template<
typename Po
int>
57 for(
unsigned int i=0; i< Point::dimension; i++)
58 res += std::pow ( (
double)abs(point[i]) , p);
60 return std::pow(res, 1.0/p);
63template <
typename VoroMap>
64void saveVoroMap(
const std::string &filename,
const VoroMap &output,
const double p)
68 const double maxdt =
mynorm( output.domain().upperBound() - output.domain().lowerBound(), p );
73 for(
typename VoroMap::Domain::ConstIterator it = output.domain().begin(),
74 itend = output.domain().end();
77 typename VoroMap::Value point = output(*it);
79 hue(
mynorm(point- (*it),p))))
83 board.
saveSVG(filename.c_str());
94template <
typename Po
int,
typename Domain >
97 auto const & lowerBound = aDomain.
lowerBound();
98 auto const & upperBound = aDomain.
upperBound();
101 aPoint[i] = (
aPoint[i] - 2*lowerBound[i] + upperBound[i] + 1 ) % ( upperBound[i] - lowerBound[i] + 1 ) + lowerBound[i];
116template < std::
size_t N >
119 std::array<bool, N> periodicity;
120 for ( std::size_t i = 0, mask = 1 ; i < N ; ++i, mask *= 2 )
121 periodicity[i] = anInteger & mask;
132template < std::
size_t N >
136 for ( std::size_t i = 0; i < N; ++i )
137 str += aPeriodicity[i] ?
'1' :
'0';
144template <
typename Set,
typename Voro>
147 typedef typename Voro::Point
Point;
150 Point const extent = voro.domain().upperBound() - voro.domain().lowerBound() + Point::diagonal();
153 std::vector< typename Voro::PeriodicitySpec > periodicShift;
154 for ( std::size_t i = 0; i < ( 1u << Voro::Space::dimension ); ++i )
160 for (
typename Voro::Space::Dimension j = 0; j < periodicity.size(); ++j )
161 if ( periodicity[j] && ! voro.isPeriodic(j) )
168 periodicShift.push_back( periodicity );
172 for (
auto const& pt : voro.domain() )
175 const Point psite = voro(pt);
176 const auto dist = voro.metric()->rawDistance( pt, psite );
180 if ( std::find( aSet.begin(), aSet.end(), voro.projectPoint( psite ) ) == aSet.end() )
183 <<
" projected to " << voro.projectPoint( psite )
184 <<
" is not a valid site." << std::endl;
189 for (
auto site : aSet )
192 for (
auto const & periodicity : periodicShift )
194 auto currSite = site;
197 for (
typename Voro::Space::Dimension
dim = 0;
dim < Voro::Space::dimension ; ++
dim )
198 if ( periodicity[
dim] )
199 currSite[
dim] += ( pt[
dim] < currSite[
dim] ? -1 : 1 ) * extent[
dim];
202 const auto dbis = voro.metric()->rawDistance( pt, currSite );
206 <<
" Voro:" << psite <<
" (" << dist <<
")"
207 <<
" from set:" << site <<
"(" << dbis <<
")"
208 <<
" projected from " << currSite <<
"." << std::endl;
231bool testVoronoiMap( std::array<bool, 2>
const& periodicity = { {
false,
false} } )
233 unsigned int nbok = 0;
247 mySet.insertNew( *it );
265 Voro2 voro(&
domain, &mySet, &l2, periodicity);
267 for(
int j=-10; j <= 10; j++)
269 for(
int i=-10; i<=10; i++)
274 trace.
info()<<
"Exporting to SVG"<<std::endl;
277 for(Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(),
278 itend = voro.domain().end();
282 const unsigned char c = ( p[1]*13 + p[0] * 7) % 256;
305template<
typename Set>
307 const std::string &name,
308 std::array<bool, 2>
const& periodicity = { {
false,
false} } )
310 unsigned int nbok = 0;
313 Set mySet(aSet.domain());
315 for(
typename Set::Domain::ConstIterator it = aSet.domain().begin(), itend = aSet.domain().end();
318 mySet.insertNew( *it );
321 for(
typename Set::ConstIterator it = aSet.begin(), itend = aSet.end();
330 Voro2 voro(aSet.domain(), mySet, l2, periodicity );
338 Voro6 voro6( aSet.domain(), mySet, l6, periodicity );
345 DT
dt( aSet.domain(), mySet, l2, periodicity );
349 if ( (aSet.domain().upperBound()[1] - aSet.domain().lowerBound()[1]) <20)
351 for(
int j= aSet.domain().lowerBound()[1]; j <= aSet.domain().upperBound()[1]; j++)
353 for(
int i=aSet.domain().lowerBound()[0]; i<=aSet.domain().upperBound()[0]; i++)
354 if ( aSet.find(
Z2i::Point(i,j) ) != aSet.end() )
363 for(
int j= aSet.domain().lowerBound()[1]; j <= aSet.domain().upperBound()[1]; j++)
365 for(
int i=aSet.domain().lowerBound()[0]; i<=aSet.domain().upperBound()[0]; i++)
372 board << voro.domain();
373 for(
typename Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(), itend = voro.domain().end();
379 std::string filename =
"Voromap-" + name +
"-orig." +
formatPeriodicity(periodicity) +
".svg";
380 board.
saveSVG(filename.c_str());
383 board << voro.domain();
385 for(
typename Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(), itend = voro.domain().end();
390 && (p != voro.domain().lowerBound()))
394 filename =
"Voromap-" + name +
"-diag." +
formatPeriodicity(periodicity) +
".svg";
395 board.
saveSVG(filename.c_str());
399 board << voro.domain();
400 for(
typename Voro2::OutputImage::Domain::ConstIterator it = voro.domain().begin(), itend = voro.domain().end();
404 unsigned char c = (p[1]*13 + p[0] * 7) % 256;
410 board.
saveSVG(filename.c_str());
412 filename =
"Voromap-" + name +
"-hue." +
formatPeriodicity(periodicity) +
".svg";
417 for(
typename Voro6::OutputImage::Domain::ConstIterator it = voro6.domain().begin(),
418 itend = voro6.domain().end();
426 filename =
"Voromap-diag-l6-" + name +
"." +
formatPeriodicity(periodicity) +
".svg";
427 board.
saveSVG(filename.c_str());
430 for(
typename Voro6::OutputImage::Domain::ConstIterator it = voro6.domain().begin(), itend = voro6.domain().end();
434 unsigned char c = (p[1]*13 + p[0] * 7) % 256;
440 board.
saveSVG(filename.c_str());
442 filename =
"Voromap-hue-l6-" + name +
"." +
formatPeriodicity(periodicity) +
".svg";
463template<
typename Set>
466 std::array<bool, Set::dimension> periodicity;
467 periodicity.fill(
false );
471template<
typename Set>
474 unsigned int nbok = 0;
477 Set mySet(aSet.domain());
479 for(
typename Set::Domain::ConstIterator it = aSet.domain().begin(),
480 itend = aSet.domain().end();
483 mySet.insertNew( *it );
486 for(
typename Set::ConstIterator it = aSet.begin(), itend = aSet.end();
496 Voro2 voro(aSet.domain(), mySet, l2, periodicity);
508 Voro3 voro3(aSet.domain(), mySet, l3, periodicity);
518 DT
dt(aSet.domain(), mySet, l2, periodicity);
539 for ( std::size_t i = 0; i < 4; ++i )
561 for(
unsigned int i = 0 ; i < size; ++i)
563 Z2i::Point p( rand() % (b[0]) - a[0], rand() % (b[1]) + a[1] );
567 for ( std::size_t i = 0; i < 4; ++i )
575 for(
unsigned int i = 0 ; i < size*size-size; ++i)
577 Z2i::Point p( rand() % (b[0]) - a[0], rand() % (b[1]) + a[1] );
581 for ( std::size_t i = 0; i < 4; ++i )
607 for (
auto i = 0; i < 8; ++i )
620 std::size_t
const N = 16;
629 for(
unsigned int i = 0 ; i < N; ++i)
632 rand() % (b[1]) + a[1],
633 rand() % (b[2]) + a[2] );
637 for ( std::size_t i = 0; i < 8; ++i )
654 Space4::Point a(0,0,0,0);
655 Space4::Point b(5,5,5,5);
661 sites.
insertNew( Space4::Point(1,4,1,1));
662 sites.
insertNew( Space4::Point(3,1,3,1));
663 sites.
insertNew( Space4::Point(0,0,0,0));
665 for ( std::size_t i = 0; i < 16; ++i )
680int main(
int argc,
char** argv )
684 for (
int i = 0; i < argc; ++i )
700 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
Space::Dimension Dimension
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)