Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
testExpander.cpp
Go to the documentation of this file.
1
16
29
31#include <iostream>
32#include <sstream>
33#include "DGtal/base/Common.h"
34#include "DGtal/kernel/SpaceND.h"
35#include "DGtal/kernel/domains/DomainPredicate.h"
36#include "DGtal/kernel/domains/HyperRectDomain.h"
37#include "DGtal/kernel/sets/DigitalSetSelector.h"
38#include "DGtal/kernel/sets/DigitalSetConverter.h"
39#include "DGtal/topology/MetricAdjacency.h"
40#include "DGtal/topology/DomainAdjacency.h"
41#include "DGtal/topology/DigitalTopology.h"
42#include "DGtal/topology/Object.h"
43#include "DGtal/graph/Expander.h"
45
46using namespace std;
47using namespace DGtal;
48
49#define INBLOCK_TEST(x) \
50 nbok += ( x ) ? 1 : 0; \
51 nb++; \
52 trace.info() << "(" << nbok << "/" << nb << ") " \
53 << #x << std::endl;
54
55#define INBLOCK_TEST2(x,y) \
56 nbok += ( x ) ? 1 : 0; \
57 nb++; \
58 trace.info() << "(" << nbok << "/" << nb << ") " \
59 << y << std::endl;
60
62// Functions for testing class Expander.
64
69{
70 unsigned int nbok = 0;
71 unsigned int nb = 0;
72
73 // ------------------------------ Types ------------------------------
74 typedef SpaceND< 3 > Z3;
75 typedef Z3::Point Point;
76 typedef Point::Coordinate Coordinate;
78 typedef Domain::ConstIterator DomainConstIterator;
79
80 typedef MetricAdjacency< Z3, 1 > MetricAdj6;
81 typedef MetricAdjacency< Z3, 2 > MetricAdj18;
84 // typedef MetricAdjacency< Z3, 1 > Adj6;
85 // typedef MetricAdjacency< Z3, 2 > Adj18;
86
87 typedef DigitalTopology< Adj6, Adj18 > DT6_18;
88
90 typedef Object<DT6_18, DigitalSet> ObjectType;
91 typedef Expander< ObjectType > ObjectExpander;
92 // ----------------------- Domain, Topology ------------------------------
93 Point p1( -50, -50, -50 );
94 Point p2( 50, 50, 50 );
95 Domain domain( p1, p2 );
96
97 MetricAdj6 madj6;
98 MetricAdj18 madj18;
99 Adj6 adj6( domain, madj6 );
100 Adj18 adj18( domain, madj18 );
101 // Adj6 adj6;
102 // Adj18 adj18;
103
104 DT6_18 dt6_18( adj6, adj18, JORDAN_DT );
105 // ------------------------------- Object ------------------------------
106 Coordinate r = 9;
107 double radius = (double) (r+1);
108 Point c( 0, 0 );
109 Point l( r, 0 );
110 DigitalSet ball_set( domain );
111 ostringstream sstr;
112 sstr << "Creating 3D ball( r < " << radius << " ) ...";
113 trace.beginBlock ( sstr.str() );
114 for ( DomainConstIterator it = domain.begin();
115 it != domain.end();
116 ++it )
117 {
118 if ( (*it - c ).norm() < radius )
119 // insertNew is very important for vector container.
120 ball_set.insertNew( *it );
121 }
122 trace.endBlock();
123
124 trace.beginBlock ( "Testing Object instanciation and smart copy ..." );
125 ObjectType ball( dt6_18, ball_set );
126 ObjectType ball2( ball );
127 INBLOCK_TEST( ball.size() == 4139 );
128 trace.info() << "ball.size() = " << ball.size()
129 << " 4/3*pi*r^3 = " << ( 4.0*M_PI*radius*radius*radius/3.0 )
130 << endl;
131 trace.info() << "ball = " << ball << endl;
132 trace.info() << "ball2 = " << ball2 << endl;
133 trace.endBlock();
134
135 trace.beginBlock ( "Testing border extraction ..." );
136 ObjectType sphere = ball.border();
137 INBLOCK_TEST( sphere.size() == 1434 );
138 trace.info() << sphere << endl;
139 trace.info() << "sphere.size() = " << sphere.size()
140 << " 4*pi*r^2 = " << ( 4.0*M_PI*radius*radius )
141 << endl;
142 trace.endBlock();
143
144 trace.beginBlock ( "Testing expansion by layers in the ball from center..." );
145 ObjectExpander expander( ball, c );
146 while ( ! expander.finished() )
147 {
148 trace.info() << expander << std::endl;
149 expander.nextLayer();
150 }
151 nbok += expander.distance() <= sqrt(3.0)*radius ? 1 : 0;
152 nb++;
153 trace.info() << "(" << nbok << "/" << nb << ") "
154 << "expander.distance() = " << expander.distance()
155 << " <= " << sqrt(3.0)*radius << std::endl;
156 trace.endBlock();
157
158 trace.beginBlock ( "Testing expansion by layers on the sphere from a point ..." );
159 ObjectExpander expander2( sphere, l );
160 while ( ! expander2.finished() )
161 {
162 trace.info() << expander2 << std::endl;
163 expander2.nextLayer();
164 }
165 nbok += expander2.distance() <= sqrt(2.0)*M_PI*radius ? 1 : 0;
166 nb++;
167 trace.info() << "(" << nbok << "/" << nb << ") "
168 << "expander2.distance() = " << expander2.distance()
169 << " <= " << sqrt(2.0)*M_PI*radius << std::endl;
170 trace.endBlock();
171
172
173 return nbok == nb;
174}
175
177// Standard services - public :
178
179int main( int argc, char** argv )
180{
181 trace.beginBlock ( "Testing class Expander" );
182 trace.info() << "Args:";
183 for ( int i = 0; i < argc; ++i )
184 trace.info() << " " << argv[ i ];
185 trace.info() << endl;
186
187 bool res = testExpander(); // && ... other tests
188 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
189 trace.endBlock();
190 return res ? 0 : 1;
191}
192// //
Aim: Given a domain and an adjacency, limits the given adjacency to the specified domain for all adja...
Aim: This class is useful to visit an object by adjacencies, layer by layer.
Definition Expander.h:98
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
Aim: Describes digital adjacencies in digital spaces that are defined with the 1-norm and the infinit...
Aim: An object (or digital object) represents a set in some digital space associated with a digital t...
Definition Object.h:120
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
STL namespace.
DigitalSetByAssociativeContainer< Domain, std::unordered_set< typename Domain::Point > > Type
int main()
Definition testBits.cpp:56
bool testExpander()
#define INBLOCK_TEST(x)
Domain domain
Z2i::DigitalSet DigitalSet