File: vsa_approximation_example.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (64 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>

#include <CGAL/Surface_mesh_approximation/approximate_triangle_mesh.h>

#include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
#include <CGAL/Polygon_mesh_processing/orientation.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>

#include <iostream>
#include <fstream>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Surface_mesh<Kernel::Point_3> Mesh;

namespace VSA = CGAL::Surface_mesh_approximation;
namespace PMP = CGAL::Polygon_mesh_processing;

int main(int argc, char** argv)
{
  const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/bear.off");

  // read input surface triangle mesh
  Mesh mesh;
  if(!CGAL::Polygon_mesh_processing::IO::read_polygon_mesh(filename, mesh) ||
     !CGAL::is_triangle_mesh(mesh))
  {
    std::cerr << "Invalid input file." << std::endl;
    return EXIT_FAILURE;
  }

  // output indexed triangles
  std::vector<Kernel::Point_3> anchors;
  std::vector<std::array<std::size_t, 3> > triangles; // triplets of indices

  // free function interface with named parameters
  bool is_manifold = VSA::approximate_triangle_mesh(mesh,
    CGAL::parameters::seeding_method(VSA::HIERARCHICAL). // hierarchical seeding
                      max_number_of_proxies(200). // seeding with maximum number of proxies
                      number_of_iterations(30). // number of clustering iterations after seeding
                      anchors(std::back_inserter(anchors)). // anchor vertices
                      triangles(std::back_inserter(triangles))); // indexed triangles

  std::cout << "#anchor vertices: " << anchors.size() << std::endl;
  std::cout << "#triangles: " << triangles.size() << std::endl;

  if (is_manifold) {
    std::cout << "oriented, 2-manifold output." << std::endl;

    // convert from soup to surface mesh
    PMP::orient_polygon_soup(anchors, triangles);
    Mesh output;
    PMP::polygon_soup_to_polygon_mesh(anchors, triangles, output);
    if (CGAL::is_closed(output) && (!PMP::is_outward_oriented(output)))
      PMP::reverse_face_orientations(output);

    std::ofstream out("dump.off");
    out << output;
    out.close();
  }

  return EXIT_SUCCESS;
}