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 #include <gudhi/Clock.h>
15 
16 #include <boost/range/size.hpp>
17 
18 #include <cstddef>
19 #include <random> // random_device, mt19937
20 #include <algorithm> // shuffle
21 #include <numeric> // iota
22 #include <iterator>
23 #include <vector>
24 
25 
26 namespace Gudhi {
27 
28 namespace subsampling {
29 
38 template <typename Point_container,
39 typename OutputIterator>
40 void pick_n_random_points(Point_container const &points,
41  std::size_t final_size,
42  OutputIterator output_it) {
43 #ifdef GUDHI_SUBSAMPLING_PROFILING
44  Gudhi::Clock t;
45 #endif
46 
47  std::size_t nbP = boost::size(points);
48  if (final_size > nbP)
49  final_size = nbP;
50 
51  std::vector<int> landmarks(nbP);
52  std::iota(landmarks.begin(), landmarks.end(), 0);
53 
54  std::random_device rd;
55  std::mt19937 g(rd());
56 
57  std::shuffle(landmarks.begin(), landmarks.end(), g);
58  landmarks.resize(final_size);
59 
60  for (int l : landmarks)
61  *output_it++ = points[l];
62 
63 #ifdef GUDHI_SUBSAMPLING_PROFILING
64  t.end();
65  std::cerr << "Random landmark choice took " << t.num_seconds()
66  << " seconds." << std::endl;
67 #endif
68 }
69 
70 } // namespace subsampling
71 
72 } // namespace Gudhi
73 
74 #endif // PICK_N_RANDOM_POINTS_H_
Gudhi::subsampling::pick_n_random_points
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:40
GUDHI  Version 3.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Jan 21 2020 09:26:33 for GUDHI by Doxygen 1.8.16