DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testHyperRectDomain.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/SpaceND.h"
#include "DGtal/kernel/PointVector.h"
#include "DGtal/kernel/domains/HyperRectDomain.h"
#include "DGtal/base/CConstBidirectionalRange.h"
#include "DGtalCatch.h"

Go to the source code of this file.

Functions

 TEST_CASE ("Simple HyperRectDomain", "[domain][4D]")
 
template<typename Iterator , typename Point , typename Domain , typename Dimension >
void testIteratorHelperImpl (Iterator &it, Iterator const &it_begin, Iterator const &it_end, typename std::iterator_traits< Iterator >::difference_type &cnt_begin, Point &pt, Domain const &domain, std::vector< Dimension > const &dimensions, std::size_t id, bool forward)
 Checking iterator given then span domain and dimensions order. More...
 
template<typename Iterator , typename Point , typename Domain , typename Dimension >
void testIteratorHelper (Iterator const &it_begin, Iterator const &it_end, Point pt, Domain const &domain, std::vector< Dimension > const &dimensions, bool forward=true)
 
template<typename Iterator , typename Point , typename Domain >
void testIteratorHelper (Iterator const &it_begin, Iterator const &it_end, Point pt, Domain const &domain, bool forward=true)
 
template<typename Point >
void testIterator (Point const &a, Point const &b, Point const &c)
 
 TEST_CASE ("Iterator 2D", "[iterator][2D]")
 
 TEST_CASE ("Iterator 4D", "[iterator][4D]")
 
 TEST_CASE ("Iterator 4D GMP", "[iterator][4D][GMP]")
 
 TEST_CASE ("STL compatiblity", "[iterator][4D][STL]")
 
 TEST_CASE ("Empty domain", "[domain][3D][empty]")
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr )
Guillaume Damiand
Date
2010/05/25

This file is part of the DGtal library

Definition in file testHyperRectDomain.cpp.

Function Documentation

◆ TEST_CASE() [1/6]

TEST_CASE ( "Empty domain"  ,
""  [domain][3D][empty] 
)

Definition at line 372 of file testHyperRectDomain.cpp.

373{
374 typedef SpaceND<3> TSpace;
375 typedef TSpace::Point TPoint;
376 typedef HyperRectDomain<TSpace> TDomain;
377
378 const TDomain nonempty_domain( TPoint::diagonal(0), TPoint::diagonal(0) );
379 REQUIRE( !nonempty_domain.isEmpty() );
380
381 const TDomain default_domain;
382 REQUIRE( default_domain.isEmpty() );
383
384 const TDomain domain( TPoint::diagonal(1), TPoint::diagonal(0) );
386 REQUIRE( domain.size() == 0 );
387 REQUIRE( domain.begin() == domain.end() );
388 REQUIRE( domain.rbegin() == domain.rend() );
389
390 TDomain::ConstSubRange range = domain.subRange( 0, 1, 2, domain.lowerBound() );
391 REQUIRE( range.begin() == range.end() );
392 REQUIRE( range.rbegin() == range.rend() );
393
394 range = domain.subRange( 2, 1, 0, domain.lowerBound() );
395 REQUIRE( range.begin() == range.end() );
396 REQUIRE( range.rbegin() == range.rend() );
397
398 range = domain.subRange( 0, 2, domain.lowerBound() );
399 REQUIRE( range.begin() == range.end() );
400 REQUIRE( range.rbegin() == range.rend() );
401
402 range = domain.subRange( 2, 0, domain.lowerBound() );
403 REQUIRE( range.begin() == range.end() );
404 REQUIRE( range.rbegin() == range.rend() );
405
406 range = domain.subRange( 1, domain.lowerBound() );
407 REQUIRE( range.begin() == range.end() );
408 REQUIRE( range.rbegin() == range.rend() );
409}
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
const ConstIterator & begin() const
const Point & lowerBound() const
ConstReverseIterator rbegin() const
ConstSubRange subRange(const std::vector< Dimension > &permutation) const
ConstReverseIterator rend() const
const ConstIterator & end() const
Domain domain
REQUIRE(domain.isInside(aPoint))

References DGtal::HyperRectDomain< TSpace >::begin(), domain, DGtal::HyperRectDomain< TSpace >::end(), DGtal::HyperRectDomain< TSpace >::isEmpty(), DGtal::HyperRectDomain< TSpace >::lowerBound(), DGtal::HyperRectDomain< TSpace >::rbegin(), DGtal::HyperRectDomain< TSpace >::rend(), REQUIRE(), DGtal::HyperRectDomain< TSpace >::size(), and DGtal::HyperRectDomain< TSpace >::subRange().

◆ TEST_CASE() [2/6]

TEST_CASE ( "Iterator 2D"  ,
""  [iterator][2D] 
)

Definition at line 306 of file testHyperRectDomain.cpp.

307{
308 using Space = SpaceND<2>;
309 using Point = Space::Point;
310
311 Point a (1, 1);
312 Point b (4, 5);
313 Point c (2, 2);
314
315 trace.beginBlock( "Iterator 2D" );
316 testIterator(a, b, c);
317 trace.endBlock();
318}
PointVector< dim, Integer > Point
Points in DGtal::SpaceND.
Definition: SpaceND.h:110
void beginBlock(const std::string &keyword="")
double endBlock()
Trace trace
Definition: Common.h:154
MyPointD Point
Definition: testClone2.cpp:383
void testIterator(Point const &a, Point const &b, Point const &c)

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), testIterator(), and DGtal::trace.

◆ TEST_CASE() [3/6]

TEST_CASE ( "Iterator 4D GMP"  ,
""  [iterator][4D][GMP] 
)

Definition at line 335 of file testHyperRectDomain.cpp.

336{
338 using Point = Space::Point;
339
340 Point a({1, 1, 1, 1});
341 Point b({2, 3, 4, 5});
342 Point c({1, 2, 3, 2});
343
344 trace.beginBlock( "Iterator 4D using GMP" );
345 testIterator(a, b, c);
346 trace.endBlock();
347}

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), testIterator(), and DGtal::trace.

◆ TEST_CASE() [4/6]

TEST_CASE ( "Iterator 4D"  ,
""  [iterator][4D] 
)

Definition at line 320 of file testHyperRectDomain.cpp.

321{
322 using Space = SpaceND<4>;
323 using Point = Space::Point;
324
325 Point a({1, 1, 1, 1});
326 Point b({2, 3, 4, 5});
327 Point c({1, 2, 3, 2});
328
329 trace.beginBlock( "Iterator 4D" );
330 testIterator(a, b, c);
331 trace.endBlock();
332}

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), testIterator(), and DGtal::trace.

◆ TEST_CASE() [5/6]

TEST_CASE ( "Simple HyperRectDomain"  ,
""  [domain][4D] 
)

Definition at line 56 of file testHyperRectDomain.cpp.

57{
58 typedef SpaceND<4> Space4Type;
59 typedef Space4Type::Point Point;
60 typedef Space4Type::RealPoint RealPoint;
61
62 Space4Type::Integer t [] = { 1, 2, 3 , 4};
63 Point a ( t );
64 Space4Type::Integer t2[] = { 5, 5, 3 , 4};
65 Point b ( t2 );
66 double td [] = { 1.1, 2.5, 3 , 4};
67 RealPoint c ( td );
68 double td2[] = { 4.9, 4.5, 3 , 4};
69 RealPoint d ( td2 );
70
71 trace.beginBlock ( "HyperRectDomain init" );
72
73 // Checking that HyperRectDomain is a model of CDomain.
74 typedef HyperRectDomain<Space4Type> HRDomain4;
75 BOOST_CONCEPT_ASSERT(( concepts::CDomain< HRDomain4 > ));
76 BOOST_CONCEPT_ASSERT(( concepts::CConstBidirectionalRange<HRDomain4> ));
77
78 // Empty domain using the default constructor
79 trace.info() << "Empty domain using the default constructor" << std::endl;
80 HyperRectDomain<Space4Type> myEmptyDomain;
81
82 trace.info() << "Empty domain = " << myEmptyDomain << std::endl;
83 REQUIRE( myEmptyDomain.isValid() );
84 REQUIRE( myEmptyDomain.isEmpty() );
85 REQUIRE( myEmptyDomain.size() == 0 );
86 REQUIRE( (myEmptyDomain.end() - myEmptyDomain.begin()) == 0 );
87 REQUIRE( (myEmptyDomain.begin() - myEmptyDomain.end()) == 0 );
88 REQUIRE( (myEmptyDomain.rend() - myEmptyDomain.rbegin()) == 0 );
89 REQUIRE( (myEmptyDomain.rbegin() - myEmptyDomain.rend()) == 0 );
90
91 // Domain characterized by points a and b
92 trace.info() << "Domain characterized by points a and b" << std::endl;
93 HyperRectDomain<Space4Type> myHyperRectDomain ( a, b );
94
95 trace.info() << "Domain = " << myHyperRectDomain << std::endl;
96 REQUIRE( myHyperRectDomain.isValid() );
97 REQUIRE( myHyperRectDomain.lowerBound() == a );
98 REQUIRE( myHyperRectDomain.upperBound() == b );
99
100 trace.info() << "Domain size = " << myHyperRectDomain.size() << std::endl;
101 REQUIRE( myHyperRectDomain.size() == 20 );
102 REQUIRE( (myHyperRectDomain.end() - myHyperRectDomain.begin()) == 20 );
103 REQUIRE( (myHyperRectDomain.begin() - myHyperRectDomain.end()) == -20 );
104 REQUIRE( (myHyperRectDomain.rend() - myHyperRectDomain.rbegin()) == 20 );
105 REQUIRE( (myHyperRectDomain.rbegin() - myHyperRectDomain.rend()) == -20 );
106
107 // Domain initialized with RealPoint
108 trace.info() << "Domain initialized with RealPoint" << std::endl;
109 HyperRectDomain<Space4Type> myHyperRectDomain_rr ( c, d );
110 trace.info() << "Domain = " << myHyperRectDomain_rr << std::endl;
111 REQUIRE( myHyperRectDomain_rr.isValid() );
112 REQUIRE( myHyperRectDomain_rr.lowerBound() == myHyperRectDomain.lowerBound() );
113 REQUIRE( myHyperRectDomain_rr.upperBound() == myHyperRectDomain.upperBound() );
114
115 HyperRectDomain<Space4Type> myHyperRectDomain_ir ( a, d );
116 trace.info() << "Domain = " << myHyperRectDomain_ir << std::endl;
117 REQUIRE( myHyperRectDomain_ir.isValid() );
118 REQUIRE( myHyperRectDomain_ir.lowerBound() == myHyperRectDomain.lowerBound() );
119 REQUIRE( myHyperRectDomain_ir.upperBound() == myHyperRectDomain.upperBound() );
120
121 HyperRectDomain<Space4Type> myHyperRectDomain_ri ( c, b );
122 trace.info() << "Domain = " << myHyperRectDomain_ri << std::endl;
123 REQUIRE( myHyperRectDomain_ri.isValid() );
124 REQUIRE( myHyperRectDomain_ri.lowerBound() == myHyperRectDomain.lowerBound() );
125 REQUIRE( myHyperRectDomain_ri.upperBound() == myHyperRectDomain.upperBound() );
126
127 trace.endBlock();
128
129 // Test Copy Constructor
130 trace.beginBlock("Test Copy Constructor");
131 HyperRectDomain<Space4Type> myHyperRectDomainBis( myHyperRectDomain );
132 trace.info() << "Domain = " << myHyperRectDomainBis << std::endl;
133 trace.info() << "Domain size = " << myHyperRectDomainBis.size() << std::endl;
134 REQUIRE( myHyperRectDomainBis.isValid() );
135 REQUIRE( myHyperRectDomainBis.lowerBound() == myHyperRectDomain.lowerBound() );
136 REQUIRE( myHyperRectDomainBis.upperBound() == myHyperRectDomain.upperBound() );
137 REQUIRE( myHyperRectDomainBis.size() == 20 );
138 trace.endBlock();
139
140 // Test Assignement
141 trace.beginBlock("Test Assignement");
142 HyperRectDomain<Space4Type> myHyperRectDomainTer;
143 myHyperRectDomainTer = myHyperRectDomain;
144 trace.info() << "Domain = " << myHyperRectDomainTer << std::endl;
145 trace.info() << "Domain size = " << myHyperRectDomainTer.size() << std::endl;
146 REQUIRE( myHyperRectDomainTer.isValid() );
147 REQUIRE( myHyperRectDomainTer.lowerBound() == myHyperRectDomain.lowerBound() );
148 REQUIRE( myHyperRectDomainTer.upperBound() == myHyperRectDomain.upperBound() );
149 REQUIRE( myHyperRectDomainTer.size() == 20 );
150 trace.endBlock();
151}
const Point & upperBound() const
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
std::ostream & info()
Aim: Defines the concept describing a bidirectional const range.
Aim: This concept represents a digital domain, i.e. a non mutable subset of points of the given digit...
Definition: CDomain.h:130
Z2i::RealPoint RealPoint

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::Trace::beginBlock(), DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::HyperRectDomain< TSpace >::isEmpty(), DGtal::HyperRectDomain< TSpace >::isValid(), DGtal::HyperRectDomain< TSpace >::lowerBound(), DGtal::HyperRectDomain< TSpace >::rbegin(), DGtal::HyperRectDomain< TSpace >::rend(), REQUIRE(), DGtal::HyperRectDomain< TSpace >::size(), DGtal::trace, and DGtal::HyperRectDomain< TSpace >::upperBound().

◆ TEST_CASE() [6/6]

TEST_CASE ( "STL compatiblity"  ,
""  [iterator][4D][STL] 
)

Definition at line 350 of file testHyperRectDomain.cpp.

351{
352 typedef SpaceND<4> TSpace4D;
353 typedef TSpace4D::Point Point4D;
354 TSpace4D::Integer t[] = {1, 1, 1, 1};
355 Point4D a4D (t);
356 TSpace4D::Integer t2[] = {3, 3, 3, 3};
357 Point4D b4D (t2);
358
359 trace.beginBlock ( "TestSTL Compatibility" );
360
361 HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
362 trace.emphase() << myHyperRectDomain4D << std::endl;
363
364 std::copy ( myHyperRectDomain4D.begin(),
365 myHyperRectDomain4D.end(),
366 ostream_iterator<Point4D> ( trace.info(), " " ) );
367
368 trace.info() << std::endl;
369 trace.endBlock();
370}
std::ostream & emphase()

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

◆ testIterator()

template<typename Point >
void testIterator ( Point const &  a,
Point const &  b,
Point const &  c 
)

Definition at line 251 of file testHyperRectDomain.cpp.

252{
253 using Dimension = typename Point::Dimension;
256
257 Domain domain(a, b);
258 trace.info() << "Domain = " << domain << std::endl;
259
260 trace.emphase() << "Iterator" << std::endl;
262
263 trace.emphase() << "Reverse iterator" << std::endl;
265
266 trace.emphase() << "Iterator from starting point" << std::endl;
268
269 trace.emphase() << "Reverse iterator from starting point" << std::endl;
271
272 trace.emphase() << "Iterator on reversed dimension order: " << std::endl;
273 std::vector<Dimension> dimensions(Point::dimension);
274 std::iota(dimensions.rbegin(), dimensions.rend(), Dimension(0));
275 const auto range = domain.subRange(dimensions);
276 testIteratorHelper(range.begin(), range.end(), domain.lowerBound(), domain, dimensions);
277
278 trace.emphase() << "Reverse iterator on reversed dimension order: " << std::endl;
279 testIteratorHelper(range.rbegin(), range.rend(), domain.upperBound(), domain, dimensions, false);
280
281 trace.emphase() << "Iterator on reversed dimension order and from a starting point: " << std::endl;
282 const auto range2 = domain.subRange(dimensions);
283 testIteratorHelper(range2.begin(c), range2.end(), c, domain, dimensions);
284
285 trace.emphase() << "Reverse iterator on reversed dimension order and from a starting point: " << std::endl;
286 testIteratorHelper(range2.rbegin(c), range2.rend(), c, domain, dimensions, false);
287
288 trace.emphase() << "Iterator along one dimension: " << std::endl;
289 const auto range3 = domain.subRange({1});
290 const std::vector<Dimension> one_dimension({1});
291 testIteratorHelper(range3.begin(), range3.end(), domain.lowerBound(), domain, one_dimension);
292
293 trace.emphase() << "Reverse iterator along one dimension: " << std::endl;
294 auto upper_one_dim = domain.lowerBound();
295 upper_one_dim.partialCopy(domain.upperBound(), one_dimension);
296 testIteratorHelper(range3.rbegin(), range3.rend(), upper_one_dim, domain, one_dimension, false);
297
298 trace.emphase() << "Iterator along one dimension and from a starting point: " << std::endl;
299 const auto range4 = domain.subRange({1}, c);
300 testIteratorHelper(range4.begin(c), range4.end(), c, domain, one_dimension);
301
302 trace.emphase() << "Reverse iterator along one dimension and from a starting point: " << std::endl;
303 testIteratorHelper(range4.rbegin(c), range4.rend(), c, domain, one_dimension, false);
304}
Self & partialCopy(const PointVector< dim, OtherComponent, OtherContainer > &pv, const std::vector< Dimension > &dimensions)
Partial copy of a given PointVector.
DGtal::uint32_t Dimension
Definition: Common.h:137
void testIteratorHelper(Iterator const &it_begin, Iterator const &it_end, Point pt, Domain const &domain, std::vector< Dimension > const &dimensions, bool forward=true)

References DGtal::HyperRectDomain< TSpace >::begin(), domain, DGtal::Trace::emphase(), DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::info(), DGtal::HyperRectDomain< TSpace >::lowerBound(), DGtal::PointVector< dim, TEuclideanRing, TContainer >::partialCopy(), DGtal::HyperRectDomain< TSpace >::rbegin(), DGtal::HyperRectDomain< TSpace >::rend(), DGtal::HyperRectDomain< TSpace >::subRange(), testIteratorHelper(), DGtal::trace, and DGtal::HyperRectDomain< TSpace >::upperBound().

Referenced by TEST_CASE().

◆ testIteratorHelper() [1/2]

template<typename Iterator , typename Point , typename Domain >
void testIteratorHelper ( Iterator const &  it_begin,
Iterator const &  it_end,
Point  pt,
Domain const &  domain,
bool  forward = true 
)

Definition at line 237 of file testHyperRectDomain.cpp.

240{
241 using Dimension = typename Domain::Dimension;
242 std::vector<Dimension> dimensions(Domain::dimension);
243 std::iota(dimensions.begin(), dimensions.end(), Dimension(0));
244
245 testIteratorHelper(it_begin, it_end, pt, domain, dimensions, forward);
246}

References domain, and testIteratorHelper().

◆ testIteratorHelper() [2/2]

template<typename Iterator , typename Point , typename Domain , typename Dimension >
void testIteratorHelper ( Iterator const &  it_begin,
Iterator const &  it_end,
Point  pt,
Domain const &  domain,
std::vector< Dimension > const &  dimensions,
bool  forward = true 
)

Definition at line 217 of file testHyperRectDomain.cpp.

221{
222 Iterator it = it_begin;
223 typename std::iterator_traits<Iterator>::difference_type cnt_begin = 0;
224
226 it, it_begin, it_end, cnt_begin,
227 pt, domain, dimensions, dimensions.size(),
228 forward);
229 REQUIRE( it == it_end );
230}
void testIteratorHelperImpl(Iterator &it, Iterator const &it_begin, Iterator const &it_end, typename std::iterator_traits< Iterator >::difference_type &cnt_begin, Point &pt, Domain const &domain, std::vector< Dimension > const &dimensions, std::size_t id, bool forward)
Checking iterator given then span domain and dimensions order.

References domain, REQUIRE(), DGtal::HyperRectDomain< TSpace >::size(), and testIteratorHelperImpl().

Referenced by testIterator(), and testIteratorHelper().

◆ testIteratorHelperImpl()

template<typename Iterator , typename Point , typename Domain , typename Dimension >
void testIteratorHelperImpl ( Iterator &  it,
Iterator const &  it_begin,
Iterator const &  it_end,
typename std::iterator_traits< Iterator >::difference_type &  cnt_begin,
Point pt,
Domain const &  domain,
std::vector< Dimension > const &  dimensions,
std::size_t  id,
bool  forward 
)

Checking iterator given then span domain and dimensions order.

Definition at line 163 of file testHyperRectDomain.cpp.

168{
169 if (id == 0)
170 {
171 trace.warning() << *it << std::endl;
172 REQUIRE( it != it_end );
173 REQUIRE( pt == *it );
174 REQUIRE( std::distance(it_begin, it) == cnt_begin );
175 REQUIRE( std::distance(it, it_begin) == -cnt_begin );
176 INFO( *(it_begin + cnt_begin) << " == " << *it );
177 REQUIRE( it_begin + cnt_begin == it );
178 INFO( *(it - cnt_begin) << " == " << *it_begin);
179 REQUIRE( it - cnt_begin == it_begin );
180 ++it;
181 ++cnt_begin;
182 return;
183 }
184
185 --id;
186
187 const auto d = dimensions[id];
188
189 if (forward)
190 {
191 while ( pt[d] <= domain.upperBound()[d] )
192 {
193 testIteratorHelperImpl(it, it_begin, it_end, cnt_begin, pt, domain, dimensions, id, forward);
194 ++pt[d];
195 }
196
197 pt[d] = domain.lowerBound()[d];
198 }
199 else
200 {
201 while ( pt[d] >= domain.lowerBound()[d] )
202 {
203 testIteratorHelperImpl(it, it_begin, it_end, cnt_begin, pt, domain, dimensions, id, forward);
204 --pt[d];
205 }
206
207 pt[d] = domain.upperBound()[d];
208 }
209}
std::ostream & warning()

References domain, DGtal::HyperRectDomain< TSpace >::lowerBound(), REQUIRE(), testIteratorHelperImpl(), DGtal::trace, DGtal::HyperRectDomain< TSpace >::upperBound(), and DGtal::Trace::warning().

Referenced by testIteratorHelper(), and testIteratorHelperImpl().