DGtal  1.5.beta
testMorton.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/kernel/PointVector.h"
34 #include "DGtal/images/Morton.h"
35 #include "DGtal/base/Bits.h"
36 
38 
39 using namespace std;
40 using namespace DGtal;
41 
43 // Functions for testing class Morton.
45 
49 bool testMorton()
50 {
51  unsigned int nbok = 0;
52  unsigned int nb = 0;
53 
54  trace.beginBlock ( "Testing Morton codes ..." );
55 
57  typedef DGtal::uint64_t HashKey;
58 
59  Point p(0,0), pbis;
60  HashKey h, h2;
61  HashKey children[4];
62 
63  Morton<HashKey,Point> morton;
64 
65  trace.info() << endl;
66 
67  morton.interleaveBits( p , h );
68  trace.info() << p << h << " = "<< Bits::bitString( h )<<endl;
69  trace.info() << "Key (level2)= "<< Bits::bitString( h2=morton.keyFromCoordinates( 2, p ))<<endl;
70  nbok += (h == 0) ? 1 : 0;
71  nb++;
72 
73  morton.coordinatesFromKey( h2 , pbis);
74  trace.info() << "Point from code= "<<pbis<<endl;
75  nbok += (p == pbis) ? 1 : 0;
76  nb++;
77 
78  trace.info()<<endl;
79 
80  Point p2(2,3);
81  morton.interleaveBits( p2 , h );
82  trace.info() << p2<<" "<< h << " = "<< Bits::bitString( h )<<endl;
83  trace.info() << "Key (level2)= "<< Bits::bitString( h2 = morton.keyFromCoordinates( 2, p2 ))<<endl;
84  nbok += (h == 14) ? 1 : 0;
85  nb++;
86 
87  morton.coordinatesFromKey( h2 , pbis);
88  trace.info() << "Point from code= "<<pbis<<endl;
89  nbok += (p2 == pbis) ? 1 : 0;
90  nb++;
91 
92  trace.beginBlock("Testing children...");
93  h2 = morton.keyFromCoordinates( 2, p2);
94  morton.childrenKeys(h2, children);
95  for(unsigned int k=0; k<4;k++)
96  trace.info()<<"child["<<k<<"]= "<<children[k]<<" "<< Bits::bitString( children[k])<<endl;
97  trace.endBlock();
98 
99  trace.beginBlock("getKey Benchmark");
100 
101  Point p3(0,0);
102  DGtal::uint64_t sum=0;
103  for(unsigned int k=0; k < 1000000 ;k++)
104  {
105  p3[1] = k/1000;
106  p3[0] = k %1000;
107  sum += morton.keyFromCoordinates(32,p);
108  }
109  trace.endBlock();
110  if (sum == 345)
111  trace.info() << "Compiler trick"<<endl;
112 
113  trace.info() << "(" << nbok << "/" << nb << ") "
114  << "true == true" << std::endl;
115  trace.endBlock();
116 
117  trace.warning() << "Log2(64)=" <<LOG2<64>::VALUE<<endl;
118  trace.warning() << "Log2(1)=" <<LOG2<sizeof(int)>::VALUE<<endl;
119 
120  return nbok == nb;
121 }
122 
124 // Standard services - public :
125 
126 int main( int argc, char** argv )
127 {
128  trace.beginBlock ( "Testing class Morton" );
129  trace.info() << "Args:";
130  for ( int i = 0; i < argc; ++i )
131  trace.info() << " " << argv[ i ];
132  trace.info() << endl;
133 
134  bool res = testMorton(); // && ... other tests
135  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
136  trace.endBlock();
137  return res ? 0 : 1;
138 }
139 // //
Aim: Implements the binary Morton code construction in nD.
Definition: Morton.h:78
void interleaveBits(const Point &aPoint, HashKey &output) const
void childrenKeys(const HashKey key, HashKey *result) const
void coordinatesFromKey(const HashKey key, Point &coordinates) const
HashKey keyFromCoordinates(const std::size_t treeDepth, const Point &coordinates) const
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
std::ostream & warning()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
MyPointD Point
Definition: testClone2.cpp:383
int main(int argc, char **argv)
Definition: testMorton.cpp:126
bool testMorton()
Definition: testMorton.cpp:49