DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testAdjacency.cpp
1 
30 
31 #include <iostream>
32 #include <vector>
33 #include "DGtal/helpers/StdDefs.h"
34 
35 #include "DGtal/base/Common.h"
36 #include "DGtal/kernel/SpaceND.h"
37 #include "DGtal/topology/MetricAdjacency.h"
38 #include "DGtal/topology/CUndirectedSimpleLocalGraph.h"
40 
41 
42 
43 using namespace std;
44 using namespace DGtal;
45 
53 
54 // Functions for testing class Adjacency.
56 
60 bool testMetricAdjacency()
61 {
62  unsigned int nbok = 0;
63  unsigned int nb = 0;
64 
65  typedef SpaceND<3> Space3D;
66  typedef Space3D::Point Point;
67  typedef Z3i::Adj6 Adj6;
68  typedef Z3i::Adj18 Adj18;
69  typedef Z3i::Adj26 Adj26;
70  Point p( 3, -5, 10 );
71 
72 
73  trace.beginBlock ( "Testing neighbors of" );
74  Adj6::selfDisplay( trace.info() );
75  trace.info() << " p = " << p << std::endl;
76  vector<Point> neigh6;
77  back_insert_iterator< vector<Point> > bii6( neigh6 );
78  Adj6::writeNeighbors( bii6, p );
79  nbok += neigh6.size() == 6 ? 1 : 0;
80  nb++;
81  trace.info() << "(" << nbok << "/" << nb << ") "
82  << "Card(6-neigh): " << neigh6.size()
83  << "== 6 ?" << std::endl;
84  trace.beginBlock ( "Enumerating neighbors." );
85  unsigned int nb_correct = 0;
86  for ( unsigned int i = 0; i < neigh6.size(); ++i )
87  {
88  if ( Adj6::isProperlyAdjacentTo( p, neigh6[ i ] ) )
89  {
90  trace.info() << neigh6[ i ] << "* " << std::endl;
91  ++nb_correct;
92  }
93  else
94  trace.info() << neigh6[ i ] << "- " << std::endl;
95  }
96  trace.endBlock();
97  nbok += nb_correct == 6 ? 1 : 0;
98  nb++;
99  trace.info() << "(" << nbok << "/" << nb << ") "
100  << "Within, #proper adjacent : " << nb_correct
101  << "== 6 ?" << std::endl;
102  trace.endBlock();
103 
104 
105 
106  trace.beginBlock ( "Testing neighborhood of" );
107  Adj18::selfDisplay( trace.info() );
108  trace.info() << " p = " << p << std::endl;
109  vector<Point> neigh18;
110  back_insert_iterator< vector<Point> > bii18( neigh18 );
111  Adj18::writeNeighbors( bii18, p );
112  nbok += neigh18.size() == 18 ? 1 : 0;
113  nb++;
114  trace.info() << "(" << nbok << "/" << nb << ") "
115  << "Card(18-neigh): " << neigh18.size()
116  << "== 18 ?" << std::endl;
117  trace.beginBlock ( "Enumerating neighbors." );
118  nb_correct = 0;
119  for ( unsigned int i = 0; i < neigh18.size(); ++i )
120  {
121  if ( Adj18::isProperlyAdjacentTo( p, neigh18[ i ] ) )
122  {
123  trace.info() << neigh18[ i ] << "* " << std::endl;
124  ++nb_correct;
125  }
126  else
127  trace.info() << neigh18[ i ] << "- " << std::endl;
128  }
129  trace.endBlock();
130  nbok += nb_correct == 18 ? 1 : 0;
131  nb++;
132  trace.info() << "(" << nbok << "/" << nb << ") "
133  << "Within, #proper adjacent : " << nb_correct
134  << "== 18 ?" << std::endl;
135  trace.endBlock();
136 
137 
138  trace.beginBlock ( "Testing neighborhood of" );
139  Adj26::selfDisplay( trace.info() );
140  trace.info() << " p = " << p << std::endl;
141  vector<Point> neigh26;
142  back_insert_iterator< vector<Point> > bii26( neigh26 );
143  Adj26::writeNeighbors( bii26, p );
144  nbok += neigh26.size() == 26 ? 1 : 0;
145  nb++;
146  trace.info() << "(" << nbok << "/" << nb << ") "
147  << "Card(26-neigh): " << neigh26.size()
148  << "== 26 ?" << std::endl;
149  trace.beginBlock ( "Enumerating neighbors." );
150  nb_correct = 0;
151  for ( unsigned int i = 0; i < neigh26.size(); ++i )
152  {
153  if ( Adj26::isProperlyAdjacentTo( p, neigh26[ i ] ) )
154  {
155  trace.info() << neigh26[ i ] << "* " << std::endl;
156  ++nb_correct;
157  }
158  else
159  trace.info() << neigh26[ i ] << "- " << std::endl;
160  }
161  trace.endBlock();
162  nbok += nb_correct == 26 ? 1 : 0;
163  nb++;
164  trace.info() << "(" << nbok << "/" << nb << ") "
165  << "Within, #proper adjacent : " << nb_correct
166  << "== 26 ?" << std::endl;
167  trace.endBlock();
168 
169  return nbok == nb;
170 }
171 
172 
173 bool testLocalGraphModel()
174 {
175  trace.beginBlock ( "Testing graph model" );
176  unsigned int nbok=0,nb=0;
177 
179  BOOST_CONCEPT_ASSERT(( CUndirectedSimpleLocalGraph<Adj> ));
180 
181 
182  nbok += Adj::bestCapacity() == 72 ? 1 : 0;
183  nb++;
184  trace.info() << "(" << nbok << "/" << nb << ") "
185  << "Within, bestCapacity : " << Adj::bestCapacity()
186  << "== 72 ?" << std::endl;
187  trace.endBlock();
188 
189 
190 
191  return nbok == nb;
192 }
193 
194 
196 // Standard services - public :
197 
198 int main( int argc, char** argv )
199 {
200  trace.beginBlock ( "Testing class Adjacency" );
201  trace.info() << "Args:";
202  for ( int i = 0; i < argc; ++i )
203  trace.info() << " " << argv[ i ];
204  trace.info() << endl;
205 
206  bool res = testMetricAdjacency() && testLocalGraphModel(); // && ... other tests
207  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
208  trace.endBlock();
209  return res ? 0 : 1;
210 }
211 // //