File: example_mesh_classification.cpp

package info (click to toggle)
cgal 6.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 141,840 kB
  • sloc: cpp: 797,081; ansic: 203,398; sh: 490; python: 411; makefile: 286; javascript: 174
file content (99 lines) | stat: -rw-r--r-- 3,528 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#if defined (_MSC_VER) && !defined (_WIN64)
#pragma warning(disable:4244) // boost::number_distance::distance()
                              // converts 64 to 32 bits integers
#endif

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>

#include <CGAL/Classification.h>
#include <CGAL/Real_timer.h>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point;
typedef CGAL::Surface_mesh<Point> Mesh;

namespace Classification = CGAL::Classification;

typedef Classification::Label_handle                                            Label_handle;
typedef Classification::Feature_handle                                          Feature_handle;
typedef Classification::Label_set                                               Label_set;
typedef Classification::Feature_set                                             Feature_set;

typedef Classification::Face_descriptor_to_center_of_mass_map<Mesh>             Face_point_map;
typedef Classification::Face_descriptor_to_face_descriptor_with_bbox_map<Mesh>  Face_with_bbox_map;
typedef Classification::Mesh_feature_generator<Kernel, Mesh, Face_point_map>    Feature_generator;

int main (int argc, char** argv)
{
  std::string filename = CGAL::data_file_path("meshes/b9_mesh.off");
  std::string filename_config = "data/b9_mesh_config.bin";

  if (argc > 1)
    filename = argv[1];
  if (argc > 2)
    filename_config = argv[2];

  Mesh mesh;
  if(!CGAL::IO::read_polygon_mesh(filename, mesh,
                                  // the PLY reader expects a binary file by default
                                  CGAL::parameters::use_binary_mode(false)))
  {
    std::cerr << "Invalid input." << std::endl;
    return 1;
  }

  std::cerr << "Generating features" << std::endl;
  CGAL::Real_timer t;
  t.start();

  ///////////////////////////////////////////////////////////////////
  //! [Generator]

  Feature_set features;

  Face_point_map face_point_map (&mesh); // Associates each face to its center of mass

  std::size_t number_of_scales = 5;
  Feature_generator generator (mesh, face_point_map, number_of_scales);

  features.begin_parallel_additions();
  generator.generate_point_based_features (features); // Features that consider the mesh as a point set
  generator.generate_face_based_features (features);  // Features computed directly on mesh faces
  features.end_parallel_additions();

  //! [Generator]
  ///////////////////////////////////////////////////////////////////

  t.stop();
  std::cerr << "Done in " << t.time() << " second(s)" << std::endl;

  Label_set labels = { "ground", "vegetation", "roof" };

  std::vector<int> label_indices(mesh.number_of_faces(), -1);

  std::cerr << "Using ETHZ Random Forest Classifier" << std::endl;
  Classification::ETHZ::Random_forest_classifier classifier (labels, features);

  std::cerr << "Loading configuration" << std::endl;
  std::ifstream in_config (filename_config, std::ios_base::in | std::ios_base::binary);
  classifier.load_configuration (in_config);

  std::cerr << "Classifying with graphcut" << std::endl;
  t.reset();
  t.start();
  Classification::classify_with_graphcut<CGAL::Parallel_if_available_tag>
    (mesh.faces(), Face_with_bbox_map(&mesh), labels, classifier,
     generator.neighborhood().n_ring_neighbor_query(2),
     0.2f, 1, label_indices);
  t.stop();

  std::cerr << "Classification with graphcut done in " << t.time() << " second(s)" << std::endl;

  return EXIT_SUCCESS;
}