DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testHashTree.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 
34 #include "DGtal/io/boards/Board2D.h"
35 #include "DGtal/io/colormaps/HueShadeColorMap.h"
36 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
37 #include "DGtal/io/colormaps/GradientColorMap.h"
38 #include "DGtal/io/colormaps/ColorBrightnessColorMap.h"
39 
40 #include "DGtal/kernel/SpaceND.h"
41 #include "DGtal/kernel/domains/HyperRectDomain.h"
42 #include "DGtal/images/ImageContainerByHashTree.h"
43 #include "DGtal/images/ImageContainerBySTLVector.h"
44 
46 
47 using namespace std;
48 using namespace DGtal;
49 
51 // Functions for testing class HashTree.
53 
57 bool testHashTree()
58 {
59  unsigned int nbok = 0;
60  unsigned int nb = 0;
61 
62  trace.beginBlock ( "Testing simple init ..." );
63 
64 
65  typedef SpaceND<4> Space4Type;
66  typedef HyperRectDomain<Space4Type> TDomain;
67  typedef TDomain::Point Point;
68  typedef Space4Type::Integer Integer;
69 
70  //Default image selector = STLVector
72 
73  const Integer t[ ] = { 1, 2, 3 ,4};
74  const Integer t2[ ] = { 5, 5, 3 ,4};
75  Point a ( t );
76  Point b ( t2 );
77 
79  Image myImage ( 3, 3,0 );
80 
82  Image myImage2 ( TDomain(a,b) );
83 
84  trace.info() << myImage;
85  nbok += true ? 1 : 0;
86  nb++;
87  trace.info() << "(" << nbok << "/" << nb << ") "
88  << "true == true" << std::endl;
89  trace.endBlock();
90 
91  return nbok == nb;
92 }
93 
94 
99 bool testGetSetVal()
100 {
101  unsigned int nbok = 0;
102  unsigned int nb = 0;
103 
104  typedef SpaceND<2> SpaceType;
105  typedef HyperRectDomain<SpaceType> TDomain;
106  typedef TDomain::Point Point;
107  Board2D board;
108  typedef HueShadeColorMap<unsigned char,2> HueTwice;
110 
111 
112  //Default image selector = STLVector
114  typedef ImageContainerBySTLVector<TDomain, int> ImageVector;
115 
116  Point a( 1,1 );
117  Point b ( 50,50 );
118  Point c(15,15);
119  Point d(128,128);
120 
121  Point l(0,0);
122  Point u(255,255);
123 
124  trace.beginBlock ( "Image init" );
126  Image myImage ( 3, 8, 0 );
127 
128  ImageVector myImageV(TDomain(l,u));
129 
130  trace.info() << myImage;
131  trace.endBlock();
132 
133 
134  trace.beginBlock("SetVal");
135  for( a[1] = 0; a[1] < 256; a[1]++)
136  for( a[0] = 0; a[0] < 256; a[0]++)
137  {
138  if ( pow((double)(a[0]-128),3.0) - pow((double)(a[1]-128),3.0) < pow(32.0,3.0))
139  {
140  myImage.setValue(a, 30);
141  myImageV.setValue(a,30);
142  }
143 
144  else
145  if ( pow((double)(a[0]-128),3.0) - pow((double)(a[1]-128),3.0) < pow(64.0,3.0))
146  {
147  myImage.setValue(a, 10);
148  myImageV.setValue(a,10);
149  }
150  }
151  trace.endBlock();
152 
153  bool result=true;
154 
155  trace.beginBlock("GetVal consistency test");
156  for( a[1] = 0; a[1] < 256; a[1]++)
157  for( a[0] = 0; a[0] < 256; a[0]++)
158  {
159  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(32,3.0))
160  result = result && (myImage(a) == 30);
161  else
162  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(64,3.0))
163  result = result && (myImage(a) == 10);
164  }
165  trace.endBlock();
166 
167  if (result)
168  trace.info() << "Get/Set test passed"<<endl;
169  else
170  trace.error() << "Get/Set test error"<<endl;
171  nbok += result ? 1 : 0;
172  nb++;
173 
174  trace.info() << myImage;
175  trace.info() << myImageV;
176 
177  Display2DFactory::drawImageHashTree<HueTwice>(board, myImage, 0, 255);
178  board.saveSVG( "hashtree.svg" );
179  board.clear();
180  Display2DFactory::drawImage<HueTwice>(board, myImageV, 0, 255);
181  board.saveSVG( "hashtree-vector.svg" );
182 
183 
185  Image myImage2 ( 5, 8, 0 );
186 
187  trace.beginBlock("SetVal (keysize=5)");
188  for( a[1] = 0; a[1] < 256; a[1]++)
189  for( a[0] = 0; a[0] < 256; a[0]++)
190  {
191  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(32,3.0))
192  myImage2.setValue(a, 30);
193  else
194  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(64,3.0))
195  myImage2.setValue(a, 10);
196  }
197  trace.endBlock();
198 
199  result=true;
200 
201  trace.beginBlock("GetVal consistency test (keysize=5)");
202  for( a[1] = 0; a[1] < 256; a[1]++)
203  for( a[0] = 0; a[0] < 256; a[0]++)
204  {
205  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(32,3.0))
206  result = result && (myImage2(a) == 30);
207  else
208  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(64,3.0))
209  result = result && (myImage2(a) == 10);
210  }
211  trace.endBlock();
212 
213  if (result)
214  trace.info() << "Get/Set test passed"<<endl;
215  else
216  trace.error() << "Get/Set test error"<<endl;
217  nbok += result ? 1 : 0;
218  nb++;
219 
220  trace.warning() << "(" << nbok << "/" << nb << ") "
221  << "true == true" << std::endl;
222 
223 
224  return nbok == nb;
225 }
226 
227 
228 bool testBadKeySizes()
229 {
230  typedef SpaceND<2> SpaceType;
231  typedef HyperRectDomain<SpaceType> TDomain;
232  typedef TDomain::Point Point;
233  Board2D board;
234  typedef HueShadeColorMap<unsigned char,2> HueTwice;
235  board.setUnit(Board2D::UCentimeter);
236 
237 
238  //Default image selector = STLVector
240  Point d(128,128);
241 
242  trace.beginBlock ( "Test maximal depth > number of bits of the HashKey type" );
243  Image myImage ( 3, 80, 0 );
244  trace.info() << myImage;
245  trace.endBlock();
246 
247  trace.beginBlock ( "Test morton hash size > number of bits of the HashKey type" );
249  // Image myImage2 ( 80, 8, 0 );
250  //trace.info() << myImage2;
251  trace.endBlock();
252 
253  //Default image selector = STLVector
255  trace.beginBlock ( "Changing the HashKey type" );
256  Image2 myImage3( 3, 80, 0 );
257  trace.info() << myImage3;
258  trace.endBlock();
259 
260 
261  return true;
262 }
263 
265 // Standard services - public :
266 
267 int main( int argc, char** argv )
268 {
269  trace.beginBlock ( "Testing class HashTree" );
270  trace.info() << "Args:";
271  for ( int i = 0; i < argc; ++i )
272  trace.info() << " " << argv[ i ];
273  trace.info() << endl;
274 
275  bool res = testHashTree() && testGetSetVal() && testBadKeySizes(); // && ... other tests
276  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
277  trace.endBlock();
278  return res ? 0 : 1;
279 }
280 // //