1

I have the following code, which matches the example for 90%. I want to "flatten" the 3D model via parameterization. But the input and output do not have the same scale.

The question I have, how can we remain the same scale of the scaling. In the image below you can find the input and the output:

enter image description here

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh_parameterization/IO/File_off.h>
//#include <CGAL/Surface_mesh_parameterization/Circular_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Discrete_authalic_parameterizer_3.h>


//#include <CGAL/Surface_mesh_parameterization/Square_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Barycentric_mapping_parameterizer_3.h>

#include <CGAL/Surface_mesh_parameterization/Two_vertices_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/LSCM_parameterizer_3.h>

#include <CGAL/Surface_mesh_parameterization/Error_code.h>
#include <CGAL/Surface_mesh_parameterization/parameterize.h>
#include <CGAL/Polygon_mesh_processing/measure.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
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>::halfedge_descriptor  halfedge_descriptor;
typedef boost::graph_traits<SurfaceMesh>::vertex_descriptor    vertex_descriptor;
typedef boost::graph_traits<SurfaceMesh>::face_descriptor      face_descriptor;
namespace SMP = CGAL::Surface_mesh_parameterization;
int main(int argc, char** argv)
{
    std::ifstream in((argc > 1) ? argv[1] : "C:/Users/Niels/Desktop/CGAL/flatten.off");
    if (!in) {
        std::cerr << "Problem loading the input data" << std::endl;
        return EXIT_FAILURE;
    }
    SurfaceMesh sm;
    in >> sm;
    // A halfedge on the border
    halfedge_descriptor bhd = CGAL::Polygon_mesh_processing::longest_border(sm).first;
    // The 2D points of the uv parametrisation will be written into this map
    typedef SurfaceMesh::Property_map<vertex_descriptor, Point_2>  UV_pmap;
    UV_pmap uv_map = sm.add_property_map<vertex_descriptor, Point_2>("v:uv").first;

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

    SMP::Error_code err = SMP::parameterize(sm, Parameterizer(), bhd, uv_map);
    if (err != SMP::OK) {
        std::cerr << "Error: " << SMP::get_error_message(err) << std::endl;
        return EXIT_FAILURE;
    }
    std::ofstream out("result.off");
    SMP::IO::output_uvmap_to_off(sm, bhd, uv_map, out);
    return EXIT_SUCCESS;
}

Update, but this runs infinite:

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh_parameterization/IO/File_off.h>
//#include <CGAL/Surface_mesh_parameterization/Circular_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Discrete_authalic_parameterizer_3.h>


//#include <CGAL/Surface_mesh_parameterization/Square_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Barycentric_mapping_parameterizer_3.h>

#include <CGAL/Surface_mesh_parameterization/Two_vertices_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/LSCM_parameterizer_3.h>

#include <CGAL/Surface_mesh_parameterization/Error_code.h>
#include <CGAL/Surface_mesh_parameterization/parameterize.h>
#include <CGAL/Polygon_mesh_processing/measure.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
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>::halfedge_descriptor  halfedge_descriptor;
typedef boost::graph_traits<SurfaceMesh>::vertex_descriptor    vertex_descriptor;
typedef boost::graph_traits<SurfaceMesh>::face_descriptor      face_descriptor;
namespace SMP = CGAL::Surface_mesh_parameterization;

#include <CGAL/Surface_mesh_parameterization/ARAP_parameterizer_3.h>



int main(int argc, char** argv)
{
    std::ifstream in((argc > 1) ? argv[1] : "FlatteningObject.off");
    if (!in) {
        std::cerr << "Problem loading the input data" << std::endl;
        return EXIT_FAILURE;
    }
    SurfaceMesh sm;
    in >> sm;
    // A halfedge on the border
    halfedge_descriptor bhd = CGAL::Polygon_mesh_processing::longest_border(sm).first;
    // The 2D points of the uv parametrisation will be written into this map
    typedef SurfaceMesh::Property_map<vertex_descriptor, Point_2>  UV_pmap;
    UV_pmap uv_map = sm.add_property_map<vertex_descriptor, Point_2>("v:uv").first;

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

    SMP::Error_code err = SMP::parameterize(
        sm, 
        SMP::ARAP_parameterizer_3<SurfaceMesh, Border_parameterizer>(
                    Border_parameterizer(),
                    CGAL::Eigen_solver_traits<Eigen::SparseLU<CGAL::Eigen_sparse_matrix<double>::EigenType> >(),
                    1000,
                    50
        ), 
        bhd, 
        uv_map);

    if (err != SMP::OK) {
        std::cerr << "Error: " << SMP::get_error_message(err) << std::endl;
        return EXIT_FAILURE;
    }
    std::ofstream out("result.off");
    SMP::IO::output_uvmap_to_off(sm, bhd, uv_map, out);
    return EXIT_SUCCESS;
}
Niels
  • 48,601
  • 4
  • 62
  • 81
  • By convention I think most of the parameterization methods map to the unit square. There is direct way to specify a scale in the output, but you could just take the bounding box of the 2D parameterization, and scale it to whichever length you wish it was? – Mael Nov 14 '19 at 13:18
  • There is **no** direct way, sorry... – Mael Nov 14 '19 at 13:28
  • 1
    I don't know the original length, if is "flatten" it out, it should have the length for the texture. – Niels Nov 14 '19 at 14:17

1 Answers1

1

I have had some success with using the ARAP method instead. The default 50 iterations was not actually enough and produced a crossed over mesh. Bumping up the iterations to 500 worked.

SMP::Error_code err = SMP::parameterize(
            in, 
            SMP::ARAP_parameterizer_3<geometry::Mesh, Border_parameterizer>(
                Border_parameterizer(),
                CGAL::Eigen_solver_traits<Eigen::SparseLU<CGAL::Eigen_sparse_matrix<double>::EigenType> >(),
                1000,
                500), 
            bhd, 
            uv_map);

...Using commit bec70a6d52

C. Dow
  • 55
  • 5