DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
test2x2DetComputers.cpp
Go to the documentation of this file.
1
16
29
31
#include <iostream>
32
#include "DGtal/base/Common.h"
33
34
#include "DGtal/kernel/NumberTraits.h"
35
#include "DGtal/geometry/tools/determinant/C2x2DetComputer.h"
36
#include "DGtal/geometry/tools/determinant/Simple2x2DetComputer.h"
37
#include "DGtal/geometry/tools/determinant/SimpleIncremental2x2DetComputer.h"
38
#include "DGtal/geometry/tools/determinant/AvnaimEtAl2x2DetSignComputer.h"
39
#include "DGtal/geometry/tools/determinant/Filtered2x2DetComputer.h"
41
42
using namespace
std
;
43
using namespace
DGtal
;
44
46
// Functions for testing various model of CIncremental2x2DeterminantComputer
48
55
template
<
typename
DetComputer>
56
bool
simpleTest2x2DetComputer
(DetComputer aComputer)
57
{
58
BOOST_CONCEPT_ASSERT((
C2x2DetComputer<DetComputer>
));
59
typedef
typename
DetComputer::Integer
Integer
;
60
typedef
typename
DetComputer::Value
Value
;
61
62
unsigned
int
nbok = 0;
63
unsigned
int
nb = 0;
64
65
trace
.
beginBlock
(
"Testing block ..."
);
66
trace
.
info
() << aComputer << endl;
67
68
Integer
a = 5;
69
Integer
b = 2;
70
71
72
//first quadrant
73
aComputer.init(a,b);
74
if
(aComputer(2,1) ==
NumberTraits<Value>::ONE
)
75
nbok++;
76
nb++;
77
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
78
79
if
(aComputer(3,1) == -
NumberTraits<Value>::ONE
)
80
nbok++;
81
nb++;
82
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
83
84
if
(aComputer(5,2) ==
NumberTraits<Value>::ZERO
)
85
nbok++;
86
nb++;
87
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
88
89
//second quadrant
90
aComputer.init(-a,b);
91
if
(aComputer(2,-1) ==
NumberTraits<Value>::ONE
)
92
nbok++;
93
nb++;
94
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
95
96
if
(aComputer(3,-1) == -
NumberTraits<Value>::ONE
)
97
nbok++;
98
nb++;
99
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
100
101
if
(aComputer(5,-2) ==
NumberTraits<Value>::ZERO
)
102
nbok++;
103
nb++;
104
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
105
106
//third quadrant
107
aComputer.init(-a,-b);
108
if
(aComputer(-2,-1) ==
NumberTraits<Value>::ONE
)
109
nbok++;
110
nb++;
111
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
112
113
if
(aComputer(-3,-1) == -
NumberTraits<Value>::ONE
)
114
nbok++;
115
nb++;
116
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
117
118
if
(aComputer(-5,-2) ==
NumberTraits<Value>::ZERO
)
119
nbok++;
120
nb++;
121
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
122
123
//fourth quadrant
124
aComputer.init(a,-b);
125
if
(aComputer(-2,1) ==
NumberTraits<Value>::ONE
)
126
nbok++;
127
nb++;
128
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
129
130
if
(aComputer(-3,1) == -
NumberTraits<Value>::ONE
)
131
nbok++;
132
nb++;
133
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
134
135
if
(aComputer(-5,2) ==
NumberTraits<Value>::ZERO
)
136
nbok++;
137
nb++;
138
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< endl;
139
140
trace
.
endBlock
();
141
142
return
nbok == nb;
143
}
144
158
template
<
typename
I1,
typename
I2>
159
bool
hasTheSameSign
(I1 x1, I2 x2)
160
{
161
if
(x1 ==
NumberTraits<I1>::ZERO
)
162
{
163
if
(x2 ==
NumberTraits<I2>::ZERO
)
164
return
true
;
165
else
166
return
false
;
167
}
168
else
if
(x1 >
NumberTraits<I1>::ZERO
)
169
{
170
if
(x2 >
NumberTraits<I2>::ZERO
)
171
return
true
;
172
else
173
return
false
;
174
}
175
else
//if (x1 < NumberTraits<I1>::ZERO)
176
{
177
if
(x2 <
NumberTraits<I2>::ZERO
)
178
return
true
;
179
else
180
return
false
;
181
}
182
}
183
184
DGtal::int32_t
randomBelow2exp15
()
185
{
186
return
static_cast<
DGtal::int32_t
>
(rand() % 32768);
187
}
188
DGtal::int32_t
adHocRandom
()
189
{
190
DGtal::int32_t
x =
randomBelow2exp15
() +
191
32768 * (
randomBelow2exp15
() );
192
return
((rand() % 2) ? x : -x);
193
}
194
204
template
<
typename
DetComputer>
205
bool
randomTest2x2DetComputer
(DetComputer aComputer)
206
{
207
BOOST_CONCEPT_ASSERT((
C2x2DetComputer<DetComputer>
));
208
209
typedef
Simple2x2DetComputer<DGtal::int32_t, DGtal::int64_t>
TrueComputer;
210
TrueComputer trueComputer;
211
212
trace
.
beginBlock
(
"Testing block ..."
);
213
trace
.
info
() << trueComputer <<
" vs "
<< aComputer << endl;
214
215
bool
isOk =
true
;
216
217
DGtal::int32_t
a, b, x, y;
218
219
const
int
n = 10000;
220
for
(
int
i = 0; ( (i < n)&&(isOk) ); ++i)
221
{
222
a =
adHocRandom
();
223
b =
adHocRandom
();
224
x =
adHocRandom
();
225
y =
adHocRandom
();
226
trueComputer.init(a, b);
227
aComputer.init(a, b);
228
TrueComputer::Value trueRes = trueComputer(x,y);
229
typename
DetComputer::Value res = aComputer(x,y);
230
// trace.info() << a << "." << y << " - "
231
// << b << " " << x << " => "
232
// << trueRes << " / " << res << endl;
233
if
( !
hasTheSameSign
( trueRes, res ) )
234
isOk =
false
;
235
}
236
237
trace
.
endBlock
();
238
239
return
isOk;
240
}
241
243
// Standard services - public :
244
245
int
main
(
int
argc,
char
** argv )
246
{
247
trace
.
beginBlock
(
"Testing class 2x2DetComputers"
);
248
trace
.
info
() <<
"Args:"
;
249
for
(
int
i = 0; i < argc; ++i )
250
trace
.
info
() <<
" "
<< argv[ i ];
251
trace
.
info
() << endl;
252
253
bool
res =
true
;
254
255
256
res = res
257
&&
simpleTest2x2DetComputer
(
Simple2x2DetComputer<DGtal::int16_t, DGtal::int32_t>
() )
258
&&
simpleTest2x2DetComputer
(
Simple2x2DetComputer<DGtal::int32_t, DGtal::int64_t>
() )
259
&&
simpleTest2x2DetComputer
(
Simple2x2DetComputer<DGtal::int32_t, DGtal::BigInteger>
() )
260
&&
simpleTest2x2DetComputer
(
SimpleIncremental2x2DetComputer<DGtal::int16_t, DGtal::int32_t>
() )
261
&&
simpleTest2x2DetComputer
(
SimpleIncremental2x2DetComputer<DGtal::int32_t, DGtal::int64_t>
() )
262
&&
simpleTest2x2DetComputer
(
SimpleIncremental2x2DetComputer<DGtal::int32_t, DGtal::BigInteger>
() )
263
&&
simpleTest2x2DetComputer
(
AvnaimEtAl2x2DetSignComputer<DGtal::int16_t>
() )
264
&&
simpleTest2x2DetComputer
(
AvnaimEtAl2x2DetSignComputer<DGtal::int32_t>
() )
265
&&
simpleTest2x2DetComputer
(
AvnaimEtAl2x2DetSignComputer<float>
() )
266
&&
simpleTest2x2DetComputer
(
AvnaimEtAl2x2DetSignComputer<double>
() )
267
&&
simpleTest2x2DetComputer
(
Filtered2x2DetComputer
<
AvnaimEtAl2x2DetSignComputer<double>
>() )
268
269
&&
randomTest2x2DetComputer
(
Simple2x2DetComputer<DGtal::int32_t, DGtal::BigInteger>
() )
270
&&
randomTest2x2DetComputer
(
SimpleIncremental2x2DetComputer<DGtal::int32_t, DGtal::BigInteger>
() )
271
&&
randomTest2x2DetComputer
(
AvnaimEtAl2x2DetSignComputer<DGtal::int32_t>
() )
272
&&
randomTest2x2DetComputer
(
Filtered2x2DetComputer
<
AvnaimEtAl2x2DetSignComputer<double>
>() )
273
;
274
275
trace
.
emphase
() << ( res ?
"Passed."
:
"Error."
) << endl;
276
trace
.
endBlock
();
277
return
res ? 0 : 1;
278
}
279
// //
DGtal::AvnaimEtAl2x2DetSignComputer
Aim: Class that provides a way of computing the sign of the determinant of a 2x2 matrix from its four...
Definition
AvnaimEtAl2x2DetSignComputer.h:145
DGtal::Filtered2x2DetComputer
Aim: Class that provides a way of computing the sign of the determinant of a 2x2 matrix from its four...
Definition
Filtered2x2DetComputer.h:84
DGtal::Simple2x2DetComputer
Aim: Small class useful to compute the determinant of a 2x2 matrix from its four coefficients,...
Definition
Simple2x2DetComputer.h:93
DGtal::SimpleIncremental2x2DetComputer
Aim: Small class useful to compute, in an incremental way, the determinant of a 2x2 matrix from its f...
Definition
SimpleIncremental2x2DetComputer.h:94
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()
Integer
Point::Coordinate Integer
Definition
examplePlaneProbingParallelepipedEstimator.cpp:44
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::int32_t
std::int32_t int32_t
signed 32-bit integer.
Definition
BasicTypes.h:71
DGtal::trace
Trace trace
std
STL namespace.
DGtal::C2x2DetComputer
Aim: This concept gathers all models that are able to compute the (sign of the) determinant of a 2x2 ...
Definition
C2x2DetComputer.h:108
DGtal::Value
DGtal::NumberTraits
Aim: The traits class for all models of Cinteger.
Definition
NumberTraits.h:562
adHocRandom
DGtal::int32_t adHocRandom()
Definition
test2x2DetComputers.cpp:188
hasTheSameSign
bool hasTheSameSign(I1 x1, I2 x2)
Definition
test2x2DetComputers.cpp:159
randomTest2x2DetComputer
bool randomTest2x2DetComputer(DetComputer aComputer)
Definition
test2x2DetComputers.cpp:205
simpleTest2x2DetComputer
bool simpleTest2x2DetComputer(DetComputer aComputer)
Definition
test2x2DetComputers.cpp:56
randomBelow2exp15
DGtal::int32_t randomBelow2exp15()
Definition
test2x2DetComputers.cpp:184
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
tests
geometry
tools
determinant
test2x2DetComputers.cpp
Generated on
for DGtal by
1.18.0