Search code examples
c++3dprimitivecgal

Create parameterized 3D primitives with cgal


I'm using cgal to evaluate scientfic data (Polyhedron surface, mass fraction, void fraction,...).

I preforme boolean operations to cut out my measurment volumes (subvolume where I want to average the data). In this special case it is a cylinder. By now I'll read a generic cylinder from an STL file and transform it to its needed size and position. This is not very elegant. For example it is not possible to adjust the resolution of surface triangulation.

A much better (more general approach) would be to generate my probe volume at runtime. Does cgal have a module to create 3D primitives? I couldn't find something like that in the documentation.

I'm quite new to cgal and a little bit confused by this documents. So it is quite possible that I've overlook something.

It would be very nice if someone could give me a hint on how to start solving this problem!

Thanks in advance!


Solution

  • For everyone whos trying something similar here is my solution:

    #include <CGAL/Polyhedron_3.h>
    #include <CGAL/IO/Polyhedron_iostream.h>
    #include <CGAL/Nef_polyhedron_3.h>
    #include <CGAL/convex_hull_3.h>
    
    Nef_polyhedron_3 create_probe_cylinder(unsigned int argNum_segments, double argRadius, double argZmin, double argZmax)
    {
    
        std::vector<Point_3> probecylinder_points;
        double x, y;
        for (unsigned int point_index = 0; point_index < argNum_segments; point_index++)
        {
            x = argRadius*cos(2*M_PI*point_index/argNum_segments);
            y = argRadius*sin(2*M_PI*point_index/argNum_segments);;
    
            Point_3 point_bot(x,y, argZmin);
            Point_3 point_top(x,y, argZmax);
    
            probecylinder_points.push_back(point_bot);
            probecylinder_points.push_back(point_top);
        }
    
        std::cout << "creating convex hull.." << std::endl;
    
        Polyhedron_3 poly_cylinder;
        CGAL::convex_hull_3(probecylinder_points.begin(), probecylinder_points.end(), poly_cylinder);
    
        std::cout << "converting to nef poly.." << std::endl;
        Nef_polyhedron_3 nef_cylinder(poly_cylinder);
    
        return nef_cylinder;
    }
    

    Code summary:

    1) create list of points (rim of the bottom / top). argNum_segments sets the resolution of the cylinder (number of segments)

    2) create a convex hull from the point list

    3) convert polyhedron to nef representation (needed for the boolean operations)

    There might be a better solution. If someone knows how to performe this task in a more elegant way please let me know.