CGAL / cgal

The public CGAL repository, see the README below
https://github.com/CGAL/cgal#readme
Other
4.88k stars 1.38k forks source link

how can i make the faces of Surface_mesh have the same direction #7947

Open BeginCGAL opened 9 months ago

BeginCGAL commented 9 months ago

As shown in the figure,when i make a Surface_mesh and i try to get each face Vertex_index(in .obj file) ,i find the direction of face is not uniform but random.please tell me what should i do to make all faces one orientation,thank you. 99

Source Code

void PointCloud2Mesh::CreateMesh2(std::vector<Point_3> points, std::string objfile)
{
    //Mesh
    Mesh dsm_mesh;
    Construct construct(dsm_mesh, points.begin(), points.end());
    CGAL::advancing_front_surface_reconstruction(points.begin(), points.end(), construct);

    auto vnormals = dsm_mesh.add_property_map<M_vertex_descriptor, Vector_3>("v:normals", CGAL::NULL_VECTOR).first;

    CGAL::Polygon_mesh_processing::compute_vertex_normals(dsm_mesh, vnormals);
    std::ofstream dsm_ofile(objfile, std::ios_base::binary);
    CGAL::IO::set_binary_mode(dsm_ofile);
    CGAL::IO::write_OBJ(dsm_ofile, dsm_mesh);

    for (M_vertex_descriptor vd : vertices(dsm_mesh))
    {
        dsm_ofile << "vn " << vnormals[vd].x() << " "
            << vnormals[vd].y() << " "
            << vnormals[vd].z() << "\n";
    }
    dsm_ofile.close();
}

Environment

pentacular commented 9 months ago

Make construct, whatever that is, construct the mesh with a consistent orientation.

BeginCGAL commented 9 months ago

Is that you mean its my fault? Surface_mesh always with a consistent orientation?

pentacular commented 9 months ago

Surface_mesh should have a consistent orientation, yes.

Perhaps Construct is building a surface mesh of unconnected faces?

I don't know, because I have no idea what Construct does.

I suggest you look there.

BeginCGAL commented 9 months ago

i'm sorry forget the Construct struct , here is the code ,could you tell me problems that make the Surface_mesh don't have a consistent orientation.and what should i do to modify the Construct .

struct Construct
{
    Mesh& mesh;
    template < typename PointIterator>
    Construct(Mesh& mesh, PointIterator b, PointIterator e)
        : mesh(mesh)
    {
        for (; b != e; ++b)
        {
            boost::graph_traits<Mesh>::vertex_descriptor v;
            v = add_vertex(mesh);
            mesh.point(v) = *b;
        }
    }
    Construct& operator=(const Facet f)
    {
        //typedef boost::graph_traits<Mesh>::M_vertex_descriptor M_vertex_descriptor;
        typedef boost::graph_traits<Mesh>::vertices_size_type size_type;
        mesh.add_face(M_vertex_descriptor(static_cast<size_type>(f[0])),
            M_vertex_descriptor(static_cast<size_type>(f[1])),
            M_vertex_descriptor(static_cast<size_type>(f[2])));
        return *this;
    }
    Construct&
        operator*() { return *this; }
    Construct&
        operator++() { return *this; }
    Construct
        operator++(int) { return *this; }
};
pentacular commented 9 months ago

I suggest checking the return value of mesh.add_face()

janetournois commented 9 months ago

You can use CGAL::Polygon_mesh_processing::orient_polygon_soup()