DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
frontierAndBoundary.cpp
1 
14 
15 
16 #include <iostream>
17 #include <QtGui/qapplication.h>
18 #include "DGtal/helpers/StdDefs.h"
19 #include "DGtal/kernel/sets/SetPredicate.h"
20 #include "DGtal/io/readers/VolReader.h"
21 #include "DGtal/io/viewers/Viewer3D.h"
22 #include "DGtal/io/DrawWithDisplay3DModifier.h"
23 #include "DGtal/io/Color.h"
24 #include "DGtal/io/colormaps/HueShadeColorMap.h"
25 #include "DGtal/images/ImageSelector.h"
26 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
27 #include "DGtal/shapes/implicit/ImplicitBall.h"
28 #include "DGtal/shapes/GaussDigitizer.h"
29 #include "DGtal/shapes/Shapes.h"
30 #include "DGtal/topology/DigitalSurface.h"
31 #include "DGtal/topology/ExplicitDigitalSurface.h"
32 #include "DGtal/topology/helpers/FrontierPredicate.h"
33 #include "DGtal/topology/helpers/BoundaryPredicate.h"
35 
37 
38 using namespace std;
39 using namespace DGtal;
40 using namespace Z3i;
41 
43 
44 int main( int argc, char** argv )
45 {
48  typedef ImplicitBall<Space> EuclideanShape;
51  Point c1( 2, 0, 0 );
52  int radius1 = 6;
53  EuclideanShape ball1( c1, radius1 ); // ball r=6
54  DigitalShape shape1;
55  shape1.attach( ball1 );
56  shape1.init( RealPoint( -10.0, -10.0, -10.0 ),
57  RealPoint( 10.0, 10.0, 10.0 ), 1.0 );
58  Point c2( -2, 0, 0 );
59  int radius2 = 5;
60  EuclideanShape ball2( c2, radius2 ); // ball r=6
61  DigitalShape shape2;
62  shape2.attach( ball2 );
63  shape2.init( RealPoint( -10.0, -10.0, -10.0 ),
64  RealPoint( 10.0, 10.0, 10.0 ), 1.0 );
65  Domain domain = shape1.getDomain();
66  Image image( domain ); // p1, p2 );
67  std::cerr << std::endl;
68  for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end();
69  it != it_end; ++it )
70  {
71  DGtal::uint8_t label = shape1( *it ) ? 1 : 0;
72  label += shape2( *it ) ? 2 : 0;
73  image.setValue( *it, label );
74  std::cerr << (int) image( *it );
75  }
76  std::cerr << std::endl;
78 
80  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
81  KSpace K;
82  bool space_ok = K.init( domain.lowerBound(), domain.upperBound(), true );
83  if (!space_ok)
84  {
85  trace.error() << "Error in the Khamisky space construction."<<std::endl;
86  return 2;
87  }
88  trace.endBlock();
90 
92  trace.beginBlock( "Set up digital surface." );
93  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
94  MySurfelAdjacency surfAdj( true ); // interior in all directions.
95  typedef FrontierPredicate<KSpace, Image> FSurfelPredicate;
96  typedef ExplicitDigitalSurface<KSpace,FSurfelPredicate> FrontierContainer;
97  typedef DigitalSurface<FrontierContainer> Frontier;
98  typedef BoundaryPredicate<KSpace, Image> BSurfelPredicate;
99  typedef ExplicitDigitalSurface<KSpace,BSurfelPredicate> BoundaryContainer;
100  typedef DigitalSurface<BoundaryContainer> Boundary;
101  // frontier between label 1 and 0 (connected part containing bel10)
102  SCell vox1 = K.sSpel( c1 + Point( radius1 - 1, 0, 0 ), K.POS );
103  SCell bel10 = K.sIncident( vox1, 0, true );
104  FSurfelPredicate surfPredicate10( K, image, 1, 0 );
105  Frontier frontier10 =
106  new FrontierContainer( K, surfPredicate10, surfAdj, bel10 );
107  // frontier between label 2 and 0 (connected part containing bel20)
108  SCell vox2 = K.sSpel( c2 - Point( radius2 - 1, 0, 0 ), K.POS );
109  SCell bel20 = K.sIncident( vox2, 0, false );
110  FSurfelPredicate surfPredicate20( K, image, 2, 0 );
111  Frontier frontier20 =
112  new FrontierContainer( K, surfPredicate20, surfAdj, bel20 );
113  // boundary of label 3 (connected part containing bel32)
114  SCell vox3 = K.sSpel( c1 - Point( radius1 - 1, 0, 0 ), K.POS );
115  SCell bel32 = K.sIncident( vox3, 0, false );
116  BSurfelPredicate surfPredicate3( K, image, 3 );
117  Boundary boundary3 =
118  new BoundaryContainer( K, surfPredicate3, surfAdj, bel32 );
119  trace.endBlock();
121 
123  trace.beginBlock( "Displaying surface in Viewer3D." );
124  QApplication application(argc,argv);
125  Viewer3D viewer;
126  viewer.show();
127  viewer << SetMode3D( domain.className(), "BoundingBox" )
128  << domain;
129  Cell dummy;
130  // Display frontier between 1 and 0.
131  unsigned int nbSurfels10 = 0;
132  viewer << CustomColors3D( Color::Red, Color::Red );
133  for ( Frontier::ConstIterator
134  it = frontier10.begin(), it_end = frontier10.end();
135  it != it_end; ++it, ++nbSurfels10 )
136  viewer << *it;
137  // Display frontier between 2 and 0.
138  unsigned int nbSurfels20 = 0;
139  viewer << CustomColors3D( Color::Yellow, Color::Yellow );
140  for ( Frontier::ConstIterator
141  it = frontier20.begin(), it_end = frontier20.end();
142  it != it_end; ++it, ++nbSurfels20 )
143  viewer << *it;
144  // Display boundary of 3.
145  unsigned int nbSurfels3 = 0;
146  viewer << CustomColors3D( Color( 255, 130, 15 ), Color( 255, 130, 15 ) );
147  for ( Boundary::ConstIterator
148  it = boundary3.begin(), it_end = boundary3.end();
149  it != it_end; ++it, ++nbSurfels3 )
150  viewer << *it;
151  trace.info() << "nbSurfels10 = " << nbSurfels10
152  << ", nbSurfels20 = " << nbSurfels20
153  << ", nbSurfels3 = " << nbSurfels3 << std::endl;
154  viewer << Viewer3D::updateDisplay;
155  trace.endBlock();
156  return application.exec();
158 }