Search code examples
c++file-iomeshcgalparameterization

CGAL::Surface_mesh_parameterization: write the vertices to off in the original order


I am trying to modify CGAL-4.13/examples/Surface_mesh_parameterization/lscm.cpp so that the order of the vertices in the resulting off file is the same as in the input file.

Update: Example

Take a file input.off with the following simple content:

OFF
4 2 0
-0.9310345 0.4333333 0 
-1 0.4333333 0 
-0.9310345 0.5 0 
-1 0.5 0 
3 1 0 2
3 2 3 1

When I call the standard lscm from CGAL with

/path/to/CGAL-4.13-build/examples/Surface_mesh_parameterization/lscm input.off

I obtain coords.off containing

OFF
4 2 0
-1 0.5 0
-0.931034 0.5 0
-0.931034 0.433333 0
-1 0.433333 0
3 3 2 1
3 1 0 3

and uvmap.off with

OFF
4 2 0
-0.0166567 0.982769 0
1 1 0
1.01666 0.0172311 0
0 0 0
3 3 2 1
3 1 0 3

The files coords.off and uvmap.off contain the vertices and their parameter pairs in the same order (which is different from that in input.off). Instead, I would like the parameters in uvmap.off to be in the order corresponding to input.off. In particular, I want uvmap.off to look like this:

OFF
4 2 0
1.01666 0.0172311 0
0 0 0
1 1 0
-0.0166567 0.982769 0
3 1 0 2
3 2 3 1

Basically, this renders coords.off redundant, as I can use input.off in its role.

Effort at a solution

From what I understand, this might be possible to achieve by calling output_uvmap_to_off(...) with 6 parameters instead of 4 (both versions can be found in CGAL-4.13/include/CGAL/Surface_mesh_parameterization/IO/File_off.h). As one of these parameters is a VertexIndexMap, I should probably also use

CGAL::Surface_mesh_parameterization::LSCM_parameterizer_3< TriangleMesh_, BorderParameterizer_, SolverTraits_ >::parameterize(...)

instead of

CGAL::Surface_mesh_parameterization::parameterize(...)

used in the example.

Here is a minimal (not really working) example. It is derived from lscm.cpp but I threw away a lot of things to remain concise.

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/Seam_mesh.h>
#include <CGAL/Surface_mesh_parameterization/IO/File_off.h>
#include <CGAL/Surface_mesh_parameterization/parameterize.h>
#include <CGAL/Surface_mesh_parameterization/Two_vertices_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/LSCM_parameterizer_3.h>
#include <CGAL/Polygon_mesh_processing/measure.h>
#include <boost/foreach.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

typedef CGAL::Simple_cartesian<double>      Kernel;
typedef Kernel::Point_2                     Point_2;
typedef Kernel::Point_3                     Point_3;
typedef CGAL::Surface_mesh<Kernel::Point_3> SurfaceMesh;
typedef boost::graph_traits<SurfaceMesh>::edge_descriptor SM_edge_descriptor;
typedef boost::graph_traits<SurfaceMesh>::halfedge_descriptor SM_halfedge_descriptor;
typedef boost::graph_traits<SurfaceMesh>::vertex_descriptor SM_vertex_descriptor;
typedef SurfaceMesh::Property_map<SM_halfedge_descriptor, Point_2> UV_pmap;
typedef SurfaceMesh::Property_map<SM_edge_descriptor, bool> Seam_edge_pmap;
typedef SurfaceMesh::Property_map<SM_vertex_descriptor, bool> Seam_vertex_pmap;
typedef CGAL::Seam_mesh<SurfaceMesh, Seam_edge_pmap, Seam_vertex_pmap> Mesh;
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;

namespace SMP = CGAL::Surface_mesh_parameterization;

int main(int argc, char** argv)
{
  std::ifstream in_mesh((argc>1) ? argv[1] : "data/lion.off");
  if(!in_mesh){
    std::cerr << "Error: problem loading the input data" << std::endl;
    return EXIT_FAILURE;
  }

  SurfaceMesh sm;
  in_mesh >> sm;

  Seam_edge_pmap seam_edge_pm = sm.add_property_map<SM_edge_descriptor, bool>("e:on_seam", false).first;
  Seam_vertex_pmap seam_vertex_pm = sm.add_property_map<SM_vertex_descriptor, bool>("v:on_seam", false).first;

  Mesh mesh(sm, seam_edge_pm, seam_vertex_pm);
  UV_pmap uv_pm = sm.add_property_map<SM_halfedge_descriptor, Point_2>("h:uv").first;

  halfedge_descriptor bhd = CGAL::Polygon_mesh_processing::longest_border(mesh, CGAL::Polygon_mesh_processing::parameters::all_default()).first;

  typedef SMP::Two_vertices_parameterizer_3<Mesh>                Border_parameterizer;
  typedef SMP::LSCM_parameterizer_3<Mesh, Border_parameterizer>  Parameterizer;

  // Here's where the big changes start.
  SurfaceMesh::Property_map<SM_halfedge_descriptor, int> vimap = sm.add_property_map<SM_halfedge_descriptor, int>("h:vi").first;
  SurfaceMesh::Property_map<SM_halfedge_descriptor, bool> vpmap = sm.add_property_map<SM_halfedge_descriptor, bool>("h:vp").first;
  Parameterizer parameterizer;
  parameterizer.parameterize(mesh, bhd, uv_pm, vimap, vpmap);

  const char* uvmap_file = "uvmap.off";
  std::ofstream uvmap_out(uvmap_file);
  SMP::IO::output_uvmap_to_off(mesh,sm.vertices(),sm.faces(),uv_pm,vimap,uvmap_out);

  return EXIT_SUCCESS;
}

This does not compile, complaining about a required conversion on line 131 in File_off.h.

Actual questions

  • Is vimap initialized correctly?
  • Is this the reasonable direction towards my goal of writing the vertices in the same order?
  • If yes, how do I pass the correct arguments to output_uvmap_to_off(...)? For instance, it asks for a VertexContainer and I provide a Vertex_range (hence the compilation error, I suppose). Shall I just collect the vertices as suggested here or here or is there a more elegant way?
  • If no, what is the right course of action?

Solution

  • The following issue answers this question.