DGtal  1.5.beta
obj-curvature-measures-icnc-3d.cpp
Go to the documentation of this file.
1 
63 #include <iostream>
64 #include <fstream>
65 #include <algorithm>
66 #include "DGtal/base/Common.h"
67 #include "DGtal/shapes/SurfaceMesh.h"
68 #include "DGtal/shapes/SurfaceMeshHelper.h"
70 #include "DGtal/geometry/meshes/CorrectedNormalCurrentComputer.h"
72 #include "DGtal/io/readers/SurfaceMeshReader.h"
73 #include "DGtal/io/writers/SurfaceMeshWriter.h"
74 #include "DGtal/io/colormaps/GradientColorMap.h"
75 #include "DGtal/io/colormaps/QuantifiedColorMap.h"
76 
78 makeColorMap( double min_value, double max_value )
79 {
80  DGtal::GradientColorMap< double > gradcmap( min_value, max_value );
81  gradcmap.addColor( DGtal::Color( 0, 0, 255 ) );
82  gradcmap.addColor( DGtal::Color( 0, 255, 255 ) );
83  gradcmap.addColor( DGtal::Color( 255, 255, 255 ) );
84  gradcmap.addColor( DGtal::Color( 255, 255, 0 ) );
85  gradcmap.addColor( DGtal::Color( 255, 0, 0 ) );
86  return gradcmap;
87 }
88 
89 void usage( int argc, char* argv[] )
90 {
91  std::cout << "Usage: " << std::endl
92  << "\t" << argv[ 0 ] << " <filename.obj> <R> <Hmax> <Gmax>" << std::endl
93  << std::endl
94  << "Computation of mean and Gaussian curvatures on a mesh, " << std::endl
95  << "using interpolated corrected curvature measures (based " << std::endl
96  << "on the theory of corrected normal currents)." << std::endl
97  << "- builds the surface mesh from file <filename.obj>" << std::endl
98  << "- <R> is the radius of the measuring balls" << std::endl
99  << "- <Hmax> gives the colormap range [-Hmax,Hmax] for" << std::endl
100  << " the output of mean curvature estimates" << std::endl
101  << "- <Gmax> gives the colormap range [-Gmax,Gmax] for" << std::endl
102  << " the output of mean curvature estimates" << std::endl
103  << "It produces several OBJ files to display mean and" << std::endl
104  << "Gaussian curvature estimation results: `example-cnc-H.obj`" << std::endl
105  << "and `example-cnc-G.obj` as well as the associated MTL file." << std::endl;
106 }
107 
108 int main( int argc, char* argv[] )
109 {
110  if ( argc <= 1 )
111  {
112  usage( argc, argv );
113  return 0;
114  }
116  using namespace DGtal;
117  using namespace DGtal::Z3i;
122  // OBJ file
123  std::string input = argv[ 1 ];
124  const double R = argc > 2 ? atof( argv[ 2 ] ) : 0.0; // radius of measuring ball
125  const double Hmax = argc > 3 ? atof( argv[ 3 ] ) : 5.0; // range mean curvature colormap
126  const double Gmax = argc > 4 ? atof( argv[ 4 ] ) : 0.5*Hmax*Hmax; // range Gaussian curvature colormap
128  SM smesh;
129  std::ifstream obj_stream( input.c_str() );
130  bool ok = SMR::readOBJ( obj_stream, smesh );
131  if ( !ok )
132  {
133  trace.error() << "Unable to read file <" << input.c_str() << ">" << std::endl;
134  return 1;
135  }
136  RealPoint lo = smesh.position( 0 );
137  RealPoint up = smesh.position( 0 );
138  for ( const auto& p : smesh.positions() )
139  lo = lo.inf( p ), up = up.sup( p );
140  const auto diameter = (up - lo).norm();
141  trace.info() << "Mesh=" << smesh
142  << " diameter=" << diameter
143  << " radius=" << R << std::endl;
145 
147  // builds a CorrectedNormalCurrentComputer object onto the SurfaceMesh object
148  CNC cnc( smesh );
149  // computes normals if necessary
150  if ( smesh.vertexNormals().empty() )
151  {
152  if ( smesh.faceNormals().empty() )
153  smesh.computeFaceNormalsFromPositions();
154  smesh.computeVertexNormalsFromFaceNormals();
155  }
156  // computes area, mean and Gaussian curvature measures
157  auto mu0 = cnc.computeMu0();
158  auto mu1 = cnc.computeMu1();
159  auto mu2 = cnc.computeMu2();
161 
163  // estimates mean (H) and Gaussian (G) curvatures by measure normalization.
164  std::vector< double > H( smesh.nbFaces() );
165  std::vector< double > G( smesh.nbFaces() );
166  for ( auto f = 0; f < smesh.nbFaces(); ++f )
167  {
168  const auto b = smesh.faceCentroid( f );
169  const auto area = mu0.measure( b, R, f );
170  H[ f ] = cnc.meanCurvature ( area, mu1.measure( b, R, f ) );
171  G[ f ] = cnc.GaussianCurvature( area, mu2.measure( b, R, f ) );
172  }
174 
176  auto H_min_max = std::minmax_element( H.cbegin(), H.cend() );
177  auto G_min_max = std::minmax_element( G.cbegin(), G.cend() );
178  std::cout << "Computed mean curvatures:"
179  << " min=" << *H_min_max.first << " max=" << *H_min_max.second
180  << std::endl;
181  std::cout << "Computed Gaussian curvatures:"
182  << " min=" << *G_min_max.first << " max=" << *G_min_max.second
183  << std::endl;
185 
188  const auto colormapH = makeQuantifiedColorMap( makeColorMap( -Hmax, Hmax ) );
189  const auto colormapG = makeQuantifiedColorMap( makeColorMap( -Gmax, Gmax ) );
190  auto colorsH = SMW::Colors( smesh.nbFaces() );
191  auto colorsG = SMW::Colors( smesh.nbFaces() );
192  for ( auto i = 0; i < smesh.nbFaces(); i++ )
193  {
194  colorsH[ i ] = colormapH( H[ i ] );
195  colorsG[ i ] = colormapG( G[ i ] );
196  }
197  SMW::writeOBJ( "example-cnc-H", smesh, colorsH );
198  SMW::writeOBJ( "example-cnc-G", smesh, colorsG );
200  return 0;
201 }
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: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
auto inf(const PointVector< dim, OtherComponent, OtherStorage > &aPoint) const -> decltype(DGtal::inf(*this, aPoint))
Implements the infimum (or greatest lower bound).
auto sup(const PointVector< dim, OtherComponent, OtherStorage > &aPoint) const -> decltype(DGtal::sup(*this, aPoint))
Implements the supremum (or least upper bound).
std::ostream & error()
std::ostream & info()
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
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[])
Aim: Utility class to compute curvature measures induced by (1) a corrected normal current defined by...
Aim: An helper class for reading mesh files (Wavefront OBJ at this point) and creating a SurfaceMesh.
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