DGtal  1.5.beta
vol-curvature-measures-icnc-3d.cpp
Go to the documentation of this file.
1 
73 #include <iostream>
74 #include <fstream>
75 #include <algorithm>
76 #include "DGtal/base/Common.h"
77 #include "DGtal/shapes/SurfaceMesh.h"
79 #include "DGtal/geometry/meshes/CorrectedNormalCurrentComputer.h"
81 #include "DGtal/helpers/Shortcuts.h"
82 #include "DGtal/helpers/ShortcutsGeometry.h"
83 #include "DGtal/io/writers/SurfaceMeshWriter.h"
84 #include "DGtal/io/colormaps/GradientColorMap.h"
85 #include "DGtal/io/colormaps/QuantifiedColorMap.h"
86 
88 makeColorMap( double min_value, double max_value )
89 {
90  DGtal::GradientColorMap< double > gradcmap( min_value, max_value );
91  gradcmap.addColor( DGtal::Color( 0, 0, 255 ) );
92  gradcmap.addColor( DGtal::Color( 0, 255, 255 ) );
93  gradcmap.addColor( DGtal::Color( 255, 255, 255 ) );
94  gradcmap.addColor( DGtal::Color( 255, 255, 0 ) );
95  gradcmap.addColor( DGtal::Color( 255, 0, 0 ) );
96  return gradcmap;
97 }
98 
99 void usage( int argc, char* argv[] )
100 {
101  std::cout << "Usage: " << std::endl
102  << "\t" << argv[ 0 ] << " <filename.vol> <R> <m> <M> <Hmax> <Gmax>" << std::endl
103  << std::endl
104  << "Computation of mean and Gaussian curvatures on a vol file, " << std::endl
105  << "using interpolated corrected curvature measures (based " << std::endl
106  << "on the theory of corrected normal currents)." << std::endl
107  << "- builds the surface mesh from file <filename.vol>" << std::endl
108  << "- <R> is the radius of the measuring balls" << std::endl
109  << "- <m> is the min threshold value for the vol file" << std::endl
110  << "- <M> is the max threshold value for the vol file" << std::endl
111  << "- <Hmax> gives the colormap range [-Hmax,Hmax] for" << std::endl
112  << " the output of mean curvature estimates" << std::endl
113  << "- <Gmax> gives the colormap range [-Gmax,Gmax] for" << std::endl
114  << " the output of mean curvature estimates" << std::endl
115  << "It produces several OBJ files to display mean and" << std::endl
116  << "Gaussian curvature estimation results: `example-cnc-H.obj`" << std::endl
117  << "and `example-cnc-G.obj` as well as the associated MTL file." << std::endl;
118 }
119 
120 int main( int argc, char* argv[] )
121 {
122  if ( argc <= 1 )
123  {
124  usage( argc, argv );
125  return 0;
126  }
128  using namespace DGtal;
129  using namespace DGtal::Z3i;
132  typedef Shortcuts< KSpace > SH;
133  typedef ShortcutsGeometry< KSpace > SHG;
135  // VOL file
136  std::string input = argv[ 1 ];
137  const double R = argc > 2 ? atof( argv[ 2 ] ) : 2.0; // radius of measuring ball
138  const int m = argc > 3 ? atoi( argv[ 3 ] ) : 0; // min threshold
139  const int M = argc > 4 ? atoi( argv[ 4 ] ) : 1; // max threshold
140  const double Hmax = argc > 5 ? atof( argv[ 5 ] ) : 0.33; // range mean curvature colormap
141  const double Gmax = argc > 6 ? atof( argv[ 6 ] ) : 0.1; // range Gaussian curvature colormap
142 
144  // Read VOL file and build digital surface
145  auto params = SH::defaultParameters() | SHG::defaultParameters();
146  params( "thresholdMin", m )( "thresholdMax", M )( "closed", 1);
147  params( "t-ring", 3 )( "surfaceTraversal", "Default" );
148  auto bimage = SH::makeBinaryImage( input.c_str(), params );
149  if ( bimage == nullptr )
150  {
151  trace.error() << "Unable to read file <" << input.c_str() << ">" << std::endl;
152  return 1;
153  }
154  auto K = SH::getKSpace( bimage, params );
155  auto sembedder = SH::getSCellEmbedder( K );
156  auto embedder = SH::getCellEmbedder( K );
157  auto surface = SH::makeDigitalSurface( bimage, K, params );
158  auto surfels = SH::getSurfelRange( surface, params );
159  trace.info() << "- surface has " << surfels.size()<< " surfels." << std::endl;
161 
163  SM smesh;
164  std::vector< SM::Vertices > faces;
165  SH::Cell2Index c2i;
166  auto pointels = SH::getPointelRange( c2i, surface );
167  auto vertices = SH::RealPoints( pointels.size() );
168  std::transform( pointels.cbegin(), pointels.cend(), vertices.begin(),
169  [&] (const SH::Cell& c) { return embedder( c ); } );
170  for ( auto&& surfel : *surface )
171  {
172  const auto primal_surfel_vtcs = SH::getPointelRange( K, surfel );
173  SM::Vertices face;
174  for ( auto&& primal_vtx : primal_surfel_vtcs )
175  face.push_back( c2i[ primal_vtx ] );
176  faces.push_back( face );
177  }
178  smesh.init( vertices.cbegin(), vertices.cend(),
179  faces.cbegin(), faces.cend() );
180  trace.info() << smesh << std::endl;
182 
184  // Builds a CorrectedNormalCurrentComputer object onto the SurfaceMesh object
185  CNC cnc( smesh );
186  // Estimates normal vectors using Convolved Trivial Normal estimator
187  auto face_normals = SHG::getCTrivialNormalVectors( surface, surfels, params );
188  smesh.setFaceNormals( face_normals.cbegin(), face_normals.cend() );
189  // if ( smesh.vertexNormals().empty() )
190  // smesh.computeVertexNormalsFromFaceNormals();
191  // computes area, mean and Gaussian curvature measures
192  std::cout << "Compute mu0" << std::endl;
193  auto mu0 = cnc.computeMu0();
194  std::cout << "Compute mu1" << std::endl;
195  auto mu1 = cnc.computeMu1();
196  std::cout << "Compute mu2" << std::endl;
197  auto mu2 = cnc.computeMu2();
199 
201  // estimates mean (H) and Gaussian (G) curvatures by measure normalization.
202  std::vector< double > H( smesh.nbFaces() );
203  std::vector< double > G( smesh.nbFaces() );
204  for ( auto f = 0; f < smesh.nbFaces(); ++f )
205  {
206  const auto b = smesh.faceCentroid( f );
207  const auto area = mu0.measure( b, R, f );
208  H[ f ] = cnc.meanCurvature ( area, mu1.measure( b, R, f ) );
209  G[ f ] = cnc.GaussianCurvature( area, mu2.measure( b, R, f ) );
210  }
212 
214  auto H_min_max = std::minmax_element( H.cbegin(), H.cend() );
215  auto G_min_max = std::minmax_element( G.cbegin(), G.cend() );
216  std::cout << "Computed mean curvatures:"
217  << " min=" << *H_min_max.first << " max=" << *H_min_max.second
218  << std::endl;
219  std::cout << "Computed Gaussian curvatures:"
220  << " min=" << *G_min_max.first << " max=" << *G_min_max.second
221  << std::endl;
223 
225  // Remove normals for better blocky display.
226  smesh.vertexNormals() = SH::RealVectors();
227  smesh.faceNormals() = SH::RealVectors();
229  const auto colormapH = makeQuantifiedColorMap( makeColorMap( -Hmax, Hmax ) );
230  const auto colormapG = makeQuantifiedColorMap( makeColorMap( -Gmax, Gmax ) );
231  auto colorsH = SMW::Colors( smesh.nbFaces() );
232  auto colorsG = SMW::Colors( smesh.nbFaces() );
233  for ( auto i = 0; i < smesh.nbFaces(); i++ )
234  {
235  colorsH[ i ] = colormapH( H[ i ] );
236  colorsG[ i ] = colormapG( G[ i ] );
237  }
238  SMW::writeOBJ( "example-cnc-H", smesh, colorsH );
239  SMW::writeOBJ( "example-cnc-G", smesh, colorsG );
241  return 0;
242 }
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
void addColor(const Color &color)
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
std::ostream & error()
std::ostream & info()
CountedPtr< SH3::DigitalSurface > surface
SMesh::Vertices Vertices
Z3i this namespace gathers the standard of types for 3D imagery.
DGtal is the top-level namespace which contains all DGtal functions and types.
QuantifiedColorMap< TColorMap > makeQuantifiedColorMap(TColorMap colormap, int nb=50)
Trace trace
Definition: Common.h:153
Aim: Utility class to compute curvature measures induced by (1) a corrected normal current defined by...
Aim: An helper class for writing mesh file formats (Waverfront OBJ at this point) and creating a Surf...
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition: SurfaceMesh.h:92
KSpace K
KSpace::Cell Cell
int main(int argc, char *argv[])
DGtal::GradientColorMap< double > makeColorMap(double min_value, double max_value)
[curvature-measures-Includes]
void usage(int argc, char *argv[])