DGtal  0.9.3
test2x2DetComputers.cpp
Go to the documentation of this file.
1 
30 #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 
185 {
186  return static_cast<DGtal::int32_t>(rand() % 32768);
187 }
189 {
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 
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
259 #ifdef WITH_BIGINTEGER
261 #endif
264 #ifdef WITH_BIGINTEGER
266 #endif
272 
273 #ifdef WITH_BIGINTEGER
276 #endif
279  ;
280 
281  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
282  trace.endBlock();
283  return res ? 0 : 1;
284 }
285 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
DGtal::int32_t randomBelow2exp15()
Aim: This concept gathers all models that are able to compute the (sign of the) determinant of a 2x2 ...
int main(int argc, char **argv)
STL namespace.
double endBlock()
Aim: Class that provides a way of computing the sign of the determinant of a 2x2 matrix from its four...
Aim: Small class useful to compute the determinant of a 2x2 matrix from its four coefficients, ie. .
std::ostream & emphase()
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:69
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
bool hasTheSameSign(I1 x1, I2 x2)
bool randomTest2x2DetComputer(DetComputer aComputer)
Aim: Class that provides a way of computing the sign of the determinant of a 2x2 matrix from its four...
Aim: Small class useful to compute, in an incremental way, the determinant of a 2x2 matrix from its f...
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
bool simpleTest2x2DetComputer(DetComputer aComputer)
DGtal::int32_t adHocRandom()