DGtal  0.9.2
testHyperRectDomain.cpp
1 
33 #include <cstdio>
34 #include <cmath>
35 #include <iostream>
36 #include <fstream>
37 #include <algorithm>
38 
39 #include "DGtal/base/Common.h"
40 #include "DGtal/kernel/SpaceND.h"
41 #include "DGtal/kernel/PointVector.h"
42 #include "DGtal/kernel/domains/HyperRectDomain.h"
43 #include "DGtal/base/CConstBidirectionalRange.h"
44 
45 using namespace DGtal;
46 using namespace std;
47 
48 
53 bool testSimpleHyperRectDomain()
54 {
55 
56  typedef SpaceND<4> Space4Type;
57  typedef Space4Type::Point Point;
58 
59  Space4Type::Integer t [] = { 1, 2, 3 , 4};
60  Point a ( t );
61  Space4Type::Integer t2[] = { 5, 5, 3 , 4};
62  Point b ( t2 );
63 
64  trace.beginBlock ( "HyperRectDomain init" );
65 
66  // Checking that HyperRectDomain is a model of CDomain.
67  typedef HyperRectDomain<Space4Type> HRDomain4;
68  BOOST_CONCEPT_ASSERT(( concepts::CDomain< HRDomain4 > ));
69  BOOST_CONCEPT_ASSERT(( concepts::CConstBidirectionalRange<HRDomain4> ));
70 
72  HyperRectDomain<Space4Type> myEmptyDomain;
73  trace.info() << "Empty Domain: " << myEmptyDomain << std::endl;
74 
76  HyperRectDomain<Space4Type> myHyperRectDomain ( a, b );
77  trace.info() << myHyperRectDomain << std::endl;
78 
79  trace.info() << "Domain Size= " << myHyperRectDomain.size() << std::endl;
80 
81 
82  trace.endBlock();
83 
84 
85  trace.beginBlock("Test Copy Constructor");
86  HyperRectDomain<Space4Type> myHyperRectDomainBis( myHyperRectDomain );
87  trace.info() << "Domain Size= " << myHyperRectDomainBis.size() << std::endl;
88  trace.endBlock();
89 
90  trace.beginBlock("Test Assignement");
91  HyperRectDomain<Space4Type> myHyperRectDomainTer;
92 
93  myHyperRectDomainTer = myHyperRectDomain;
94 
95  trace.info() << "Domain Size= " << myHyperRectDomainTer.size() << std::endl;
96  trace.endBlock();
97 
98  return myHyperRectDomain.isValid();
99 
100 }
101 
102 bool testIterator()
103 {
104  typedef SpaceND<2> TSpace;
105  typedef TSpace::Point Point;
106  Point a ( 1, 1);
107  Point b ( 5, 5);
108  Point c (2, 2);
109 
110  trace.beginBlock ( "HyperRectDomain Iterator" );
111  HyperRectDomain<TSpace> myHyperRectDomain ( a, b );
112 
113  trace.info() << myHyperRectDomain << std::endl;
114 
115  trace.emphase() << "Iterator 2d: ";
116  for ( HyperRectDomain<TSpace>::ConstIterator it = myHyperRectDomain.begin();
117  it != myHyperRectDomain.end(); ++it )
118  trace.warning() << ( *it ) << std::endl;
119 
120  trace.emphase() << "Reverse Iterator 2d: ";
121  for ( HyperRectDomain<TSpace>::ConstReverseIterator it = myHyperRectDomain.rbegin(),
122  itend = myHyperRectDomain.rend(); it != itend; ++it )
123  trace.warning() << ( *it ) << std::endl;
124 
125  trace.emphase() << "Iterator 2d (permutation initializer list): ";
127  it = myHyperRectDomain.subRange( {1, 0} ).begin();
128  it != myHyperRectDomain.subRange( {1, 0} ).end(); ++it )
129  trace.warning() << ( *it ) << std::endl;
130  trace.emphase() << "Reverse Iterator 2d (permutation initializer list): ";
132  it = myHyperRectDomain.subRange( {1, 0} ).rbegin(),
133  itend=myHyperRectDomain.subRange( {1, 0} ).rend(); it!=itend; ++it )
134  trace.warning() << ( *it ) << std::endl;
135 
136  trace.emphase() << "Iterator 2d (permutation+starting initializer list): ";
138  it = myHyperRectDomain.subRange( {1, 0} ).begin(c);
139  it != myHyperRectDomain.subRange( {1, 0} ).end(); ++it )
140  trace.warning() << ( *it ) << std::endl;
141  trace.emphase() << "Reverse Iterator 2d (permutation+starting initializer list): ";
143  it = myHyperRectDomain.subRange( {1, 0} ).rbegin(c),
144  itend=myHyperRectDomain.subRange( {1, 0} ).rend(); it !=itend ; ++it )
145  trace.warning() << ( *it ) << std::endl;
146 
147  trace.emphase() << "Iterator 2d (span initializer list): ";
149  it = myHyperRectDomain.subRange( {1} ).begin();
150  it != myHyperRectDomain.subRange( {1} ).end(); ++it )
151  trace.warning() << ( *it ) << std::endl;
152  trace.emphase() << "Reverse Iterator 2d (span initializer list): ";
154  it = myHyperRectDomain.subRange( {1} ).rbegin(),
155  itend=myHyperRectDomain.subRange( {1} ).rend(); it != itend; ++it )
156  trace.warning() << ( *it ) << std::endl;
157 
158  trace.emphase() << "Iterator 2d (span+starting initializer list): ";
160  it = myHyperRectDomain.subRange( {1} , c ).begin(c);
161  it != myHyperRectDomain.subRange( {1} , c ).end(); ++it )
162  trace.warning() << ( *it ) << std::endl;
163  trace.emphase() << "Reverse Iterator 2d (span+starting initializer list): ";
165  it = myHyperRectDomain.subRange( {1} , c ).rbegin(c),
166  itend=myHyperRectDomain.subRange( {1} , c ).rend(); it !=itend; ++it )
167  trace.warning() << ( *it ) << std::endl;
168 
169  trace.emphase() << "Iterator 4d: ";
170  typedef SpaceND<4> TSpace4D;
171  typedef TSpace4D::Point Point4D;
172 
173  TSpace4D::Integer t[] = {1, 1, 1, 1};
174  Point4D a4D ( t );
175  TSpace4D::Integer t2[] = {3, 3, 3, 3};
176  Point4D b4D ( t2 );
177 
178  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
179  trace.emphase() << myHyperRectDomain4D << std::endl;
181  it = myHyperRectDomain4D.begin();
182  it != myHyperRectDomain4D.end(); ++it )
183  trace.info() << ( *it ) << std::endl;
184 
185  trace.emphase() << "Reverse Iterator 4d: ";
187  it = myHyperRectDomain4D.rbegin(),
188  itend=myHyperRectDomain4D.rend(); it != itend; ++it )
189  trace.info() << ( *it ) << std::endl;
190 
191 
192  trace.emphase() << "Iterator 4d by using order different from lexicographic initializer list: ";
194  it = myHyperRectDomain4D.subRange( {3, 2, 1, 0}).begin();
195  it != myHyperRectDomain4D.subRange( {3, 2, 1, 0}).end(); ++it )
196  trace.info() << ( *it ) << std::endl;
197 
198  trace.emphase() << "Decreasing Iterator 4d by using order different from lexicographic initializer list: ";
200  it1 = myHyperRectDomain4D.subRange( {3, 2, 1, 0}).begin();
202  it2 = myHyperRectDomain4D.subRange( {3, 2, 1, 0}).end();
203  --it1;
204  --it2;
205  for ( ; it1 != it2; --it2 )
206  trace.info() << ( *it2 ) << std::endl;
207 
208  trace.emphase() << "Iterator on a subset of 4d by using order different from lexicographic initializer list: ";
210  it3 = myHyperRectDomain4D.subRange( {1, 3}).begin();
211  it3 != myHyperRectDomain4D.subRange( {1, 3}).end(); ++it3 )
212  trace.info() << ( *it3 ) << std::endl;
213 
214  trace.emphase() << "Decreasing iterator on a subset of 4d by using order different from lexicographic initializer list: ";
216  it4 = myHyperRectDomain4D.subRange({1, 3}).begin();
218  it5 = myHyperRectDomain4D.subRange({1, 3}).end();
219  --it4;
220  --it5;
221  for ( ; it4 != it5; --it5 )
222  trace.info() << ( *it5 ) << std::endl;
223 
224  return myHyperRectDomain.isValid();
225 }
226 
227 
228 bool testReverseIterator()
229 {
230  typedef SpaceND<4> TSpace4D;
231  typedef TSpace4D::Point Point4D;
232  TSpace4D::Integer t[] = {1, 1, 1, 1};
233  Point4D a4D (t);
234  TSpace4D::Integer t2[] = {3, 3, 3, 3};
235  Point4D b4D (t2);
236 
237  trace.beginBlock ( "Test reverse iterator" );
238 
239  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
240  trace.emphase() << myHyperRectDomain4D << std::endl;
241 
242  trace.emphase() << "Increasing order: ";
243 
245  it = myHyperRectDomain4D.begin();
246  for ( ; it != myHyperRectDomain4D.end(); ++it )
247  trace.info() << ( *it ) << std::endl;
248 
249  trace.emphase() << "Now decreasing order: ";
251  it2 = myHyperRectDomain4D.begin();
252  --it;
253  --it2;
254  for ( ; it != it2; --it )
255  trace.info() << ( *it ) << std::endl;
256 
257  trace.endBlock();
258 
259  return myHyperRectDomain4D.isValid();
260 }
261 
262 
263 
264 bool testSTLCompat()
265 {
266  typedef SpaceND<4> TSpace4D;
267  typedef TSpace4D::Point Point4D;
268  TSpace4D::Integer t[] = {1, 1, 1, 1};
269  Point4D a4D (t);
270  TSpace4D::Integer t2[] = {3, 3, 3, 3};
271  Point4D b4D (t2);
272 
273  trace.beginBlock ( "TestSTL Compatibility" );
274 
275  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
276  trace.emphase() << myHyperRectDomain4D << std::endl;
277 
278  std::copy ( myHyperRectDomain4D.begin(),
279  myHyperRectDomain4D.end(),
280  ostream_iterator<Point4D> ( trace.info(), " " ) );
281 
282  trace.info() << std::endl;
283  trace.endBlock();
284 
285  return myHyperRectDomain4D.isValid();
286 }
287 
288 bool testEmptyDomain()
289 {
290  typedef SpaceND<3> TSpace;
291  typedef TSpace::Point TPoint;
292  typedef HyperRectDomain<TSpace> TDomain;
293 
294  unsigned int nb = 0;
295  unsigned int nbok = 0;
296 
297  trace.beginBlock( "Test empty domain." );
298 
299  const TDomain nonempty_domain( TPoint::diagonal(0), TPoint::diagonal(0) );
300  ++nb; nbok += nonempty_domain.isEmpty() ? 0 : 1;
301  trace.info() << "(" << nbok << "/" << nb << ") Creating non-empty domain & checking isEmpty." << std::endl;
302 
303  const TDomain default_domain;
304  ++nb; nbok += default_domain.isEmpty() ? 1 : 0;
305  trace.info() << "(" << nbok << "/" << nb << ") Creating default empty domain & checking isEmpty." << std::endl;
306 
307  const TDomain domain( TPoint::diagonal(1), TPoint::diagonal(0) );
308  ++nb; nbok += domain.isEmpty() ? 1 : 0;
309  trace.info() << "(" << nbok << "/" << nb << ") Creating default custom domain & checking isEmpty." << std::endl;
310 
311  ++nb; nbok += domain.size() == 0 ? 1 : 0;
312  trace.info() << "(" << nbok << "/" << nb << ") Empty domain has size = " << domain.size() << std::endl;
313 
314  ++nb; nbok += domain.begin() == domain.end() ? 1 : 0;
315  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end()" << std::endl;
316 
317  ++nb; nbok += domain.rbegin() == domain.rend() ? 1 : 0;
318  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend()" << std::endl;
319 
320  TDomain::ConstSubRange range = domain.subRange( 0, 1, 2, domain.lowerBound() );
321  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
322  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {0,1,2}" << std::endl;
323 
324  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
325  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {0,1,2}" << std::endl;
326 
327  range = domain.subRange( 2, 1, 0, domain.lowerBound() );
328  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
329  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {2,1,0}" << std::endl;
330 
331  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
332  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {2,1,0}" << std::endl;
333 
334  range = domain.subRange( 0, 2, domain.lowerBound() );
335  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
336  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {0,2}" << std::endl;
337 
338  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
339  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {0,2}" << std::endl;
340 
341  range = domain.subRange( 2, 0, domain.lowerBound() );
342  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
343  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {2,0}" << std::endl;
344 
345  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
346  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {2,0}" << std::endl;
347 
348  range = domain.subRange( 1, domain.lowerBound() );
349  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
350  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {1}" << std::endl;
351 
352  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
353  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-rang {1}" << std::endl;
354 
355  trace.endBlock();
356  return nb == nbok;
357 
358 }
359 
360 int main()
361 {
362  if ( testSimpleHyperRectDomain() && testIterator() && testReverseIterator() && testSTLCompat() && testEmptyDomain() )
363  return 0;
364  else
365  return 1;
366 }
367 
void beginBlock(const std::string &keyword="")
Aim: Defines the concept describing a bidirectional const range.
const ConstIterator & begin() const
const ConstIterator & end() const
ConstReverseIterator rbegin() const
Trace trace
Definition: Common.h:130
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
bool isEmpty() const
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
STL namespace.
double endBlock()
ConstReverseIterator rend() const
Aim: This concept represents a digital domain, i.e. a non mutable subset of points of the given digit...
Definition: CDomain.h:129
std::ostream & emphase()
ConstSubRange subRange(const std::vector< Dimension > &permutation) const
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & warning()
std::ostream & info()
const Point & lowerBound() const