DGtal  0.9.3
testHashTree.cpp
Go to the documentation of this file.
1 
30 #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 
45 #include "DGtal/helpers/StdDefs.h"
46 
48 
49 using namespace std;
50 using namespace DGtal;
51 
53 // Functions for testing class HashTree.
55 
60 {
61  unsigned int nbok = 0;
62  unsigned int nb = 0;
63 
64  trace.beginBlock ( "Testing simple init ..." );
65 
66 
67  typedef SpaceND<4> Space4Type;
68  typedef HyperRectDomain<Space4Type> TDomain;
69  typedef TDomain::Point Point;
70  typedef Space4Type::Integer Integer;
71 
72  //Default image selector = STLVector
74 
75  const Integer t[ ] = { 1, 2, 3 ,4};
76  const Integer t2[ ] = { 5, 5, 3 ,4};
77  Point a ( t );
78  Point b ( t2 );
79 
81  Image myImage ( 3, 3,0 );
82 
84  TDomain domain(a,b);
85  Image myImage2 ( domain );
86 
87  trace.info() << myImage;
88  trace.info() << myImage2;
89 
90  nbok += true ? 1 : 0;
91  nb++;
92  trace.info() << "(" << nbok << "/" << nb << ") "
93  << "true == true" << std::endl;
94  trace.endBlock();
95 
96  return nbok == nb;
97 }
98 
99 
104 {
105  trace.beginBlock("Testing 2D");
106  Z2i::Domain domain(Z2i::Point(0,0), Z2i::Point(255,255));
108 
109  Image myImage(domain);
110  trace.info()<< myImage<<std::endl;
111  trace.endBlock();
112 
113  return true;
114 }
115 
121 {
122  unsigned int nbok = 0;
123  unsigned int nb = 0;
124 
125  typedef SpaceND<2> SpaceType;
126  typedef HyperRectDomain<SpaceType> TDomain;
127  typedef TDomain::Point Point;
128  Board2D board;
129  typedef HueShadeColorMap<unsigned char,2> HueTwice;
131 
132 
133  //Default image selector = STLVector
135  typedef ImageContainerBySTLVector<TDomain, int> ImageVector;
136 
137  Point a( 1,1 );
138  Point b ( 50,50 );
139  Point c(15,15);
140  Point d(128,128);
141 
142  Point l(0,0);
143  Point u(255,255);
144 
145  trace.beginBlock ( "Image init" );
147  Image myImage ( 3, 8, 0 );
148 
149  ImageVector myImageV(TDomain(l,u));
150 
151  trace.info() << myImage;
152  trace.endBlock();
153 
154 
155  trace.beginBlock("SetVal");
156  for( a[1] = 0; a[1] < 256; a[1]++)
157  for( a[0] = 0; a[0] < 256; a[0]++)
158  {
159  if ( pow((double)(a[0]-128),3.0) - pow((double)(a[1]-128),3.0) < pow(32.0,3.0))
160  {
161  myImage.setValue(a, 30);
162  myImageV.setValue(a,30);
163  }
164 
165  else
166  if ( pow((double)(a[0]-128),3.0) - pow((double)(a[1]-128),3.0) < pow(64.0,3.0))
167  {
168  myImage.setValue(a, 10);
169  myImageV.setValue(a,10);
170  }
171  }
172  trace.endBlock();
173 
174  bool result=true;
175 
176  trace.beginBlock("GetVal consistency test");
177  for( a[1] = 0; a[1] < 256; a[1]++)
178  for( a[0] = 0; a[0] < 256; a[0]++)
179  {
180  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(32,3.0))
181  result = result && (myImage(a) == 30);
182  else
183  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(64,3.0))
184  result = result && (myImage(a) == 10);
185  }
186  trace.endBlock();
187 
188  if (result)
189  trace.info() << "Get/Set test passed"<<endl;
190  else
191  trace.error() << "Get/Set test error"<<endl;
192  nbok += result ? 1 : 0;
193  nb++;
194 
195  trace.info() << myImage;
196  trace.info() << myImageV;
197 
198  Display2DFactory::drawImageHashTree<HueTwice>(board, myImage, 0, 255);
199  board.saveSVG( "hashtree.svg" );
200  board.clear();
201  Display2DFactory::drawImage<HueTwice>(board, myImageV, 0, 255);
202  board.saveSVG( "hashtree-vector.svg" );
203 
204 
206  Image myImage2 ( 5, 8, 0 );
207 
208  trace.beginBlock("SetVal (keysize=5)");
209  for( a[1] = 0; a[1] < 256; a[1]++)
210  for( a[0] = 0; a[0] < 256; a[0]++)
211  {
212  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(32,3.0))
213  myImage2.setValue(a, 30);
214  else
215  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(64,3.0))
216  myImage2.setValue(a, 10);
217  }
218  trace.endBlock();
219 
220  result=true;
221 
222  trace.beginBlock("GetVal consistency test (keysize=5)");
223  for( a[1] = 0; a[1] < 256; a[1]++)
224  for( a[0] = 0; a[0] < 256; a[0]++)
225  {
226  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(32,3.0))
227  result = result && (myImage2(a) == 30);
228  else
229  if ( pow((a[0]-128),3.0) - pow((a[1]-128),3.0) < pow(64,3.0))
230  result = result && (myImage2(a) == 10);
231  }
232  trace.endBlock();
233 
234  if (result)
235  trace.info() << "Get/Set test passed"<<endl;
236  else
237  trace.error() << "Get/Set test error"<<endl;
238  nbok += result ? 1 : 0;
239  nb++;
240 
241  trace.warning() << "(" << nbok << "/" << nb << ") "
242  << "true == true" << std::endl;
243 
244 
245  return nbok == nb;
246 }
247 
248 
250 {
251  typedef SpaceND<2> SpaceType;
252  typedef HyperRectDomain<SpaceType> TDomain;
253  typedef TDomain::Point Point;
254  Board2D board;
255  board.setUnit(Board2D::UCentimeter);
256 
257 
258  //Default image selector = STLVector
260  Point d(128,128);
261 
262  trace.beginBlock ( "Test maximal depth > number of bits of the HashKey type" );
263  Image myImage ( 3, 80, 0 );
264  trace.info() << myImage;
265  trace.endBlock();
266 
267  trace.beginBlock ( "Test morton hash size > number of bits of the HashKey type" );
269  // Image myImage2 ( 80, 8, 0 );
270  //trace.info() << myImage2;
271  trace.endBlock();
272 
273  //Default image selector = STLVector
275  trace.beginBlock ( "Changing the HashKey type" );
276  Image2 myImage3( 3, 80, 0 );
277  trace.info() << myImage3;
278  trace.endBlock();
279 
280 
281  return true;
282 }
283 
285 // Standard services - public :
286 
287 int main( int argc, char** argv )
288 {
289  trace.beginBlock ( "Testing class HashTree" );
290  trace.info() << "Args:";
291  for ( int i = 0; i < argc; ++i )
292  trace.info() << " " << argv[ i ];
293  trace.info() << endl;
294 
295  bool res = testHashTree() && testHashTree2D() && testGetSetVal() && testBadKeySizes(); // && ... other tests
296  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
297  trace.endBlock();
298  return res ? 0 : 1;
299 }
300 // //
void beginBlock(const std::string &keyword="")
bool testHashTree2D()
bool testHashTree()
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
int main(int argc, char **argv)
Aim: Parallelepidec region of a digital space, model of a &#39;CDomain&#39;.
STL namespace.
double endBlock()
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
void setValue(const Point &aPoint, const Value &aValue)
Definition: Image.h:247
std::ostream & emphase()
Model of CImageContainer implementing the association key<->Value using a hash tree. This class provides a built-in iterator.
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
std::ostream & warning()
std::ostream & info()
ImageContainerBySTLVector< Domain, Value > Image
bool testBadKeySizes()
void setUnit(Unit unit)
Definition: Board.cpp:240
bool testGetSetVal()
std::ostream & error()
Aim: This class specializes a &#39;Board&#39; class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70