File: clustering_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 (63 lines) | stat: -rw-r--r-- 2,251 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
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/Point_set_3/IO.h>
#include <CGAL/cluster_point_set.h>
#include <CGAL/compute_average_spacing.h>

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

#include <fstream>
#include <iostream>

using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_3 = Kernel::Point_3;
using Point_set = CGAL::Point_set_3<Point_3>;

int main (int argc, char** argv)
{
  // Read input file
  std::ifstream ifile((argc > 1) ? argv[1] : CGAL::data_file_path("points_3/hippo1.ply"), std::ios_base::binary);
  Point_set points;
  ifile >> points;

  // Add a cluster map
  Point_set::Property_map<int> cluster_map = points.add_property_map<int>("cluster", -1).first;

  // Compute average spacing
  double spacing = CGAL::compute_average_spacing<CGAL::Parallel_if_available_tag> (points, 12);
  std::cerr << "Spacing = " << spacing << std::endl;

  // Adjacencies stored in vector
  std::vector<std::pair<std::size_t, std::size_t> > adjacencies;

  // Compute clusters
  CGAL::Real_timer t;
  t.start();
  std::size_t nb_clusters
    = CGAL::cluster_point_set(points, cluster_map,
                              points.parameters().neighbor_radius(spacing)
                                                 .adjacencies(std::back_inserter(adjacencies)));
  t.stop();
  std::cerr << "Found " << nb_clusters << " clusters with " << adjacencies.size()
            << " adjacencies in " << t.time() << " seconds" << std::endl;

  // Output a colored PLY file
  Point_set::Property_map<unsigned char> red = points.add_property_map<unsigned char>("red", 0).first;
  Point_set::Property_map<unsigned char> green = points.add_property_map<unsigned char>("green", 0).first;
  Point_set::Property_map<unsigned char> blue = points.add_property_map<unsigned char>("blue", 0).first;
  for(Point_set::Index idx : points)
  {
    // One color per cluster
    CGAL::Random rand (cluster_map[idx]);
    red[idx] = rand.get_int(64, 192);
    green[idx] = rand.get_int(64, 192);
    blue[idx] = rand.get_int(64, 192);
  }

  std::ofstream ofile("out.ply", std::ios_base::binary);
  CGAL::IO::set_binary_mode(ofile);
  ofile << points;

  return EXIT_SUCCESS;
}