Search code examples
c++visualizationmeshcgalconcave-hull

Creating 3D Alpha Shapes in CGAL and the visualization


I am a new CGAL user with basic C++ experience, trying to find the 3D Alpha shapes of a set of points. I am using the ex_alpha_shapes_3 example code, and then using the instructions in saving CGAL alpha shape surface mesh for visualization of the results. Everything seems to work properly, but when I try to change the value of alpha by replacing

    Alpha_shape_3 as(lp.begin(),lp.end());

with

    Alpha_shape_3 as(lp.begin(),lp.end(),1, Alpha_shape_3::GENERAL);

assuming the third variable is the value of alpha (= 1) and change this value each time, no change in the results is obtained.

More specifically, in my set of particles, some are detached from the majority and I would like to represent them with separate volumes (similar to the Figure 41.1) using the concave hull or alpha shapes. Currently what I get (using the Tecplot for visualization) is: enter image description here and as you can see, the detached particles are connected to the other particles. I am also attaching my code, at the end. I would appreciate any help on this matter.

    #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
    #include <CGAL/Delaunay_triangulation_3.h>
    #include <CGAL/Alpha_shape_3.h>

    #include <iostream>
    #include <fstream>
    #include <list>
    #include <cassert>

    typedef CGAL::Exact_predicates_inexact_constructions_kernel Gt;

    typedef CGAL::Alpha_shape_vertex_base_3<Gt>          Vb;
    typedef CGAL::Alpha_shape_cell_base_3<Gt>            Fb;
    typedef CGAL::Triangulation_data_structure_3<Vb,Fb>  Tds;
    typedef CGAL::Delaunay_triangulation_3<Gt,Tds>       Triangulation_3;
    typedef CGAL::Alpha_shape_3<Triangulation_3>         Alpha_shape_3;

    typedef Gt::Point_3                                  Point;
    typedef Alpha_shape_3::Alpha_iterator               Alpha_iterator;

    using namespace std;

    int main()
    {
      std::list<Point> lp;

      //read input
      std::ifstream is("./data/finalwater4.dat");
      int n;
      is >> n;
      std::cout << "Reading " << n << " points " << std::endl;
      Point p;
      for( ; n>0 ; n--)    {
        is >> p;
        lp.push_back(p);
      }

      // compute alpha shape
    //  Alpha_shape_3 as(lp.begin(),lp.end());
      Alpha_shape_3 as(lp.begin(),lp.end(),0.001, Alpha_shape_3::GENERAL);

      // find optimal alpha value
      Alpha_iterator opt = as.find_optimal_alpha(1);
      std::cout << "Optimal alpha value to get one connected component is "
            <<  *opt    << std::endl;
      as.set_alpha(*opt);
      assert(as.number_of_solid_components() == 1);

      /// the rest of the code, prepares the output to be written into a file

      /// collect all regular facets (fetch regular facets from as and inserts in facets)
      std::vector<Alpha_shape_3::Facet> facets;
      as.get_alpha_shape_facets(std::back_inserter(facets), Alpha_shape_3::REGULAR);

      std::stringstream pts;
      std::stringstream ind;

      std::size_t nbf=facets.size();
      for (std::size_t i=0;i<nbf;++i)
      { 
        //To have a consistent orientation of the facet, always consider an exterior cell
        if ( as.classify( facets[i].first )!=Alpha_shape_3::EXTERIOR )
          facets[i]=as.mirror_facet( facets[i] );
        CGAL_assertion(  as.classify( facets[i].first )==Alpha_shape_3::EXTERIOR  );

        int indices[3]={
          (facets[i].second+1)%4,
          (facets[i].second+2)%4,
          (facets[i].second+3)%4,
        };

        /// according to the encoding of vertex indices, this is needed to get
        /// a consistent orienation
        if ( facets[i].second%2==0 ) std::swap(indices[0], indices[1]);


        pts << 
        facets[i].first->vertex(indices[0])->point() << "\n" <<
        facets[i].first->vertex(indices[1])->point() << "\n" <<
        facets[i].first->vertex(indices[2])->point() << "\n"; 
        ind << 3*i+1 << " " << 3*i+2 << " " << 3*i+3 << "\n";
      }

      ofstream myfile;
      myfile.open ("output.dat");
      myfile << "variables = x, y, z\n";
      myfile << "zone n="<< 3*nbf << " , e=" << nbf << " , f=fepoint, et=triangle\n";
      myfile << pts.str();
      myfile << ind.str();
      myfile.close();

      return 0;
    }

Solution

  • If you want to filter things out, you need to take a alpha shape with value less than the one returned by as.find_optimal_alpha(1). I suggest to run the alpha-shape demo which feature a slider that will display what the alpha-shape looks like depending on the value of alpha. Input file should be with a .pts extension and must contains the number of points, followed by the coordinates of the points.