Computation of the alpha shape of the border of a digital shape.
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/IteratorCirculatorTraits.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/geometry/tools/Hull2DHelpers.h"
#include "DGtal/geometry/tools/PolarPointComparatorBy2x2DetComputer.h"
#include "DGtal/geometry/tools/determinant/AvnaimEtAl2x2DetSignComputer.h"
#include "DGtal/geometry/tools/determinant/InHalfPlaneBySimple3x3Matrix.h"
#include "DGtal/geometry/tools/determinant/InGeneralizedDiskOfGivenRadius.h"
#include "DGtal/shapes/ShapeFactory.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/topology/DigitalSetBoundary.h"
#include "DGtal/topology/DigitalSurface.h"
#include "DGtal/graph/DepthFirstVisitor.h"
#include "DGtal/io/boards/Board2D.h"
template <typename ForwardIterator, typename Board>
void drawPolygon(const ForwardIterator& itb, const ForwardIterator& ite,
Board& aBoard, bool isClosed = true)
{
ForwardIterator it = itb;
if (it != ite)
{
Point p1 = *it;
Point p = p1;
aBoard <<
SetMode( p.className(),
"Grid" )
<< p
Point prev = p;
for (++it; it != ite; ++it, prev = p)
{
p = *it;
aBoard << p;
if (prev != p)
aBoard.drawArrow(prev[0], prev[1], p[0], p[1]);
}
if (isClosed)
{
if (prev != p1)
aBoard.drawArrow(prev[0], prev[1], p1[0], p1[1]);
}
}
}
void alphaShape()
{
Z2i::Domain domain(ball.getLowerBound(), ball.getUpperBound());
DigitalSurfaceContainer digitalSurfaceContainer( kspace, digitalSet );
CustomDigitalSurface digitalSurface( digitalSurfaceContainer );
CustomVisitor visitor( digitalSurface, *digitalSurface.begin() );
while ( ! visitor.finished() )
{
gridCurve.
pushBack( visitor.current().first );
visitor.expand();
}
vector<Z2i::Point> border;
unique_copy( pointsRange.begin(), pointsRange.end(), back_inserter( border ) );
using namespace functions::Hull2D;
{
vector<Z2i::Point> res;
Functor functor(true, 1, 0);
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShape0.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShape0.png", Board2D::CairoPNG);
#endif
}
{
vector<Z2i::Point> res;
Functor functor;
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShape0bis.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShape0bis.png", Board2D::CairoPNG);
#endif
}
{
vector<Z2i::Point> res;
Functor functor(false, 1, 1);
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShapeM1.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShapeM1.png", Board2D::CairoPNG);
#endif
}
{
trace.
info() <<
" alpha == -sqrt(5) " << endl;
vector<Z2i::Point> res;
Functor functor(false, 5, 1);
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShapeMSqrt5.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShapeMSqrt5.png", Board2D::CairoPNG);
#endif
}
{
vector<Z2i::Point> res;
Functor functor(false, 25, 1);
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShapeM5.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShapeM5.png", Board2D::CairoPNG);
#endif
}
{
vector<Z2i::Point> res;
Functor functor(true, 64, 1);
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShapeP8.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShapeP8.png", Board2D::CairoPNG);
#endif
}
{
vector<Z2i::Point> res;
Functor functor(true, 81, 1);
Predicate predicate( functor );
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "AlphaShapeP9.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AlphaShapeP9.png", Board2D::CairoPNG);
#endif
}
}
int main( int argc, char** argv )
{
for ( int i = 0; i < argc; ++i )
alphaShape();
return 0;
}