Search code examples
c++cgal

CGAL 2D alpha shape outline


My problem is probably due to the newbiness in the CGAL c++ library, but the task I have keeps slipping away. Namely, I want to find the alpha shape of a set of points, but it seems I don`t understand the iterators available to 2D alpha shapes.

This is what I tried:

Alpha_shape_2 alpha(pointsVec.begin(), pointsVec.end(), FT(1000), Alpha_shape_2::GENERAL);
//which compiles nicely and provides the regular output where pointsVec is the list of Point_2

//Then I saw the edge iterator at the CGAL documentation

template <class OutputIterator> //Method-iterator for CGAL alpha shapes in order to get the edges from alpha shape object
void alpha_edges( const Alpha_shape_2& A,
                  OutputIterator out)
{
    for(Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin();
        it != A.alpha_shape_edges_end();
        ++it){
        *out++ = A.segment(*it);
    }
}

//Then when using the following code:

std::vector<Segment> segments;
alpha_edges(alpha, std::back_inserter(segments));

//I get the list of all the edges in the triangulation used for alpha shapes.

The thing is that I need the boundary like in the following plot (obtained with R alphahull library)

Alphahull

Instead I get the triangulation edges in the segments vector. One other thing I tried is to use vertex iterators:

for (Alpha_shape_2::Alpha_shape_vertices_iterator it = alpha.Alpha_shape_vertices_begin(); it != alpha.Alpha_shape_vertices_end(); ++it)
      {
          int xalpha = (*it)->point().x();
          int yalpha = (*it)->point().y();
          alphaCoords.push_back(cv::Point(xalpha, yalpha)); //this for openCV
      }

but the result is the same. It outputs all the vertices so the drawing only connects them, without the outline (draw lines across the image).

I know that for 3D there is a function for finding the boundary shape vertices:

as.get_alpha_shape_vertices(back_inserter(al_vs), Alpha_shape_3::REGULAR); 

but it does not exist for 2D. Also I`m interested to find out where can you specify the alpha value used for the radius of the shaping circle, like in the windows demo supplied at the CGAL 2D shapes manual.


Solution

  • Alpha_shape_edges_iterator gives you edges which are not EXTERIOR. I guess you are interested in REGULAR and SINGULAR edges.

    Have a look at the Classification_type and at the classify function to filter out the edges.