DGtal 2.1.0
Loading...
Searching...
No Matches
fullConvexityShortestPaths3D.cpp File Reference
#include <iostream>
#include <queue>
#include "DGtal/base/Common.h"
#include "DGtal/io/viewers/PolyscopeViewer.h"
#include "DGtal/io/Color.h"
#include "DGtal/io/colormaps/SimpleDistanceColorMap.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/helpers/Shortcuts.h"
#include "DGtal/images/ImageContainerBySTLVector.h"
#include "DGtal/geometry/volumes/TangencyComputer.h"
#include "ConfigExamples.h"
Include dependency graph for fullConvexityShortestPaths3D.cpp:

Go to the source code of this file.

Typedefs

typedef Z3i::Space Space
 
typedef Z3i::KSpace KSpace
 
typedef Z3i::Domain Domain
 
typedef Z3i::SCell SCell
 
typedef Shortcuts< KSpaceSH3
 
typedef Space::Point Point
 
typedef Space::RealPoint RealPoint
 
typedef Space::Vector Vector
 

Functions

int reaction (void *viewer, DGtal::int32_t name, void *data)
 
int main (int argc, char **argv)
 

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
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2021/06/20

An example file named fullConvexityShortestPaths3D

This file is part of the DGtal library.

Definition in file fullConvexityShortestPaths3D.cpp.

Typedef Documentation

◆ Domain

Definition at line 81 of file fullConvexityShortestPaths3D.cpp.

◆ KSpace

Definition at line 80 of file fullConvexityShortestPaths3D.cpp.

◆ Point

typedef Space::Point Point

Definition at line 84 of file fullConvexityShortestPaths3D.cpp.

◆ RealPoint

typedef Space::RealPoint RealPoint

Definition at line 85 of file fullConvexityShortestPaths3D.cpp.

◆ SCell

typedef Z3i::SCell SCell

Definition at line 82 of file fullConvexityShortestPaths3D.cpp.

◆ SH3

typedef Shortcuts< KSpace > SH3

Definition at line 83 of file fullConvexityShortestPaths3D.cpp.

◆ Space

typedef Z3i::Space Space

Definition at line 79 of file fullConvexityShortestPaths3D.cpp.

◆ Vector

typedef Space::Vector Vector

Definition at line 86 of file fullConvexityShortestPaths3D.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

[Tangency3D-shortest-paths]

[Tangency3D-shortest-paths]

[Tangency3D-shortest-path]

[Tangency3D-shortest-path]

Definition at line 99 of file fullConvexityShortestPaths3D.cpp.

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; //< vol filename
108 int m = argc > 2 ? atoi( argv[ 2 ] ) : 0; //< low for thresholding
109 int M = argc > 3 ? atoi( argv[ 3 ] ) : 255; //< up for thresholding
110 double opt = argc > 4 ? atof( argv[ 4 ] ) : sqrt(3.0); //< exact (sqrt(3)) or inexact (0) computations
111
112 PolyscopeViewer<> viewer;
113
114 // Set up shortcuts parameters.
115 auto params = SH3::defaultParameters();
116 params( "thresholdMin", m )( "thresholdMax", M );
117 params( "surfaceComponents" , "All" );
118
119 // Domain creation from two bounding points.
120 trace.info() << "Building set or importing vol ... ";
121 KSpace K;
122 auto bimage = SH3::makeBinaryImage( fn, params );
123 K = SH3::getKSpace( bimage );
124 trace.info() << " [Done]" << std::endl;
125
126 // Compute surface
127 const auto surface = SH3::makeDigitalSurface( bimage, K, params );
128
129 // Compute interior boundary points
130 // They are less immediate interior points than surfels.
131 std::vector< Point > points;
132 std::map< SCell, int > surfel2idx;
133 std::map< Point, int > point2idx;
134 int idx = 0;
135 for ( auto s : (*surface) )
136 {
137 // get inside point on the border of the shape.
138 Dimension k = K.sOrthDir( s );
139 auto voxel = K.sIncident( s, k, K.sDirect( s, k ) );
140 Point p = K.sCoords( voxel );
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 // Select a starting point.
155 DGtal::int32_t selected_surfels[ 2 ] = { 0, 0 };
156 typedef PolyscopeViewer<> MViewer3D;
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 // Get selected surfel/point
170 const auto s0 = surfels[ selected_surfels[ 0 ] ];
171 Dimension k0 = K.sOrthDir( s0 );
172 auto voxel0 = K.sIncident( s0, k0, K.sDirect( s0, k0 ) );
173 Point p0 = K.sCoords( voxel0 );
174 auto start0 = point2idx[ p0 ];
175 std::cout << "Start0 index is " << start0 << std::endl;
176 const auto s1 = surfels[ selected_surfels[ 1 ] ];
177 Dimension k1 = K.sOrthDir( s1 );
178 auto voxel1 = K.sIncident( s1, k1, K.sDirect( s1, k1 ) );
179 Point p1 = K.sCoords( voxel1 );
180 auto start1 = point2idx[ p1 ];
181 std::cout << "Start1 index is " << start1 << std::endl;
182
183 // (I) Extracts shortest paths to a target
184
188 TC.init( points.cbegin(), points.cend() );
189 auto SP = TC.makeShortestPaths( opt );
190 SP.init( start0 ); //< set source
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;
203 SimpleDistanceColorMap< double > cmap( 0.0, period, false );
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 // JOL: Left if you wish to display it as a surface, and to display paths.
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 );
228 viewerCore.drawColor( Color::Green );
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 // (II) Extracts a shortest path between two points.
238
240 auto SP0 = TC.makeShortestPaths( opt );
241 auto SP1 = TC.makeShortestPaths( opt );
242 SP0.init( start0 ); //< source point
243 SP1.init( start1 ); //< target point
244 std::vector< Index > Q; //< the output shortest path
245 while ( ! SP0.finished() && ! SP1.finished() )
246 {
247 auto n0_ = SP0.current(); ((void) n0_);
248 auto n1_ = SP1.current();
249 // auto p0_ = std::get<0>( n0_ );
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 // Q is empty if there is no path.
265
266 // Display computed distances and shortest path
267 {
268 const int nb_repetitions = 10;
269 const double period = last_distance / nb_repetitions;
270 SimpleDistanceColorMap< double > cmap( 0.0, period, false );
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 ) )
281 : Color::Black;
282 viewerCore.drawColor( c_s );
283 viewerCore.drawBall( RealPoint( points[ i ][ 0 ],
284 points[ i ][ 1 ],
285 points[ i ][ 2 ] ) );
286 }
287
288 viewerCore.drawColor( Color::Green );
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 // (III) Extracts multiple shortest paths between many sources and two targets.
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 // Display them.
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 }
320 viewerCore.drawColor( Color::Green );
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.
Definition Color.h:77
static const Color Green
Definition Color.h:426
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...
std::ostream & info()
CountedPtr< SH3::DigitalSurface > surface
Space::RealPoint RealPoint
SMesh::Index Index
std::int32_t int32_t
signed 32-bit integer.
Definition BasicTypes.h:71
DGtal::uint32_t Dimension
Definition Common.h:119
Trace trace
KSpace K

References DGtal::Color::Black, DGtal::Color::Green, DGtal::Trace::info(), K, DGtal::KhalimskySpaceND< dim, TInteger >::sCoords(), DGtal::KhalimskySpaceND< dim, TInteger >::sDirect(), DGtal::KhalimskySpaceND< dim, TInteger >::sIncident(), DGtal::KhalimskySpaceND< dim, TInteger >::sOrthDir(), surface, and DGtal::trace.

◆ reaction()

int reaction ( void *  viewer,
DGtal::int32_t  name,
void *  data 
)
Examples
geometry/volumes/fullConvexityShortestPaths3D.cpp.

Definition at line 89 of file fullConvexityShortestPaths3D.cpp.

90{
91 ((void) viewer);
92
93 DGtal::int32_t* selected = (DGtal::int32_t*) data;
94 *selected = name;
95 std::cout << "Selected surfel=" << *selected << std::endl;
96 return 0;
97}