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