Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
fullConvexityThinning3D.cpp
Go to the documentation of this file.
1
16
29
30
36
37
39#include <iostream>
40#include <queue>
41#include "DGtal/base/Common.h"
42#include "DGtal/io/viewers/PolyscopeViewer.h"
43#include "DGtal/io/Color.h"
44#include "DGtal/shapes/Shapes.h"
45#include "DGtal/helpers/StdDefs.h"
46#include "DGtal/helpers/Shortcuts.h"
47#include "DGtal/images/ImageContainerBySTLVector.h"
48#include "DGtal/geometry/volumes/NeighborhoodConvexityAnalyzer.h"
49
51
52using namespace std;
53using namespace DGtal;
54using namespace Z3i;
56
58
60
61int main( int argc, char** argv )
62{
63 trace.info() << "Usage: " << argv[ 0 ] << " <thickness> <convexity> <input.vol> <m> <M>" << std::endl;
64 trace.info() << " - convexity in {0,1}: 0=0-convexity, 1=full-convexity"<< std::endl;
65
66 int thickness = argc > 1 ? atoi( argv[ 1 ] ) : 2;
67 bool full_cvx = argc > 2 ? atoi( argv[ 2 ] ) == 1 : false;
68 std::string fn= argc > 3 ? argv[ 3 ] : "";
69 int m = argc > 4 ? atoi( argv[ 4 ] ) : 0;
70 int M = argc > 5 ? atoi( argv[ 5 ] ) : 255;
71 trace.beginBlock ( "Example of 3D shape thinning with full convexity properties" );
72
73 PolyscopeViewer<> viewer;
74
75 auto params = SH3::defaultParameters();
76
77 // Domain creation from two bounding points.
78 trace.info() << "Building set or importing vol ... ";
79 Point c( 0, 0, 0 );
80 Point p1( -50, -50, -50 );
81 Point p2( 50, 50, 50 );
82 Domain domain( p1, p2 );
83 KSpace K;
84 std::set< Point > shape_set;
86 if ( fn == "" )
87 {
88 K.init( p1, p2, true );
89 for (Domain::ConstIterator it = domain.begin(); it != domain.end(); ++it )
90 {
91 Point p = *it;
92 if ( ((p - c ).norm() <= 22+thickness ) && ((p - c ).norm() >= 20-thickness)
93 && ( ((p[0] <= thickness)&& (p[0] >= -thickness))
94 || ((p[1] <= thickness)&& (p[1] >= -thickness))))
95 {
96 shape_set.insert( p );
97 bimage->setValue( p, true );
98 }
99 else
100 bimage->setValue( p, false );
101 }
102 }
103 else
104 {
105 params( "thresholdMin", m );
106 params( "thresholdMax", M );
107 bimage = SH3::makeBinaryImage( fn, params );
108 K = SH3::getKSpace( bimage );
109 p1 = K.lowerBound();
110 p2 = K.upperBound();
111 domain = Domain( p1, p2 );
112 for ( auto p : domain )
113 if ( (*bimage)( p ) ) shape_set.insert( p );
114 }
115 std::set< Point > origin_set( shape_set );
116 trace.info() << " [Done]" << std::endl;
117
118 {
119 params( "surfaceComponents" , "All" );
120 auto surface = SH3::makeDigitalSurface( bimage, K, params );
121 SH3::saveOBJ( surface, "source.obj" );
122 }
123
124 trace.beginBlock ( "Thinning" );
125 SH3::BinaryImage& image = *bimage;
126 NCA nca( p1, p2, 10000 );
127 int nb_simple=0;
128 std::set< Point >::iterator it, itE;
129 std::set< Point > to_process( shape_set );
130 do
131 {
132 std::set< Point > next_to_process;
133 nb_simple = 0;
134 trace.info() << "Pass #S=" << shape_set.size()
135 << " #Q=" << to_process.size() << std::endl;
136 for ( it = to_process.begin(), itE = to_process.end(); it != itE; ++it )
137 {
138 Point p = *it;
139 if ( ! image( p ) ) continue; // already removed
140 nca.setCenter( p, image );
141 if ( full_cvx
143 : nca.is0ConvexCollapsible() )
144 {
145 std::vector< Point > neighbors;
146 nca.getLocalX( neighbors, false );
147 for ( auto q : neighbors ) next_to_process.insert( q );
148 shape_set.erase( p );
149 image.setValue( p, false );
150 ++nb_simple;
151 }
152 }
153 trace.info() << " => nb_removed=" << nb_simple<< std::endl;
154 if ( nb_simple != 0 )
155 std::swap( to_process, next_to_process );
156 }
157 while ( nb_simple != 0 );
158 trace.endBlock();
159
160 {
161 params( "surfaceComponents" , "All" );
162 auto surface = SH3::makeDigitalSurface( bimage, K, params );
163 SH3::saveOBJ( surface, "geom-thinned.obj" );
164 }
165
166 // Display by using two different list to manage OpenGL transparency.
167 DigitalSet origin( domain );
168 DigitalSet output( domain );
169 for ( auto p : origin_set ) origin.insert( p );
170 for ( auto p : shape_set ) output.insert( p );
171
172 viewer << Color(25,25,255, 255);
173 viewer << output;
174
175 viewer << Color(250, 0,0, 25);
176 viewer << origin;
177
178 trace.endBlock();
179 viewer.show();
180 return 0;
181
182}
183// //
185
Structure representing an RGB triple with alpha component.
Definition Color.h:77
Aim: Smart pointer based on reference counts.
Definition CountedPtr.h:80
void setCenter(Point c, const PointPredicate &X)
void getLocalX(std::vector< Point > &localX, bool with_center) const
void show() override
Starts the event loop and display of elements.
ImageContainerBySTLVector< Domain, bool > BinaryImage
Definition Shortcuts.h:142
static KSpace getKSpace(const Point &low, const Point &up, Parameters params=parametersKSpace())
Definition Shortcuts.h:333
static CountedPtr< DigitalSurface > makeDigitalSurface(CountedPtr< TPointPredicate > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition Shortcuts.h:1210
static Parameters defaultParameters()
Definition Shortcuts.h:204
static bool saveOBJ(CountedPtr< ::DGtal::DigitalSurface< TDigitalSurfaceContainer > > digsurf, const TCellEmbedder &embedder, const RealVectors &normals, const Colors &diffuse_colors, std::string objfile, const Color &ambient_color=Color(32, 32, 32), const Color &diffuse_color=Color(200, 200, 255), const Color &specular_color=Color::White)
Definition Shortcuts.h:1740
static CountedPtr< BinaryImage > makeBinaryImage(Domain shapeDomain)
Definition Shortcuts.h:562
CountedPtr< SH3::DigitalSurface > surface
NeighborhoodConvexityAnalyzer< KSpace, 1 > NCA
Z3i this namespace gathers the standard of types for 3D imagery.
HyperRectDomain< Space > Domain
Definition StdDefs.h:172
KhalimskySpaceND< 3, Integer > KSpace
Definition StdDefs.h:146
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition StdDefs.h:173
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
STL namespace.
Shortcuts< KSpace > SH3
int main()
Definition testBits.cpp:56
KSpace K
Domain domain
Image image(domain)