DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
digpoly-curvature-measures-cnc-3d.cpp File Reference
#include <iostream>
#include <fstream>
#include <algorithm>
#include "DGtal/base/Common.h"
#include "DGtal/shapes/SurfaceMesh.h"
#include "DGtal/geometry/meshes/CorrectedNormalCurrentComputer.h"
#include "DGtal/helpers/Shortcuts.h"
#include "DGtal/helpers/ShortcutsGeometry.h"
#include "DGtal/io/writers/SurfaceMeshWriter.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
#include "DGtal/io/colormaps/QuantifiedColorMap.h"

Go to the source code of this file.

Functions

DGtal::GradientColorMap< double > makeColorMap (double min_value, double max_value)
 [curvature-measures-Includes] More...
 
void usage (int argc, char *argv[])
 
int main (int argc, char *argv[])
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2021/10/25

An example file named digpoly-curvature-measures-cnc-3d.

This file is part of the DGtal library.

Definition in file digpoly-curvature-measures-cnc-3d.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

[curvature-measures-Typedefs]

[curvature-measures-Typedefs]

[curvature-measures-DigitalSurface]

[curvature-measures-DigitalSurface]

[curvature-measures-SurfaceMesh]

[curvature-measures-SurfaceMesh]

[curvature-measures-TrueCurvatures]

[curvature-measures-TrueCurvatures]

[curvature-measures-CNC]

[curvature-measures-CNC]

[curvature-measures-estimations]

[curvature-measures-estimations]

[curvature-measures-check]

[curvature-measures-check]

[curvature-measures-output]

[curvature-measures-output]

Definition at line 168 of file digpoly-curvature-measures-cnc-3d.cpp.

169{
170 if ( argc <= 1 )
171 {
172 usage( argc, argv );
173 return 0;
174 }
176 using namespace DGtal;
177 using namespace DGtal::Z3i;
180 typedef Shortcuts< KSpace > SH;
181 typedef ShortcutsGeometry< KSpace > SHG;
183 std::string poly = argv[ 1 ]; // polynomial
184 const double B = argc > 2 ? atof( argv[ 2 ] ) : 1.0; // max ||_oo bbox
185 const double h = argc > 3 ? atof( argv[ 3 ] ) : 1.0; // gridstep
186 const double R = argc > 4 ? atof( argv[ 4 ] ) : 2.0; // radius of measuring ball
187 std::string mode = argc > 5 ? argv[ 5 ] : "Const"; // either Const or Interp
188 bool interpolated = mode == "Interp";
189 if ( interpolated )
190 std::cout << "Using vertex-*Interpolated* Corrected Normal Current" << std::endl;
191 else
192 std::cout << "Using face-*Constant* Corrected Normal Current" << std::endl;
194 // Read polynomial and build digital surface
195 auto params = SH::defaultParameters() | SHG::defaultParameters();
196 params( "t-ring", 3 )( "surfaceTraversal", "Default" );
197 params( "polynomial", poly )( "gridstep", h );
198 params( "minAABB", -B )( "maxAABB", B );
199 params( "offset", 3.0 );
200 auto shape = SH::makeImplicitShape3D( params );
201 auto K = SH::getKSpace( params );
202 auto dshape = SH::makeDigitizedImplicitShape3D( shape, params );
203 auto bimage = SH::makeBinaryImage( dshape, params );
204 if ( bimage == nullptr )
205 {
206 trace.error() << "Unable to read polynomial <"
207 << poly.c_str() << ">" << std::endl;
208 return 1;
209 }
210 auto sembedder = SH::getSCellEmbedder( K );
211 auto embedder = SH::getCellEmbedder( K );
212 auto surface = SH::makeDigitalSurface( bimage, K, params );
213 auto surfels = SH::getSurfelRange( surface, params );
214 trace.info() << "- surface has " << surfels.size()<< " surfels." << std::endl;
216
218 SM smesh;
219 std::vector< SM::Vertices > faces;
220 SH::Cell2Index c2i;
221 auto pointels = SH::getPointelRange( c2i, surface );
222 auto vertices = SH::RealPoints( pointels.size() );
223 std::transform( pointels.cbegin(), pointels.cend(), vertices.begin(),
224 [&] (const SH::Cell& c) { return h * embedder( c ); } );
225 for ( auto&& surfel : *surface )
226 {
227 const auto primal_surfel_vtcs = SH::getPointelRange( K, surfel );
228 SM::Vertices face;
229 for ( auto&& primal_vtx : primal_surfel_vtcs )
230 face.push_back( c2i[ primal_vtx ] );
231 faces.push_back( face );
232 }
233 smesh.init( vertices.cbegin(), vertices.cend(),
234 faces.cbegin(), faces.cend() );
235 trace.info() << smesh << std::endl;
237
239 auto exp_H = SHG::getMeanCurvatures( shape, K, surfels, params );
240 auto exp_G = SHG::getGaussianCurvatures( shape, K, surfels, params );
242
244 // Builds a CorrectedNormalCurrentComputer object onto the SurfaceMesh object
245 CNC cnc( smesh );
246 // Estimates normal vectors using Convolved Trivial Normal estimator
247 auto face_normals = SHG::getCTrivialNormalVectors( surface, surfels, params );
248 // Set corrected face normals => Corrected Normal Current with
249 // constant per face corrected vector field.
250 smesh.setFaceNormals( face_normals.cbegin(), face_normals.cend() ); // CCNC
251 // Set corrected vertex normals => Corrected Normal Current with
252 // smooth linearly interpolated per face corrected vector field.
253 if ( interpolated ) smesh.computeVertexNormalsFromFaceNormals(); // ICNC
254 // computes area, mean and Gaussian curvature measures
255 auto mu0 = cnc.computeMu0();
256 auto mu1 = cnc.computeMu1();
257 auto mu2 = cnc.computeMu2();
259
261 // estimates mean (H) and Gaussian (G) curvatures by measure normalization.
262 std::vector< double > H( smesh.nbFaces() );
263 std::vector< double > G( smesh.nbFaces() );
264 for ( auto f = 0; f < smesh.nbFaces(); ++f )
265 {
266 const auto b = smesh.faceCentroid( f );
267 const auto area = mu0.measure( b, R, f );
268 H[ f ] = cnc.meanCurvature ( area, mu1.measure( b, R, f ) );
269 G[ f ] = cnc.GaussianCurvature( area, mu2.measure( b, R, f ) );
270 }
272
274 auto H_min_max = std::minmax_element( H.cbegin(), H.cend() );
275 auto G_min_max = std::minmax_element( G.cbegin(), G.cend() );
276 auto exp_H_min_max = std::minmax_element( exp_H.cbegin(), exp_H.cend() );
277 auto exp_G_min_max = std::minmax_element( exp_G.cbegin(), exp_G.cend() );
278 std::cout << "Expected mean curvatures:"
279 << " min=" << *exp_H_min_max.first << " max=" << *exp_H_min_max.second
280 << std::endl;
281 std::cout << "Computed mean curvatures:"
282 << " min=" << *H_min_max.first << " max=" << *H_min_max.second
283 << std::endl;
284 std::cout << "Expected Gaussian curvatures:"
285 << " min=" << *exp_G_min_max.first << " max=" << *exp_G_min_max.second
286 << std::endl;
287 std::cout << "Computed Gaussian curvatures:"
288 << " min=" << *G_min_max.first << " max=" << *G_min_max.second
289 << std::endl;
290 const auto error_H = SHG::getScalarsAbsoluteDifference( H, exp_H );
291 const auto stat_error_H = SHG::getStatistic( error_H );
292 const auto error_H_l2 = SHG::getScalarsNormL2( H, exp_H );
293 trace.info() << "|H-H_CNC|_oo = " << stat_error_H.max() << std::endl;
294 trace.info() << "|H-H_CNC|_2 = " << error_H_l2 << std::endl;
295 const auto error_G = SHG::getScalarsAbsoluteDifference( G, exp_G );
296 const auto stat_error_G = SHG::getStatistic( error_G );
297 const auto error_G_l2 = SHG::getScalarsNormL2( G, exp_G );
298 trace.info() << "|G-G_CNC|_oo = " << stat_error_G.max() << std::endl;
299 trace.info() << "|G-G_CNC|_2 = " << error_G_l2 << std::endl;
301
303 // Remove normals for better blocky display.
304 smesh.vertexNormals() = SH::RealVectors();
305 smesh.faceNormals() = SH::RealVectors();
307 const double Hmax = std::max( fabs( *exp_H_min_max.first ),
308 fabs( *exp_H_min_max.second ) );
309 const double Gmax = std::max( fabs( *exp_G_min_max.first ),
310 fabs( *exp_G_min_max.second ) );
311 const auto colormapH = makeQuantifiedColorMap( makeColorMap( -Hmax, Hmax ) );
312 const auto colormapG = makeQuantifiedColorMap( makeColorMap( -Gmax, Gmax ) );
313 auto colorsH = SMW::Colors( smesh.nbFaces() );
314 auto colorsG = SMW::Colors( smesh.nbFaces() );
315 for ( auto i = 0; i < smesh.nbFaces(); i++ )
316 {
317 colorsH[ i ] = colormapH( H[ i ] );
318 colorsG[ i ] = colormapG( G[ i ] );
319 }
320 SMW::writeOBJ( "example-cnc-H", smesh, colorsH );
321 SMW::writeOBJ( "example-cnc-G", smesh, colorsG );
323 return 0;
324}
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()
DGtal::GradientColorMap< double > makeColorMap(double min_value, double max_value)
[curvature-measures-Includes]
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:154
std::pair< typename graph_traits< DGtal::DigitalSurface< TDigitalSurfaceContainer > >::vertex_iterator, typename graph_traits< DGtal::DigitalSurface< TDigitalSurfaceContainer > >::vertex_iterator > vertices(const DGtal::DigitalSurface< TDigitalSurfaceContainer > &digSurf)
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

References DGtal::Trace::error(), DGtal::Trace::info(), K, and makeColorMap().

◆ makeColorMap()

DGtal::GradientColorMap< double > makeColorMap ( double  min_value,
double  max_value 
)

[curvature-measures-Includes]

[curvature-measures-Includes]

Definition at line 128 of file digpoly-curvature-measures-cnc-3d.cpp.

129{
130 DGtal::GradientColorMap< double > gradcmap( min_value, max_value );
131 gradcmap.addColor( DGtal::Color( 0, 0, 255 ) );
132 gradcmap.addColor( DGtal::Color( 0, 255, 255 ) );
133 gradcmap.addColor( DGtal::Color( 255, 255, 255 ) );
134 gradcmap.addColor( DGtal::Color( 255, 255, 0 ) );
135 gradcmap.addColor( DGtal::Color( 255, 0, 0 ) );
136 return gradcmap;
137}
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...

References DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >::addColor().

Referenced by main().

◆ usage()

void usage ( int  argc,
char *  argv[] 
)

Definition at line 139 of file digpoly-curvature-measures-cnc-3d.cpp.

140{
141 using namespace DGtal;
142 using namespace DGtal::Z3i;
143 typedef Shortcuts< KSpace > SH;
144 std::cout << "Usage: " << std::endl
145 << "\t" << argv[ 0 ] << " <P> <B> <h> <R> <mode>" << std::endl
146 << std::endl
147 << "Computation of mean and Gaussian curvatures on an " << std::endl
148 << "digitized implicit shape using constant or " << std::endl
149 << "interpolated corrected curvature measures (based " << std::endl
150 << "on the theory of corrected normal currents)." << std::endl
151 << "- builds the surface mesh from polynomial <P>" << std::endl
152 << "- <B> defines the digitization space size [-B,B]^3" << std::endl
153 << "- <h> is the gridstep digitization" << std::endl
154 << "- <R> is the radius of the measuring balls" << std::endl
155 << "- <mode> is either Const for constant corrected normal" << std::endl
156 << " vector field or Interp for interpolated corrected" << std::endl
157 << " normal vector field." << std::endl
158 << "It produces several OBJ files to display mean and" << std::endl
159 << "Gaussian curvature estimation results: `example-cnc-H.obj`" << std::endl
160 << "and `example-cnc-G.obj` as well as the associated MTL file." << std::endl;
161 std::cout << "You may either write your own polynomial as 3*x^2*y-z^2*x*y+1" << std::endl
162 <<"or use a predefined polynomial in the following list:" << std::endl;
163 auto L = SH::getPolynomialList();
164 for ( const auto& p : L )
165 std::cout << p.first << " : " << p.second << std::endl;
166}