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