DGtal  1.2.0
testCOBANaivePlaneComputer-benchmark.cpp
Go to the documentation of this file.
1 
31 #include <cstdlib>
32 #include <iostream>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/math/Statistic.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/kernel/CPointPredicate.h"
37 #include "DGtal/geometry/surfaces/COBANaivePlaneComputer.h"
39 
40 using namespace std;
41 using namespace DGtal;
42 
44 // Functions for testing class COBANaivePlaneComputer.
46 
47 template <typename Integer>
48 Integer getRandomInteger( const Integer & first, const Integer & after_last )
49 {
50  Integer r = (Integer) rand();
51  return ( r % (after_last - first) ) + first;
52 }
53 
57 template <typename Integer, typename NaivePlaneComputer>
58 bool
60  int diameter, unsigned int nbpoints,
61  Statistic<double> & stats )
62 {
63  typedef typename NaivePlaneComputer::Point Point;
64  typedef typename Point::Component PointInteger;
66  Integer absA = ic.abs( a );
67  Integer absB = ic.abs( b );
68  Integer absC = ic.abs( c );
69  Integer x, y, z;
70  Dimension axis;
71  if ( ( absA >= absB ) && ( absA >= absC ) )
72  axis = 0;
73  else if ( ( absB >= absA ) && ( absB >= absC ) )
74  axis = 1;
75  else
76  axis = 2;
77  Point p;
78  NaivePlaneComputer plane;
79  plane.init( axis, diameter, 1, 1 );
80  // Checks that points within the naive plane are correctly recognized.
81  unsigned int nb = 0;
82  unsigned int nbok = 0;
83  unsigned int nbchanges = 0;
84  unsigned int complexity = plane.complexity();
85  while ( nb != nbpoints )
86  {
87  p[ 0 ] = getRandomInteger<PointInteger>( -diameter+1, diameter );
88  p[ 1 ] = getRandomInteger<PointInteger>( -diameter+1, diameter );
89  p[ 2 ] = getRandomInteger<PointInteger>( -diameter+1, diameter );
90  x = (Integer) p[ 0 ];
91  y = (Integer) p[ 1 ];
92  z = (Integer) p[ 2 ];
93  switch ( axis ) {
94  case 0: p[ 0 ] = NumberTraits<Integer>::castToInt64_t( ic.ceilDiv( d - b * y - c * z, a ) ); break;
95  case 1: p[ 1 ] = NumberTraits<Integer>::castToInt64_t( ic.ceilDiv( d - a * x - c * z, b ) ); break;
96  case 2: p[ 2 ] = NumberTraits<Integer>::castToInt64_t( ic.ceilDiv( d - a * x - b * y, c ) ); break;
97  }
98  bool ok = plane.extend( p ); // should be ok
99  ++nb; nbok += ok ? 1 : 0;
100  if ( ! ok )
101  {
102  std::cerr << "[ERROR] p=" << p << " NOT IN plane=" << plane << std::endl;
103  break;
104  }
105  if ( plane.complexity() != complexity )
106  {
107  complexity = plane.complexity();
108  ++nbchanges;
109  }
110  }
111  stats.addValue( (double) nbchanges );
112  return nb == nbok;
113 }
114 
115 template <typename NaivePlaneComputer>
116 bool
117 checkPlanes( unsigned int nbplanes, int diameter, unsigned int nbpoints,
118  Statistic<double> & stats )
119 {
120  //using namespace Z3i;
122  unsigned int nb = 0;
123  unsigned int nbok = 0;
124  for ( unsigned int nbp = 0; nbp < nbplanes; ++nbp )
125  {
126  Integer a = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
127  Integer b = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
128  Integer c = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
129  Integer d = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
130  if ( ( a != 0 ) || ( b != 0 ) || ( c != 0 ) )
131  {
132  ++nb; nbok += checkPlane<Integer, NaivePlaneComputer>( a, b, c, d, diameter, nbpoints, stats ) ? 1 : 0;
133  if ( nb != nbok )
134  {
135  std::cerr << "[ERROR] for plane " << a << " * x + "
136  << b << " * y + " << c << " * z = " << d << std::endl;
137  break;
138  }
139  }
140  }
141  return nb == nbok;
142 }
143 
144 
146 // Standard services - public :
147 
148 int main( int argc, char** argv )
149 {
150  using namespace Z3i;
151  Statistic<double> stats;
152  unsigned int nbtries = ( argc > 1 ) ? atoi( argv[ 1 ] ) : 100;
153  unsigned int nbpoints = ( argc > 2 ) ? atoi( argv[ 2 ] ) : 100;
154  unsigned int diameter = ( argc > 3 ) ? atoi( argv[ 3 ] ) : 100;
155  std::cout << "# Usage: " << argv[0] << " <nbtries> <nbpoints> <diameter>." << std::endl;
156  std::cout << "# Test class COBANaivePlaneComputer. Points are randomly chosen in [-diameter,diameter]^3." << std::endl;
157  std::cout << "# Integer nbtries nbpoints diameter time/plane(ms) E(comp) V(comp)" << std::endl;
158 
159  // Max diameter is ~20 for int32_t, ~500 for int64_t, any with BigInteger.
160  trace.beginBlock ( "Testing class COBANaivePlaneComputer" );
161  bool res = true
162  && checkPlanes<COBANaivePlaneComputer<Z3, DGtal::BigInteger> >( nbtries, diameter, nbpoints, stats );
163  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
164  long t = trace.endBlock();
165  stats.terminate();
166  std::cout << "BigInteger" << " " << stats.samples()
167  << " " << nbpoints
168  << " " << diameter
169  << " " << ( (double) t / (double) stats.samples() )
170  << " " << stats.mean()
171  << " " << stats.variance()
172  << std::endl;
173  return res ? 0 : 1;
174 }
175 // //
void init(Dimension axis, InternalInteger diameter, InternalInteger widthNumerator=NumberTraits< InternalInteger >::ONE, InternalInteger widthDenominator=NumberTraits< InternalInteger >::ONE)
bool extend(const Point &p)
static Integer abs(IntegerParamType a)
Integer ceilDiv(IntegerParamType na, IntegerParamType nb) const
Aim: This class processes a set of sample values for one variable and can then compute different stat...
Definition: Statistic.h:70
double variance() const
unsigned int samples() const
double mean() const
void addValue(Quantity v)
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Definition: Common.h:137
Trace trace
Definition: Common.h:154
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:533
bool checkPlane(Integer a, Integer b, Integer c, Integer d, int diameter, unsigned int nbpoints, Statistic< double > &stats)
int main(int argc, char **argv)
Integer getRandomInteger(const Integer &first, const Integer &after_last)
bool checkPlanes(unsigned int nbplanes, int diameter, unsigned int nbpoints, Statistic< double > &stats)
MyPointD Point
Definition: testClone2.cpp:383