DGtal 1.3.0
Loading...
Searching...
No Matches
shortcuts.cpp
Go to the documentation of this file.
1
31#include <iostream>
32#include "ConfigExamples.h"
33#include "DGtal/base/Common.h"
34#include "DGtal/helpers/StdDefs.h"
35#include "DGtal/helpers/Shortcuts.h"
37
38using namespace std;
39using namespace DGtal;
40
42
43int main( int /* argc */, char** /* argv */ )
44{
45 unsigned int nb = 0, nbok = 0;
46 // Using standard 3D digital space.
48
49 // 3d tests
50 trace.beginBlock ( "Load vol file -> noisify -> save as vol file." );
51 {
52 auto params = SH3::defaultParameters();
54 // load and noisify image directly.
55 auto al_capone = SH3::makeBinaryImage( examplesPath + "samples/Al.100.vol",
56 params( "noise", 0.3 ) );
57 auto ok = SH3::saveBinaryImage( al_capone, "noisy-Al.vol" );
59 ++nb; nbok += ok ? 1 : 0;
60 }
62 trace.beginBlock ( "Load vol file -> build main connected digital surface." );
63 {
64 auto params = SH3::defaultParameters();
66 auto al_capone = SH3::makeBinaryImage( examplesPath + "samples/Al.100.vol", params );
67 auto K = SH3::getKSpace( al_capone );
68 auto surface = SH3::makeLightDigitalSurface( al_capone, K, params );
69 trace.info() << "#surfels=" << surface->size() << std::endl;
71 ++nb; nbok += surface->size() == 21239 ? 1 : 0;
72 }
74
75 trace.beginBlock ( "Load vol file -> extract 2 isosurfaces -> build mesh" );
76 {
77 auto params = SH3::defaultParameters();
79 params( "faceSubdivision", "Centroid" )( "surfelAdjacency", 1);
80 auto gimage = SH3::makeGrayScaleImage( examplesPath + "samples/lobster.vol" );
81 auto trisurf150= SH3::makeTriangulatedSurface( gimage, params( "thresholdMin", 150 ) );
82 auto trisurf40 = SH3::makeTriangulatedSurface( gimage, params( "thresholdMin", 40 ) );
83 auto mesh150 = SH3::makeMesh( trisurf150 );
84 auto mesh40 = SH3::makeMesh( trisurf40 );
85 trace.info() << "#mesh150=" << mesh150->nbVertex()
86 << " #mesh40=" << mesh40->nbVertex() << std::endl;
88 ++nb; nbok += ( mesh150->nbVertex() < mesh40->nbVertex() )
89 && ( mesh40->nbVertex() == 273182 ) ? 1 : 0;
90 }
92
93 trace.beginBlock ( "Load vol file -> extract 2 triangulated isosurfaces -> save as OBJ." );
94 {
95 auto params = SH3::defaultParameters();
97 params( "faceSubdivision", "Centroid" )( "surfelAdjacency", 1);
98 auto gimage = SH3::makeGrayScaleImage( examplesPath + "samples/lobster.vol" );
99 auto trisurf150= SH3::makeTriangulatedSurface( gimage, params( "thresholdMin", 150 ) );
100 auto trisurf40 = SH3::makeTriangulatedSurface( gimage, params( "thresholdMin", 40 ) );
101 auto ok40 = SH3::saveOBJ( trisurf40, SH3::RealVectors(), SH3::Colors(),
102 "lobster-40.obj", // semi-transparent red diffuse color
103 SH3::Color( 30,30,30 ), SH3::Color( 255,0,0,100 ) );
104 auto ok150 = SH3::saveOBJ( trisurf150, SH3::RealVectors(), SH3::Colors(),
105 "lobster-150.obj", // opaque blue diffuse color
106 SH3::Color( 30,30,30 ), SH3::Color( 0,0,255,255 ) );
108 ++nb; nbok += ok40 ? 1 : 0;
109 ++nb; nbok += ok150 ? 1 : 0;
110 }
111 trace.endBlock();
112
113 trace.beginBlock ( "Load vol file -> build main digital surface -> breadth first traversal -> save OBJ with colored distance." );
114 {
115 auto params = SH3::defaultParameters();
117 params( "surfaceTraversal", "BreadthFirst" ) // specifies breadth-first traversal
118 ( "colormap", "Jet" ); // specifies the colormap
119 auto al_capone = SH3::makeBinaryImage( examplesPath + "samples/Al.100.vol", params );
120 auto K = SH3::getKSpace( al_capone );
121 auto surface = SH3::makeLightDigitalSurface( al_capone, K, params );
122 auto surfels = SH3::getSurfelRange( surface, params );
123 auto cmap = SH3::getColorMap( 0, surfels.size(), params );
124 SH3::Colors colors( surfels.size() );
125 for ( unsigned int i = 0; i < surfels.size(); ++i ) colors[ i ] = cmap( i );
126 bool ok = SH3::saveOBJ( surface, SH3::RealVectors(), colors, "al-primal-bft.obj" );
128 ++nb; nbok += ok ? 1 : 0;
129 }
130 trace.endBlock();
131
132 trace.beginBlock ( "Build polynomial shape -> digitize -> noisify -> save as vol file." );
133 {
134 auto params = SH3::defaultParameters();
136 params( "polynomial", "3*x^2+2*y^2+z^2-90" )( "gridstep", 0.25 )
137 ( "noise", 0.3 );
138 auto implicit_shape = SH3::makeImplicitShape3D( params );
139 auto digitized_shape = SH3::makeDigitizedImplicitShape3D( implicit_shape, params );
140 auto noisy_shape = SH3::makeBinaryImage ( digitized_shape, params );
141 auto ok = SH3::saveBinaryImage ( noisy_shape, "noisy-ellipsoid.vol" );
143 ++nb; nbok += ok ? 1 : 0;
144 }
145 trace.endBlock();
146
147 trace.beginBlock ( "Build polynomial shape -> digitize -> build digital surface -> save primal surface as obj." );
148 {
149 auto params = SH3::defaultParameters();
151 params( "polynomial", "goursat" )( "gridstep", 0.25 );
152 auto implicit_shape = SH3::makeImplicitShape3D ( params );
153 auto digitized_shape = SH3::makeDigitizedImplicitShape3D( implicit_shape, params );
154 auto K = SH3::getKSpace( params );
155 auto binary_image = SH3::makeBinaryImage( digitized_shape, params );
156 auto surface = SH3::makeDigitalSurface( binary_image, K, params );
157 bool ok = SH3::saveOBJ( surface, "goursat-primal.obj" );
159 ++nb; nbok += ok ? 1 : 0;
160 }
161 trace.endBlock();
162
163 trace.beginBlock ( "Build polynomial shape -> digitize -> build indexed surface on a subpart." );
164 {
165 auto params = SH3::defaultParameters();
167 params( "polynomial", "leopold" )( "gridstep", 0.25 )
168 ( "minAABB", -12.0 )( "maxAABB", 12.0 )
169 ( "surfaceComponents", "All" );
170 auto implicit_shape = SH3::makeImplicitShape3D ( params );
171 auto digitized_shape = SH3::makeDigitizedImplicitShape3D( implicit_shape, params );
172 auto Kwhole = SH3::getKSpace( params );
173 auto K = SH3::getKSpace( SH3::Point::zero, Kwhole.upperBound(), params );
174 auto binary_image = SH3::makeBinaryImage( digitized_shape,
176 params );
177 auto surface = SH3::makeIdxDigitalSurface( binary_image, K, params );
178 trace.info() << "#surfels=" << surface->size() << std::endl;
180 ++nb; nbok += surface->size() > 1000 ? 1 : 0;
181 }
182 trace.endBlock();
183
184 trace.beginBlock ( "Build polynomial shape -> digitize -> noisify -> count components -> save OBJ with different colors." );
185 {
186 auto params = SH3::defaultParameters();
188 params( "polynomial", "leopold" )( "gridstep", 0.25 )
189 ( "minAABB", -12.0 )( "maxAABB", 12.0 )
190 ( "surfaceComponents", "All" )( "noise", 0.5 );
191 auto implicit_shape = SH3::makeImplicitShape3D ( params );
192 auto digitized_shape = SH3::makeDigitizedImplicitShape3D( implicit_shape, params );
193 auto K = SH3::getKSpace( params );
194 auto binary_image = SH3::makeBinaryImage(digitized_shape,
196 params );
197 // Extracts the whole surface (with all components)
198 auto surface = SH3::makeDigitalSurface( binary_image, K, params );
199 // Extracts a vector of connected surfaces.
200 auto vec_surfs = SH3::makeLightDigitalSurfaces( binary_image, K, params );
201 trace.info() << "#connected components = " << vec_surfs.size() << std::endl;
202 std::map< SH3::Surfel, unsigned int> label;
203 unsigned int n = 0;
204 for ( auto&& surf : vec_surfs ) {
205 auto surfels = SH3::getSurfelRange( surf, params );
206 for ( auto&& s : surfels ) label[ s ] = n;
207 n += 1;
208 }
209 auto cmap = SH3::getColorMap( 0, vec_surfs.size(), params );
210 auto all_surfels = SH3::getSurfelRange( surface, params );
211 SH3::Colors colors( all_surfels.size() );
212 for ( unsigned int i = 0; i < all_surfels.size(); ++i )
213 colors[ i ] = cmap( label[ all_surfels[ i ] ] );
214 bool ok = SH3::saveOBJ( surface, SH3::RealVectors(), colors, "leopold-primal-cc.obj" );
216 ++nb; nbok += ok ? 1 : 0;
217 }
218 trace.endBlock();
219
220 trace.info() << nbok << "/" << nb << " passed tests." << std::endl;
221
222 return 0;
223}
224// //
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
const Point & lowerBound() const
Return the lower bound for digital points in this space.
const Point & upperBound() const
Return the upper bound for digital points in this space.
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
static KSpace getKSpace(const Point &low, const Point &up, Parameters params=parametersKSpace())
Definition: Shortcuts.h:332
static CountedPtr< DigitizedImplicitShape3D > makeDigitizedImplicitShape3D(CountedPtr< ImplicitShape3D > shape, Parameters params=parametersDigitizedImplicitShape3D())
Definition: Shortcuts.h:523
std::vector< Color > Colors
Definition: Shortcuts.h:192
static CountedPtr< Mesh > makeMesh(CountedPtr< TriangulatedSurface > triSurf, const Color &aColor=Color::White)
Definition: Shortcuts.h:2071
static std::vector< CountedPtr< LightDigitalSurface > > makeLightDigitalSurfaces(CountedPtr< BinaryImage > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1119
static CountedPtr< GrayScaleImage > makeGrayScaleImage(Domain aDomain)
Definition: Shortcuts.h:736
static SurfelRange getSurfelRange(CountedPtr< ::DGtal::DigitalSurface< TDigitalSurfaceContainer > > surface, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1547
static CountedPtr< LightDigitalSurface > makeLightDigitalSurface(CountedPtr< BinaryImage > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1062
static CountedPtr< DigitalSurface > makeDigitalSurface(CountedPtr< TPointPredicate > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1209
std::vector< RealVector > RealVectors
Definition: Shortcuts.h:179
static bool saveBinaryImage(CountedPtr< BinaryImage > bimage, std::string output)
Definition: Shortcuts.h:708
static Parameters defaultParameters()
Definition: Shortcuts.h:203
static CountedPtr< IdxDigitalSurface > makeIdxDigitalSurface(CountedPtr< BinaryImage > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1275
static ColorMap getColorMap(Scalar min, Scalar max, const Parameters &params=parametersUtilities())
Definition: Shortcuts.h:2668
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:1739
static CountedPtr< BinaryImage > makeBinaryImage(Domain shapeDomain)
Definition: Shortcuts.h:561
static CountedPtr< ImplicitShape3D > makeImplicitShape3D(const Parameters &params=parametersImplicitShape3D())
Definition: Shortcuts.h:282
static CountedPtr< TriangulatedSurface > makeTriangulatedSurface(CountedPtr< Mesh > aMesh)
Definition: Shortcuts.h:2054
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
STL namespace.
Shortcuts< KSpace > SH3
int main()
Definition: testBits.cpp:56
KSpace K