DGtal  0.9.3
testBasicPointFunctors.cpp
Go to the documentation of this file.
1 
31 #include <cstdio>
32 #include <cmath>
33 #include <iostream>
34 #include <fstream>
35 #include <vector>
36 #include "DGtal/base/Common.h"
37 #include "DGtal/base/CUnaryFunctor.h"
38 #include "DGtal/kernel/PointVector.h"
39 #include "DGtal/kernel/SpaceND.h"
40 #include "DGtal/kernel/BasicPointFunctors.h"
41 #include "DGtal/kernel/domains/HyperRectDomain.h"
42 
43 using namespace DGtal;
44 using namespace std;
45 
46 template <typename TFunctor, typename TArg, typename TRes >
48 {
49  BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, TArg, TRes > ));
50 }
51 
53 {
54  unsigned int nbok = 0;
55  unsigned int nb = 0;
56 
57  trace.beginBlock ( "Checking projection on a subspace" );
58  {
59  //a 3d point
60  PointVector<3,int> p(0,1,2);
61 
62  //projectors
63  typedef DGtal::functors::Projector<SpaceND<2,int> > Projector2D;
64  std::vector<Dimension> v1, v2;
65  v1.push_back(0); v1.push_back(2);
66  v2.push_back(2); v2.push_back(1);
67  Projector2D proj1, proj2, proj3;
68  proj1.init(v1.begin(), v1.end());
69  proj2.init(v2.begin(), v2.end());
70 
71  //comparison
72  PointVector<2,int> res1(0,2);
73  trace.info() << "p " << p << " => " << proj1(p) << " == " << res1 << std::endl;
74  nbok += ( proj1(p) == res1 ) ? 1 : 0;
75  nb++;
76 
77  PointVector<2,int> res2(2,1);
78  trace.info() << "p " << p << " => " << proj2(p) << " == " << res2 << std::endl;
79  nbok += ( proj2(p) == res2 ) ? 1 : 0;
80  nb++;
81 
82  PointVector<2,int> res3(0,1);
83  trace.info() << "p " << p << " => " << proj3(p) << " == " << res3 << std::endl;
84  nbok += ( proj3(p) == res3 ) ? 1 : 0;
85  nb++;
86  }
87  trace.endBlock();
88 
89  trace.beginBlock ( "Checking projection on a greater space" );
90  {
91  //a 2d point
92  PointVector<2,int> p(5,2);
93 
94  //projectors
95  typedef DGtal::functors::Projector<SpaceND<3,int> > Projector3D;
96  std::vector<Dimension> v1, v2, v4;
97  v1.push_back(0); v1.push_back(2); v1.push_back(1);
98  v2.push_back(1); v2.push_back(0);
99  v4.push_back(1);
100  Projector3D proj1, proj2, proj3;
101  proj1.init(v1.begin(), v1.end());
102  proj2.init(v2.begin(), v2.end());
103  Projector3D proj4(-1);
104  proj4.init(v4.begin(), v4.end());
105 
106  //SliceRotator2D
107  PointVector<3, int> pt1(0,0, 0);
108  PointVector<3, int> pt2(10,10, 10);
110  DGtal::functors::SliceRotator2D< HyperRectDomain<SpaceND<3, int> >, int> sliceRot(2, domain, 6, 1, pt1, 0.1);
111  DGtal::functors::SliceRotator2D< HyperRectDomain<SpaceND<3, int> >, int> sliceRot2(2, domain, 7, 2, 3.14);
112  PointVector<2, int> pt(5,5);
113  PointVector<2, int> pt_2(10, 9);
114  PointVector<3, int> ptR(4,5,6);
115  PointVector<3, int> ptR2(0, 1, 7);
116 
117  trace.info() << "pt " << pt << " => " << sliceRot(pt) << " == " << ptR << std::endl;
118  nbok += ( sliceRot(pt) == ptR ) ? 1 : 0;
119  nb++;
120 
121  trace.info() << "pt " << pt_2 << " => " << sliceRot2(pt_2) << " == " << ptR2 << std::endl;
122  nbok += ( sliceRot2(pt_2) == ptR2 ) ? 1 : 0;
123  nb++;
124 
125  //Point2DEmbedderIn3D
126  PointVector<3,int> ptOrigin3D(3,3,3);
128  ptOrigin3D,
129  PointVector<3,int>(6,6,3),
130  PointVector<3,int>(3,3,5),
131  PointVector<3,int>(0,0,0));
132  PointVector<2, int> ptb(0, 0);
133  PointVector<2, int> pt_2b(4, 2);
134  trace.info() << "pt " << ptb << " => " << embedder(ptb) << " == " << PointVector<3,int>(3,3,3) << std::endl;
135  nbok += ( embedder(ptb) == PointVector<3,int>(3,3,3) ) ? 1 : 0;
136  nb++;
137 
138  trace.info() << "pt " << pt_2b << " => " << embedder(pt_2b) << " == " << PointVector<3,int>(5,5,5) << std::endl;
139  nbok += ( embedder(pt_2b) == PointVector<3,int>(5,5,5) ) ? 1 : 0;
140  nb++;
141 
142  //Point2DEmbedderIn3D (constructor from normal point)
143  PointVector<3,int> pt2Origin3D(5,5,3);
145  pt2Origin3D,
146  PointVector<3,int>(0,0,3),
147  4);
148  PointVector<2, int> pt2b(0, 0);
149  PointVector<2, int> pt2_2b(2, 2);
150  trace.info() << "pt " << pt2b << " => " << embedder2(pt2b) << " == " << PointVector<3,int>(3,5,3) << std::endl;
151  nbok += ( embedder2(pt2b) == PointVector<3,int>(3,5,3) ) ? 1 : 0;
152  nb++;
153 
154  trace.info() << "pt " << pt2_2b << " => " << embedder2(pt2_2b) << " == " << PointVector<3,int>(5,4,3) << std::endl;
155  nbok += ( embedder2(pt2_2b) == PointVector<3,int>(5,4,3) ) ? 1 : 0;
156  nb++;
157 
158 
159  //comparison
160  PointVector<3,int> res1(5,0,2);
161  trace.info() << "p " << p << " => " << proj1(p) << " == " << res1 << std::endl;
162  nbok += ( proj1(p) == res1 ) ? 1 : 0;
163  nb++;
164 
165  PointVector<3,int> res2(2,5,0);
166  trace.info() << "p " << p << " => " << proj2(p) << " == " << res2 << std::endl;
167  nbok += ( proj2(p) == res2 ) ? 1 : 0;
168  nb++;
169 
170  PointVector<3,int> res3(5,2,0);
171  trace.info() << "p " << p << " => " << proj3(p) << " == " << res3 << std::endl;
172  nbok += ( proj3(p) == res3 ) ? 1 : 0;
173  nb++;
174 
175  PointVector<3,int> res4(2,-1,-1);
176  trace.info() << "p " << p << " => " << proj4(p) << " == " << res4
177  << "(-1 as default value)" << std::endl;
178  nbok += ( proj4(p) == res4 ) ? 1 : 0;
179  nb++;
180  }
181  trace.endBlock();
182 
183 
184  trace.beginBlock ( "Checking Basic Domain SubSampler" );
185  {
186  // BasicDomainSubSampler 2D
188  std::vector< SpaceND<2, int>::Size > aGridSize;
189  aGridSize.push_back(5);
190  aGridSize.push_back(5);
191  PointVector<2,int> shiftVector(0 ,0);
193  aGridSize, shiftVector);
194  trace.info()<< "Subsampling functor on 2D domain " << domainSource <<" with grid size "
195  << aGridSize[0] << " " << aGridSize[1] << " and shift vector "<< shiftVector <<std::endl ;
196  PointVector<2,int> pointTest(1,0);
197  PointVector<2,int> pointInSourceDomain = subSampler(pointTest);
198  trace.info() << "Sampling point of coordinate "<< pointTest << ", => coordinates in source domain:"
199  << pointInSourceDomain << " == " << PointVector<2,int>(5,0) << std::endl;
200  nb++;
201  nbok += (pointInSourceDomain== PointVector<2,int>(5,0));
202 
203  // BasicDomainSubSampler 3D
204  HyperRectDomain<SpaceND<3, int> > domainSource3D (PointVector<3,int>(0,0, 0), PointVector<3,int>(10,10, 10));
205  std::vector< SpaceND<3, int>::Size > aGridSize3D;
206  aGridSize3D.push_back(5);
207  aGridSize3D.push_back(3);
208  aGridSize3D.push_back(1);
209  PointVector<3,int> shiftVector3D(0 ,1, -1);
211  aGridSize3D, shiftVector3D);
212  trace.info()<< "Subsampling functor on 3D domain " << domainSource3D <<" with grid size "
213  << aGridSize3D[0] << " " << aGridSize3D[1]<< " " << aGridSize3D[2] << " and shift vector "<< shiftVector3D <<std::endl ;
214  PointVector<3,int> pointTest3D(0,1,2);
215  PointVector<3,int> pointTest3D2(0,0,0);
216  PointVector<3,int> pointInSourceDomain3D = subSampler3D(pointTest3D);
217  PointVector<3,int> pointInSourceDomain3D2 = subSampler3D(pointTest3D2);
218  trace.info() << "Sampling point of coordinate "<< pointTest3D << ", => coordinates in source domain:"
219  << pointInSourceDomain3D << " == " << PointVector<3,int>(0, 4, 1) << std::endl;
220  trace.info() << "Sampling point of coordinate "<< pointTest3D2 << ", => coordinates in source domain:"
221  << pointInSourceDomain3D2 << " == " << PointVector<3,int>(0, 1, 0) << std::endl;
222  nb++;
223  nbok += (pointInSourceDomain3D== PointVector<3,int>(0, 4, 1)) &&
224  (pointInSourceDomain3D2== PointVector<3,int>(0, 1, 0));
225 
226  // FlipDomainAxis
227  std::vector<unsigned int> vectFlip;
228  vectFlip.push_back(1);
229  vectFlip.push_back(2);
230  functors::FlipDomainAxis<HyperRectDomain<SpaceND<3, int> > > flipFunctorAxis12(domainSource3D, vectFlip);
231  trace.info() << "Flip point of coordinate "<< pointTest3D << ", => fliped coordinates with axis 1 and 2:"
232  << flipFunctorAxis12(pointTest3D) << " == " << PointVector<3,int>(0, 9, 8) << std::endl;
233  nb++;
234  nbok += (flipFunctorAxis12(pointTest3D)== PointVector<3,int>(0, 9, 8));
235 
236 
237 
238  // BasicDomainReSampler 2D
239  std::vector< double > aGridSizeReSample;
240  aGridSizeReSample.push_back(0.25);
241  aGridSizeReSample.push_back(0.5);
243  DGtal::int32_t, double > reSampler(domainSource,
244  aGridSizeReSample, shiftVector);
245 
246  trace.info()<< "Resampling functor on 2D domain " << domainSource <<" with grid size "
247  << aGridSizeReSample[0] << " " << aGridSizeReSample[1] << " and shift vector "<< shiftVector <<std::endl ;
248  PointVector<2,int> pointTestRS(9,4);
249  PointVector<2,int> pointInSourceDomainRS = reSampler(pointTestRS);
250  trace.info() << "Sampling point of coordinate "<< pointTestRS << ", => coordinates in source domain:"
251  << pointInSourceDomainRS << " == " << PointVector<2,int>(2,2) << std::endl;
252  nb++;
253  nbok += (pointInSourceDomainRS== PointVector<2,int>(2,2));
254 
255 
256 
257  }
258  return nbok == nb;
259 }
260 
262 // Standard services - public :
263 
264 int main( int argc, char** argv )
265 {
266  trace.beginBlock ( "Testing basic point functors" );
267  trace.info() << "Args:";
268  for ( int i = 0; i < argc; ++i )
269  trace.info() << " " << argv[ i ];
270  trace.info() << endl;
271 
272 
273  checkingConcepts<functors::Projector<SpaceND<2,int> >, PointVector<6,int>, PointVector<2,int> >();
274  //for instance, this does not compile because
275  //the point of dim 6 is projected on a point of dim 2 (and not 3)
276  //checkingConcepts<Projector<SpaceND<2,int> >, PointVector<6,int>, PointVector<3,int> >();
277 
278  bool res = testProjector();
279 
280  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
281  trace.endBlock();
282  return res ? 0 : 1;
283 }
284 
void beginBlock(const std::string &keyword="")
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
void checkingConcepts()
bool testProjector()
Aim: Parallelepidec region of a digital space, model of a &#39;CDomain&#39;.
STL namespace.
double endBlock()
Aim: Functor that maps a point P of dimension i to a point Q of dimension j. The member myDims is an ...
Aim: Functor that subsamples an initial domain by given a grid size and a shift vector. By this way, for a given point considered in a new domain, it allows to recover the point coordinates in the source domain. Such functor can be usefull to apply basic image subsampling in any dimensions by using ImageAdapter class.
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:89
std::ostream & emphase()
Aim: Functor that embeds a 2D point into a 3D space from two axis vectors and an origin point given i...
Aim: Functor that flips the domain coordinate system from some selected axis. For instance...
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
int main(int argc, char **argv)
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
Special Point Functor that adds one dimension to a 2D point and apply on it a rotation of angle alpha...