DGtal  1.2.0
testChordGenericStandardPlaneComputer.cpp
Go to the documentation of this file.
1 
31 #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/ChordGenericStandardPlaneComputer.h"
40 
41 using namespace std;
42 using namespace DGtal;
43 
45 // Functions for testing class ChordGenericStandardPlaneComputer.
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( 1, 1 );
105  ++nb;
106  nbok += computer.extend( pts.begin(), pts.end() ) ? 1 : 0;
107  trace.info() << "Primitive=" << computer.primitive() << std::endl;
108  trace.info() << "(" << nbok << "/" << nb << ") extend "
109  << pts.size() << " points of plane "
110  << mu << " <= " << a << "*x+" << b << "*y+" << c << "*z+"
111  << " < " << (mu+a+b+c) << std::endl;
112  computer.init( 1, 1 );
113  std::random_shuffle( pts.begin(), pts.end() );
114  ++nb;
115  nbok += computer.extend( pts.begin(), pts.end() ) ? 1 : 0;
116  trace.info() << "Primitive=" << computer.primitive() << std::endl;
117  trace.info() << "(" << nbok << "/" << nb << ") extend "
118  << pts.size() << " shuffled points of plane "
119  << mu << " <= " << a << "*x+" << b << "*y+" << c << "*z+"
120  << " < " << (mu+a+b+c) << std::endl;
121  }
122  return nb == nbok;
123 }
124 
125 
126 int main( int /*argc*/, char** /*argv*/ )
127 {
128  using namespace Z3i;
129 
131  PlaneComputer;
132 
133  bool ok;
134  PlaneComputer plane;
135  plane.init( 1, 1 );
136  ok = plane.extend( Point(0,0,0) );
137  trace.info() << "Point(0,0,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
138  trace.info() << plane << std::endl;
139  ok = plane.extend( Point(1,0,0) );
140  trace.info() << "Point(1,0,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
141  trace.info() << plane << std::endl;
142  ok = plane.extend( Point(0,1,0) );
143  trace.info() << "Point(0,1,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
144  trace.info() << plane << std::endl;
145  ok = plane.extend( Point(1,1,0) );
146  trace.info() << "Point(1,1,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
147  trace.info() << plane << std::endl;
148  ok = plane.extend( Point(2,0,0) );
149  trace.info() << "Point(2,0,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(0,2,0) );
155  trace.info() << "Point(0,2,0) is " << ( ok ? "ok" : "ko" ) << std::endl;
156  trace.info() << plane << std::endl;
157  ok = plane.extend( Point(1,1,1) );
158  trace.info() << "Point(1,1,1) is " << ( ok ? "ok" : "ko" ) << std::endl;
159  trace.info() << plane << std::endl;
161 
162  return 0;
163 }
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:154
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