DGtal  0.9.2
testDistanceTransformationND.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/base/Common.h"
34 #include "DGtal/kernel/SpaceND.h"
35 #include "DGtal/kernel/domains/HyperRectDomain.h"
36 #include "DGtal/images/ImageSelector.h"
37 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
38 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
39 #include "DGtal/io/colormaps/HueShadeColorMap.h"
40 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
41 #include "DGtal/io/boards/Board2D.h"
42 #include "DGtal/images/SimpleThresholdForegroundPredicate.h"
44 
45 using namespace std;
46 using namespace DGtal;
47 using namespace DGtal::functors;
48 
50 // Functions for testing class DistanceTransformND.
52 
56 bool testDistanceTransformND()
57 {
58  unsigned int nbok = 0;
59  unsigned int nb = 0;
60 
61  trace.beginBlock ( "Testing dT dim=5 ..." );
62 
63  typedef SpaceND<5> TSpace;
64  typedef TSpace::Point Point;
66  TSpace::Integer t[5] = {0,0,0,0,0};
67  Point a ( t );
68  TSpace::Integer t2[5] = {15,15,15,15,15};
69  Point b ( t2 );
70  TSpace::Integer t3[5] = {3,3,3,3,3};
71  Point c ( t3 );
72  Point d;
73 
75  Domain domain(a,b);
76  Image image(domain);
77 
78  //We create an object image with a signle background point (set to 0)
79  for (Image::Iterator it=image.begin(),itend=image.end(); it!=itend; ++it)
80  *it = 128;
81  image.setValue( c , 0 );
82 
84  Predicate aPredicate(image,0);
85 
87  L2Metric l2;
88  DistanceTransformation<TSpace,Predicate,L2Metric> dt(&domain,&aPredicate, &l2 );
89 
90  //We check the result
91  bool res=true;
92  for(Domain::ConstIterator itDom = domain.begin(), itDomend = domain.end();
93  itDom != itDomend; ++itDom)
94  {
95  //distance from the point to the seed
96  d = (*itDom) - c;
97  L2Metric::RawValue norm2=0;
98  for(Point::Iterator itd=d.begin(), itdend=d.end(); itd!=itdend; ++itd)
99  norm2+= (*itd)*(*itd);
100 
101  if ( dt.metric()->rawDistance( (*itDom), dt.getVoronoiVector(*itDom) ) != norm2)
102  {
103  trace.error()<<"Error at "<<(*itDom)
104  << ": expected="<<norm2<<" and computed="
105  <<dt.metric()->rawDistance( (*itDom), dt.getVoronoiVector(*itDom) )
106  <<endl;
107  res=false;
108  }
109  }
110  nbok += res ? 1 : 0;
111  nb++;
112  trace.info() << "(" << nbok << "/" << nb << ") "
113  << "true == true" << std::endl;
114  trace.endBlock();
115 
116  return nbok == nb;
117 }
118 
120 // Standard services - public :
121 
122 int main( int argc, char** argv )
123 {
124  trace.beginBlock ( "Testing class DistanceTransformND" );
125  trace.info() << "Args:";
126  for ( int i = 0; i < argc; ++i )
127  trace.info() << " " << argv[ i ];
128  trace.info() << endl;
129 
130  bool res = testDistanceTransformND(); // && ... other tests
131  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
132  trace.endBlock();
133  return res ? 0 : 1;
134 }
135 // //
void beginBlock(const std::string &keyword="")
const ConstIterator & begin() const
Aim: Implementation of the linear in time distance transformation for separable metrics.
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
const ConstIterator & end() const
Trace trace
Definition: Common.h:130
ExactPredicateLpSeparableMetric< Space, 2 > L2Metric
Definition: StdDefs.h:118
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
STL namespace.
double endBlock()
functors namespace gathers all DGtal functors.
Aim: implements separable l_p metrics with exact predicates.
Aim: Define a simple Foreground predicate thresholding image values given a single thresold...
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
std::ostream & error()