DGtal  0.9.2
testAdjacency.cpp
1 
30 #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/graph/CUndirectedSimpleLocalGraph.h"
40 
41 
42 
43 using namespace std;
44 using namespace DGtal;
45 using namespace DGtal::concepts;
46 
54 // Functions for testing class Adjacency.
57 
61 bool testMetricAdjacency()
62 {
63  unsigned int nbok = 0;
64  unsigned int nb = 0;
65 
66  typedef SpaceND<3> Space3D;
67  typedef Space3D::Point Point;
68  typedef Z3i::Adj6 Adj6;
69  typedef Z3i::Adj18 Adj18;
70  typedef Z3i::Adj26 Adj26;
71  Point p( 3, -5, 10 );
72 
73 
74  trace.beginBlock ( "Testing neighbors of" );
75  Adj6::selfDisplay( trace.info() );
76  trace.info() << " p = " << p << std::endl;
77  vector<Point> neigh6;
78  back_insert_iterator< vector<Point> > bii6( neigh6 );
79  Adj6::writeNeighbors( bii6, p );
80  nbok += neigh6.size() == 6 ? 1 : 0;
81  nb++;
82  trace.info() << "(" << nbok << "/" << nb << ") "
83  << "Card(6-neigh): " << neigh6.size()
84  << "== 6 ?" << std::endl;
85  trace.beginBlock ( "Enumerating neighbors." );
86  unsigned int nb_correct = 0;
87  for ( unsigned int i = 0; i < neigh6.size(); ++i )
88  {
89  if ( Adj6::isProperlyAdjacentTo( p, neigh6[ i ] ) )
90  {
91  trace.info() << neigh6[ i ] << "* " << std::endl;
92  ++nb_correct;
93  }
94  else
95  trace.info() << neigh6[ i ] << "- " << std::endl;
96  }
97  trace.endBlock();
98  nbok += nb_correct == 6 ? 1 : 0;
99  nb++;
100  trace.info() << "(" << nbok << "/" << nb << ") "
101  << "Within, #proper adjacent : " << nb_correct
102  << "== 6 ?" << std::endl;
103  trace.endBlock();
104 
105 
106 
107  trace.beginBlock ( "Testing neighborhood of" );
108  Adj18::selfDisplay( trace.info() );
109  trace.info() << " p = " << p << std::endl;
110  vector<Point> neigh18;
111  back_insert_iterator< vector<Point> > bii18( neigh18 );
112  Adj18::writeNeighbors( bii18, p );
113  nbok += neigh18.size() == 18 ? 1 : 0;
114  nb++;
115  trace.info() << "(" << nbok << "/" << nb << ") "
116  << "Card(18-neigh): " << neigh18.size()
117  << "== 18 ?" << std::endl;
118  trace.beginBlock ( "Enumerating neighbors." );
119  nb_correct = 0;
120  for ( unsigned int i = 0; i < neigh18.size(); ++i )
121  {
122  if ( Adj18::isProperlyAdjacentTo( p, neigh18[ i ] ) )
123  {
124  trace.info() << neigh18[ i ] << "* " << std::endl;
125  ++nb_correct;
126  }
127  else
128  trace.info() << neigh18[ i ] << "- " << std::endl;
129  }
130  trace.endBlock();
131  nbok += nb_correct == 18 ? 1 : 0;
132  nb++;
133  trace.info() << "(" << nbok << "/" << nb << ") "
134  << "Within, #proper adjacent : " << nb_correct
135  << "== 18 ?" << std::endl;
136  trace.endBlock();
137 
138 
139  trace.beginBlock ( "Testing neighborhood of" );
140  Adj26::selfDisplay( trace.info() );
141  trace.info() << " p = " << p << std::endl;
142  vector<Point> neigh26;
143  back_insert_iterator< vector<Point> > bii26( neigh26 );
144  Adj26::writeNeighbors( bii26, p );
145  nbok += neigh26.size() == 26 ? 1 : 0;
146  nb++;
147  trace.info() << "(" << nbok << "/" << nb << ") "
148  << "Card(26-neigh): " << neigh26.size()
149  << "== 26 ?" << std::endl;
150  trace.beginBlock ( "Enumerating neighbors." );
151  nb_correct = 0;
152  for ( unsigned int i = 0; i < neigh26.size(); ++i )
153  {
154  if ( Adj26::isProperlyAdjacentTo( p, neigh26[ i ] ) )
155  {
156  trace.info() << neigh26[ i ] << "* " << std::endl;
157  ++nb_correct;
158  }
159  else
160  trace.info() << neigh26[ i ] << "- " << std::endl;
161  }
162  trace.endBlock();
163  nbok += nb_correct == 26 ? 1 : 0;
164  nb++;
165  trace.info() << "(" << nbok << "/" << nb << ") "
166  << "Within, #proper adjacent : " << nb_correct
167  << "== 26 ?" << std::endl;
168  trace.endBlock();
169 
170  return nbok == nb;
171 }
172 
173 
174 bool testLocalGraphModel()
175 {
176  trace.beginBlock ( "Testing graph model" );
177  unsigned int nbok=0,nb=0;
178 
180  BOOST_CONCEPT_ASSERT(( CUndirectedSimpleLocalGraph<Adj> ));
181 
182 
183  nbok += Adj::bestCapacity() == 72 ? 1 : 0;
184  nb++;
185  trace.info() << "(" << nbok << "/" << nb << ") "
186  << "Within, bestCapacity : " << Adj::bestCapacity()
187  << "== 72 ?" << std::endl;
188  trace.endBlock();
189 
190 
191 
192  return nbok == nb;
193 }
194 
195 
197 // Standard services - public :
198 
199 int main( int argc, char** argv )
200 {
201  trace.beginBlock ( "Testing class Adjacency" );
202  trace.info() << "Args:";
203  for ( int i = 0; i < argc; ++i )
204  trace.info() << " " << argv[ i ];
205  trace.info() << endl;
206 
207  bool res = testMetricAdjacency() && testLocalGraphModel(); // && ... other tests
208  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
209  trace.endBlock();
210  return res ? 0 : 1;
211 }
212 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
STL namespace.
double endBlock()
Aim: Gathers several functions useful for concept checks.
Aim: Describes digital adjacencies in digital spaces that are defined with the 1-norm and the infinit...
MetricAdjacency< Space, 3 > Adj26
Definition: StdDefs.h:163
MetricAdjacency< Space, 1 > Adj6
Definition: StdDefs.h:159
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
MetricAdjacency< Space, 2 > Adj18
Definition: StdDefs.h:161