DGtal  0.9.3
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 #include <list>
35 
36 #include "DGtal/base/Common.h"
37 #include "DGtal/base/BasicFunctors.h"
38 #include "DGtal/base/CConstBidirectionalRange.h"
39 #include "DGtal/base/ConstRangeAdapter.h"
40 
41 #include "DGtal/topology/KhalimskySpaceND.h"
42 #include "DGtal/topology/SCellsFunctors.h"
43 #include "DGtal/kernel/BasicPointFunctors.h"
44 
45 #include "DGtal/base/IteratorFunctions.h"
46 
47 using namespace std;
48 using namespace DGtal;
49 
50 
51 /*
52  * Ranges
53  *
54  */
55 template <typename Range>
56 bool testRange(const Range &aRange)
57 {
58 
59  trace.info() << endl;
60  trace.info() << "Testing Range" << endl;
61 
63  std::vector<Value> v1, v2;
64  std::list<Value> l3, l4;
65  {
66  trace.info() << "Forward" << endl;
67  typename Range::ConstIterator i = aRange.begin();
68  typename Range::ConstIterator end = aRange.end();
69  for ( ; i != end; ++i) {
70  cout << *i << " ";
71  v1.push_back(*i);
72  }
73  cout << endl;
74  }
75  {
76  trace.info() << "Backward" << endl;
77  typename Range::ConstReverseIterator i = aRange.rbegin();
78  typename Range::ConstReverseIterator end = aRange.rend();
79  for ( ; i != end; ++i) {
80  cout << *i << " ";
81  v2.push_back(*i);
82  }
83  cout << endl;
84  }
85  {
86  trace.info() << "Circulator" << endl;
87  typename Range::ConstCirculator c = aRange.c();
88  typename Range::ConstCirculator cend = aRange.c();
89 
90  trace.info() << "c is valid: "<< (int)c.isValid() << " -- " << *c << std::endl;
91  trace.info() << "cend is valid: "<< (int)cend.isValid() << " -- " << *cend << std::endl;
92 
93 
94  if (isNotEmpty(c,cend))
95  {
96  do
97  {
98  cout << *c << " ";
99  l3.push_back(*c);
100  c++;
101  } while (c!=cend);
102  }
103  cout << endl;
104  }
105 
106  {
107  trace.info() << "Reverse Circulator" << endl;
108  typename Range::ConstReverseCirculator c = aRange.rc();
109  typename Range::ConstReverseCirculator cend = aRange.rc();
110  if (isNotEmpty(c,cend))
111  {
112  do
113  {
114  cout << *c << " ";
115  l4.push_back(*c);
116  c++;
117  } while (c!=cend);
118  }
119  cout << endl;
120  }
121 
122  return ( std::equal(v1.begin(),v1.end(),l3.begin())
123  && std::equal(v1.begin(),v1.end(),v2.rbegin())
124  && (l3.front() == l4.front()) );
125 }
126 
127 
128 template <typename Range>
129 void testRangeConceptChecking()
130 {
131  BOOST_CONCEPT_ASSERT(( concepts::CConstBidirectionalRange<Range> ));
132 }
133 
134 /*
135  * Standard services - public :
136  */
137 
138 int main( int argc, char** argv )
139 {
140  trace.beginBlock ( "Testing class ConstRangeAdapter" );
141  trace.info() << "Args:";
142  for ( int i = 0; i < argc; ++i )
143  trace.info() << " " << argv[ i ];
144  trace.info() << endl;
145 
146  //1) simple range of integers
147  const int n = 10;
148  std::vector<int> v;
149  std::back_insert_iterator<std::vector<int> > ito(v);
150  for (int i = 1; i < n; ++i)
151  *ito++ = i;
152 
154  functors::Identity df;
155  SimpleRange r1(v.begin(), v.end(), df);
156 
157 
158  //2) thresholded range of integers
159  typedef ConstRangeAdapter<std::vector<int>::iterator, DGtal::functors::Thresholder<int>, bool > BoolRange;
160  DGtal::functors::Thresholder<int> t(n/2);
161  BoolRange r2(v.begin(), v.end(), t);
162 
163  //3) range of signed cells...
164  typedef KhalimskySpaceND<3> K;
165  typedef K::Point Point3;
166  vector<K::SCell> v3;
167  K ks;
168  v3.push_back(ks.sCell(Point3(1,1,0)));
169  v3.push_back(ks.sCell(Point3(2,1,1)));
170  v3.push_back(ks.sCell(Point3(3,1,2)));
171  //... transformed into inner voxels,
172  //which are projected into 2d points
173  typedef SpaceND<2> S;
174  typedef S::Point Point2;
176 
179  functors::Projector<S>, Point2 > c(f,p);
180 
183  functors::Projector<S>, Point2 >, Point2 > PointRange;
184  PointRange r3(v3.begin(), v3.end(), c);
185 
187  testRangeConceptChecking<SimpleRange>();
188  testRangeConceptChecking<BoolRange>();
189  testRangeConceptChecking<PointRange>();
190 
192  bool res = testRange(r1)
193  && testRange(r2)
194  && testRange(r3);
195 
196  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
197  trace.endBlock();
198  return res ? 0 : 1;
199 }
void beginBlock(const std::string &keyword="")
Aim: Defines the concept describing a bidirectional const range.
bool testRange(Container c)
Trace trace
Definition: Common.h:137
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()
bool isValid() const
Definition: Circulator.h:206
Aim: Functor that maps a point P of dimension i to a point Q of dimension j. The member myDims is an ...
Aim: Define a simple default functor that just returns its argument.
Aim: Define a new Functor from the composition of two other functors.
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
bool isNotEmpty(const IC &itb, const IC &ite, IteratorType)
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
std::ostream & emphase()
std::reverse_iterator< ConstIterator > ConstReverseIterator
int main(int argc, char **argv)
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
Aim: model of CConstBidirectionalRange that adapts any range of elements bounded by two iterators [it...
std::ostream & info()
std::reverse_iterator< ConstCirculator > ConstReverseCirculator
KSpace K
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
Aim: transforms a signed cell c into a point corresponding to the signed cell of greater dimension th...