DGtal  1.5.beta
vol-curvature-measures-icnc-XY-3d.cpp
Go to the documentation of this file.
1 
76 #include <iostream>
77 #include <algorithm>
78 #include "DGtal/base/Common.h"
79 #include "DGtal/math/linalg/EigenDecomposition.h"
80 #include "DGtal/shapes/SurfaceMesh.h"
82 #include "DGtal/geometry/meshes/CorrectedNormalCurrentComputer.h"
84 #include "DGtal/io/writers/SurfaceMeshWriter.h"
85 #include "DGtal/io/colormaps/GradientColorMap.h"
86 #include "DGtal/helpers/Shortcuts.h"
87 #include "DGtal/helpers/ShortcutsGeometry.h"
88 #include "DGtal/io/readers/SurfaceMeshReader.h"
89 #include "DGtal/io/colormaps/GradientColorMap.h"
90 #include "DGtal/io/colormaps/QuantifiedColorMap.h"
91 
93 makeColorMap( double min_value, double max_value )
94 {
95  DGtal::GradientColorMap< double > gradcmap( min_value, max_value );
96  gradcmap.addColor( DGtal::Color( 0, 0, 255 ) );
97  gradcmap.addColor( DGtal::Color( 0, 255, 255 ) );
98  gradcmap.addColor( DGtal::Color( 255, 255, 255 ) );
99  gradcmap.addColor( DGtal::Color( 255, 255, 0 ) );
100  gradcmap.addColor( DGtal::Color( 255, 0, 0 ) );
101  return gradcmap;
102 }
103 
104 void usage( int argc, char* argv[] )
105 {
106  std::cout << "Usage: " << std::endl
107  << "\t" << argv[ 0 ] << " <filename.vol> <R> <m> <M> <Kmax>" << std::endl
108  << std::endl
109  << "Computation of principal curvatures and directions on a vol file, " << std::endl
110  << "using interpolated corrected curvature measures (based " << std::endl
111  << "on the theory of corrected normal currents)." << std::endl
112  << "- builds the surface mesh from file <filename.obj>" << std::endl
113  << "- <R> is the radius of the measuring balls." << std::endl
114  << "- <m> is the min threshold value for the vol file" << std::endl
115  << "- <M> is the max threshold value for the vol file" << std::endl
116  << "- <Kmax> gives the colormap range [-Kmax,Kmax] for" << std::endl
117  << " the output of principal curvatures estimates" << std::endl
118  << "It produces several OBJ files to display principal " << std::endl
119  << "curvatures and directions estimations: `example-cnc-K1.obj`" << std::endl
120  << "`example-cnc-K2.obj`, `example-cnc-D1.obj`, and" << std::endl
121  << "`example-cnc-D2.obj` as well as associated MTL files." << std::endl;
122 }
123 
124 int main( int argc, char* argv[] )
125 {
126  if ( argc <= 1 )
127  {
128  usage( argc, argv );
129  return 0;
130  }
132  using namespace DGtal;
133  using namespace DGtal::Z3i;
136  typedef Shortcuts< KSpace > SH;
137  typedef ShortcutsGeometry< KSpace > SHG;
139  // VOL file
140  std::string input = argv[ 1 ];
141  const double R = argc > 2 ? atof( argv[ 2 ] ) : 2.0; // radius of measuring ball
142  const int m = argc > 3 ? atoi( argv[ 3 ] ) : 0; // min threshold
143  const int M = argc > 4 ? atoi( argv[ 4 ] ) : 1; // max threshold
144  const double Kmax = argc > 5 ? atof( argv[ 5 ] ) : 0.33; // range mean curvature colormap
145 
147  // Read VOL file and build digital surface
148  auto params = SH::defaultParameters() | SHG::defaultParameters();
149  params( "thresholdMin", m )( "thresholdMax", M )( "closed", 1);
150  params( "t-ring", 3 )( "surfaceTraversal", "Default" );
151  auto bimage = SH::makeBinaryImage( input.c_str(), params );
152  if ( bimage == nullptr )
153  {
154  trace.error() << "Unable to read file <" << input.c_str() << ">" << std::endl;
155  return 1;
156  }
157  auto K = SH::getKSpace( bimage, params );
158  auto sembedder = SH::getSCellEmbedder( K );
159  auto embedder = SH::getCellEmbedder( K );
160  auto surface = SH::makeDigitalSurface( bimage, K, params );
161  auto surfels = SH::getSurfelRange( surface, params );
162  trace.info() << "- surface has " << surfels.size()<< " surfels." << std::endl;
164 
166  SM smesh;
167  std::vector< SM::Vertices > faces;
168  SH::Cell2Index c2i;
169  auto pointels = SH::getPointelRange( c2i, surface );
170  auto vertices = SH::RealPoints( pointels.size() );
171  std::transform( pointels.cbegin(), pointels.cend(), vertices.begin(),
172  [&] (const SH::Cell& c) { return embedder( c ); } );
173  for ( auto&& surfel : *surface )
174  {
175  const auto primal_surfel_vtcs = SH::getPointelRange( K, surfel );
176  SM::Vertices face;
177  for ( auto&& primal_vtx : primal_surfel_vtcs )
178  face.push_back( c2i[ primal_vtx ] );
179  faces.push_back( face );
180  }
181  smesh.init( vertices.cbegin(), vertices.cend(),
182  faces.cbegin(), faces.cend() );
183  trace.info() << smesh << std::endl;
185 
187  // Builds a CorrectedNormalCurrentComputer object onto the SurfaceMesh object
188  CNC cnc( smesh );
189  // Estimates normal vectors using Convolved Trivial Normal estimator
190  auto face_normals = SHG::getCTrivialNormalVectors( surface, surfels, params );
191  smesh.setFaceNormals( face_normals.cbegin(), face_normals.cend() );
192  if ( smesh.vertexNormals().empty() )
193  smesh.computeVertexNormalsFromFaceNormals();
194  // computes area, anisotropic XY curvature measures
195  auto mu0 = cnc.computeMu0();
196  auto muXY = cnc.computeMuXY();
198 
200  // estimates principal curvatures (K1,K2) and directions (D1,D2) by
201  // measure normalization.
202  std::vector< double > K1( smesh.nbFaces() );
203  std::vector< double > K2( smesh.nbFaces() );
204  std::vector< RealVector > D1( smesh.nbFaces() );
205  std::vector< RealVector > D2( smesh.nbFaces() );
206  for ( auto f = 0; f < smesh.nbFaces(); ++f )
207  {
208  const auto b = smesh.faceCentroid( f );
209  const auto N = smesh.faceNormals()[ f ];
210  const auto area = mu0 .measure( b, R, f );
211  const auto M = muXY.measure( b, R, f );
212  std::tie( K1[ f ], K2[ f ], D1[ f ], D2[ f ] )
213  = cnc.principalCurvatures( area, M, N );
214  }
216 
218  auto K1_min_max = std::minmax_element( K1.cbegin(), K1.cend() );
219  auto K2_min_max = std::minmax_element( K2.cbegin(), K2.cend() );
220  std::cout << "Computed k1 curvatures:"
221  << " min=" << *K1_min_max.first << " max=" << *K1_min_max.second
222  << std::endl;
223  std::cout << "Computed k2 curvatures:"
224  << " min=" << *K2_min_max.first << " max=" << *K2_min_max.second
225  << std::endl;
227 
229  // Remove normals for better blocky display.
230  smesh.vertexNormals() = SH::RealVectors();
231  smesh.faceNormals() = SH::RealVectors();
233  const auto colormapK1 = makeQuantifiedColorMap( makeColorMap( -Kmax, Kmax ) );
234  const auto colormapK2 = makeQuantifiedColorMap( makeColorMap( -Kmax, Kmax ) );
235  auto colorsK1 = SMW::Colors( smesh.nbFaces() );
236  auto colorsK2 = SMW::Colors( smesh.nbFaces() );
237  for ( auto i = 0; i < smesh.nbFaces(); i++ )
238  {
239  colorsK1[ i ] = colormapK1( K1[ i ] );
240  colorsK2[ i ] = colormapK2( K2[ i ] );
241  }
242  SMW::writeOBJ( "example-cnc-K1", smesh, colorsK1 );
243  SMW::writeOBJ( "example-cnc-K2", smesh, colorsK2 );
244  const auto avg_e = smesh.averageEdgeLength();
245  SH::RealPoints positions( smesh.nbFaces() );
246  for ( auto f = 0; f < positions.size(); ++f )
247  {
248  D1[ f ] *= smesh.localWindow( f );
249  positions[ f ] = smesh.faceCentroid( f ) - 0.5 * D1[ f ];
250  }
251  SH::saveVectorFieldOBJ( positions, D1, 0.05 * avg_e, SH::Colors(),
252  "example-cnc-D1",
253  SH::Color::Black, SH::Color( 0, 128, 0 ) );
254  for ( auto f = 0; f < positions.size(); ++f )
255  {
256  D2[ f ] *= smesh.localWindow( f );
257  positions[ f ] = smesh.faceCentroid( f ) - 0.5 * D2[ f ];
258  }
259  SH::saveVectorFieldOBJ( positions, D2, 0.05 * avg_e, SH::Colors(),
260  "example-cnc-D2",
261  SH::Color::Black, SH::Color(128, 0,128 ) );
263  return 0;
264 }
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[])