DGtal  1.0.0
testHyperRectDomain.cpp
Go to the documentation of this file.
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 
54 {
55 
56  typedef SpaceND<4> Space4Type;
57  typedef Space4Type::Point Point;
59 
60  Space4Type::Integer t [] = { 1, 2, 3 , 4};
61  Point a ( t );
62  Space4Type::Integer t2[] = { 5, 5, 3 , 4};
63  Point b ( t2 );
64  double td [] = { 1.1, 2.5, 3 , 4};
65  RealPoint c ( td );
66  double td2[] = { 4.9, 4.5, 3 , 4};
67  RealPoint d ( td2 );
68 
69  trace.beginBlock ( "HyperRectDomain init" );
70 
71  unsigned int nb = 0;
72  unsigned int nbok = 0;
73 
74  // Checking that HyperRectDomain is a model of CDomain.
75  typedef HyperRectDomain<Space4Type> HRDomain4;
76  BOOST_CONCEPT_ASSERT(( concepts::CDomain< HRDomain4 > ));
77  BOOST_CONCEPT_ASSERT(( concepts::CConstBidirectionalRange<HRDomain4> ));
78 
79  // Empty domain using the default constructor
80  HyperRectDomain<Space4Type> myEmptyDomain;
81  ++nb; nbok += myEmptyDomain.isEmpty() && myEmptyDomain.size() == 0 ? 1 : 0;
82  trace.info() << "(" << nbok << "/" << nb << ") Empty domain: " << myEmptyDomain << std::endl;
83 
84  // Domain characterized by points a and b
85  HyperRectDomain<Space4Type> myHyperRectDomain ( a, b );
86 
87  ++nb; nbok += myHyperRectDomain.lowerBound() == a && myHyperRectDomain.upperBound() == b ? 1 : 0;
88  trace.info() << "(" << nbok << "/" << nb << ") Domain = " << myHyperRectDomain << std::endl;
89 
90  ++nb; nbok += myHyperRectDomain.size() == 20 ? 1 : 0;
91  trace.info() << "(" << nbok << "/" << nb << ") Domain size = " << myHyperRectDomain.size() << std::endl;
92 
93  // Domain initialized with RealPoint
94  HyperRectDomain<Space4Type> myHyperRectDomain_rr ( c, d );
95  ++nb; nbok += myHyperRectDomain_rr.lowerBound() == myHyperRectDomain.lowerBound() && myHyperRectDomain_rr.upperBound() == myHyperRectDomain.upperBound() ? 1 : 0;
96  trace.info() << "(" << nbok << "/" << nb << ") Domain(" << c << ", " << d << ") = " << myHyperRectDomain_rr << std::endl;
97 
98  HyperRectDomain<Space4Type> myHyperRectDomain_ir ( a, d );
99  ++nb; nbok += myHyperRectDomain_ir.lowerBound() == myHyperRectDomain.lowerBound() && myHyperRectDomain_ir.upperBound() == myHyperRectDomain.upperBound() ? 1 : 0;
100  trace.info() << "(" << nbok << "/" << nb << ") Domain(" << a << ", " << d << ") = " << myHyperRectDomain_ir << std::endl;
101 
102  HyperRectDomain<Space4Type> myHyperRectDomain_ri ( c, b );
103  ++nb; nbok += myHyperRectDomain_ri.lowerBound() == myHyperRectDomain.lowerBound() && myHyperRectDomain_ri.upperBound() == myHyperRectDomain.upperBound() ? 1 : 0;
104  trace.info() << "(" << nbok << "/" << nb << ") Domain(" << c << ", " << b << ") = " << myHyperRectDomain_ri << std::endl;
105 
106  trace.endBlock();
107 
108 
109  trace.beginBlock("Test Copy Constructor");
110  HyperRectDomain<Space4Type> myHyperRectDomainBis( myHyperRectDomain );
111  ++nb; nbok += myHyperRectDomainBis.lowerBound() == myHyperRectDomain.lowerBound() && myHyperRectDomainBis.upperBound() == myHyperRectDomain.upperBound() && myHyperRectDomainBis.size() == 20 ? 1 : 0;
112  trace.info() << "(" << nbok << "/" << nb << ") Domain size= " << myHyperRectDomainBis.size() << std::endl;
113  trace.endBlock();
114 
115  trace.beginBlock("Test Assignement");
116  HyperRectDomain<Space4Type> myHyperRectDomainTer;
117 
118  myHyperRectDomainTer = myHyperRectDomain;
119 
120  ++nb; nbok += myHyperRectDomainTer.lowerBound() == myHyperRectDomain.lowerBound() && myHyperRectDomainTer.upperBound() == myHyperRectDomain.upperBound() && myHyperRectDomainTer.size() == 20 ? 1 : 0;
121  trace.info() << "(" << nbok << "/" << nb << ") Domain size= " << myHyperRectDomainTer.size() << std::endl;
122 
123  trace.endBlock();
124 
125  return myHyperRectDomain.isValid() && nb == nbok;
126 
127 }
128 
130 {
131  typedef SpaceND<2> TSpace;
132  typedef TSpace::Point Point;
133  Point a ( 1, 1);
134  Point b ( 5, 5);
135  Point c (2, 2);
136 
137  trace.beginBlock ( "HyperRectDomain Iterator" );
138  HyperRectDomain<TSpace> myHyperRectDomain ( a, b );
139 
140  trace.info() << myHyperRectDomain << std::endl;
141 
142  trace.emphase() << "Iterator 2d: ";
143  for ( HyperRectDomain<TSpace>::ConstIterator it = myHyperRectDomain.begin();
144  it != myHyperRectDomain.end(); ++it )
145  trace.warning() << ( *it ) << std::endl;
146 
147  trace.emphase() << "Reverse Iterator 2d: ";
148  for ( HyperRectDomain<TSpace>::ConstReverseIterator it = myHyperRectDomain.rbegin(),
149  itend = myHyperRectDomain.rend(); it != itend; ++it )
150  trace.warning() << ( *it ) << std::endl;
151 
152  trace.emphase() << "Iterator 2d (permutation initializer list): ";
154  it = myHyperRectDomain.subRange( {1, 0} ).begin();
155  it != myHyperRectDomain.subRange( {1, 0} ).end(); ++it )
156  trace.warning() << ( *it ) << std::endl;
157  trace.emphase() << "Reverse Iterator 2d (permutation initializer list): ";
159  it = myHyperRectDomain.subRange( {1, 0} ).rbegin(),
160  itend=myHyperRectDomain.subRange( {1, 0} ).rend(); it!=itend; ++it )
161  trace.warning() << ( *it ) << std::endl;
162 
163  trace.emphase() << "Iterator 2d (permutation+starting initializer list): ";
165  it = myHyperRectDomain.subRange( {1, 0} ).begin(c);
166  it != myHyperRectDomain.subRange( {1, 0} ).end(); ++it )
167  trace.warning() << ( *it ) << std::endl;
168  trace.emphase() << "Reverse Iterator 2d (permutation+starting initializer list): ";
170  it = myHyperRectDomain.subRange( {1, 0} ).rbegin(c),
171  itend=myHyperRectDomain.subRange( {1, 0} ).rend(); it !=itend ; ++it )
172  trace.warning() << ( *it ) << std::endl;
173 
174  trace.emphase() << "Iterator 2d (span initializer list): ";
176  it = myHyperRectDomain.subRange( {1} ).begin();
177  it != myHyperRectDomain.subRange( {1} ).end(); ++it )
178  trace.warning() << ( *it ) << std::endl;
179  trace.emphase() << "Reverse Iterator 2d (span initializer list): ";
181  it = myHyperRectDomain.subRange( {1} ).rbegin(),
182  itend=myHyperRectDomain.subRange( {1} ).rend(); it != itend; ++it )
183  trace.warning() << ( *it ) << std::endl;
184 
185  trace.emphase() << "Iterator 2d (span+starting initializer list): ";
187  it = myHyperRectDomain.subRange( {1} , c ).begin(c);
188  it != myHyperRectDomain.subRange( {1} , c ).end(); ++it )
189  trace.warning() << ( *it ) << std::endl;
190  trace.emphase() << "Reverse Iterator 2d (span+starting initializer list): ";
192  it = myHyperRectDomain.subRange( {1} , c ).rbegin(c),
193  itend=myHyperRectDomain.subRange( {1} , c ).rend(); it !=itend; ++it )
194  trace.warning() << ( *it ) << std::endl;
195 
196  trace.emphase() << "Iterator 4d: ";
197  typedef SpaceND<4> TSpace4D;
198  typedef TSpace4D::Point Point4D;
199 
200  TSpace4D::Integer t[] = {1, 1, 1, 1};
201  Point4D a4D ( t );
202  TSpace4D::Integer t2[] = {3, 3, 3, 3};
203  Point4D b4D ( t2 );
204 
205  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
206  trace.emphase() << myHyperRectDomain4D << std::endl;
208  it = myHyperRectDomain4D.begin();
209  it != myHyperRectDomain4D.end(); ++it )
210  trace.info() << ( *it ) << std::endl;
211 
212  trace.emphase() << "Reverse Iterator 4d: ";
214  it = myHyperRectDomain4D.rbegin(),
215  itend=myHyperRectDomain4D.rend(); it != itend; ++it )
216  trace.info() << ( *it ) << std::endl;
217 
218 
219  trace.emphase() << "Iterator 4d by using order different from lexicographic initializer list: ";
221  it = myHyperRectDomain4D.subRange( {3, 2, 1, 0}).begin();
222  it != myHyperRectDomain4D.subRange( {3, 2, 1, 0}).end(); ++it )
223  trace.info() << ( *it ) << std::endl;
224 
225  trace.emphase() << "Decreasing Iterator 4d by using order different from lexicographic initializer list: ";
227  it1 = myHyperRectDomain4D.subRange( {3, 2, 1, 0}).begin();
229  it2 = myHyperRectDomain4D.subRange( {3, 2, 1, 0}).end();
230  --it1;
231  --it2;
232  for ( ; it1 != it2; --it2 )
233  trace.info() << ( *it2 ) << std::endl;
234 
235  trace.emphase() << "Iterator on a subset of 4d by using order different from lexicographic initializer list: ";
237  it3 = myHyperRectDomain4D.subRange( {1, 3}).begin();
238  it3 != myHyperRectDomain4D.subRange( {1, 3}).end(); ++it3 )
239  trace.info() << ( *it3 ) << std::endl;
240 
241  trace.emphase() << "Decreasing iterator on a subset of 4d by using order different from lexicographic initializer list: ";
243  it4 = myHyperRectDomain4D.subRange({1, 3}).begin();
245  it5 = myHyperRectDomain4D.subRange({1, 3}).end();
246  --it4;
247  --it5;
248  for ( ; it4 != it5; --it5 )
249  trace.info() << ( *it5 ) << std::endl;
250 
251  return myHyperRectDomain.isValid();
252 }
253 
254 
256 {
257  typedef SpaceND<4> TSpace4D;
258  typedef TSpace4D::Point Point4D;
259  TSpace4D::Integer t[] = {1, 1, 1, 1};
260  Point4D a4D (t);
261  TSpace4D::Integer t2[] = {3, 3, 3, 3};
262  Point4D b4D (t2);
263 
264  trace.beginBlock ( "Test reverse iterator" );
265 
266  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
267  trace.emphase() << myHyperRectDomain4D << std::endl;
268 
269  trace.emphase() << "Increasing order: ";
270 
272  it = myHyperRectDomain4D.begin();
273  for ( ; it != myHyperRectDomain4D.end(); ++it )
274  trace.info() << ( *it ) << std::endl;
275 
276  trace.emphase() << "Now decreasing order: ";
278  it2 = myHyperRectDomain4D.begin();
279  --it;
280  --it2;
281  for ( ; it != it2; --it )
282  trace.info() << ( *it ) << std::endl;
283 
284  trace.endBlock();
285 
286  return myHyperRectDomain4D.isValid();
287 }
288 
289 
290 
292 {
293  typedef SpaceND<4> TSpace4D;
294  typedef TSpace4D::Point Point4D;
295  TSpace4D::Integer t[] = {1, 1, 1, 1};
296  Point4D a4D (t);
297  TSpace4D::Integer t2[] = {3, 3, 3, 3};
298  Point4D b4D (t2);
299 
300  trace.beginBlock ( "TestSTL Compatibility" );
301 
302  HyperRectDomain<TSpace4D> myHyperRectDomain4D ( a4D, b4D );
303  trace.emphase() << myHyperRectDomain4D << std::endl;
304 
305  std::copy ( myHyperRectDomain4D.begin(),
306  myHyperRectDomain4D.end(),
307  ostream_iterator<Point4D> ( trace.info(), " " ) );
308 
309  trace.info() << std::endl;
310  trace.endBlock();
311 
312  return myHyperRectDomain4D.isValid();
313 }
314 
316 {
317  typedef SpaceND<3> TSpace;
318  typedef TSpace::Point TPoint;
319  typedef HyperRectDomain<TSpace> TDomain;
320 
321  unsigned int nb = 0;
322  unsigned int nbok = 0;
323 
324  trace.beginBlock( "Test empty domain." );
325 
326  const TDomain nonempty_domain( TPoint::diagonal(0), TPoint::diagonal(0) );
327  ++nb; nbok += nonempty_domain.isEmpty() ? 0 : 1;
328  trace.info() << "(" << nbok << "/" << nb << ") Creating non-empty domain & checking isEmpty." << std::endl;
329 
330  const TDomain default_domain;
331  ++nb; nbok += default_domain.isEmpty() ? 1 : 0;
332  trace.info() << "(" << nbok << "/" << nb << ") Creating default empty domain & checking isEmpty." << std::endl;
333 
334  const TDomain domain( TPoint::diagonal(1), TPoint::diagonal(0) );
335  ++nb; nbok += domain.isEmpty() ? 1 : 0;
336  trace.info() << "(" << nbok << "/" << nb << ") Creating default custom domain & checking isEmpty." << std::endl;
337 
338  ++nb; nbok += domain.size() == 0 ? 1 : 0;
339  trace.info() << "(" << nbok << "/" << nb << ") Empty domain has size = " << domain.size() << std::endl;
340 
341  ++nb; nbok += domain.begin() == domain.end() ? 1 : 0;
342  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end()" << std::endl;
343 
344  ++nb; nbok += domain.rbegin() == domain.rend() ? 1 : 0;
345  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend()" << std::endl;
346 
347  TDomain::ConstSubRange range = domain.subRange( 0, 1, 2, domain.lowerBound() );
348  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
349  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {0,1,2}" << std::endl;
350 
351  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
352  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {0,1,2}" << std::endl;
353 
354  range = domain.subRange( 2, 1, 0, domain.lowerBound() );
355  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
356  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {2,1,0}" << std::endl;
357 
358  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
359  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {2,1,0}" << std::endl;
360 
361  range = domain.subRange( 0, 2, domain.lowerBound() );
362  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
363  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {0,2}" << std::endl;
364 
365  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
366  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {0,2}" << std::endl;
367 
368  range = domain.subRange( 2, 0, domain.lowerBound() );
369  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
370  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {2,0}" << std::endl;
371 
372  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
373  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-range {2,0}" << std::endl;
374 
375  range = domain.subRange( 1, domain.lowerBound() );
376  ++nb; nbok += range.begin() == range.end() ? 1 : 0;
377  trace.info() << "(" << nbok << "/" << nb << ") Checking that begin() == end() for sub-range {1}" << std::endl;
378 
379  ++nb; nbok += range.rbegin() == range.rend() ? 1 : 0;
380  trace.info() << "(" << nbok << "/" << nb << ") Checking that rbegin() == rend() for sub-rang {1}" << std::endl;
381 
382  trace.endBlock();
383  return nb == nbok;
384 
385 }
386 
387 int main()
388 {
390  return 0;
391  else
392  return 1;
393 }
394 
void beginBlock(const std::string &keyword="")
Aim: Defines the concept describing a bidirectional const range.
const ConstIterator & end() const
ConstSubRange subRange(const std::vector< Dimension > &permutation) const
Trace trace
Definition: Common.h:144
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
const Point & upperBound() const
bool testSTLCompat()
double endBlock()
Domain domain
PointVector< 3, double > RealPoint
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()
ConstReverseIterator rbegin() const
const ConstIterator & begin() const
bool testEmptyDomain()
bool testIterator()
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
bool testReverseIterator()
std::ostream & warning()
std::ostream & info()
const Point & lowerBound() const
bool testSimpleHyperRectDomain()
int main()