Order of vertices changes after surface mesh parametrization
Issue Details
In square_border_parameterizer example of Triangulated Surface Mesh Parameterization, after parametrisation completes and the resultant UV Map is saved as off file in
https://github.com/CGAL/cgal/blob/5c9285f84fac459714a799f6b42d76b03c68318a/Surface_mesh_parameterization/examples/Surface_mesh_parameterization/square_border_parameterizer.cpp#L139
the resultant off file has vertices ordered in a different sequence as the input mesh file.
I think the source of the problem is the unordered set of container filler elements being generated at https://github.com/CGAL/cgal/blob/39b1431fca8631bc323038a6f253edae5fb28d0c/Surface_mesh_parameterization/include/CGAL/Surface_mesh_parameterization/IO/File_off.h#L116
I thought to use the another instance of output_uvmap_to_off:
output_uvmap_to_off(const TriangleMesh& mesh,
const VertexContainer& vertices,
const FacesContainer& faces,
const VertexUVMap uvmap,
VertexIndexMap vimap,
std::ostream& os)
But I am not sure about the how to generate correct vimap from TriangleMesh and VertexUVMap
The ordering is intentionally discarded because in the case of a mesh with multiple components, only the connected component that you specify via the border halfedge bhd is parameterized. You will then not have a nice 1-to-1 correspondence with your input anyway.
I imagine that it would be possible to always have vertices in the output in the same order as the input, even if you only parameterize a subset of them. However, you do not need to use that function to find your mapping; you can simply use something like:
boost::graph_traits<PolyMesh>::vertex_iterator vit, vend;
boost::tie(vit, vend) = vertices(sm);
while(vit!=vend)
{
vertex_descriptor vd = *vit++;
std::cout << "Input point: " << vd->point() << " is mapped to " << get(uv_map, vd) << '\n';
}
because vertices(mesh) will (for our structures) traverse points in the same order that they were in your file.
Note that the above assumes a single connected component. If you have multiple connected components, you will have to ignore vertices that are not part of your parameterized connected component because there's no uv value associated to them.
Does this work for you?
Thanks for the quick response, your suggestion seems to work as I wanted.
I had to add further lines to your code to make it a function for writing off files. And yes this assumes a single connected component.
bool uvmap_off(std::ofstream &out, PolyMesh & sm, UV_pmap uv_map) {
std::size_t vertices_counter = 0, faces_counter = 0;
typedef boost::unordered_map<vertex_descriptor, std::size_t> Vertex_index_map;
Vertex_index_map vium;
boost::associative_property_map<Vertex_index_map> vimap(vium);
out << "OFF\n" ;
out << sm.size_of_vertices() <<" "<< sm.size_of_facets() << " 0\n";
boost::graph_traits<PolyMesh>::vertex_iterator vit, vend;
boost::tie(vit, vend) = vertices(sm);
while(vit!=vend)
{
vertex_descriptor vd = *vit++;
out << get(uv_map, vd) << " 0\n";
put(vimap, vd, vertices_counter++);
}
BOOST_FOREACH(face_descriptor fd, faces(sm)){
halfedge_descriptor hd = halfedge(fd, sm);
out << "3";
BOOST_FOREACH(vertex_descriptor vd, vertices_around_face(hd, sm)){
out << " " << get(vimap, vd);
}
out << '\n';
faces_counter++;
}
if(vertices_counter != sm.size_of_vertices())
return 0;
else if(faces_counter != sm.size_of_facets())
return 0;
else
return 1;
}