DGtal 1.3.0
Loading...
Searching...
No Matches
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
43using namespace DGtal;
44using namespace std;
45
46template <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
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 }
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
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);
109
113 PointVector<2, int> pt(5,5);
114 PointVector<2, int> pt_2(10, 9);
115 PointVector<3, int> ptR(4,5,6);
116 PointVector<3, int> ptR2(0, 1, 7);
117
118 trace.info() << "pt " << pt << " => " << sliceRot(pt) << " == " << ptR << std::endl;
119 nbok += ( sliceRot(pt) == ptR ) ? 1 : 0;
120 nb++;
121
122 trace.info() << "pt " << pt_2 << " => " << sliceRot2(pt_2) << " == " << ptR2 << std::endl;
123 nbok += ( sliceRot2(pt_2) == ptR2 ) ? 1 : 0;
124 nb++;
125
126 //Point2DEmbedderIn3D
127 PointVector<3,int> ptOrigin3D(3,3,3);
129 ptOrigin3D,
130 PointVector<3,int>(6,6,3),
131 PointVector<3,int>(3,3,5),
132 PointVector<3,int>(0,0,0));
133 PointVector<2, int> ptb(0, 0);
134 PointVector<2, int> pt_2b(4, 2);
135 trace.info() << "pt " << ptb << " => " << embedder(ptb) << " == " << PointVector<3,int>(3,3,3) << std::endl;
136 nbok += ( embedder(ptb) == PointVector<3,int>(3,3,3) ) ? 1 : 0;
137 nb++;
138
139 trace.info() << "pt " << pt_2b << " => " << embedder(pt_2b) << " == " << PointVector<3,int>(5,5,5) << std::endl;
140 nbok += ( embedder(pt_2b) == PointVector<3,int>(5,5,5) ) ? 1 : 0;
141 nb++;
142
143 //Point2DEmbedderIn3D (constructor from normal point)
144 PointVector<3,int> pt2Origin3D(5,5,3);
146 pt2Origin3D,
147 PointVector<3,int>(0,0,3),
148 4);
149 PointVector<2, int> pt2b(0, 0);
150 PointVector<2, int> pt2_2b(2, 2);
151 trace.info() << "pt " << pt2b << " => " << embedder2(pt2b) << " == " << PointVector<3,int>(3,5,3) << std::endl;
152 nbok += ( embedder2(pt2b) == PointVector<3,int>(3,5,3) ) ? 1 : 0;
153 nb++;
154
155 trace.info() << "pt " << pt2_2b << " => " << embedder2(pt2_2b) << " == " << PointVector<3,int>(5,4,3) << std::endl;
156 nbok += ( embedder2(pt2_2b) == PointVector<3,int>(5,4,3) ) ? 1 : 0;
157 nb++;
158
159 //Point2DEmbedderIn3D (constructor from normal point)
160 PointVector<3,int> dirXaxis(1,0,0);
162 int> embedder3(domain,
163 pt2Origin3D,
164 PointVector<3,int>(0,0,3),
165 dirXaxis, 4);
166 PointVector<2, int> pt2c(0, 0);
167 PointVector<2, int> pt2_2c(2, 0);
168 trace.info() << "pt " << pt2c << " => " << embedder3(pt2c) << " == " << PointVector<3,int>(3,7,3) << std::endl;
169 nbok += ( embedder3(pt2c) == PointVector<3,int>(3,7,3) ) ? 1 : 0;
170 nb++;
171
172 trace.info() << "pt " << pt2_2c << " => " << embedder3(pt2_2c) << " == " << PointVector<3,int>(5,7,3) << std::endl;
173 nbok += ( embedder3(pt2_2c) == PointVector<3,int>(5,7,3) ) ? 1 : 0;
174 nb++;
175
176 // testing shift functor
177 embedder3.shiftOriginPoint(Z3i::RealPoint(2, 0.4, 0.0));
178 trace.info() << "pt " << pt2_2c << " => " << embedder3(pt2_2c) << " == " << PointVector<3,int>(7,7,3) << std::endl;
179 nbok += ( embedder3(pt2_2c) == PointVector<3,int>(7,7,3) ) ? 1 : 0;
180 nb++;
181
182 //comparison
183 PointVector<3,int> res1(5,0,2);
184 trace.info() << "p " << p << " => " << proj1(p) << " == " << res1 << std::endl;
185 nbok += ( proj1(p) == res1 ) ? 1 : 0;
186 nb++;
187
188 PointVector<3,int> res2(2,5,0);
189 trace.info() << "p " << p << " => " << proj2(p) << " == " << res2 << std::endl;
190 nbok += ( proj2(p) == res2 ) ? 1 : 0;
191 nb++;
192
193 PointVector<3,int> res3(5,2,0);
194 trace.info() << "p " << p << " => " << proj3(p) << " == " << res3 << std::endl;
195 nbok += ( proj3(p) == res3 ) ? 1 : 0;
196 nb++;
197
198 PointVector<3,int> res4(2,-1,-1);
199 trace.info() << "p " << p << " => " << proj4(p) << " == " << res4
200 << "(-1 as default value)" << std::endl;
201 nbok += ( proj4(p) == res4 ) ? 1 : 0;
202 nb++;
203 }
204 trace.endBlock();
205
206
207 trace.beginBlock ( "Checking Basic Domain SubSampler" );
208 {
209 // BasicDomainSubSampler 2D
211 std::vector< SpaceND<2, int>::Size > aGridSize;
212 aGridSize.push_back(5);
213 aGridSize.push_back(5);
214 PointVector<2,int> shiftVector(0 ,0);
216 aGridSize,
217 shiftVector);
218 trace.info()<< "Subsampling functor on 2D domain " << domainSource <<" with grid size "
219 << aGridSize[0] << " " << aGridSize[1] << " and shift vector "<< shiftVector <<std::endl ;
220 PointVector<2,int> pointTest(1,0);
221 PointVector<2,int> pointInSourceDomain = subSampler(pointTest);
222 trace.info() << "Sampling point of coordinate "<< pointTest << ", => coordinates in source domain:"
223 << pointInSourceDomain << " == " << PointVector<2,int>(5,0) << std::endl;
224 nb++;
225 nbok += (pointInSourceDomain== PointVector<2,int>(5,0));
226
227 // BasicDomainSubSampler 3D
228 HyperRectDomain<SpaceND<3, int> > domainSource3D (PointVector<3,int>(0,0, 0), PointVector<3,int>(10,10, 10));
229 std::vector< SpaceND<3, int>::Size > aGridSize3D;
230 aGridSize3D.push_back(5);
231 aGridSize3D.push_back(3);
232 aGridSize3D.push_back(1);
233 PointVector<3,int> shiftVector3D(0 ,1, -1);
235 aGridSize3D, shiftVector3D);
236 trace.info()<< "Subsampling functor on 3D domain " << domainSource3D <<" with grid size "
237 << aGridSize3D[0] << " " << aGridSize3D[1]<< " " << aGridSize3D[2] << " and shift vector "<< shiftVector3D <<std::endl ;
238 PointVector<3,int> pointTest3D(0,1,2);
239 PointVector<3,int> pointTest3D2(0,0,0);
240 PointVector<3,int> pointInSourceDomain3D = subSampler3D(pointTest3D);
241 PointVector<3,int> pointInSourceDomain3D2 = subSampler3D(pointTest3D2);
242 trace.info() << "Sampling point of coordinate "<< pointTest3D << ", => coordinates in source domain:"
243 << pointInSourceDomain3D << " == " << PointVector<3,int>(0, 4, 1) << std::endl;
244 trace.info() << "Sampling point of coordinate "<< pointTest3D2 << ", => coordinates in source domain:"
245 << pointInSourceDomain3D2 << " == " << PointVector<3,int>(0, 1, 0) << std::endl;
246 nb++;
247 nbok += (pointInSourceDomain3D== PointVector<3,int>(0, 4, 1)) &&
248 (pointInSourceDomain3D2== PointVector<3,int>(0, 1, 0));
249
250 // FlipDomainAxis
251 std::vector<HyperRectDomain<SpaceND<3, int> >::Size> vectFlip;
252 vectFlip.push_back(1);
253 vectFlip.push_back(2);
254 functors::FlipDomainAxis<HyperRectDomain<SpaceND<3, int> > > flipFunctorAxis12(domainSource3D, vectFlip);
255 trace.info() << "Flip point of coordinate "<< pointTest3D << ", => fliped coordinates with axis 1 and 2:"
256 << flipFunctorAxis12(pointTest3D) << " == " << PointVector<3,int>(0, 9, 8) << std::endl;
257 nb++;
258 nbok += (flipFunctorAxis12(pointTest3D)== PointVector<3,int>(0, 9, 8));
259
260
261
262 // BasicDomainReSampler 2D
263 std::vector< double > aGridSizeReSample;
264 aGridSizeReSample.push_back(0.25);
265 aGridSizeReSample.push_back(0.5);
267 DGtal::int32_t, double > reSampler(domainSource,
268 aGridSizeReSample, shiftVector);
269
270 trace.info()<< "Resampling functor on 2D domain " << domainSource <<" with grid size "
271 << aGridSizeReSample[0] << " " << aGridSizeReSample[1] << " and shift vector "<< shiftVector <<std::endl ;
272 PointVector<2,int> pointTestRS(9,4);
273 PointVector<2,int> pointInSourceDomainRS = reSampler(pointTestRS);
274 trace.info() << "Sampling point of coordinate "<< pointTestRS << ", => coordinates in source domain:"
275 << pointInSourceDomainRS << " == " << PointVector<2,int>(2,2) << std::endl;
276 nb++;
277 nbok += (pointInSourceDomainRS== PointVector<2,int>(2,2));
278
279
280
281 }
282 return nbok == nb;
283}
284
286// Standard services - public :
287
288int main( int argc, char** argv )
289{
290 trace.beginBlock ( "Testing basic point functors" );
291 trace.info() << "Args:";
292 for ( int i = 0; i < argc; ++i )
293 trace.info() << " " << argv[ i ];
294 trace.info() << endl;
295
296
297 checkingConcepts<functors::Projector<SpaceND<2,int> >, PointVector<6,int>, PointVector<2,int> >();
298 //for instance, this does not compile because
299 //the point of dim 6 is projected on a point of dim 2 (and not 3)
300 //checkingConcepts<Projector<SpaceND<2,int> >, PointVector<6,int>, PointVector<3,int> >();
301
302 bool res = testProjector();
303
304 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
305 trace.endBlock();
306 return res ? 0 : 1;
307}
308
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Aim: Functor that subsamples an initial domain by given a grid size and a shift vector....
Aim: Functor that flips the domain coordinate system from some selected axis. For instance,...
Aim: Functor that embeds a 2D point into a 3D space from two axis vectors and an origin point given i...
void shiftOriginPoint(const typename Space::RealPoint &shift)
Special Point Functor that adds one dimension to a 2D point and apply on it a rotation of angle alpha...
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
STL namespace.
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:90
Aim: Functor that maps a point P of dimension i to a point Q of dimension j. The member myDims is an ...
void init(const TIterator &itb, const TIterator &ite)
bool testProjector()
void checkingConcepts()
int main()
Definition: testBits.cpp:56
HalfEdgeDataStructure::Size Size
Domain domain