DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testConstRangeAdapter.cpp
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15 
16 
17  * @file testConstRangeAdapter.cpp
18  * @author Tristan Roussillon (\c
19  * tristan.roussillon@liris.cnrs.fr ) Laboratoire d'InfoRmatique en
20  * Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS,
21  * France
22  *
23  *
24  * @date 2012/02/06
25  *
26  * Functions for testing class ConstRangeAdapter
27  *
28  * This file is part of the DGtal library.
29  */
30 
31 #include <iostream>
32 #include <fstream>
33 #include <sstream>
34 
35 #include "DGtal/base/Common.h"
36 #include "DGtal/base/BasicFunctors.h"
37 #include "DGtal/base/CConstBidirectionalRange.h"
38 #include "DGtal/base/ConstRangeAdapter.h"
39 
40 #include "DGtal/topology/KhalimskySpaceND.h"
41 #include "DGtal/topology/SCellsFunctors.h"
42 #include "DGtal/kernel/BasicPointFunctors.h"
43 
44 
45 
46 using namespace std;
47 using namespace DGtal;
48 
49 
50 /*
51  * Ranges
52  *
53  */
54 template <typename Range>
55 bool testRange(const Range &aRange)
56 {
57 
58  trace.info() << endl;
59  trace.info() << "Testing Range" << endl;
60 
62  std::vector<Value> v1,v2,v3,v4;
63 
64  {
65  trace.info() << "Forward" << endl;
66  typename Range::ConstIterator i = aRange.begin();
67  typename Range::ConstIterator end = aRange.end();
68  for ( ; i != end; ++i) {
69  cout << *i << " ";
70  v1.push_back(*i);
71  }
72  cout << endl;
73  }
74  {
75  trace.info() << "Backward" << endl;
76  typename Range::ConstReverseIterator i = aRange.rbegin();
77  typename Range::ConstReverseIterator end = aRange.rend();
78  for ( ; i != end; ++i) {
79  cout << *i << " ";
80  v2.push_back(*i);
81  }
82  cout << endl;
83  }
84  {
85  trace.info() << "Circulator" << endl;
86  typename Range::ConstCirculator c = aRange.c();
87  typename Range::ConstCirculator cend = aRange.c();
88  if (isNotEmpty(c,cend))
89  {
90  do
91  {
92  cout << *c << " ";
93  v3.push_back(*c);
94  c++;
95  } while (c!=cend);
96  }
97  cout << endl;
98  }
99 
100  {
101  trace.info() << "Reverse Circulator" << endl;
102  typename Range::ConstReverseCirculator c = aRange.rc();
103  typename Range::ConstReverseCirculator cend = aRange.rc();
104  if (isNotEmpty(c,cend))
105  {
106  do
107  {
108  cout << *c << " ";
109  v4.push_back(*c);
110  c++;
111  } while (c!=cend);
112  }
113  cout << endl;
114  }
115 
116  return ( std::equal(v1.begin(),v1.end(),v3.begin())
117  && std::equal(v2.begin(),v2.end(),v4.begin())
118  && std::equal(v1.begin(),v1.end(),v2.rbegin())
119  && std::equal(v3.begin(),v3.end(),v4.rbegin()) );
120 }
121 
122 
123 template <typename Range>
124 void testRangeConceptChecking()
125 {
126  BOOST_CONCEPT_ASSERT(( CConstBidirectionalRange<Range> ));
127 }
128 
129 /*
130  * Standard services - public :
131  */
132 
133 int main( int argc, char** argv )
134 {
135  trace.beginBlock ( "Testing class ConstRangeAdapter" );
136  trace.info() << "Args:";
137  for ( int i = 0; i < argc; ++i )
138  trace.info() << " " << argv[ i ];
139  trace.info() << endl;
140 
141  //1) simple range of integers
142  const int n = 10;
143  std::vector<int> v;
144  std::back_insert_iterator<std::vector<int> > ito(v);
145  for (int i = 1; i < n; ++i)
146  *ito++ = i;
147 
149  DefaultFunctor df;
150  SimpleRange r1(v.begin(), v.end(), df);
151 
152 
153  //2) thresholded range of integers
154  typedef ConstRangeAdapter<std::vector<int>::iterator, Thresholder<int>, bool > BoolRange;
155  Thresholder<int> t(n/2);
156  BoolRange r2(v.begin(), v.end(), t);
157 
158  //3) range of signed cells...
159  typedef KhalimskySpaceND<3> K;
160  typedef K::Point Point3;
161  vector<K::SCell> v3;
162  K ks;
163  v3.push_back(ks.sCell(Point3(1,1,0)));
164  v3.push_back(ks.sCell(Point3(2,1,1)));
165  v3.push_back(ks.sCell(Point3(3,1,2)));
166  //... transformed into inner voxels,
167  //which are projected into 2d points
168  typedef SpaceND<2> S;
169  typedef S::Point Point2;
170  SCellToInnerPoint<K> f(ks);
171  Projector<S> p;
173 
175  Composer<SCellToInnerPoint<K>,Projector<S>,Point2>, Point2 > PointRange;
176  PointRange r3(v3.begin(), v3.end(), c);
177 
179  testRangeConceptChecking<SimpleRange>();
180  testRangeConceptChecking<BoolRange>();
181  testRangeConceptChecking<PointRange>();
182 
184  bool res = testRange(r1)
185  && testRange(r2)
186  && testRange(r3);
187 
188  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
189  trace.endBlock();
190  return res ? 0 : 1;
191 }