DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testDistanceTransformationND.cpp
1 
30 
31 #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/SeparableMetricHelper.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/imagesSetsUtils/SimpleThresholdForegroundPredicate.h"
44 
45 using namespace std;
46 using namespace DGtal;
47 
49 // Functions for testing class DistanceTransformND.
51 
55 bool testDistanceTransformND()
56 {
57  unsigned int nbok = 0;
58  unsigned int nb = 0;
59 
60  trace.beginBlock ( "Testing dT dim=5 ..." );
61 
62  typedef SpaceND<5> TSpace;
63  typedef TSpace::Point Point;
65  typedef HueShadeColorMap<unsigned char, 2> HueTwice;
67  int t[5] = {0,0,0,0,0};
68  Point a ( t );
69  int t2[5] = {15,15,15,15,15};
70  Point b ( t2 );
71  int t3[5] = {3,3,3,3,3};
72  Point c ( t3 );
73  Point d;
74 
76  Domain domain(a,b);
77  Image image(domain);
78 
79  //We create an object image with a signle background point (set to 0)
80  for (Image::Iterator it=image.begin(),itend=image.end(); it!=itend; ++it)
81  *it = 128;
82  image.setValue( c , 0 );
83 
85  Predicate aPredicate(image,0);
86 
87  DistanceTransformation<TSpace,Predicate,2> dt(domain,aPredicate);
89 
90  dt.checkTypesValidity ( );
91 
92  //Distance transformation computation
93  ImageLong result = dt.compute ( );
94 
95  //We check the result
96  bool res=true;
97  for(Domain::ConstIterator itDom = domain.begin(), itDomend = domain.end();
98  itDom != itDomend; ++itDom)
99  {
100  //distance from the point to the seed
101  d = (*itDom) - c;
102  ImageLong::Value norm2=0;
103  for(Point::Iterator itd=d.begin(), itdend=d.end(); itd!=itdend; ++itd)
104  norm2+= (*itd)*(*itd);
105 
106  if ( result( (*itDom) ) != norm2)
107  {
108  trace.error()<<"Error at "<<(*itDom)
109  << ": expected="<<norm2<<" and computed="<<result(*itDom)<<endl;
110  res=false;
111  }
112  }
113  nbok += res ? 1 : 0;
114  nb++;
115  trace.info() << "(" << nbok << "/" << nb << ") "
116  << "true == true" << std::endl;
117  trace.endBlock();
118 
119  return nbok == nb;
120 }
121 
123 // Standard services - public :
124 
125 int main( int argc, char** argv )
126 {
127  trace.beginBlock ( "Testing class DistanceTransformND" );
128  trace.info() << "Args:";
129  for ( int i = 0; i < argc; ++i )
130  trace.info() << " " << argv[ i ];
131  trace.info() << endl;
132 
133  bool res = testDistanceTransformND(); // && ... other tests
134  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
135  trace.endBlock();
136  return res ? 0 : 1;
137 }
138 // //