DGtal  1.5.beta
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"
Include dependency graph for testHyperRectDomain.cpp:

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 370 of file testHyperRectDomain.cpp.

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

305 {
306  using Space = SpaceND<2>;
307  using Point = Space::Point;
308 
309  Point a (1, 1);
310  Point b (4, 5);
311  Point c (2, 2);
312 
313  trace.beginBlock( "Iterator 2D" );
314  testIterator(a, b, c);
315  trace.endBlock();
316 }
void beginBlock(const std::string &keyword="")
double endBlock()
Trace trace
Definition: Common.h:153
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 333 of file testHyperRectDomain.cpp.

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

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

◆ TEST_CASE() [4/6]

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

Definition at line 318 of file testHyperRectDomain.cpp.

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

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;
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
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
PointVector< 3, double > 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 348 of file testHyperRectDomain.cpp.

349 {
350  typedef SpaceND<4> TSpace4D;
351  typedef TSpace4D::Point Point4D;
352  TSpace4D::Integer t[] = {1, 1, 1, 1};
353  Point4D a4D (t);
354  TSpace4D::Integer t2[] = {3, 3, 3, 3};
355  Point4D b4D (t2);
356 
357  trace.beginBlock ( "TestSTL Compatibility" );
358 
359  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
360  trace.emphase() << myHyperRectDomain4D << std::endl;
361 
362  std::copy ( myHyperRectDomain4D.begin(),
363  myHyperRectDomain4D.end(),
364  ostream_iterator<Point4D> ( trace.info(), " " ) );
365 
366  trace.info() << std::endl;
367  trace.endBlock();
368 }
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 250 of file testHyperRectDomain.cpp.

251 {
254 
255  Domain domain(a, b);
256  trace.info() << "Domain = " << domain << std::endl;
257 
258  trace.emphase() << "Iterator" << std::endl;
260 
261  trace.emphase() << "Reverse iterator" << std::endl;
263 
264  trace.emphase() << "Iterator from starting point" << std::endl;
266 
267  trace.emphase() << "Reverse iterator from starting point" << std::endl;
268  testIteratorHelper(domain.rbegin(c), domain.rend(), c, domain, false);
269 
270  trace.emphase() << "Iterator on reversed dimension order: " << std::endl;
271  std::vector<Dimension> dimensions(Point::dimension);
272  std::iota(dimensions.rbegin(), dimensions.rend(), Dimension(0));
273  const auto range = domain.subRange(dimensions);
274  testIteratorHelper(range.begin(), range.end(), domain.lowerBound(), domain, dimensions);
275 
276  trace.emphase() << "Reverse iterator on reversed dimension order: " << std::endl;
277  testIteratorHelper(range.rbegin(), range.rend(), domain.upperBound(), domain, dimensions, false);
278 
279  trace.emphase() << "Iterator on reversed dimension order and from a starting point: " << std::endl;
280  const auto range2 = domain.subRange(dimensions);
281  testIteratorHelper(range2.begin(c), range2.end(), c, domain, dimensions);
282 
283  trace.emphase() << "Reverse iterator on reversed dimension order and from a starting point: " << std::endl;
284  testIteratorHelper(range2.rbegin(c), range2.rend(), c, domain, dimensions, false);
285 
286  trace.emphase() << "Iterator along one dimension: " << std::endl;
287  const auto range3 = domain.subRange({1});
288  const std::vector<Dimension> one_dimension({1});
289  testIteratorHelper(range3.begin(), range3.end(), domain.lowerBound(), domain, one_dimension);
290 
291  trace.emphase() << "Reverse iterator along one dimension: " << std::endl;
292  auto upper_one_dim = domain.lowerBound();
293  upper_one_dim.partialCopy(domain.upperBound(), one_dimension);
294  testIteratorHelper(range3.rbegin(), range3.rend(), upper_one_dim, domain, one_dimension, false);
295 
296  trace.emphase() << "Iterator along one dimension and from a starting point: " << std::endl;
297  const auto range4 = domain.subRange({1}, c);
298  testIteratorHelper(range4.begin(c), range4.end(), c, domain, one_dimension);
299 
300  trace.emphase() << "Reverse iterator along one dimension and from a starting point: " << std::endl;
301  testIteratorHelper(range4.rbegin(c), range4.rend(), c, domain, one_dimension, false);
302 }
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:136
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  std::vector<Dimension> dimensions(Domain::dimension);
242  std::iota(dimensions.begin(), dimensions.end(), Dimension(0));
243 
244  testIteratorHelper(it_begin, it_end, pt, domain, dimensions, forward);
245 }

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(), DGtal::trace, DGtal::HyperRectDomain< TSpace >::upperBound(), and DGtal::Trace::warning().

Referenced by testIteratorHelper().