DGtal  1.4.2
testChordNaivePlaneComputer-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/arithmetic/IntegerComputer.h"
38 #include "DGtal/geometry/surfaces/ChordNaivePlaneComputer.h"
40 
41 using namespace std;
42 using namespace DGtal;
43 
45 // Functions for testing class ChordNaivePlaneComputer.
47 
48 template <typename Integer>
49 Integer getRandomInteger( const Integer & first, const Integer & after_last )
50 {
51  Integer r = (Integer) rand();
52  return ( r % (after_last - first) ) + first;
53 }
54 
58 template <typename Integer, typename NaivePlaneComputer>
59 bool
61  int diameter, unsigned int nbpoints )
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, 1, 1 );
80  // Checks that points within the naive plane are correctly recognized.
81  unsigned int nb = 0;
82  unsigned int nbok = 0;
83  while ( nb != nbpoints )
84  {
85  p[ 0 ] = getRandomInteger<PointInteger>( -diameter+1, diameter );
86  p[ 1 ] = getRandomInteger<PointInteger>( -diameter+1, diameter );
87  p[ 2 ] = getRandomInteger<PointInteger>( -diameter+1, diameter );
88  x = (Integer) p[ 0 ];
89  y = (Integer) p[ 1 ];
90  z = (Integer) p[ 2 ];
91  switch ( axis ) {
92  case 0: p[ 0 ] = (PointInteger)NumberTraits<Integer>::castToInt64_t( ic.ceilDiv( d - b * y - c * z, a ) ); break;
93  case 1: p[ 1 ] = (PointInteger)NumberTraits<Integer>::castToInt64_t( ic.ceilDiv( d - a * x - c * z, b ) ); break;
94  case 2: p[ 2 ] = (PointInteger)NumberTraits<Integer>::castToInt64_t( ic.ceilDiv( d - a * x - b * y, c ) ); break;
95  }
96  bool ok = plane.extend( p ); // should be ok
97  ++nb; nbok += ok ? 1 : 0;
98  if ( ! ok )
99  {
100  std::cerr << "[ERROR] p=" << p << " NOT IN plane=" << plane << std::endl;
101  break;
102  }
103  }
104  return nb == nbok;
105 }
106 
107 template <typename NaivePlaneComputer>
108 bool
109 checkPlanes( unsigned int nbplanes, unsigned int diameter, unsigned int nbpoints )
110 {
111  //using namespace Z3i;
112  typedef typename NaivePlaneComputer::InternalScalar Integer;
113  unsigned int nb = 0;
114  unsigned int nbok = 0;
115  for ( unsigned int nbp = 0; nbp < nbplanes; ++nbp )
116  {
117  Integer a = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
118  Integer b = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
119  Integer c = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
120  Integer d = getRandomInteger<Integer>( (Integer) 0, (Integer) diameter / 2 );
121  if ( ( a != 0 ) || ( b != 0 ) || ( c != 0 ) )
122  {
123  ++nb; nbok += checkPlane<Integer, NaivePlaneComputer>( a, b, c, d, diameter, nbpoints ) ? 1 : 0;
124  if ( nb != nbok )
125  {
126  std::cerr << "[ERROR] for plane " << a << " * x + "
127  << b << " * y + " << c << " * z = " << d << std::endl;
128  break;
129  }
130  }
131  }
132  return nb == nbok;
133 }
134 
135 
137 // Standard services - public :
138 
139 int main( int argc, char** argv )
140 {
141  using namespace Z3i;
142  unsigned int nbtries = ( argc > 1 ) ? atoi( argv[ 1 ] ) : 100;
143  unsigned int nbpoints = ( argc > 2 ) ? atoi( argv[ 2 ] ) : 100;
144  unsigned int diameter = ( argc > 3 ) ? atoi( argv[ 3 ] ) : 100;
145  std::cout << "# Usage: " << argv[0] << " <nbtries> <nbpoints> <diameter>." << std::endl;
146  std::cout << "# Test class ChordNaivePlaneComputer. Points are randomly chosen in [-diameter,diameter]^3." << std::endl;
147  std::cout << "# Integer nbtries nbpoints diameter time/plane(ms)" << std::endl;
148 
149  trace.beginBlock ( "Testing class ChordNaivePlaneComputer" );
150  bool res = true
151  && checkPlanes<ChordNaivePlaneComputer<Space, Point, DGtal::int64_t> >( nbtries, diameter, nbpoints );
152  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
153  long t = trace.endBlock();
154  std::cout << "int64_t" << " " << nbtries
155  << " " << nbpoints
156  << " " << diameter
157  << " " << ( (double) t / (double) nbtries )
158  << std::endl;
159  return res ? 0 : 1;
160 }
161 // //
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
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:136
Trace trace
Definition: Common.h:153
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:564
int main(int argc, char **argv)
bool checkPlane(Integer a, Integer b, Integer c, Integer d, int diameter, unsigned int nbpoints)
bool checkPlanes(unsigned int nbplanes, unsigned int diameter, unsigned int nbpoints)
Integer getRandomInteger(const Integer &first, const Integer &after_last)
MyPointD Point
Definition: testClone2.cpp:383