File: callback_example.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 (89 lines) | stat: -rw-r--r-- 2,586 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
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/Real_timer.h>

#include <CGAL/compute_average_spacing.h>
#include <CGAL/grid_simplify_point_set.h>
#include <CGAL/jet_smooth_point_set.h>

#include <boost/lexical_cast.hpp>

#include <vector>
#include <fstream>

// Types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef CGAL::Random_points_on_sphere_3<Point> Generator;

// Concurrency
typedef CGAL::Parallel_if_available_tag Concurrency_tag;

// instance of std::function<bool(double)>
struct Progress_to_std_cerr_callback
{
  mutable std::size_t nb;
  CGAL::Real_timer timer;
  double t_start;
  mutable double t_latest;
  const std::string name;

  Progress_to_std_cerr_callback (const char* name)
    : name (name)
  {
    timer.start();
    t_start = timer.time();
    t_latest = t_start;
  }

  bool operator()(double advancement) const
  {
    // Avoid calling time() at every single iteration, which could
    // impact performances very badly
    ++ nb;
    if (advancement != 1 && nb % 100 != 0)
      return true;

    double t = timer.time();
    if (advancement == 1 || (t - t_latest) > 0.1) // Update every 1/10th of second
    {
      std::cerr << "\r" // Return at the beginning of same line and overwrite
                << name << ": " << int(advancement * 100) << "%";

      if (advancement == 1)
        std::cerr << std::endl;
      t_latest = t;
    }

    return true;
  }
};

int main (int argc, char* argv[])
{
  int N = (argc > 1) ? boost::lexical_cast<int>(argv[1]) : 1000;

  // Generate N points on a sphere of radius 100.
  std::vector<Point> points;
  points.reserve(N);
  Generator generator(100.);
  std::copy_n(generator, N, std::back_inserter(points));

  // Compute average spacing
  FT average_spacing = CGAL::compute_average_spacing<Concurrency_tag>
    (points, 6,
     CGAL::parameters::callback(Progress_to_std_cerr_callback("Computing average spacing")));

  // Simplify on a grid with a size of twice the average spacing
  points.erase(CGAL::grid_simplify_point_set(points, 2. * average_spacing,
                                             CGAL::parameters::callback(Progress_to_std_cerr_callback("Grid simplification"))),
               points.end());

  // Smooth simplified point set
  CGAL::jet_smooth_point_set<Concurrency_tag>(points, 6,
                                              CGAL::parameters::callback(Progress_to_std_cerr_callback("Jet smoothing")));

  return EXIT_SUCCESS;
}