DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testPolynomial.cpp
1 
29 
30 
31 
32 #include <iostream>
33 #include <QtGui/QApplication>
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/topology/helpers/Surfaces.h"
36 #include "DGtal/topology/DigitalSurface.h"
37 #include "DGtal/topology/SetOfSurfels.h"
38 #include "DGtal/math/MPolynomial.h"
39 #include "DGtal/shapes/GaussDigitizer.h"
40 #include "DGtal/shapes/implicit/ImplicitPolynomial3Shape.h"
41 #include "DGtal/shapes/implicit/ImplicitFunctionDiff1LinearCellEmbedder.h"
42 #include "DGtal/io/readers/MPolynomialReader.h"
43 #include "DGtal/io/viewers/Viewer3D.h"
44 #include "DGtal/topology/SCellsFunctors.h"
45 #include "DGtal/topology/helpers/BoundaryPredicate.h"
46 #include "DGtal/io/viewers/Viewer3D.h"
47 #include "DGtal/topology/SetOfSurfels.h"
48 #include "DGtal/io/colormaps/GradientColorMap.h"
49 #include <boost/math/special_functions/fpclassify.hpp>
50 
52 
53 using namespace std;
54 using namespace DGtal;
55 using namespace Z3i;
56 
58 
59 
60 void usage( int /*argc*/, char** argv )
61 {
62  std::cerr << "Usage: " << argv[ 0 ] << " <Polynomial> <Px> <Py> <Pz> <Qx> <Qy> <Qz> <step>" << std::endl;
63  std::cerr << "\t - displays the boundary of a shape defined implicitly by a 3-polynomial <Polynomial>." << std::endl;
64  std::cerr << "\t - P and Q defines the bounding box." << std::endl;
65  std::cerr << "\t - step is the grid step." << std::endl;
66  std::cerr << "\t - You may try x^3y+xz^3+y^3z+z^3+5z or (y^2+z^2-1)^2 +(x^2+y^2-1)^3 " << std::endl;
67  std::cerr << "\t - See http://www.freigeist.cc/gallery.html" << std::endl;
68 }
69 
71  typedef RealPoint::Coordinate Ring;
73  typedef MPolynomialReader<3, Ring> Polynomial3Reader;
77 
78 
79 
80 int main( int argc, char** argv )
81 {
82  if ( argc < 9 )
83  {
84  usage( argc, argv );
85  return 1;
86  }
87  double p1[ 3 ];
88  double p2[ 3 ];
89  for ( unsigned int i = 0; i < 3; ++i )
90  {
91  p1[ i ] = atof( argv[ 2 + i ] );
92  p2[ i ] = atof( argv[ 5 + i ] );
93  }
94  double step = atof( argv[ 8 ] );
95 
96 
97  Polynomial3 P;
98  Polynomial3Reader reader;
99  std::string poly_str = argv[ 1 ];
100  std::string::const_iterator iter
101  = reader.read( P, poly_str.begin(), poly_str.end() );
102  if ( iter != poly_str.end() )
103  {
104  std::cerr << "ERROR: I read only <"
105  << poly_str.substr( 0, iter - poly_str.begin() )
106  << ">, and I built P=" << P << std::endl;
107  return 1;
108  }
109 
110 
111  ImplicitShape ishape( P );
112  DigitalShape dshape;
113  dshape.attach( ishape );
114  dshape.init( RealPoint( p1 ), RealPoint( p2 ), step );
115  Domain domain = dshape.getDomain();
116 
117 
118  KSpace K;
119 
120  bool space_ok = K.init( domain.lowerBound(),
121  domain.upperBound(), true
122  );
123  if ( !space_ok )
124  {
125  trace.error() << "Error in the Khamisky space construction." << std::endl;
126  return 2;
127  }
128 
129  typedef SurfelAdjacency< KSpace::dimension > MySurfelAdjacency;
130  MySurfelAdjacency surfAdj( true ); // interior in all directions.
131 
132 
133  typedef KSpace::Surfel Surfel;
134  typedef KSpace::SurfelSet SurfelSet;
135  typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
137 
138 
139  MySetOfSurfels theSetOfSurfels( K, surfAdj );
140  Surfel bel = Surfaces< KSpace >::findABel( K, dshape, 100000 );
141  Surfaces< KSpace >::trackBoundary( theSetOfSurfels.surfelSet(),
142  K, surfAdj,
143  dshape, bel );
144 
145 
146 
147 
148  QApplication application( argc, argv );
149  Viewer3D viewer;
150  viewer.show();
151  viewer << SetMode3D( domain.className(), "BoundingBox" ) << domain;
152 
153 
154 
155 
156  //-----------------------------------------------------------------------
157  // Looking for the min and max values
158 
159  double minCurv = 1;
160  double maxCurv = 0;
161  SCellToMidPoint< KSpace > midpoint( K );
162  for ( std::set< SCell >::iterator it = theSetOfSurfels.begin(), it_end = theSetOfSurfels.end();
163  it != it_end; ++it)
164  {
165 
166  RealPoint A = midpoint( *it ) * step;
167  A = ishape.nearestPoint (A,0.01,200,0.1);
168 // double a = ishape.meanCurvature( A );
169  double a=ishape.gaussianCurvature(A);
170  if ( boost::math::isnan( a ))
171  {
172  a = 0;
173  }
174  else
175  {
176  if ( a > maxCurv )
177  {
178  maxCurv = a;
179  }
180  else if ( a < minCurv )
181  {
182  minCurv = a;
183  }
184  }
185  }
186 
187  trace.info() << " Min = " << minCurv << std::endl;
188  trace.info() << " Max = " << maxCurv << std::endl;
189 
190 
191  //-----------------------------------------------------------------------
192  //Specifing a color map
193 
194  GradientColorMap< double > cmap_grad( minCurv, maxCurv );
195  cmap_grad.addColor( Color( 50, 50, 255 ) );
196  cmap_grad.addColor( Color( 255, 0, 0 ) );
197  cmap_grad.addColor( Color( 255, 255, 10 ) );
198 
199  //------------------------------------------------------------------------------------
200  //drawing
201  unsigned int nbSurfels = 0;
202 
203  for ( std::set<SCell>::iterator it = theSetOfSurfels.begin(),
204  it_end = theSetOfSurfels.end();
205  it != it_end; ++it, ++nbSurfels )
206  {
207 
208 
209  RealPoint A = midpoint( *it ) * step;
210  A = ishape.nearestPoint (A,0.01,200,0.1);
211  double a=ishape.gaussianCurvature(A);
212 // double a = ishape.meanCurvature( A );
213  if ( boost::math::isnan( a ))
214  {
215  a = 0;
216  }
217 
218  viewer << CustomColors3D( Color::Black, cmap_grad( a ));
219  viewer << *it;
220  }
221 
222  viewer << Viewer3D::updateDisplay;
223 
224  return application.exec();
225 }