DGtal  0.9.3
testCOBAGenericStandardPlaneComputer.cpp
Go to the documentation of this file.
1 
30 #include <cstdlib>
32 #include <iostream>
33 #include <algorithm>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/kernel/CPointPredicate.h"
37 #include "DGtal/geometry/surfaces/CAdditivePrimitiveComputer.h"
38 #include "DGtal/geometry/surfaces/COBAGenericStandardPlaneComputer.h"
40 
41 using namespace std;
42 using namespace DGtal;
43 
45 // Functions for testing class COBAGenericStandardPlaneComputer.
47 
49 // Standard services - public :
50 template <typename Integer>
51 Integer getRandomInteger( const Integer & first, const Integer & after_last )
52 {
53  Integer r = (Integer) rand();
54  return ( r % (after_last - first) ) + first;
55 }
56 
57 template <typename Domain>
58 std::vector<typename Domain::Point> pointsInStandardPlane
59 ( const Domain & domain,
60  typename Domain::Integer a,
61  typename Domain::Integer b,
62  typename Domain::Integer c,
63  typename Domain::Integer mu )
64 {
65  typedef typename Domain::Integer Integer;
66  typedef typename Domain::Point Point;
67  typedef typename Domain::ConstIterator ConstIterator;
68  std::vector<Point> pts;
69  Integer mup = mu + abs(a) + abs(b) + abs(c);
70  for ( ConstIterator it = domain.begin(), itE = domain.end();
71  it != itE; ++it )
72  {
73  Point p = *it;
74  Integer r = a * p[ 0 ] + b * p[ 1 ] + c * p[ 2 ];
75  if ( ( mu <= r ) && ( r < mup ) )
76  pts.push_back( p );
77  }
78  return pts;
79 }
80 
81 template <typename PlaneComputer >
83 ( PlaneComputer & computer, unsigned int nbplanes, int diameter )
84 {
85  typedef typename PlaneComputer::Space Space;
87  typedef typename Space::Integer Integer;
88  typedef typename Space::Point Point;
89  unsigned int nb = 0;
90  unsigned int nbok = 0;
91  Domain domain( Point( -diameter, -diameter, -diameter ),
92  Point( diameter, diameter, diameter ) );
93  Integer a, b, c, mu;
94  for ( unsigned int p = 0; p < nbplanes; ++p )
95  {
96  do {
97  a = getRandomInteger( -diameter, diameter+1 );
98  b = getRandomInteger( -diameter, diameter+1 );
99  c = getRandomInteger( -diameter, diameter+1 );
100  }
101  while ( ( a == 0 ) && ( b == 0 ) && ( c == 0 ) );
102  mu = getRandomInteger( -diameter, diameter );
103  std::vector<Point> pts = pointsInStandardPlane( domain, a, b, c, mu );
104  computer.init( 2*diameter, 1, 1 );
105  ++nb, nbok += computer.extend( pts.begin(), pts.end() ) ? 1 : 0;
106  trace.info() << "Primitive=" << computer.primitive() << std::endl;
107  trace.info() << "(" << nbok << "/" << nb << ") extend "
108  << pts.size() << " points of plane "
109  << mu << " <= " << a << "*x+" << b << "*y+" << c << "*z+"
110  << " < " << (mu+a+b+c) << std::endl;
111  computer.init( 2*diameter, 1, 1 );
112  std::random_shuffle( pts.begin(), pts.end() );
113  ++nb, nbok += computer.extend( pts.begin(), pts.end() ) ? 1 : 0;
114  trace.info() << "Primitive=" << computer.primitive() << std::endl;
115  trace.info() << "(" << nbok << "/" << nb << ") extend "
116  << pts.size() << " shuffled points of plane "
117  << mu << " <= " << a << "*x+" << b << "*y+" << c << "*z+"
118  << " < " << (mu+a+b+c) << std::endl;
119  }
120  return nb == nbok;
121 }
122 
123 
124 int main( int /*argc*/, char** /*argv*/ )
125 {
126  using namespace Z3i;
127 
129 
130  bool ok;
131  PlaneComputer plane;
132  plane.init( 100, 1, 1 );
133  ok = plane.extend( Point(0,0,0) );
134  trace.info() << "Point(0,0,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
135  trace.info() << plane << std::endl;
136  ok = plane.extend( Point(1,0,0) );
137  trace.info() << "Point(1,0,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
138  trace.info() << plane << std::endl;
139  ok = plane.extend( Point(0,1,0) );
140  trace.info() << "Point(0,1,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
141  trace.info() << plane << std::endl;
142  ok = plane.extend( Point(1,1,0) );
143  trace.info() << "Point(1,1,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
144  trace.info() << plane << std::endl;
145  ok = plane.extend( Point(2,0,0) );
146  trace.info() << "Point(2,0,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
147  trace.info() << plane << std::endl;
148  ok = plane.extend( Point(0,2,0) );
149  trace.info() << "Point(0,2,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
150  trace.info() << plane << std::endl;
151  ok = plane.extend( Point(0,2,0) );
152  trace.info() << "Point(0,2,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
153  trace.info() << plane << std::endl;
154  ok = plane.extend( Point(1,1,1) );
155  trace.info() << "Point(1,1,1) is " << ( ok ? "ok" : "ko" ) << std::endl;
156  trace.info() << plane << std::endl;
158 
159  return 0;
160 }
const ConstIterator & end() const
HyperRectDomain< Space > Domain
const Domain domain(Point(1, 2), Point(6, 5))
MyDigitalSurface::ConstIterator ConstIterator
Trace trace
Definition: Common.h:137
std::vector< typename Domain::Point > pointsInStandardPlane(const Domain &domain, typename Domain::Integer a, typename Domain::Integer b, typename Domain::Integer c, typename Domain::Integer mu)
STL namespace.
Integer getRandomInteger(const Integer &first, const Integer &after_last)
Aim: A class that recognizes pieces of digital planes of given axis width. When the diagonal width is...
void init(InternalInteger diameter, InternalInteger widthNumerator=NumberTraits< InternalInteger >::ONE, InternalInteger widthDenominator=NumberTraits< InternalInteger >::ONE)
const ConstIterator & begin() const
TInteger Integer
Arithmetic ring induced by (+,-,*) and Integer numbers.
Definition: SpaceND.h:102
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
std::ostream & info()
int main(int, char **)
bool checkCOBAGenericStandardPlaneComputer(PlaneComputer &computer, unsigned int nbplanes, int diameter)