DGtal 1.4.0
Loading...
Searching...
No Matches
testSimpleRandomAccessRangeFromPoint.cpp
Go to the documentation of this file.
1
27#include "DGtalCatch.h"
28
29#include <DGtal/kernel/SpaceND.h>
30#include <DGtal/kernel/domains/HyperRectDomain.h>
31#include <DGtal/kernel/domains/Linearizer.h>
32#include <DGtal/images/ImageContainerBySTLVector.h>
33
34#include <algorithm>
35#include <cstddef>
36
37TEST_CASE( "Testing SimpleRandomAccess(Const)RangeFromPoint from ImageContainerBySTLVector", "" )
38{
39 using namespace DGtal;
40
41 typedef double Value;
49
50 const Domain domain( Point(1,2), Point(6,5) );
51 const Point aPoint(3,4);
52 REQUIRE( domain.isInside(aPoint) );
53
54 Image image(domain);
56
57 // Initialization
58 const double hour = 4.29; // Oups
59 std::size_t cnt = 0;
60 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end; ++it, ++cnt )
61 {
62 image.setValue(*it, cnt*hour);
63 refImage.setValue(*it, cnt*hour);
64 }
65
66 SECTION( "Testing constant forward iterators" )
67 {
68 const Range range = image.range();
69 REQUIRE( static_cast<Domain::Size>(range.end() - range.begin()) == domain.size() );
70 REQUIRE( std::equal(range.begin(), range.end(), refImage.begin()) );
71
72 ConstRange crange = image.constRange();
73 REQUIRE( static_cast<Domain::Size>(crange.end() - crange.begin()) == domain.size() );
74 REQUIRE( std::equal(crange.begin(), crange.end(), refImage.begin()) );
75 }
76
77 SECTION( "Testing constant forward iterators from a point" )
78 {
79 const Range range = image.range();
80 REQUIRE( static_cast<Domain::Size>(range.end() - range.begin(aPoint)) == domain.size() - Linearizer::getIndex(aPoint, domain) );
81 REQUIRE( std::equal( range.begin(aPoint), range.end(), refImage.begin() + Linearizer::getIndex(aPoint, domain) ) );
82
83 ConstRange crange = image.constRange();
84 REQUIRE( static_cast<Domain::Size>(crange.end() - crange.begin(aPoint)) == domain.size() - Linearizer::getIndex(aPoint, domain) );
85 REQUIRE( std::equal( crange.begin(aPoint), crange.end(), refImage.begin() + Linearizer::getIndex(aPoint, domain) ) );
86 }
87
88 SECTION( "Testing mutable forward iterators" )
89 {
90 Range range = image.range();
91 REQUIRE( static_cast<Domain::Size>(range.end() - range.begin()) == domain.size() );
92
93 cnt = 1;
94 for ( Range::Iterator it = range.begin(), it_end = range.end(); it != it_end; ++it )
95 {
96 *it += cnt++;
97 }
98
99 cnt = 1;
100 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end; ++it )
101 {
102 refImage.setValue( *it, refImage(*it)+cnt );
103 ++cnt;
104 }
105
106 REQUIRE( std::equal(range.begin(), range.end(), refImage.begin()) );
107 }
108
109 SECTION( "Testing mutable forward iterators from a point" )
110 {
111 Range range = image.range();
112 REQUIRE( static_cast<Domain::Size>(range.end() - range.begin(aPoint)) == domain.size() - Linearizer::getIndex(aPoint, domain) );
113
114 cnt = 1;
115 for ( Range::Iterator it = range.begin(aPoint), it_end = range.end(); it != it_end; ++it )
116 {
117 *it += cnt++;
118 }
119
120 cnt = 1;
121 for ( Domain::ConstIterator it = domain.begin(aPoint), it_end = domain.end(); it != it_end; ++it )
122 {
123 refImage.setValue( *it, refImage(*it)+cnt );
124 ++cnt;
125 }
126
127 REQUIRE( std::equal(range.begin(), range.end(), refImage.begin()) );
128 }
129
130 SECTION( "Testing constant reverse iterators" )
131 {
132 const Range range = image.range();
133 REQUIRE( static_cast<Domain::Size>(range.rend() - range.rbegin()) == domain.size() );
134 REQUIRE( std::equal(range.rbegin(), range.rend(), refImage.rbegin()) );
135
136 ConstRange crange = image.constRange();
137 REQUIRE( static_cast<Domain::Size>(crange.rend() - crange.rbegin()) == domain.size() );
138 REQUIRE( std::equal(crange.rbegin(), crange.rend(), refImage.rbegin()) );
139 }
140
141 SECTION( "Testing constant reverse iterators from a point" )
142 {
143 const Range range = image.range();
144 REQUIRE( static_cast<Domain::Size>(range.rend() - range.rbegin(aPoint)) == Linearizer::getIndex(aPoint, domain) + 1 );
145 REQUIRE( std::equal( range.rbegin(aPoint), range.rend(), refImage.rbegin() + (domain.size() - Linearizer::getIndex(aPoint, domain) - 1) ) );
146
147 ConstRange crange = image.constRange();
148 REQUIRE( static_cast<Domain::Size>(crange.rend() - crange.rbegin(aPoint)) == Linearizer::getIndex(aPoint, domain) + 1 );
149 REQUIRE( std::equal( crange.rbegin(aPoint), crange.rend(), refImage.rbegin() + (domain.size() - Linearizer::getIndex(aPoint, domain) - 1) ) );
150 }
151
152 SECTION( "Testing mutable reverse iterators" )
153 {
154 Range range = image.range();
155 REQUIRE( static_cast<Domain::Size>(range.rend() - range.rbegin()) == domain.size() );
156
157 cnt = 1;
158 for ( Range::ReverseIterator it = range.rbegin(), it_end = range.rend(); it != it_end; ++it )
159 {
160 *it += cnt++;
161 }
162
163 cnt = 1;
164 for ( Domain::ConstReverseIterator it = domain.rbegin(), it_end = domain.rend(); it != it_end; ++it )
165 {
166 refImage.setValue( *it, refImage(*it)+cnt );
167 ++cnt;
168 }
169
170 REQUIRE( std::equal(range.rbegin(), range.rend(), refImage.rbegin()) );
171 }
172
173 SECTION( "Testing mutable reverse iterators from a point" )
174 {
175 Range range = image.range();
176 REQUIRE( static_cast<Domain::Size>(range.rend() - range.rbegin(aPoint)) == Linearizer::getIndex(aPoint, domain) + 1 );
177
178 cnt = 1;
179 for ( Range::ReverseIterator it = range.rbegin(aPoint), it_end = range.rend(); it != it_end; ++it )
180 {
181 *it += cnt++;
182 }
183
184
185 cnt = 1;
186 for ( Domain::ConstReverseIterator it = domain.rbegin(aPoint), it_end = domain.rend(); it != it_end; ++it )
187 {
188 refImage.setValue( *it, refImage(*it)+cnt );
189 ++cnt;
190 }
191
192 REQUIRE( std::equal(range.rbegin(), range.rend(), refImage.rbegin()) );
193 }
194
195 SECTION( "Testing forward output iterators" )
196 {
197 Range range = image.range();
198
199 cnt = 1;
200 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end; ++it )
201 {
202 refImage.setValue( *it, refImage(*it)+cnt );
203 ++cnt;
204 }
205
206 std::copy( refImage.begin(), refImage.end(), range.outputIterator() );
207
208 REQUIRE( std::equal(range.begin(), range.end(), refImage.begin()) );
209 }
210
211 SECTION( "Testing forward output iterators from a point" )
212 {
213 Range range = image.range();
214
215 cnt = 1;
216 for ( Domain::ConstIterator it = domain.begin(aPoint), it_end = domain.end(); it != it_end; ++it )
217 {
218 refImage.setValue( *it, refImage(*it)+cnt );
219 ++cnt;
220 }
221
222 std::copy( refImage.begin() + Linearizer::getIndex(aPoint, domain), refImage.end(), range.outputIterator(aPoint) );
223
224 REQUIRE( std::equal(range.begin(), range.end(), refImage.begin()) );
225 }
226
227 SECTION( "Testing reverse output iterators" )
228 {
229 Range range = image.range();
230
231 cnt = 1;
232 for ( Domain::ConstReverseIterator it = domain.rbegin(), it_end = domain.rend(); it != it_end; ++it )
233 {
234 refImage.setValue( *it, refImage(*it)+cnt );
235 ++cnt;
236 }
237
238 std::copy( refImage.rbegin(), refImage.rend(), range.routputIterator() );
239
240 REQUIRE( std::equal(range.rbegin(), range.rend(), refImage.rbegin()) );
241 }
242
243 SECTION( "Testing reverse output iterators from a point" )
244 {
245 Range range = image.range();
246
247 cnt = 1;
248 for ( Domain::ConstReverseIterator it = domain.rbegin(aPoint), it_end = domain.rend(); it != it_end; ++it )
249 {
250 refImage.setValue( *it, refImage(*it)+cnt );
251 ++cnt;
252 }
253
254 std::copy( refImage.rbegin() + (domain.size() - Linearizer::getIndex(aPoint, domain) - 1), refImage.rend(), range.routputIterator(aPoint) );
255
256 REQUIRE( std::equal(range.rbegin(), range.rend(), refImage.rbegin()) );
257 }
258
259 SECTION( "Testing constant forward circulators" )
260 {
261 // Reference sum
262 Value refSum = 0;
263 for ( Image::ConstIterator it = refImage.begin(), it_end = refImage.end(); it != it_end; ++it )
264 {
265 refSum += 2 * (*it);
266 }
267
268 // Sum in forward way
269 const Range range = image.range();
270 Value sum = 0;
271 cnt = 1;
272 for ( Range::ConstCirculator it = range.c(); cnt <= 2*domain.size(); ++it )
273 {
274 sum += *it;
275 ++cnt;
276 }
277 REQUIRE( sum == Approx(refSum) );
278 REQUIRE( std::equal( refImage.begin(), refImage.end(), range.c() ) );
279
280 // Sum in backward way
281 sum = 0;
282 cnt = 1;
283 for ( Range::ConstCirculator it = range.c(); cnt <= 2*domain.size(); --it )
284 {
285 sum += *it;
286 ++cnt;
287 }
288 REQUIRE( sum == Approx(refSum) );
289
290 // Sum in forward way
291 ConstRange crange = image.constRange();
292 sum = 0;
293 cnt = 1;
294 for ( Range::ConstCirculator it = crange.c(); cnt <= 2*domain.size(); ++it )
295 {
296 sum += *it;
297 ++cnt;
298 }
299 REQUIRE( sum == Approx(refSum) );
300 REQUIRE( std::equal( refImage.begin(), refImage.end(), crange.c() ) );
301
302 // Sum in backward way
303 sum = 0;
304 cnt = 1;
305 for ( Range::ConstCirculator it = crange.c(); cnt <= 2*domain.size(); --it )
306 {
307 sum += *it;
308 ++cnt;
309 }
310 REQUIRE( sum == Approx(refSum) );
311 }
312
313 SECTION( "Testing constant reverse circulators" )
314 {
315 // Reference sum
316 Value refSum = 0;
317 for ( Image::ConstIterator it = refImage.begin(), it_end = refImage.end(); it != it_end; ++it )
318 {
319 refSum += 2 * (*it);
320 }
321
322 // Sum in forward way
323 const Range range = image.range();
324 Value sum = 0;
325 cnt = 1;
326 for ( Range::ConstReverseCirculator it = range.rc(); cnt <= 2*domain.size(); ++it )
327 {
328 sum += *it;
329 ++cnt;
330 }
331 REQUIRE( sum == Approx(refSum) );
332 REQUIRE( std::equal( refImage.rbegin(), refImage.rend(), range.rc() ) );
333
334 // Sum in backward way
335 sum = 0;
336 cnt = 1;
337 for ( Range::ConstReverseCirculator it = range.rc(); cnt <= 2*domain.size(); --it )
338 {
339 sum += *it;
340 ++cnt;
341 }
342 REQUIRE( sum == Approx(refSum) );
343
344 // Sum in forward way
345 ConstRange crange = image.constRange();
346 sum = 0;
347 cnt = 1;
348 for ( Range::ConstReverseCirculator it = crange.rc(); cnt <= 2*domain.size(); ++it )
349 {
350 sum += *it;
351 ++cnt;
352 }
353 REQUIRE( sum == Approx(refSum) );
354 REQUIRE( std::equal( refImage.rbegin(), refImage.rend(), crange.rc() ) );
355
356 // Sum in backward way
357 sum = 0;
358 cnt = 1;
359 for ( Range::ConstReverseCirculator it = crange.rc(); cnt <= 2*domain.size(); --it )
360 {
361 sum += *it;
362 ++cnt;
363 }
364 REQUIRE( sum == Approx(refSum) );
365 }
366
367 SECTION( "Testing mutable circulators in forward way" )
368 {
369 Range range = image.range();
370
371 cnt = 1;
372 for ( Range::Circulator it = range.c(); cnt <= 2*domain.size(); ++it )
373 {
374 *it += cnt++;
375 }
376
377 cnt = 1;
378 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end; ++it )
379 {
380 refImage.setValue( *it, refImage(*it) + 2*cnt + domain.size());
381 ++cnt;
382 }
383
384 REQUIRE( std::equal( range.begin(), range.end(), refImage.begin() ) );
385 }
386
387 SECTION( "Testing mutable circulators in backward way" )
388 {
389 Range range = image.range();
390
391 cnt = 1;
392 for ( Range::Circulator it = --range.c(); cnt <= 2*domain.size(); --it )
393 {
394 *it += cnt++;
395 }
396
397 cnt = 1;
398 for ( Domain::ConstReverseIterator it = domain.rbegin(), it_end = domain.rend(); it != it_end; ++it )
399 {
400 refImage.setValue( *it, refImage(*it) + 2*cnt + domain.size());
401 ++cnt;
402 }
403
404 REQUIRE( std::equal( range.begin(), range.end(), refImage.begin() ) );
405 }
406
407 SECTION( "Testing mutable reverse circulators in forward way" )
408 {
409 Range range = image.range();
410
411 cnt = 1;
412 for ( Range::ReverseCirculator it = range.rc(); cnt <= 2*domain.size(); ++it )
413 {
414 *it += cnt++;
415 }
416
417 cnt = 1;
418 for ( Domain::ConstReverseIterator it = domain.rbegin(), it_end = domain.rend(); it != it_end; ++it )
419 {
420 refImage.setValue( *it, refImage(*it) + 2*cnt + domain.size());
421 ++cnt;
422 }
423
424 REQUIRE( std::equal( range.begin(), range.end(), refImage.begin() ) );
425 }
426
427 SECTION( "Testing mutable reverse circulators in backward way" )
428 {
429 Range range = image.range();
430
431 cnt = 1;
432 for ( Range::ReverseCirculator it = --range.rc(); cnt <= 2*domain.size(); --it )
433 {
434 *it += cnt++;
435 }
436
437 cnt = 1;
438 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end; ++it )
439 {
440 refImage.setValue( *it, refImage(*it) + 2*cnt + domain.size());
441 ++cnt;
442 }
443
444 REQUIRE( std::equal( range.begin(), range.end(), refImage.begin() ) );
445 }
446}
447
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition Circulator.h:86
Reverse iterator for HyperRectDomain.
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Aim: model of CConstBidirectionalRangeFromPoint that adapts any range of elements bounded by two iter...
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
std::reverse_iterator< Circulator > ReverseCirculator
std::reverse_iterator< ConstCirculator > ConstReverseCirculator
PointVector< dim, Integer > Point
Points in DGtal::SpaceND.
Definition SpaceND.h:110
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
TEST_CASE("int container traits", "[int][traits]")
Domain domain
Linearizer< Domain, ColMajorStorage > Linearizer
ImageContainerBySTLVector< Domain, Value > Image
const Point aPoint(3, 4)
Image::ConstRange ConstRange
SECTION("Testing constant forward iterators")
HyperRectDomain< Space > Domain
REQUIRE(domain.isInside(aPoint))
Image refImage(domain)