Skip to content

Correct Inputs to PMP::refine() #7046

@zhaoyuanyuan2011

Description

@zhaoyuanyuan2011

Issue Details

I'm trying to use PMP::refine() to do refining over a mesh. I'm using Pybind11 to accept vertices and faces from a python script, first turn them into a surface_mesh and then convert to a Polyhedron_3 so it can be input to PMP::refine(). I apologize for the redundancy here since I'm pretty new to both C++ and CGAL. It looks like PMP::refine() has problem accepting such a Polyhedron. I'm pasting my code and error messages as follows. Thanks in advance for any suggestions!

Source Code

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <pybind11/eigen.h> 

#include <vector>
#include <string>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/copy_face_graph.h>

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polygon_mesh_processing/triangulate_hole.h>
#include <CGAL/Polygon_mesh_processing/refine.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/utility.h>
#include <iterator>
#include <cassert>

typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;


namespace py = pybind11;
namespace pmp = CGAL::Polygon_mesh_processing;

typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point;
typedef K::Vector_3 Vector;
typedef CGAL::Surface_mesh<Point> Mesh;
typedef CGAL::Polyhedron_3<Kernel>                          Polyhedron;
typedef Polyhedron::Vertex_handle                           Vertex_handle;


// typedef Mesh::Vertex_index vertex_descriptor;
// typedef Mesh::Face_index face_descriptor;

typedef boost::graph_traits<Mesh>::vertex_descriptor        vertex_descriptor;
typedef boost::graph_traits<Mesh>::halfedge_descriptor      halfedge_descriptor;
typedef boost::graph_traits<Mesh>::face_descriptor          face_descriptor;



std::tuple<Eigen::MatrixX3d, Eigen::MatrixX3i> mesh_to_vf(Mesh &m){
  Eigen::MatrixX3d vertices_out(m.number_of_vertices(), 3);
  int row = 0;
  BOOST_FOREACH(vertex_descriptor vd, m.vertices()){
    // std::cout << m.point(vd) << std::endl;
    vertices_out(row, 0) = m.point(vd)[0];
    vertices_out(row, 1) = m.point(vd)[1];
    vertices_out(row, 2) = m.point(vd)[2];      
    row++;
  }

  Eigen::MatrixX3i faces_out(m.number_of_faces(), 3);
  unsigned i_face = 0;
  // std::cout << "output mesh.number_of_vertices() = " << m.number_of_vertices() << std::endl;
  // std::cout << "output mesh.number_of_faces() = " << m.number_of_faces() << std::endl;
  BOOST_FOREACH(face_descriptor fd, m.faces()){
    int j = 0;
    BOOST_FOREACH(vertex_descriptor vd, vertices_around_face(m.halfedge(fd), m)){
      faces_out(i_face, j++) = vd;
    }
    i_face++;
  }

  return std::make_tuple(vertices_out, faces_out);
}


void vf_to_mesh(Mesh &m, const Eigen::MatrixX3d &vertices, const Eigen::MatrixX3i &faces){
  std::vector<vertex_descriptor> vert_vec;
  std::cout << "#vertices = " << vertices.rows() << std::endl;
  std::cout << "#faces = " << faces.rows() << std::endl;
  for(int i = 0; i < vertices.rows(); i++){
    vert_vec.push_back(m.add_vertex(K::Point_3(vertices(i, 0), vertices(i, 1), vertices(i, 2))));
  }
  for(int j = 0; j < faces.rows(); j++){
    m.add_face(vert_vec[faces(j, 0)], vert_vec[faces(j, 1)], vert_vec[faces(j, 2)]);
  }
}

std::tuple<Eigen::MatrixX3d, Eigen::MatrixX3i> refine_patch(const Eigen::MatrixX3d &vertices, const Eigen::MatrixX3i &faces){
  Mesh m;
  vf_to_mesh(m, vertices, faces);
  Polyhedron poly;
  CGAL::copy_face_graph(m, poly);

  std::vector<Polyhedron::Facet_handle>  new_facets;
  std::vector<Vertex_handle> new_vertices;
  pmp::refine(poly, faces(poly),
              std::back_inserter(new_facets),
              std::back_inserter(new_vertices),
              CGAL::parameters::density_control_factor(2.));

  Mesh tm;
  CGAL::copy_face_graph(poly, tm);

  return mesh_to_vf(m);
}


// ----------------
// Python interface
// ----------------

// wrap as Python module
PYBIND11_MODULE(mesh_hole_fill_eric, m)
{
  m.doc() = "mesh hole filling with pybind11 by eric";

  m.def("ref", &refine_patch, "Refine a patch");

}

Environment

  • Operating system: Mac, 32/64 bits
  • Compiler: Using /Library/Developer/CommandLineTools/usr/bin/c++ compiler
  • Release mode
  • CGAL version: 5.5.1
  • Boost version: 1.74.0
  • Other libraries versions if used: Eigen

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions