pick_n_random_points.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Siargey Kachanovich
6  *
7  * Copyright (C) 2016 INRIA
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef PICK_N_RANDOM_POINTS_H_
24 #define PICK_N_RANDOM_POINTS_H_
25 
26 #include <gudhi/Clock.h>
27 
28 #include <boost/range/size.hpp>
29 
30 #include <cstddef>
31 #include <random> // random_device, mt19937
32 #include <algorithm> // shuffle
33 #include <numeric> // iota
34 #include <iterator>
35 #include <vector>
36 
37 
38 namespace Gudhi {
39 
40 namespace subsampling {
41 
50 template <typename Point_container,
51 typename OutputIterator>
52 void pick_n_random_points(Point_container const &points,
53  std::size_t final_size,
54  OutputIterator output_it) {
55 #ifdef GUDHI_SUBSAMPLING_PROFILING
56  Gudhi::Clock t;
57 #endif
58 
59  std::size_t nbP = boost::size(points);
60  if (final_size > nbP)
61  final_size = nbP;
62 
63  std::vector<int> landmarks(nbP);
64  std::iota(landmarks.begin(), landmarks.end(), 0);
65 
66  std::random_device rd;
67  std::mt19937 g(rd());
68 
69  std::shuffle(landmarks.begin(), landmarks.end(), g);
70  landmarks.resize(final_size);
71 
72  for (int l : landmarks)
73  *output_it++ = points[l];
74 
75 #ifdef GUDHI_SUBSAMPLING_PROFILING
76  t.end();
77  std::cerr << "Random landmark choice took " << t.num_seconds()
78  << " seconds." << std::endl;
79 #endif
80 }
81 
82 } // namespace subsampling
83 
84 } // namespace Gudhi
85 
86 #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:52
Definition: SimplicialComplexForAlpha.h:26
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 18:07:51 for GUDHI by Doxygen 1.8.13