RandomSampling.cpp
Go to the documentation of this file.
1 // kate: replace-tabs off; indent-width 4; indent-mode normal
2 // vim: ts=4:sw=4:noexpandtab
3 /*
4 
5 Copyright (c) 2010--2018,
6 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
7 You can contact the authors at <f dot pomerleau at gmail dot com> and
8 <stephane at magnenat dot net>
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of the <organization> nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 #include "RandomSampling.h"
36 
37 #include <random>
38 
39 // RandomSamplingDataPointsFilter
40 // Constructor
41 template<typename T>
43  PointMatcher<T>::DataPointsFilter("RandomSamplingDataPointsFilter", RandomSamplingDataPointsFilter::availableParameters(), params),
44  prob(Parametrizable::get<double>("prob")),
45  randomSamplingMethod(Parametrizable::get<int>("randomSamplingMethod")),
46  seed(Parametrizable::get<int>("seed"))
47 {
48 }
49 
50 // Compute
51 template<typename T>
54 {
55  DataPoints output(input);
56  inPlaceFilter(output);
57  return output;
58 }
59 
60 // In-place filter
61 template<typename T>
62 Eigen::VectorXf RandomSamplingDataPointsFilter<T>::sampleRandomIndices(const size_t nbPoints)
63 {
64  std::minstd_rand randomNumberGenerator;
65  if (seed == -1)
66  {
67  std::random_device randomDevice;
68  randomNumberGenerator = std::minstd_rand(randomDevice());
69  }
70  else
71  {
72  randomNumberGenerator = std::minstd_rand(seed);
73  }
74 
75  switch(randomSamplingMethod)
76  {
77  default: // Direct RNG.
78  {
79  const float randomNumberRange{static_cast<float>(randomNumberGenerator.max() - randomNumberGenerator.min())};
80  return Eigen::VectorXf::NullaryExpr(nbPoints, [&](float){return static_cast<float>(randomNumberGenerator() / randomNumberRange);});
81  }
82  case 1: // Uniform distribution.
83  {
84  std::uniform_real_distribution<float> distribution(0, 1);
85  return Eigen::VectorXf::NullaryExpr(nbPoints, [&](float){return distribution(randomNumberGenerator);});
86  }
87  }
88 }
89 
90 // In-place filter
91 template<typename T>
93  DataPoints& cloud)
94 {
95  const size_t nbPointsIn = cloud.features.cols();
96  const size_t nbPointsOut = nbPointsIn * prob;
97 
98  const Eigen::VectorXf randomNumbers{sampleRandomIndices(nbPointsIn)};
99  size_t j{0u};
100  for (size_t i{0u}; i < nbPointsIn && j<=nbPointsOut; ++i)
101  {
102  if (randomNumbers(i) < prob)
103  {
104  cloud.setColFrom(j, cloud, i);
105  ++j;
106  }
107  }
108  cloud.conservativeResize(j);
109 }
110 
113 
114 
void setColFrom(Index thisCol, const DataPoints &that, Index thatCol)
Set column thisCol equal to column thatCol of that, copy features and descriptors if any...
virtual DataPoints filter(const DataPoints &input)
Apply filters to input point cloud. This is the non-destructive version and returns a copy...
Eigen::VectorXf sampleRandomIndices(const size_t nbPoints)
Functions and classes that are dependant on scalar type are defined in this templatized class...
Definition: PointMatcher.h:130
const M::mapped_type & get(const M &m, const typename M::key_type &k)
The superclass of classes that are constructed using generic parameters. This class provides the para...
void conservativeResize(Index pointCount)
Resize the cloud to pointCount points, conserving existing ones.
RandomSamplingDataPointsFilter(const Parameters &params=Parameters())
virtual void inPlaceFilter(DataPoints &cloud)
Apply these filters to a point cloud without copying.
Matrix features
features of points in the cloud
Definition: PointMatcher.h:331
PM::DataPointsFilter DataPointsFilter
Parametrizable::Parameters Parameters


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:38:03