DGtal 1.4.0
Loading...
Searching...
No Matches
topology/area-estimation-with-digital-surface.cpp

Computing the area of a sphere with a digital surface. First normals are estimated by averaging the trivial normals in a k-neighborhood of each surfel. Then both the corrected area (scalar product of estimated normal with trivial normal) and the averaged area (inverse of L1-norm of estimated normal) is computed per surfel, and summed in order to get area estimation. This example illustrates the use of a DGtal::DigitalSurface and DGtal::BreadthFirstVisitor onto it. This method is a bit slower than using an DGtal::IndexedDigitalSurface.

See also
Precomputed 3D digital surface with IndexedDigitalSurface
$ ./examples/topology/area-estimation-with-digital-surface 13 6
New Block [Creating surface]
  Sphere of radius 13, 6-ring neighborhood
  [OK] Sphere has 3174 vertices/surfels
EndBlock [Creating surface] (8.355 ms)
New Block [Estimating normals]
EndBlock [Estimating normals] (332.386 ms)
New Block [Estimating area]
  - true area      = 2123.72
  - corrected area = 2109.58
  - averaged area  = 2150.34
EndBlock [Estimating area] (0.626 ms)
#include <cstdlib>
#include <iostream>
#include <map>
#include "DGtal/base/Common.h"
#include "DGtal/base/CountedPtr.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/kernel/PointVector.h"
#include "DGtal/graph/CUndirectedSimpleGraph.h"
#include "DGtal/graph/BreadthFirstVisitor.h"
#include "DGtal/topology/DigitalSetBoundary.h"
#include "DGtal/topology/DigitalSurface.h"
#include "DGtal/shapes/Shapes.h"
using namespace std;
using namespace DGtal;
using namespace DGtal::Z3i;
int main( int argc, char** argv )
{
const double R = argc >= 2 ? atof( argv[ 1 ] ) : 13.0; // radius of ball
const unsigned int KN = argc >= 3 ? atoi( argv[ 2 ] ) : 6; // size of neighborhood
const int M = (int) ceil( R + 2.0 );
trace.beginBlock( "Creating surface" );
trace.info() << "Sphere of radius " << R
<< ", " << KN << "-ring neighborhood" << std::endl;
typedef DigitalSetBoundary < KSpace, DigitalSet > DigitalSurfaceContainer;
typedef DigitalSurface < DigitalSurfaceContainer > DigSurface;
typedef BreadthFirstVisitor < DigSurface > BFSVisitor;
Point p1( -M, -M, -M );
Point p2( M, M, M );
K.init( p1, p2, true );
DigitalSet aSet( Domain( p1, p2 ) );
Shapes<Domain>::addNorm2Ball( aSet, Point( 0, 0, 0 ), R );
DigSurface dsurf( new DigitalSurfaceContainer( K, aSet ) );
trace.info() << "[OK]" << " Sphere has " << dsurf.size() << " vertices/surfels"
<< std::endl;
trace.endBlock();
trace.beginBlock( "Estimating normals" );
std::map< SCell, RealPoint > v2n;
for ( auto v : dsurf )
{
int nbv = 0;
RealPoint normal = RealPoint::zero;
BFSVisitor bfv( dsurf, v );
while( ! bfv.finished() )
{ // Vertex are colored according to the distance to initial seed.
auto node = bfv.current();
if ( KN < node.second ) break;
auto surfel = node.first;
RealPoint nv = RealPoint::zero;
Dimension k = K.sOrthDir( surfel );
nv[ k ] = K.sDirect( surfel, k ) ? 1.0 : -1.0;
normal += nv;
nbv += 1;
bfv.expand();
}
normal /= nbv;
v2n[ v ] = normal.getNormalized();
}
trace.endBlock();
trace.beginBlock( "Estimating area" );
const double area_true = 4.0 * M_PI * R * R;
double area_averaged = 0.0;
double area_corrected = 0.0;
for ( auto v : dsurf )
{
auto surfel = v;
Dimension k = K.sOrthDir( surfel );
area_corrected += fabs( v2n[ v ][ k ] );
area_averaged += 1.0 / v2n[ v ].norm( RealPoint::L_1 );
}
trace.info() << "- true area = " << area_true << std::endl;
trace.info() << "- corrected area = " << area_corrected << std::endl;
trace.info() << "- averaged area = " << area_averaged << std::endl;
trace.endBlock();
return 0;
}
PointVector< dim, double, std::array< double, dim > > getNormalized() const
Aim: A utility class for constructing different shapes (balls, diamonds, and others).
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Z3i this namespace gathers the standard of types for 3D imagery.
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Definition Common.h:136
STL namespace.
int main()
Definition testBits.cpp:56
MyPointD Point
KSpace K
HyperRectDomain< Space > Domain
PointVector< 3, double > RealPoint
Z2i::DigitalSet DigitalSet