DGtal  0.9.3beta
viewer3D-7bis-stdplane.cpp
1 
30 #include <cstdlib>
32 #include <iostream>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/geometry/surfaces/ChordGenericStandardPlaneComputer.h"
36 #include "DGtal/io/viewers/Viewer3D.h"
38 
39 using namespace std;
40 using namespace DGtal;
41 
43 // Standard services - public :
44 
45 /*
46  Displays in the \a viewer all the points in the \a domain that
47  satisfies the predicate \a pred.
48 */
49 template <typename Viewer3D, typename Domain, typename Predicate>
50 void
51 displayPredicate( Viewer3D & viewer,
52  const Domain & domain, const Predicate & pred )
53 {
54  for ( typename Domain::ConstIterator itB = domain.begin(), itE = domain.end();
55  itB != itE; ++itB )
56  {
57  if ( pred( *itB ) )
58  viewer << *itB;
59  }
60 }
61 /*
62  Displays in the \a viewer all the points in the range [it,itE).
63 */
64 template <typename Viewer3D, typename InputIterator>
65 void
66 displayRange( Viewer3D & viewer, InputIterator it, InputIterator itE )
67 {
68  for ( ; it != itE; ++it )
69  viewer << *it;
70 }
71 
72 /*
73  Returns all the points in the \a domain that belongs to the standard
74  plane mu <= a*x+b*y+c*z < mu+|a|+|b|+|c|.
75 */
76 template <typename Domain>
77 std::vector<typename Domain::Point> pointsInStandardPlane
78 ( const Domain & domain,
79  typename Domain::Integer a,
80  typename Domain::Integer b,
81  typename Domain::Integer c,
82  typename Domain::Integer mu )
83 {
84  typedef typename Domain::Integer Integer;
85  typedef typename Domain::Point Point;
86  typedef typename Domain::ConstIterator ConstIterator;
87  std::vector<Point> pts;
88  Integer mup = mu + abs(a) + abs(b) + abs(c);
89  for ( ConstIterator it = domain.begin(), itE = domain.end();
90  it != itE; ++it )
91  {
92  Point p = *it;
93  Integer r = a * p[ 0 ] + b * p[ 1 ] + c * p[ 2 ];
94  if ( ( mu <= r ) && ( r < mup ) )
95  pts.push_back( p );
96  }
97  return pts;
98 }
99 
100 
101 int main( int argc, char** argv )
102 {
103  using namespace Z3i;
104  unsigned int nb = 0;
105  unsigned int nbok = 0;
106 
107  QApplication application(argc,argv);
108 
109  unsigned int diameter = argc > 1 ? atoi( argv[ 1 ] ) : 10;
110  int a = argc > 2 ? atoi( argv[ 2 ] ) : 2;
111  int b = argc > 3 ? atoi( argv[ 3 ] ) : 3;
112  int c = argc > 4 ? atoi( argv[ 4 ] ) : 5;
113  int mu = argc > 5 ? atoi( argv[ 5 ] ) : 0;
114  trace.beginBlock ( "Testing class ChordGenericStandardPlaneComputer" );
115  trace.info() << "Recognizing plane "
116  << mu << " <= " << a << " * x + "
117  << b << " * y + " << c << " * z < "
118  << (mu+abs(a)+abs(b)+abs(c)) << std::endl;
119  Domain domain1( Point( -diameter, -diameter, -diameter ),
120  Point( diameter, diameter, diameter ) );
121 
122  typedef int64_t Integer;
124  typedef PlaneComputer::Primitive Primitive;
125  PlaneComputer plane;
126  plane.init( 1, 1 );
127 
128  std::vector<Point> recognized = pointsInStandardPlane( domain1,
129  a, b , c, mu );
130  ++nb, nbok += plane.extend( recognized.begin(), recognized.end() ) ? 1 : 0;
131  trace.info() << "(" << nbok << "/" << nb
132  << ") All points are recognized." << std::endl;
133  trace.info() << " - Plane=" << plane
134  << std::endl;
135  Primitive strip = plane.primitive();
136  trace.info() << "strip=" << strip
137  << " axis=" << strip.mainAxis()
138  << " axiswidth=" << strip.axisWidth()
139  << " diag=" << strip.mainDiagonal()
140  << " diagwidth=" << strip.diagonalWidth()
141  << std::endl;
142  ++nb, nbok += ( strip.diagonalWidth() < sqrt(3.0) ) ? 1 : 0;
143  trace.info() << "(" << nbok << "/" << nb
144  << ") Diagonal width < sqrt(3)." << std::endl;
145  trace.emphase() << ( (nb == nbok) ? "Passed." : "Error." ) << endl;
146  trace.endBlock();
147 
148  typedef Viewer3D<> MyViewer;
149  MyViewer viewer;
150  viewer.show();
151  Color red( 255, 0, 0 );
152  Color green( 0, 255, 0 );
153  Color grey( 200, 200, 200 );
154  Domain domain2( Point( -2*diameter, -2*diameter, -2*diameter ),
155  Point( 2*diameter, 2*diameter, 2*diameter ) );
156  viewer << CustomColors3D( red, red );
157  for ( std::vector<Point>::const_iterator it = recognized.begin(),
158  itE = recognized.end(); it != itE; ++it )
159  if ( ! strip( *it ) ) viewer << *it;
160  viewer << CustomColors3D( green, green );
161  displayRange( viewer, plane.begin(), plane.end() );
162  viewer << CustomColors3D( grey, grey );
163  displayPredicate( viewer, domain2, strip );
164  viewer << MyViewer::updateDisplay;
165  trace.info() << "- Points in green have been recognized as belonging to this standard plane." << std::endl;
166  trace.info() << "- Points in grey belongs also to the parallel strip of the recognized standard plane." << std::endl;
167  trace.info() << "- Points in red belongs to the parallel strip of the recognized standard plane but not to the input standard plane: NONE should be red." << std::endl;
168 
169  return application.exec();
170 }
171 // //
void beginBlock(const std::string &keyword="")
DGtal::int32_t Integer
Definition: StdDefs.h:74
const ConstIterator & begin() const
const ConstIterator & end() const
Trace trace
Definition: Common.h:137
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
STL namespace.
double endBlock()
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
void init(InternalScalar widthNumerator=NumberTraits< InternalScalar >::ONE, InternalScalar widthDenominator=NumberTraits< InternalScalar >::ONE)
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
Aim: A class that recognizes pieces of digital planes of given diagonal width. When the width is ...