DGtal  1.5.beta
curvature-measures-nc-XY-3d.cpp
Go to the documentation of this file.
1 
82 #include <iostream>
83 #include <algorithm>
84 #include "DGtal/base/Common.h"
85 #include "DGtal/math/linalg/EigenDecomposition.h"
86 #include "DGtal/shapes/SurfaceMesh.h"
87 #include "DGtal/shapes/SurfaceMeshHelper.h"
89 #include "DGtal/geometry/meshes/NormalCycleComputer.h"
91 #include "DGtal/io/writers/SurfaceMeshWriter.h"
92 #include "DGtal/io/colormaps/GradientColorMap.h"
93 #include "DGtal/io/colormaps/QuantifiedColorMap.h"
94 #include "DGtal/helpers/Shortcuts.h"
95 
97 makeColorMap( double min_value, double max_value )
98 {
99  DGtal::GradientColorMap< double > gradcmap( min_value, max_value );
100  gradcmap.addColor( DGtal::Color( 0, 0, 255 ) );
101  gradcmap.addColor( DGtal::Color( 0, 255, 255 ) );
102  gradcmap.addColor( DGtal::Color( 255, 255, 255 ) );
103  gradcmap.addColor( DGtal::Color( 255, 255, 0 ) );
104  gradcmap.addColor( DGtal::Color( 255, 0, 0 ) );
105  return gradcmap;
106 }
107 
108 void usage( int argc, char* argv[] )
109 {
110  std::cout << "Usage: " << std::endl
111  << "\t" << argv[ 0 ] << " <shape> <m> <n> <R>" << std::endl
112  << std::endl
113  << "Computation of principal curvatures and directions on a shape, " << std::endl
114  << "using Normal Cycle anisotropic curvature measure." << std::endl
115  << "- builds a <shape> in {torus,lantern,sphere}, with " << std::endl
116  << " <m> latitude points and <n> longitude points." << std::endl
117  << "- <R> is the radius of the measuring balls." << 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;
138  // a shape in "torus|lantern|sphere"
139  std::string input = argv[ 1 ];
140  int m = argc > 2 ? atoi( argv[ 2 ] ) : 20; // nb latitude points
141  int n = argc > 3 ? atoi( argv[ 3 ] ) : 20; // nb longitude points
142  double R = argc > 4 ? atof( argv[ 4 ] ) : 0.5; // radius of measuring ball
143 
145  SM smesh;
146  double exp_K1_min = 0.0;
147  double exp_K1_max = 0.0;
148  double exp_K2_min = 0.0;
149  double exp_K2_max = 0.0;
150  if ( input == "torus" )
151  {
152  const double big_radius = 3.0;
153  const double small_radius = 1.00001; // avoid codacy warnings
154  smesh = SMH::makeTorus( big_radius, small_radius,
155  RealPoint { 0.0, 0.0, 0.0 }, m, n, 0,
156  SMH::NormalsType::VERTEX_NORMALS );
157  exp_K1_min = ( 1.0 / ( small_radius - big_radius ) );
158  exp_K1_max = ( 1.0 / ( big_radius + small_radius ) );
159  exp_K2_min = 1.0 / small_radius;
160  exp_K2_max = 1.0 / small_radius;
161  }
162  else if ( input == "sphere" )
163  {
164  const double radius = 2.0;
165  smesh = SMH::makeSphere( radius, RealPoint { 0.0, 0.0, 0.0 }, m, n,
166  SMH::NormalsType::VERTEX_NORMALS );
167  exp_K1_min = 1.0 / radius;
168  exp_K1_max = 1.0 / radius;
169  exp_K2_min = 1.0 / radius;
170  exp_K2_max = 1.0 / radius;
171  }
172  else if ( input == "lantern" )
173  {
174  const double radius = 2.0;
175  smesh = SMH::makeLantern( radius, 1.0, RealPoint { 0.0, 0.0, 0.0 }, m, n,
176  SMH::NormalsType::VERTEX_NORMALS );
177  exp_K1_min = 0.0;
178  exp_K1_max = 0.0;
179  exp_K2_min = 1.0 / radius;
180  exp_K2_max = 1.0 / radius;
181  }
183 
185  // builds a NormalCycleComputer object onto the torus mesh
186  NC nc( smesh );
187  // computes area, anisotropic XY curvature measures
188  auto mu0 = nc.computeMu0();
189  auto muXY = nc.computeMuXY();
191 
193  // Estimates principal curvatures (K1,K2) and directions (D1,D2) by
194  // measure normalization and eigen decomposition.
195  std::vector< double > K1( smesh.nbFaces() );
196  std::vector< double > K2( smesh.nbFaces() );
197  std::vector< RealVector > D1( smesh.nbFaces() );
198  std::vector< RealVector > D2( smesh.nbFaces() );
199  // Principal directions computation requires a local face normal
200  smesh.computeFaceNormalsFromPositions();
201  for ( auto f = 0; f < smesh.nbFaces(); ++f )
202  {
203  const auto b = smesh.faceCentroid( f );
204  const auto N = smesh.faceNormals()[ f ];
205  const auto area = mu0 .measure( b, R, f );
206  const auto M = muXY.measure( b, R, f );
207  std::tie( K1[ f ], K2[ f ], D1[ f ], D2[ f ] )
208  = nc.principalCurvatures( area, M, N );
209  }
211 
213  auto K1_min_max = std::minmax_element( K1.cbegin(), K1.cend() );
214  auto K2_min_max = std::minmax_element( K2.cbegin(), K2.cend() );
215  std::cout << "Expected k1 curvatures:"
216  << " min=" << exp_K1_min << " max=" << exp_K1_max
217  << std::endl;
218  std::cout << "Computed k1 curvatures:"
219  << " min=" << *K1_min_max.first << " max=" << *K1_min_max.second
220  << std::endl;
221  std::cout << "Expected k2 curvatures:"
222  << " min=" << exp_K2_min << " max=" << exp_K2_max
223  << std::endl;
224  std::cout << "Computed k2 curvatures:"
225  << " min=" << *K2_min_max.first << " max=" << *K2_min_max.second
226  << std::endl;
228 
231  typedef Shortcuts< KSpace > SH;
232  const auto colormapK1 = makeQuantifiedColorMap( makeColorMap( -0.625, 0.625 ) );
233  const auto colormapK2 = makeQuantifiedColorMap( makeColorMap( -0.625, 0.625 ) );
234  auto colorsK1 = SMW::Colors( smesh.nbFaces() );
235  auto colorsK2 = SMW::Colors( smesh.nbFaces() );
236  for ( auto i = 0; i < smesh.nbFaces(); i++ )
237  {
238  colorsK1[ i ] = colormapK1( K1[ i ] );
239  colorsK2[ i ] = colormapK2( K2[ i ] );
240  }
241  SMW::writeOBJ( "example-nc-K1", smesh, colorsK1 );
242  SMW::writeOBJ( "example-nc-K2", smesh, colorsK2 );
243  const auto avg_e = smesh.averageEdgeLength();
244  SH::RealPoints positions( smesh.nbFaces() );
245  for ( auto f = 0; f < positions.size(); ++f )
246  {
247  D1[ f ] *= smesh.localWindow( f );
248  positions[ f ] = smesh.faceCentroid( f ) - 0.5 * D1[ f ];
249  }
250  SH::saveVectorFieldOBJ( positions, D1, 0.05 * avg_e, SH::Colors(),
251  "example-nc-D1",
252  SH::Color::Black, SH::Color( 0, 128, 0 ) );
253  for ( auto f = 0; f < positions.size(); ++f )
254  {
255  D2[ f ] *= smesh.localWindow( f );
256  positions[ f ] = smesh.faceCentroid( f ) - 0.5 * D2[ f ];
257  }
258  SH::saveVectorFieldOBJ( positions, D2, 0.05 * avg_e, SH::Colors(),
259  "example-nc-D2",
260  SH::Color::Black, SH::Color(128, 0,128 ) );
261 
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: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
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[])
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)
Aim: Utility class to compute curvatures measures induced by (1) the normal cycle induced by a Surfac...
Aim: An helper class for building classical meshes.
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