DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testConstIteratorAdapter.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/kernel/SpaceND.h"
34 #include "DGtal/kernel/PointVector.h"
35 #include "DGtal/kernel/BasicPointFunctors.h"
36 #include "DGtal/base/ConstIteratorAdapter.h"
37 #include "DGtal/base/Circulator.h"
38 
39 #include <boost/concept_check.hpp>
40 
41 
43 
44 using namespace std;
45 using namespace DGtal;
46 
48 // Functions for testing class ConstIteratorAdapter.
50 
54 bool testProjection()
55 {
56 
57  unsigned int nb = 0;
58  unsigned int nbok = 0;
59 
60  typedef PointVector<3,int> Point3;
61  typedef PointVector<2,int> Point2;
62  typedef std::vector<Point3>::iterator Iterator3;
63  typedef std::vector<Point2>::iterator Iterator2;
64 
65  //projector
66  typedef Projector<SpaceND<2,int> > Projector2;
67 
69  BOOST_CONCEPT_ASSERT(( boost::RandomAccessIterator<Iterator3> ));
70 
71  BOOST_CONCEPT_ASSERT(( boost_concepts::ReadableIteratorConcept<Adapter> ));
72  BOOST_CONCEPT_ASSERT(( boost_concepts::RandomAccessTraversalConcept<Adapter> ));
73 
74  //range of 3d Points
75  std::vector<Point3> r;
76  r.push_back(Point3(0,0,0));
77  r.push_back(Point3(1,0,0));
78  r.push_back(Point3(2,0,0));
79  r.push_back(Point3(2,1,0));
80  r.push_back(Point3(2,1,1));
81  r.push_back(Point3(3,1,1));
82  r.push_back(Point3(4,1,1));
83  r.push_back(Point3(4,2,1));
84  r.push_back(Point3(4,2,2));
85  r.push_back(Point3(5,2,2));
86  r.push_back(Point3(6,2,2));
87  r.push_back(Point3(6,3,2));
88  r.push_back(Point3(6,3,3));
89  r.push_back(Point3(6,4,3));
90  r.push_back(Point3(6,4,4));
91  r.push_back(Point3(6,5,4));
92 
93  //true projection
94  std::vector<Point2> rtrue;
95  rtrue.push_back(Point2(0,0));
96  rtrue.push_back(Point2(1,0));
97  rtrue.push_back(Point2(2,0));
98  rtrue.push_back(Point2(2,1));
99  rtrue.push_back(Point2(2,1));
100  rtrue.push_back(Point2(3,1));
101  rtrue.push_back(Point2(4,1));
102  rtrue.push_back(Point2(4,2));
103  rtrue.push_back(Point2(4,2));
104  rtrue.push_back(Point2(5,2));
105  rtrue.push_back(Point2(6,2));
106  rtrue.push_back(Point2(6,3));
107  rtrue.push_back(Point2(6,3));
108  rtrue.push_back(Point2(6,4));
109  rtrue.push_back(Point2(6,4));
110  rtrue.push_back(Point2(6,5));
111 
112  trace.beginBlock ( "Testing block ..." );
113 
114  trace.info() << "2d points after projection (XY)" << endl;
115 
116  Projector2 proj;
117 
118  Adapter aitBegin(r.begin(),proj);
119  Adapter ait = aitBegin;
120  Adapter aitEnd(r.end(),proj);
121 
122  for ( ; ait != aitEnd; ++ait)
123  {
124  trace.info() << *(ait.base());
125  trace.info() << *ait;
126  trace.info() << "(" << ait->operator[](0) << ", " << ait->operator[](1) << ")" << endl;
127  }
128 
129  //comparison
130  if ( std::equal( rtrue.begin(), rtrue.end(), aitBegin ) == true )
131  nbok++;
132  nb++;
133  trace.info() << nbok << "/" << nb << std::endl;
134 
135  //basic operators
136  trace.info() << "basic operators (operator==)" << endl;
137  if ( ( ait != aitBegin ) && ( ait == aitEnd ) )
138  nbok++;
139  nb++;
140  trace.info() << nbok << "/" << nb << std::endl;
141 
142  //random access
143  ait = (aitBegin + 3);
144  ait += 1;
145  ait = 1 + ait;
146  trace.info() << "random access operators (operator+)" << endl;
147  trace.info() << *(aitBegin.base()) << *aitBegin << endl;
148  trace.info() << "+5" << std::endl;
149  trace.info() << *(ait.base()) << *ait << endl;
150  if ( ( *ait == Point2(3,1) ) == true )
151  nbok++;
152  nb++;
153  trace.info() << nbok << "/" << nb << std::endl;
154 
155  trace.info() << "backward scanning" << endl;
156  std::reverse_iterator<Adapter> raitBegin( aitEnd );
157  if ( std::equal( rtrue.rbegin(), rtrue.rend(), raitBegin ) == true )
158  nbok++;
159  nb++;
160  trace.info() << nbok << "/" << nb << std::endl;
161 
162  trace.info() << "circular scanning" << endl;
163  Circulator<Adapter> caitBegin( aitBegin, aitBegin, aitEnd );
164  if ( std::equal( rtrue.begin(), rtrue.end(), caitBegin ) == true )
165  nbok++;
166  nb++;
167  trace.info() << nbok << "/" << nb << std::endl;
168 
169 
170  trace.endBlock();
171 
172  return (nb == nbok);
173 }
174 
176 // Standard services - public :
177 
178 int main( int argc, char** argv )
179 {
180  trace.beginBlock ( "Testing class ConstIteratorAdapter" );
181  trace.info() << "Args:";
182  for ( int i = 0; i < argc; ++i )
183  trace.info() << " " << argv[ i ];
184  trace.info() << endl;
185 
186  bool res = testProjection()
187  ; // && ... other tests
188  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
189  trace.endBlock();
190  return res ? 0 : 1;
191 }
192 // //