DGtal 1.3.0
Loading...
Searching...
No Matches
digpoly-curvature-measures-cnc-3d.cpp
Go to the documentation of this file.
1
113#include <iostream>
114#include <fstream>
115#include <algorithm>
116#include "DGtal/base/Common.h"
117#include "DGtal/shapes/SurfaceMesh.h"
119#include "DGtal/geometry/meshes/CorrectedNormalCurrentComputer.h"
121#include "DGtal/helpers/Shortcuts.h"
122#include "DGtal/helpers/ShortcutsGeometry.h"
123#include "DGtal/io/writers/SurfaceMeshWriter.h"
124#include "DGtal/io/colormaps/GradientColorMap.h"
125#include "DGtal/io/colormaps/QuantifiedColorMap.h"
126
128makeColorMap( double min_value, double max_value )
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}
138
139void usage( int argc, char* argv[] )
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}
167
168int main( int argc, char* argv[] )
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}
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()
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.
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
int main()
Definition: testBits.cpp:56
KSpace K