pick_n_random_points.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): Siargey Kachanovich
4  *
5  * Copyright (C) 2016 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef PICK_N_RANDOM_POINTS_H_
12 #define PICK_N_RANDOM_POINTS_H_
13 
14 #ifdef GUDHI_SUBSAMPLING_PROFILING
15 # include <gudhi/Clock.h>
16 #endif
17 
18 #include <boost/range/size.hpp>
19 
20 #include <cstddef>
21 #include <random> // random_device, mt19937
22 #include <algorithm> // shuffle
23 #include <numeric> // iota
24 #include <iterator>
25 #include <vector>
26 
27 
28 namespace Gudhi {
29 
30 namespace subsampling {
31 
40 template <typename Point_container,
41 typename OutputIterator>
42 void pick_n_random_points(Point_container const &points,
43  std::size_t final_size,
44  OutputIterator output_it) {
45 #ifdef GUDHI_SUBSAMPLING_PROFILING
46  Gudhi::Clock t;
47 #endif
48 
49  std::random_device rd;
50  std::mt19937 g(rd());
51 
52 #if __cplusplus >= 201703L
53  std::sample(std::begin(points), std::end(points), output_it, final_size, g);
54 #else
55  std::size_t nbP = boost::size(points);
56  if (final_size > nbP)
57  final_size = nbP;
58 
59  std::vector<int> landmarks(nbP);
60  std::iota(landmarks.begin(), landmarks.end(), 0);
61 
62  std::shuffle(landmarks.begin(), landmarks.end(), g);
63  landmarks.resize(final_size);
64 
65  for (int l : landmarks)
66  *output_it++ = points[l];
67 #endif
68 
69 #ifdef GUDHI_SUBSAMPLING_PROFILING
70  t.end();
71  std::cerr << "Random landmark choice took " << t.num_seconds()
72  << " seconds." << std::endl;
73 #endif
74 }
75 
76 } // namespace subsampling
77 
78 } // namespace Gudhi
79 
80 #endif // PICK_N_RANDOM_POINTS_H_
void pick_n_random_points(Point_container const &points, std::size_t final_size, OutputIterator output_it)
Subsample a point set by picking random vertices.
Definition: pick_n_random_points.h:42
GUDHIdev  Version 3.5.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 16 2022 14:01:50 for GUDHIdev by Doxygen 1.9.1