DGtal
1.4.0
Loading...
Searching...
No Matches
testImageContainerByHashTree.cpp
Go to the documentation of this file.
1
31
#include <iostream>
32
#include "DGtal/base/Common.h"
33
#include <cmath>
34
#include "DGtal/kernel/SpaceND.h"
35
#include "DGtal/kernel/domains/HyperRectDomain.h"
36
#include "DGtal/images/ImageContainerByHashTree.h"
37
39
40
#define dim experimental::ImageContainerByHashTree<Domain , T, DGtal::uint64_t >::dim
41
#define defHashKey typename experimental::ImageContainerByHashTree<Domain , int, DGtal::uint64_t >::HashKey
42
43
using namespace
DGtal
;
44
45
int
iRand
(
int
iMin,
int
iMax )
46
{
47
double
f = ( double ) rand() / RAND_MAX;
48
return
(
int
) (iMin +
static_cast<
double
>
( f * ( iMax - iMin ) ));
49
}
50
51
template
<
typename
Domain,
typename
T >
52
bool
test_setVal
(
experimental::ImageContainerByHashTree<Domain , T, DGtal::uint64_t >
& container,
bool
checkAfterEachSet )
53
{
54
srand
( (
unsigned
int
)time ( NULL ) );
55
56
//phase 1
57
trace
.
info
() <<
"Test: set"
<<std::endl;
58
trace
.
info
() <<
"phase 1..."
<<std::endl;
59
//unsigned DepthMask = container.ROOT_KEY << container.getDepth()*dim;
60
unsigned
DepthMask = 100000;
61
//cerr << "DepthMask = " << DepthMask << std::endl;
62
for
(
int
key = DepthMask; key > 1; --key )
63
{
64
if
( !container.
isKeyValid
( key ) )
65
{
66
continue
;
67
}
68
//count ++;
69
container.
setValue
( key,
iRand
( 0, 100 ) );
70
if
( checkAfterEachSet )
71
{
72
if
( !container.
checkIntegrity
() )
73
{
74
trace
.
info
() <<
"test_set: failure in phase 1"
<< std::endl
75
<<
"at key "
<<
Bits::bitString
( key ) << std::endl;
76
return
false
;
77
}
78
else
79
trace
.
info
() <<
"ok"
<<std::endl;
80
}
81
}
82
trace
.
info
() <<
"checking the container's validity..."
<< std::endl;
83
if
( !container.
checkIntegrity
() )
84
{
85
86
trace
.
info
() <<
"test_set: failure in phase 1"
<< std::endl;
87
return
false
;
88
}
89
90
//phase 2
91
trace
.
info
() <<
"phase 2..."
<<std::endl;
92
for
(
unsigned
key = container.ROOT_KEY; key < DepthMask; ++key )
93
{
94
if
( !container.
isKeyValid
( key ) )
95
{
96
continue
;
97
}
98
container.
setValue
( key,
iRand
( 0, 100 ) );
99
if
( checkAfterEachSet )
100
if
( !container.
checkIntegrity
() )
101
{
102
trace
.
info
() <<
"test_set: failure in phase 2"
<< std::endl
103
<<
"at key "
<<
Bits::bitString
( key ) << std::endl;
104
return
false
;
105
}
106
}
107
108
if
( !container.
checkIntegrity
() )
109
{
110
trace
.
info
() <<
"test_set: failure in phase 2"
<< std::endl;
111
return
false
;
112
}
113
114
trace
.
info
() <<
"test_set: success !"
<< std::endl;
115
return
true
;
116
}
117
118
119
template
<
typename
Domain,
typename
T >
120
bool
test_get
(
experimental::ImageContainerByHashTree<Domain, T, DGtal::uint64_t >
& container,
bool
)
121
{
122
srand
( (
unsigned
int
)time ( NULL ) );
123
unsigned
count = 0;
124
//unsigned DepthMask = container.ROOT_KEY << container.getDepth()*dim;
125
unsigned
DepthMask = 100000;
126
//trace.info() << "DepthMask "<< DepthMask << std::endl;
127
for
(
unsigned
key = DepthMask; key > container.ROOT_KEY; --key )
128
{
129
if
( !container.
isKeyValid
( key ) )
130
{
131
//trace.info() << "invalid key "<< Bits::bitString(key) << std::endl;
132
continue
;
133
}
134
++count;
135
T val =
iRand
( 0, 100 );
136
//trace.info() << "plop1" << std::endl;
137
//trace.info() << "___________________________ set: " << Bits::bitString(key, 16) << std::endl;
138
container.
setValue
( key, val );
139
140
141
typename
experimental::ImageContainerByHashTree<Domain , T, DGtal::uint64_t >::HashKey
key2 = key;
142
while
( container.
isKeyValid
( key2 ) )
143
{
144
key2 = key2 <<
dim
;
145
if
( val != container.
get
( key2 ) )
146
{
147
trace
.
info
() <<
"test_get: failure"
<< std::endl
148
<<
"at key "
<<
Bits::bitString
( key2 ) << std::endl;
149
return
false
;
150
}
151
//trace.info() << "check " << Bits::bitString(key2) << " ok." << std::endl;
152
}
153
//key2 <<=dim;
154
}
155
trace
.
info
() <<
"test_get: success !"
<< std::endl
156
<<
"tested with "
<< count <<
" keys"
<< std::endl;
157
return
true
;
158
}
159
160
161
162
int
main
(
int
argc,
char
** argv )
163
{
164
trace
.
beginBlock
(
"Testing class ImageContainerBenchmark"
);
165
trace
.
info
() <<
"Args:"
;
166
for
(
int
i = 0; i < argc; ++i )
167
trace
.
info
() <<
" "
<< argv[ i ];
168
trace
.
info
() << std::endl;
169
170
typedef
SpaceND<5>
Space
;
171
typedef
HyperRectDomain<Space>
Dom;
172
typedef
DGtal::experimental::ImageContainerByHashTree<Dom, int, DGtal::uint64_t>
Tree;
173
Tree tree ( 12,5,1 );
174
// Do not pass concept.
175
//BOOST_CONCEPT_ASSERT((CDrawableWithBoard2D<Tree>));
176
177
//tree.printInternalState(cerr, 12);
178
Dom::Point p1, p2, p3;
179
trace
.
info
() <<
"azertyuiop"
<< std::endl;
180
p1[0] = -32;
181
p1[1] = -10;
182
p1[2] = -30;
183
p1[3] = 20;
184
p1[4] = 0;
185
186
p2[0] = 32;
187
p2[1] = 32;
188
p2[2] = 20;
189
p2[3] = 64;
190
p2[4] = 120;
191
192
p3[0] = 1;
193
p3[1] = 1;
194
p3[2] = 1;
195
p3[3] = 1;
196
p3[4] = 1;
197
trace
.
info
() <<
"azertyuiop"
<< std::endl;
198
DGtal::experimental::ImageContainerByHashTree<Dom, int, DGtal::uint64_t>
tree2 ( 12,p1, p2,1 );
199
trace
.
info
() <<
"azertyuiop"
<< std::endl;
200
trace
.
info
() <<
"coord get "
<< tree2.
get
( p1 ) << std::endl;
201
trace
.
info
() <<
"_-_-_-_-_-_-_-_-_-_-_-_-"
<< std::endl;
202
trace
.
info
() <<
"coord get "
<< tree2.
get
( p3 ) << std::endl;
203
trace
.
info
() <<
"coord get "
<< tree2.
get
( p1+=p3 ) << std::endl;
204
trace
.
info
() <<
"coord get "
<< tree2.
get
( p1+=p3 ) << std::endl;
205
trace
.
info
() <<
"coord get "
<< tree2.
get
( p1+=p3 ) << std::endl;
206
trace
.
info
() <<
"coord get "
<< tree2.
get
( p1+=p3 ) << std::endl;
207
208
// check that the iterator stuff compiles as it should
209
typedef
DGtal::experimental::ImageContainerByHashTree<Dom, int, DGtal::uint64_t>::Iterator
HashTreeIterator;
210
HashTreeIterator it = tree.begin();
211
for
( it = tree.begin(); it != tree.end(); ++it )
212
tree ( *it );
213
214
215
216
bool
res =
217
(
218
test_setVal
( tree,
false
) &&
219
test_get
( tree,
false
)
220
);
221
222
223
224
trace
.
emphase
() << ( res ?
"Passed."
:
"Error."
) << std::endl;
225
trace
.
endBlock
();
226
return
res ? 0 : 1;
227
}
DGtal::HyperRectDomain
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Definition
HyperRectDomain.h:100
DGtal::SpaceND
Definition
SpaceND.h:96
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::Trace::info
std::ostream & info()
DGtal::Trace::endBlock
double endBlock()
DGtal::experimental::ImageContainerByHashTree::Iterator
Built-in iterator on an HashTree. This iterator visits all node in the tree.
Definition
ImageContainerByHashTree.h:517
DGtal::experimental::ImageContainerByHashTree
Model of CImageContainer implementing the association key<->Value using a hash tree....
Definition
ImageContainerByHashTree.h:129
DGtal::experimental::ImageContainerByHashTree::checkIntegrity
bool checkIntegrity(HashKey key=ROOT_KEY, bool leafAbove=false) const
DGtal::experimental::ImageContainerByHashTree::get
Value get(const HashKey key) const
DGtal::experimental::ImageContainerByHashTree::HashKey
THashKey HashKey
Definition
ImageContainerByHashTree.h:139
DGtal::experimental::ImageContainerByHashTree::isKeyValid
bool isKeyValid(HashKey key) const
DGtal::experimental::ImageContainerByHashTree::setValue
void setValue(const HashKey key, const Value object)
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
Definition
Common.h:153
DGtal::Bits::bitString
static std::string bitString(T value, unsigned nbBits=0)
Bits Structs grouping all the functions of this tiny library for bitwise calculation.
Definition
Bits.h:57
main
int main()
Definition
testBits.cpp:56
srand
srand(0)
test_setVal
bool test_setVal(experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool checkAfterEachSet)
Definition
testImageContainerByHashTree.cpp:52
dim
#define dim
Definition
testImageContainerByHashTree.cpp:40
iRand
int iRand(int iMin, int iMax)
Definition
testImageContainerByHashTree.cpp:45
test_get
bool test_get(experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool)
Definition
testImageContainerByHashTree.cpp:120
Space
SpaceND< 2 > Space
Definition
testSimpleRandomAccessRangeFromPoint.cpp:42
tests
images
testImageContainerByHashTree.cpp
Generated on Mon Jun 10 2024 17:36:12 for DGtal by
1.11.0