DGtal 1.4.0
Loading...
Searching...
No Matches
curvature-comparator-ii-cnc-3d.cpp
Go to the documentation of this file.
1
73#include <iostream>
74#include <fstream>
75#include <algorithm>
76#include "DGtal/base/Common.h"
77#include "DGtal/shapes/SurfaceMesh.h"
79#include "DGtal/geometry/meshes/CorrectedNormalCurrentComputer.h"
81
82#include "DGtal/helpers/Shortcuts.h"
83#include "DGtal/helpers/ShortcutsGeometry.h"
84#include "DGtal/io/writers/SurfaceMeshWriter.h"
85#include "DGtal/io/colormaps/GradientColorMap.h"
86#include "DGtal/io/colormaps/QuantifiedColorMap.h"
87
89makeColorMap( double min_value, double max_value )
90{
91 DGtal::GradientColorMap< double > gradcmap( min_value, max_value );
92 gradcmap.addColor( DGtal::Color( 0, 0, 255 ) );
93 gradcmap.addColor( DGtal::Color( 0, 255, 255 ) );
94 gradcmap.addColor( DGtal::Color( 255, 255, 255 ) );
95 gradcmap.addColor( DGtal::Color( 255, 255, 0 ) );
96 gradcmap.addColor( DGtal::Color( 255, 0, 0 ) );
97 return gradcmap;
98}
99
100void usage( int argc, char* argv[] )
101{
102 using namespace DGtal;
103 using namespace DGtal::Z3i;
104 typedef Shortcuts< KSpace > SH;
105 std::cout << "Usage: " << std::endl
106 << "\t" << argv[ 0 ] << " <P> <B> <h> <mode>" << std::endl
107 << std::endl
108 << "Compare Integral Invariant (II) curvature estimations " << std::endl
109 << "with Corrected Normal Current (CNC) estimations, either "<< std::endl
110 << "interpolated (Interp) or constant per face (Const)." << std::endl
111 << "- builds the surface mesh from polynomial <P>" << std::endl
112 << "- <B> defines the digitization space size [-B,B]^3" << std::endl
113 << "- <h> is the gridstep digitization" << std::endl
114 << "- <mode> is either Const for constant corrected normal" << std::endl
115 << " vector field or Interp for interpolated corrected" << std::endl
116 << " normal vector field." << std::endl
117 << "It outputs timings and accuracy statistics for both " << std::endl
118 << "methods." << std::endl;
119 std::cout << "You may either write your own polynomial as 3*x^2-z^2*x*y+1" << std::endl
120 <<"or use a predefined polynomial in the following list:" << std::endl;
121 auto L = SH::getPolynomialList();
122 for ( const auto& p : L )
123 std::cout << p.first << " : " << p.second << std::endl;
124}
125
126int main( int argc, char* argv[] )
127{
128 if ( argc <= 1 )
129 {
130 usage( argc, argv );
131 return 0;
132 }
134 using namespace DGtal;
135 using namespace DGtal::Z3i;
138 typedef Shortcuts< KSpace > SH;
139 typedef ShortcutsGeometry< KSpace > SHG;
141 std::string poly = argv[ 1 ]; // polynomial
142 const double B = argc > 2 ? atof( argv[ 2 ] ) : 1.0; // max ||_oo bbox
143 const double h = argc > 3 ? atof( argv[ 3 ] ) : 1.0; // gridstep
144 std::string mode = argc > 4 ? argv[ 4 ] : "Const"; // either Const or Interp
145 bool interpolated = mode == "Interp";
146 if ( interpolated )
147 trace.info() << "Using vertex-*Interpolated* Corrected Normal Current" << std::endl;
148 else
149 trace.info() << "Using face-*Constant* Corrected Normal Current" << std::endl;
151 // Read polynomial and build digital surface
152 auto params = SH::defaultParameters() | SHG::defaultParameters();
153 // Choose depth-first traversal to speed-up II computations.
154 params( "surfaceTraversal", "DepthFirst" );
155 params( "t-ring", 3 );
156 params( "polynomial", poly )( "gridstep", h );
157 params( "minAABB", -B )( "maxAABB", B );
158 params( "offset", 3.0 );
159 auto shape = SH::makeImplicitShape3D( params );
160 auto K = SH::getKSpace( params );
161 auto dshape = SH::makeDigitizedImplicitShape3D( shape, params );
162 auto bimage = SH::makeBinaryImage( dshape, params );
163 if ( bimage == nullptr )
164 {
165 trace.error() << "Unable to read polynomial <"
166 << poly.c_str() << ">" << std::endl;
167 return 1;
168 }
169 auto sembedder = SH::getSCellEmbedder( K );
170 auto embedder = SH::getCellEmbedder( K );
171 auto surface = SH::makeDigitalSurface( bimage, K, params );
172 auto surfels = SH::getSurfelRange( surface, params );
173 trace.info() << "- surface has " << surfels.size()<< " surfels." << std::endl;
175
177 params( "r-radius", 3.0 );
178 params( "alpha", 0.33 );
179 double ii_r = 3.0 * pow( h, 0.33 );
180 Clock c;
181 trace.beginBlock( "Computing II curvatures" );
182 std::vector< double > HII = SHG::getIIMeanCurvatures ( bimage, surfels, params );
183 std::vector< double > GII = SHG::getIIGaussianCurvatures( bimage, surfels, params );
184 auto ii_t = trace.endBlock();
186
188 SM smesh;
189 std::vector< SM::Vertices > faces;
190 SH::Cell2Index c2i;
191 auto pointels = SH::getPointelRange( c2i, surface );
192 auto vertices = SH::RealPoints( pointels.size() );
193 std::transform( pointels.cbegin(), pointels.cend(), vertices.begin(),
194 [&] (const SH::Cell& c) { return h * embedder( c ); } );
195 for ( auto&& surfel : surfels )
196 {
197 const auto primal_surfel_vtcs = SH::getPointelRange( K, surfel );
198 SM::Vertices face;
199 for ( auto&& primal_vtx : primal_surfel_vtcs )
200 face.push_back( c2i[ primal_vtx ] );
201 faces.push_back( face );
202 }
203 smesh.init( vertices.cbegin(), vertices.cend(),
204 faces.cbegin(), faces.cend() );
205 trace.info() << smesh << std::endl;
207
209 trace.beginBlock( "Computing True curvatures" );
210 auto exp_H = SHG::getMeanCurvatures( shape, K, surfels, params );
211 auto exp_G = SHG::getGaussianCurvatures( shape, K, surfels, params );
212 trace.endBlock();
214
215 trace.beginBlock( "Computing CNC curvatures" );
217 // Builds a CorrectedNormalCurrentComputer object onto the SurfaceMesh object
218 CNC cnc( smesh );
219 // Estimates normal vectors using Integral Invariant Normal estimator
220 trace.beginBlock( "Computing II normal vectors" );
221 auto face_normals = SHG::getIINormalVectors( bimage, surfels, params );
222 double cnc_tn = trace.endBlock();
223 // Set corrected face normals => Corrected Normal Current with
224 // constant per face corrected vector field.
225 smesh.setFaceNormals( face_normals.cbegin(), face_normals.cend() ); // CCNC
226 // Set corrected vertex normals => Corrected Normal Current with
227 // smooth linearly interpolated per face corrected vector field.
228 if ( interpolated ) smesh.computeVertexNormalsFromFaceNormals(); // ICNC
229 // computes area, mean and Gaussian curvature measures
230 auto mu0 = cnc.computeMu0();
231 auto mu1 = cnc.computeMu1();
232 auto mu2 = cnc.computeMu2();
234
236 // estimates mean (H) and Gaussian (G) curvatures by measure normalization.
237 double cnc_mr = 1.0 * sqrt( h );
238 trace.info() << "CNC measuring radius = " << cnc_mr << std::endl;
239 std::vector< double > H( smesh.nbFaces() );
240 std::vector< double > G( smesh.nbFaces() );
241 for ( auto f = 0; f < smesh.nbFaces(); ++f )
242 {
243 const auto b = smesh.faceCentroid( f );
244 const auto area = mu0.measure( b, cnc_mr, f );
245 H[ f ] = cnc.meanCurvature ( area, mu1.measure( b, cnc_mr, f ) );
246 G[ f ] = cnc.GaussianCurvature( area, mu2.measure( b, cnc_mr, f ) );
247 }
249 double cnc_t = trace.endBlock();
250
252 auto HII_min_max = std::minmax_element( HII.cbegin(), HII.cend() );
253 auto GII_min_max = std::minmax_element( GII.cbegin(), GII.cend() );
254 auto H_min_max = std::minmax_element( H.cbegin(), H.cend() );
255 auto G_min_max = std::minmax_element( G.cbegin(), G.cend() );
256 auto exp_H_min_max = std::minmax_element( exp_H.cbegin(), exp_H.cend() );
257 auto exp_G_min_max = std::minmax_element( exp_G.cbegin(), exp_G.cend() );
258 trace.info() << "Expected mean curvatures:"
259 << " min=" << *exp_H_min_max.first << " max=" << *exp_H_min_max.second
260 << std::endl;
261 trace.info() << "Computed II mean curvatures:"
262 << " min=" << *HII_min_max.first << " max=" << *HII_min_max.second
263 << std::endl;
264 trace.info() << "Computed CNC mean curvatures:"
265 << " min=" << *H_min_max.first << " max=" << *H_min_max.second
266 << std::endl;
267 trace.info() << "Expected Gaussian curvatures:"
268 << " min=" << *exp_G_min_max.first << " max=" << *exp_G_min_max.second
269 << std::endl;
270 trace.info() << "Computed II Gaussian curvatures:"
271 << " min=" << *GII_min_max.first << " max=" << *GII_min_max.second
272 << std::endl;
273 trace.info() << "Computed CNC Gaussian curvatures:"
274 << " min=" << *G_min_max.first << " max=" << *G_min_max.second
275 << std::endl;
276 const auto error_HII = SHG::getScalarsAbsoluteDifference( HII, exp_H );
277 const auto stat_error_HII = SHG::getStatistic( error_HII );
278 const auto error_HII_l2 = SHG::getScalarsNormL2( HII, exp_H );
279 trace.info() << "|H-H_II|_oo = " << stat_error_HII.max() << std::endl;
280 trace.info() << "|H-H_II|_2 = " << error_HII_l2 << std::endl;
281 const auto error_H = SHG::getScalarsAbsoluteDifference( H, exp_H );
282 const auto stat_error_H = SHG::getStatistic( error_H );
283 const auto error_H_l2 = SHG::getScalarsNormL2( H, exp_H );
284 trace.info() << "|H-H_CNC|_oo = " << stat_error_H.max() << std::endl;
285 trace.info() << "|H-H_CNC|_2 = " << error_H_l2 << std::endl;
286 const auto error_GII = SHG::getScalarsAbsoluteDifference( GII, exp_G );
287 const auto stat_error_GII = SHG::getStatistic( error_GII );
288 const auto error_GII_l2 = SHG::getScalarsNormL2( GII, exp_G );
289 trace.info() << "|G-G_II|_oo = " << stat_error_GII.max() << std::endl;
290 trace.info() << "|G-G_II|_2 = " << error_GII_l2 << std::endl;
291 const auto error_G = SHG::getScalarsAbsoluteDifference( G, exp_G );
292 const auto stat_error_G = SHG::getStatistic( error_G );
293 const auto error_G_l2 = SHG::getScalarsNormL2( G, exp_G );
294 trace.info() << "|G-G_CNC|_oo = " << stat_error_G.max() << std::endl;
295 trace.info() << "|G-G_CNC|_2 = " << error_G_l2 << std::endl;
297
299 std::cout << "# " << argv[ 0 ] << std::endl
300 << "# polynomial: " << poly << std::endl
301 << "# CNC mode: " << mode << std::endl;
302 // 1 2 3 4 5 6 7 8
303 std::cout << "# h nb_surfels ii_t ii_r ii_Hoo ii_H2 ii_Goo ii_G2 ";
304 // 9 10 11 12 13 14 15
305 std::cout << "cnc_tn cnc_t cnc_mr cnc_Hoo cnc_H2 cnc_Goo cnc_G2" << std::endl;
306 std::cout << h << " " << surfels.size() << " " << ii_t << " " << ii_r
307 << " " << stat_error_HII.max() << " " << error_HII_l2
308 << " " << stat_error_GII.max() << " " << error_GII_l2
309 << " " << cnc_tn << " " << cnc_t << " " << cnc_mr
310 << " " << stat_error_H.max() << " " << error_H_l2
311 << " " << stat_error_G.max() << " " << error_G_l2 << std::endl;
313 return 0;
314}
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
void beginBlock(const std::string &keyword="")
std::ostream & error()
std::ostream & info()
double endBlock()
DGtal::GradientColorMap< double > makeColorMap(double min_value, double max_value)
[curvature-comparator-Includes]
CountedPtr< SH3::DigitalSurface > surface
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: 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