34#include "DGtal/base/Common.h"
36#include "DGtal/kernel/SpaceND.h"
37#include "DGtal/kernel/domains/HyperRectDomain.h"
38#include "DGtal/kernel/BasicPointPredicates.h"
39#include "DGtal/kernel/domains/DomainPredicate.h"
40#include "DGtal/kernel/sets/DigitalSetFromMap.h"
41#include "DGtal/images/ImageContainerBySTLMap.h"
42#include "DGtal/images/SimpleThresholdForegroundPredicate.h"
45#include "DGtal/images/ImageSelector.h"
46#include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
47#include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
50#include "DGtal/geometry/volumes/distance/FMM.h"
53#include "DGtal/io/colormaps/HueShadeColorMap.h"
54#include "DGtal/io/boards/Board2D.h"
57#include "DGtal/shapes/ShapeFactory.h"
58#include "DGtal/shapes/Shapes.h"
59#include "DGtal/topology/helpers/Surfaces.h"
60#include "DGtal/shapes/GaussDigitizer.h"
61#include "DGtal/geometry/curves/GridCurve.h"
71template <
typename TImage,
typename TSet,
int norm>
77template <
typename TImage,
typename TSet>
78struct DistanceTraits<TImage, TSet, 1>
85template <
typename TPo
int>
93 BallPredicate(
double aCx,
double aCy,
double aR ):
94 myCx(aCx), myCy(aCy), myR(aR)
97 bool operator()(
const TPoint&
aPoint)
const
99 double d = std::sqrt( std::pow( (myCx-
aPoint[0] ), 2)
100 + std::pow( (myCy-
aPoint[1] ), 2) );
101 if (d <= myR)
return true;
105 double myCx, myCy, myR;
108template <
typename TPo
int>
112 typedef TPoint
Point;
113 typedef double Value;
116 BallFunctor(
double aCx,
double aCy,
double aR ):
117 myCx(aCx), myCy(aCy), myR(aR)
118 { ASSERT(myR > 0); };
122 double d = std::sqrt( std::pow( (myCx-
aPoint[0] ), 2)
123 + std::pow( (myCy-
aPoint[1] ), 2) );
127 double myCx, myCy, myR;
131template<
typename TKSpace>
136 ASSERT( aR < (
double) size );
148 std::cerr <<
" error in creating KSpace." << std::endl;
152 BallPredicate<Point> dig(aCx, aCy, aR);
157 std::vector<Point> points;
163 std::cerr <<
" error in finding a bel." << std::endl;
167template<
typename TIterator >
168void draw(
const TIterator& itb,
const TIterator& ite,
const int& size, std::string basename)
170 typedef typename std::iterator_traits<TIterator>::value_type Pair;
171 typedef typename Pair::first_type
Point;
178 for ( ; it != ite; ++it)
186 s << basename <<
".eps";
187 b.saveEPS(s.str().c_str());
203 Domain d(Point::diagonal(-size), Point::diagonal(size));
208 Image map( d, (size*size) );
209 map.setValue( Point::diagonal(0), 0.0 );
217 FMM fmm(map, set, dp, area, distance);
225 s <<
"DTFrom2dPt-" << size <<
"-" << area <<
"-" << distance;
226 draw(map.begin(), map.end(), size, s.str());
241 Domain d(Point::diagonal(-size), Point::diagonal(size));
242 double h = 1.0/(double)size;
245 int radius = (size/2);
246 typedef BallPredicate<Point> Predicate;
247 Predicate predicate( 0, 0, radius );
250 trace.
info() <<
" # circle of radius 0.5 "
251 <<
"digitized in a square of size 1 "
252 <<
"at step h=" << h << endl;
256 KSpace K;
K.
init( Point::diagonal(-size), Point::diagonal(size),
true);
259 std::vector<KSpace::SCell> vSCells;
262 double diff1, diff2, diff3 = 0.0;
277 vSCells.begin(), vSCells.end(),
283 FMM fmm(map, set, predicate);
289 double truth = radius*h;
290 double found = ( std::max(std::abs(fmm.
max()),std::abs(fmm.
min())) )*h;
291 double diff = std::abs(found-truth);
292 trace.
info() <<
" # radius (low accuracy)" << std::endl;
293 trace.
info() <<
" # truth: " << truth << std::endl;
294 trace.
info() <<
" # found: " << found << std::endl;
295 trace.
info() <<
" # diff.: " << diff << std::endl;
310 typedef BallFunctor<Point>
Functor;
311 Functor functor( 0, 0, radius );
314 vSCells.begin(), vSCells.end(),
319 FMM fmm(map, set, predicate);
324 double truth = radius*h;
325 double found = ( std::max(std::abs(fmm.
max()),std::abs(fmm.
min())) )*h;
326 double diff = std::abs(found-truth);
327 trace.
info() <<
" # radius (medium accuracy)" << std::endl;
328 trace.
info() <<
" # truth: " << truth << std::endl;
329 trace.
info() <<
" # found: " << found << std::endl;
330 trace.
info() <<
" # diff.: " << diff << std::endl;
348 typedef BallFunctor<Point>
Functor;
349 Functor functor( 0, 0, radius );
352 vSCells.begin(), vSCells.end(),
356 Distance distance(map, set);
357 FMM fmm(map, set, predicate, distance);
362 double truth = radius*h;
363 double found = ( std::max(std::abs(fmm.
max()),std::abs(fmm.
min())) )*h;
364 double diff = std::abs(found-truth);
365 trace.
info() <<
" # radius (high accuracy)" << std::endl;
366 trace.
info() <<
" # truth: " << truth << std::endl;
367 trace.
info() <<
" # found: " << found << std::endl;
368 trace.
info() <<
" # diff.: " << diff << std::endl;
375 return ( (diff1 >= diff2)&&(diff2 >= diff3) );
390 Domain d(Point::diagonal(-size), Point::diagonal(size));
396 map.setValue( Point::diagonal(0), 0.0 );
404 FMM fmm(map, set, dp, area, distance);
417 for ( ; it != d.
end(); ++it)
430 s <<
"DTFrom3dPt-" << size <<
"-" << area <<
"-" << distance
432 b.saveEPS(s.str().c_str());
446 Domain d(Point::diagonal(-size), Point::diagonal(size));
456 double radius = (rand()%size);
457 trace.
info() <<
" #ball c(" << 0 <<
"," << 0 <<
") r=" << radius << endl;
461 unsigned int nbok = 0;
467 typedef BallPredicate<Point> Predicate;
477 Predicate bp(0,0,radius);
478 FMM fmm(map, set, bp);
482 trace.
info() << nbok <<
"/" << ++nb << std::endl;
489 s <<
"DTInCircle-" << size;
490 draw(map.begin(), map.end(), size, s.str());
510 BallPredicate<Point> bp(0,0,radius);
511 PointPredicate nbp( bp );
512 Predicate pred( nbp, dp,
andBF2 );
513 FMM fmm(map, set, pred);
517 trace.
info() << nbok <<
"/" << ++nb << std::endl;
524 s <<
"DTOutCircle-" << size;
525 draw(map.begin(), map.end(), size, s.str());
543 FMM fmm(map, set, dp);
547 trace.
info() << nbok <<
"/" << ++nb << std::endl;
555 s <<
"DTfromCircle-" << size;
556 draw(map.begin(), map.end(), size, s.str());
562 double epsilon = 0.0001;
563 nbok += ( ( (std::abs(-dmaxInt - dmin) < epsilon)
564 && (std::abs(dmaxExt - dmax) < epsilon) )?1:0);
565 trace.
info() << nbok <<
"/" << ++nb << std::endl;
578template<Dimension dim,
int norm>
587 Domain d(Point::diagonal(-size), Point::diagonal(size));
593 map.setValue( Point::diagonal(0), 0);
596 set.insert( Point::diagonal(0) );
599 typedef Image Image2;
603 for ( ; dit != ditEnd; ++dit)
605 image.setValue(*dit, 128);
607 image.setValue(Point::diagonal(0), 0);
614 Distance distance(map, set);
615 FMM fmm( map, set, dp, area, dist, distance );
623 Predicate aPredicate(image,0);
627 DT
dt(&d,&aPredicate, &lnorm);
632 bool flagIsOk =
true;
638 for ( ; ( (it != itEnd)&&(flagIsOk) ); ++it)
640 if (set.find(*it) == set.end())
644 if (
dt(*it) != map(*it))
659int main (
int argc,
char** argv )
663 for (
int i = 0; i < argc; ++i )
669 int area = int( std::pow(
double(2*size+1),2) )+1;
679 area = 4*int( std::pow(
double(size),3) );
686 area = int( std::pow(
double(2*size+1),3) )+1;
691 area = int( std::pow(
double(2*size+1),4) ) + 1;
697 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
Aim: A container class for storing sets of digital points within some given domain.
Aim: An adapter for viewing an associative image container like ImageContainerBySTLMap as a simple di...
Aim: implements separable l_p metrics with exact predicates.
Aim: Fast Marching Method (FMM) for nd distance transforms.
static void initFromIncidentPointsRange(const TIteratorOnPairs &itb, const TIteratorOnPairs &ite, Image &aImg, AcceptedPointSet &aSet, const Value &aValue, bool aFlagIsPositive=true)
static void initFromBelsRange(const KSpace &aK, const TIteratorOnBels &itb, const TIteratorOnBels &ite, Image &aImg, AcceptedPointSet &aSet, const Value &aValue, bool aFlagIsPositive=true)
static void initFromPointsRange(const TIteratorOnPoints &itb, const TIteratorOnPoints &ite, Image &aImg, AcceptedPointSet &aSet, const Value &aValue)
Aim: describes, in a cellular space of dimension n, a closed or open sequence of signed d-cells (or d...
IncidentPointsRange getIncidentPointsRange() const
OuterPointsRange getOuterPointsRange() const
InnerPointsRange getInnerPointsRange() const
bool initFromVector(const std::vector< Point > &aVectorOfPoints)
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Iterator for HyperRectDomain.
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
const ConstIterator & begin() const
const ConstIterator & end() const
Aim: implements association bewteen points lying in a digital domain and values.
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Aim: Class for the computation of the L1-distance at some point p, from the available distance values...
Aim: Class for the computation of the Euclidean distance at some point p, from the available distance...
Aim: Class for the computation of the LInf-distance at some point p, from the available distance valu...
std::string className() const
static void track2DBoundaryPoints(std::vector< Point > &aVectorOfPoints, const KSpace &K, const SurfelAdjacency< KSpace::dimension > &surfel_adj, const PointPredicate &pp, const SCell &start_surfel)
static SCell findABel(const KSpace &K, const PointPredicate &pp, unsigned int nbtries=1000)
static void track2DBoundary(std::vector< SCell > &aSCellContour2D, const KSpace &K, const SurfelAdjacency< KSpace::dimension > &surfel_adj, const PointPredicate &pp, const SCell &start_surfel)
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
void beginBlock(const std::string &keyword="")
Aim: Define a simple Foreground predicate thresholding image values given a single thresold....
functors namespace gathers all DGtal functors.
static const BoolFunctor2 andBF2
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Custom style class redefining the fill color. You may use Board2D::Color::None for transparent color.
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Aim: The predicate returns true when the given binary functor returns true for the two PointPredicate...
Aim: The predicate returning true iff the point is in the domain given at construction....
Aim: The predicate returns true when the point predicate given at construction return false....
InHalfPlaneBySimple3x3Matrix< Point, double > Functor
bool testComparison(int size, int area, double dist)
bool accuracyTest(int size)
bool testDisplayDT3d(int size, int area, double distance)
bool testDisplayDT2d(int size, int area, double distance)
void draw(const TIterator &itb, const TIterator &ite, const int &size, std::string basename)
bool testDisplayDTFromCircle(int size)
void ballGenerator(const int &size, double aCx, double aCy, double aR, GridCurve< TKSpace > &gc)
ImageContainerBySTLVector< Domain, Value > Image
HyperRectDomain< Space > Domain