Andy
Andy

Reputation: 3839

How do I extract the output from CGAL::poisson_surface_reconstruction_delaunay?

I am trying to convert a point cloud to a trimesh using CGAL::poisson_surface_reconstruction_delaunay() and extract the data inside the trimesh to an OpenGL friendly format:

// The function below should set vertices and indices so that: 
// triangle 0: (vertices[indices[0]],vertices[indices[1]],vertices[indices[2]]), 
// triangle 1: (vertices[indices[3]],vertices[indices[4]],vertices[indices[5]]) 
// ...
// triangle n - 1
void reconstructPointsToSurfaceInOpenGLFormat(const& std::list<std::pair<Kernel::Point_3, Kernel::Vector_3>> points, // input: points and normals
    std::vector<glm::vec3>& vertices, // output
    std::vector<unsigned int>& indices) { // output
   
    CGAL::Surface_mesh<Kernel::Point_3> trimesh;

    double spacing = 10;

    bool ok = CGAL::poisson_surface_reconstruction_delaunay(points.begin(), points.end(),
        CGAL::First_of_pair_property_map<std::pair<Kernel::Point_3, Kernel::Vector_3>>(),
        CGAL::Second_of_pair_property_map<std::pair<Kernel::Point_3, Kernel::Vector_3>>(),
        trimesh, spacing);

    // How do I set the vertices and indices values?
}

Please help me on iterating trough the triangles in trimesh and setting the vertices and indices in the code above.

Upvotes: 0

Views: 203

Answers (2)

Andy
Andy

Reputation: 3839

Based on sloriots code from his answer:

void mesh2GLM(CGAL::Surface_mesh<Kernel::Point_3>& trimesh, std::vector<glm::vec3>& vertices, std::vector<int>& indices) {
    std::map<size_t, size_t> meshIndex2Index; 
    // Loop over all vertices in mesh:
    size_t index = 0;
    for (Mesh::Vertex_index v : CGAL::vertices(trimesh)) {
        CGAL::Epick::Point_3 point = trimesh.point(v);
        std::size_t vi = v;
        vertices.push_back(glm::vec3(point.x(), point.y(), point.z()));
        meshIndex2Index[vi] = index;
        index++;
    }

    // Loop over all triangles (faces):
    for (Mesh::Face_index f : faces(trimesh)) {
        for (Mesh::Vertex_index v : CGAL::vertices_around_face(CGAL::halfedge(f, trimesh), trimesh)) {
            trimesh.point(v);
            std::size_t vi = v;
            size_t index = meshIndex2Index[vi];
            indices.push_back(index);
        }
    }
}

Seems to work fine.

Upvotes: 0

sloriot
sloriot

Reputation: 6273

The class Polyhedron_3 is not indexed based so you need to provide a item class with ids like Polyhedron_items_with_id_3. You will then need to call CGAL::set_halfedgeds_items_id(trimesh) to init the ids. If you can't modify the Polyhedron type, then you can use dynamic properties and will need to init the ids.

Note that Surface_mesh is indexed based and no particular handling is needed to get indices.

Upvotes: 1

Related Questions