DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testBasicPointFunctors.cpp
1 
28 #include <cstdio>
29 #include <cmath>
30 #include <iostream>
31 #include <fstream>
32 #include <vector>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/base/CUnaryFunctor.h"
35 #include "DGtal/kernel/PointVector.h"
36 #include "DGtal/kernel/SpaceND.h"
37 #include "DGtal/kernel/BasicPointFunctors.h"
38 
39 using namespace DGtal;
40 using namespace std;
41 
42 template <typename TFunctor, typename TArg, typename TRes >
43 void checkingConcepts()
44 {
45  BOOST_CONCEPT_ASSERT(( CUnaryFunctor<TFunctor, TArg, TRes > ));
46 }
47 
48 bool testProjector()
49 {
50  unsigned int nbok = 0;
51  unsigned int nb = 0;
52 
53  trace.beginBlock ( "Checking projection on a subspace" );
54  {
55  //a 3d point
56  PointVector<3,int> p(0,1,2);
57 
58  //projectors
59  typedef Projector<SpaceND<2,int> > Projector2D;
60  std::vector<Dimension> v1, v2;
61  v1.push_back(0); v1.push_back(2);
62  v2.push_back(2); v2.push_back(1);
63  Projector2D proj1, proj2, proj3;
64  proj1.init(v1.begin(), v1.end());
65  proj2.init(v2.begin(), v2.end());
66 
67  //comparison
68  PointVector<2,int> res1(0,2);
69  trace.info() << "p " << p << " => " << proj1(p) << " == " << res1 << std::endl;
70  nbok += ( proj1(p) == res1 ) ? 1 : 0;
71  nb++;
72 
73  PointVector<2,int> res2(2,1);
74  trace.info() << "p " << p << " => " << proj2(p) << " == " << res2 << std::endl;
75  nbok += ( proj2(p) == res2 ) ? 1 : 0;
76  nb++;
77 
78  PointVector<2,int> res3(0,1);
79  trace.info() << "p " << p << " => " << proj3(p) << " == " << res3 << std::endl;
80  nbok += ( proj3(p) == res3 ) ? 1 : 0;
81  nb++;
82  }
83  trace.endBlock();
84 
85  trace.beginBlock ( "Checking projection on a greater space" );
86  {
87  //a 2d point
88  PointVector<2,int> p(5,2);
89 
90  //projectors
91  typedef Projector<SpaceND<3,int> > Projector3D;
92  std::vector<Dimension> v1, v2, v4;
93  v1.push_back(0); v1.push_back(2); v1.push_back(1);
94  v2.push_back(1); v2.push_back(0);
95  v4.push_back(1);
96  Projector3D proj1, proj2, proj3;
97  proj1.init(v1.begin(), v1.end());
98  proj2.init(v2.begin(), v2.end());
99  Projector3D proj4(-1);
100  proj4.init(v4.begin(), v4.end());
101 
102  //comparison
103  PointVector<3,int> res1(5,0,2);
104  trace.info() << "p " << p << " => " << proj1(p) << " == " << res1 << std::endl;
105  nbok += ( proj1(p) == res1 ) ? 1 : 0;
106  nb++;
107 
108  PointVector<3,int> res2(2,5,0);
109  trace.info() << "p " << p << " => " << proj2(p) << " == " << res2 << std::endl;
110  nbok += ( proj2(p) == res2 ) ? 1 : 0;
111  nb++;
112 
113  PointVector<3,int> res3(5,2,0);
114  trace.info() << "p " << p << " => " << proj3(p) << " == " << res3 << std::endl;
115  nbok += ( proj3(p) == res3 ) ? 1 : 0;
116  nb++;
117 
118  PointVector<3,int> res4(2,-1,-1);
119  trace.info() << "p " << p << " => " << proj4(p) << " == " << res4
120  << "(-1 as default value)" << std::endl;
121  nbok += ( proj4(p) == res4 ) ? 1 : 0;
122  nb++;
123  }
124  trace.endBlock();
125 
126  return nbok == nb;
127 }
128 
130 // Standard services - public :
131 
132 int main( int argc, char** argv )
133 {
134  trace.beginBlock ( "Testing basic point functors" );
135  trace.info() << "Args:";
136  for ( int i = 0; i < argc; ++i )
137  trace.info() << " " << argv[ i ];
138  trace.info() << endl;
139 
140 
141  checkingConcepts<Projector<SpaceND<2,int> >, PointVector<6,int>, PointVector<2,int> >();
142  //for instance, this does not compile because
143  //the point of dim 6 is projected on a point of dim 2 (and not 3)
144  //checkingConcepts<Projector<SpaceND<2,int> >, PointVector<6,int>, PointVector<3,int> >();
145 
146  bool res = testProjector();
147 
148  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
149  trace.endBlock();
150  return res ? 0 : 1;
151 }
152