DGtal  1.4.beta
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 
38 using namespace std;
39 using namespace DGtal;
40 
42 
43 int 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  }
61  trace.endBlock();
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  }
73  trace.endBlock();
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  }
91  trace.endBlock();
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.
static Self zero
Static const for zero PointVector.
Definition: PointVector.h:1595
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
std::vector< Color > Colors
Definition: Shortcuts.h:192
std::vector< RealVector > RealVectors
Definition: Shortcuts.h:179
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:153
int main(int, char **)
Definition: shortcuts.cpp:43
Shortcuts< KSpace > SH3
KSpace K