100{
101 trace.
info() <<
"Usage: " << argv[ 0 ] <<
" <input.vol> <m> <M> <opt>" << std::endl;
102 trace.
info() <<
"\tComputes shortest paths to a source point" << std::endl;
103 trace.
info() <<
"\t- input.vol: choose your favorite shape" << std::endl;
104 trace.
info() <<
"\t- m [==0], M [==255]: used to threshold input vol image" << std::endl;
105 trace.
info() <<
"\t- opt >= sqrt(3): secure shortest paths, 0: fast" << std::endl;
106 string inputFilename = examplesPath + "samples/Al.100.vol";
107 std::string fn= argc > 1 ? argv[ 1 ] : inputFilename;
108 int m = argc > 2 ? atoi( argv[ 2 ] ) : 0;
109 int M = argc > 3 ? atoi( argv[ 3 ] ) : 255;
110 double opt = argc > 4 ? atof( argv[ 4 ] ) : sqrt(3.0);
111
113
114
115 auto params = SH3::defaultParameters();
116 params( "thresholdMin", m )( "thresholdMax", M );
117 params( "surfaceComponents" , "All" );
118
119
120 trace.
info() <<
"Building set or importing vol ... ";
122 auto bimage = SH3::makeBinaryImage( fn, params );
123 K = SH3::getKSpace( bimage );
125
126
127 const auto surface = SH3::makeDigitalSurface( bimage,
K, params );
128
129
130
131 std::vector< Point > points;
132 std::map< SCell, int > surfel2idx;
133 std::map< Point, int > point2idx;
134 int idx = 0;
136 {
137
141 auto it = point2idx.find( p );
142 if ( it == point2idx.end() )
143 {
144 points.push_back( p );
145 surfel2idx[ s ] = idx;
146 point2idx [ p ] = idx++;
147 }
148 else
149 surfel2idx[ s ] = it->second;
150 }
151 trace.
info() <<
"Shape has " << points.size() <<
" interior boundary points"
152 << std::endl;
153
154
157 auto surfels = SH3::getSurfelRange (
surface );
158 for ( int i = 0; i < 2; i++ )
159 {
160 MViewer3D viewerCore(
K );
161 Color colSurfel( 200, 200, 255, 255 );
162 Color colStart( 255, 0, 0, 255 );
163 viewerCore.drawColor( colSurfel );
164 for ( auto && s : surfels ) viewerCore << s;
165
166 viewerCore.show();
167 }
168
169
170 const auto s0 = surfels[ selected_surfels[ 0 ] ];
174 auto start0 = point2idx[ p0 ];
175 std::cout << "Start0 index is " << start0 << std::endl;
176 const auto s1 = surfels[ selected_surfels[ 1 ] ];
180 auto start1 = point2idx[ p1 ];
181 std::cout << "Start1 index is " << start1 << std::endl;
182
183
184
188 TC.init( points.cbegin(), points.cend() );
189 auto SP = TC.makeShortestPaths( opt );
190 SP.init( start0 );
191 double last_distance = 0.0;
192 while ( ! SP.finished() )
193 {
194 last_distance = std::get<2>( SP.current() );
195 SP.expand();
196 }
197 std::cout << "Max distance is " << last_distance << std::endl;
199
200 {
201 const int nb_repetitions = 10;
202 const double period = last_distance / nb_repetitions;
204 MViewer3D viewerCore;
205 Color colSurfel( 200, 200, 255, 128 );
206 Color colStart( 255, 0, 0, 128 );
207
208 for ( size_t i = 0; i < points.size(); ++i )
209 {
210 const double d_s = SP.distance( i );
211 Color c_s = cmap( fmod( d_s, period ) );
212 viewerCore.drawColor( c_s );
213 viewerCore.drawBall(
RealPoint( points[ i ][ 0 ],
214 points[ i ][ 1 ],
215 points[ i ][ 2 ] ) );
216 }
217
218
219
220 for ( auto && s : surfels )
221 {
222 const double d_s = SP.distance( surfel2idx[ s ] );
223 Color c_s = cmap( fmod( d_s, period ) );
224 viewerCore.drawColor( c_s );
225 viewerCore << s;
226 }
227 viewerCore.drawColor( colStart );
229 for (
Index i = 0; i < SP.size(); i++ ) {
230 Point p1_ = SP.point( i );
231 Point p2_ = SP.point( SP.ancestor( i ) );
232 viewerCore.drawLine( p1_, p2_ );
233 }
234 viewerCore.show();
235 }
236
237
238
240 auto SP0 = TC.makeShortestPaths( opt );
241 auto SP1 = TC.makeShortestPaths( opt );
242 SP0.init( start0 );
243 SP1.init( start1 );
244 std::vector< Index > Q;
245 while ( ! SP0.finished() && ! SP1.finished() )
246 {
247 auto n0_ = SP0.current(); ((void) n0_);
248 auto n1_ = SP1.current();
249
250 auto p1_ = std::get<0>( n1_ );
251 SP0.expand();
252 SP1.expand();
253 if ( SP0.isVisited( p1_ ) )
254 {
255 auto c0 = SP0.pathToSource( p1_ );
256 auto c1 = SP1.pathToSource( p1_ );
257 std::copy(c0.rbegin(), c0.rend(), std::back_inserter(Q));
258 Q.pop_back();
259 std::copy(c1.begin(), c1.end(), std::back_inserter(Q));
260 break;
261 }
262 }
263
265
266
267 {
268 const int nb_repetitions = 10;
269 const double period = last_distance / nb_repetitions;
271 MViewer3D viewerCore;
272 Color colSurfel( 200, 200, 255, 128 );
273 Color colStart( 255, 0, 0, 128 );
274 for ( size_t i = 0; i < points.size(); ++i )
275 {
276 const double d_s0 = SP0.isVisited( i ) ? SP0.distance( i ) : SP0.infinity();
277 const double d_s1 = SP1.isVisited( i ) ? SP1.distance( i ) : SP1.infinity();
278 const double d_s = std::min( d_s0, d_s1 );
279 Color c_s = ( d_s != SP0.infinity() )
280 ? cmap( fmod( d_s, period ) )
282 viewerCore.drawColor( c_s );
283 viewerCore.drawBall(
RealPoint( points[ i ][ 0 ],
284 points[ i ][ 1 ],
285 points[ i ][ 2 ] ) );
286 }
287
289 for ( size_t i = 1; i < Q.size(); i++ )
290 {
291 Point p1_ = TC.point( Q[ i-1 ] );
292 Point p2_ = TC.point( Q[ i ] );
293 viewerCore.drawLine( p1_, p2_ );
294 }
295 viewerCore.show();
296 }
297
298
299
300 std::vector< Index > sources;
301 std::vector< Index > dests;
302 for ( int i = 0; i < 20; i++ )
303 sources.push_back( rand() % TC.size() );
304 dests.push_back( start0 );
305 dests.push_back( start1 );
306 auto paths = TC.shortestPaths( sources, dests, opt );
307
308
309 {
310 MViewer3D viewerCore;
311 Color colSurfel( 200, 200, 255, 128 );
312 Color colStart( 255, 0, 0, 128 );
313 for ( size_t i = 0; i < points.size(); ++i )
314 {
315 viewerCore.drawColor(
Color( 150, 150, 150, 255 ) );
316 viewerCore.drawBall(
RealPoint( points[ i ][ 0 ],
317 points[ i ][ 1 ],
318 points[ i ][ 2 ] ) );
319 }
321 for ( auto path : paths )
322 {
323 for ( size_t i = 1; i < path.size(); i++ )
324 {
325 Point p1_ = TC.point( path[ i-1 ] );
326 Point p2_ = TC.point( path[ i ] );
327 viewerCore.drawLine( p1_, p2_ );
328 }
329 trace.
info() <<
"length=" << TC.length( path ) << std::endl;
330 }
331 viewerCore.show();
332 }
333
334 return 0;
335}
Structure representing an RGB triple with alpha component.
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Dimension sOrthDir(const SCell &s) const
Given a signed surfel [s], returns its orthogonal direction (ie, the coordinate where the surfel is c...
Point sCoords(const SCell &c) const
Return its digital coordinates.
bool sDirect(const SCell &p, Dimension k) const
Return 'true' if the direct orientation of [p] along [k] is in the positive coordinate direction.
SCell sIncident(const SCell &c, Dimension k, bool up) const
Return the forward or backward signed cell incident to [c] along axis [k], depending on [up].
Aim: simple blue to red colormap for distance information for instance.
Aim: A class that computes tangency to a given digital set. It provides services to compute all the c...
CountedPtr< SH3::DigitalSurface > surface
Space::RealPoint RealPoint
std::int32_t int32_t
signed 32-bit integer.
DGtal::uint32_t Dimension