Point Cloud Library (PCL)
1.15.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
filters
impl
farthest_point_sampling.hpp
1
/*
2
* SPDX-License-Identifier: BSD-3-Clause
3
*
4
* Point Cloud Library (PCL) - www.pointclouds.org
5
* Copyright (c) 2020-, Open Perception, Inc.
6
*
7
* All rights reserved.
8
*/
9
10
#pragma once
11
12
#include <
pcl/common/geometry.h
>
13
#include <pcl/filters/farthest_point_sampling.h>
14
#include <pcl/point_cloud.h>
15
#include <
pcl/point_types.h
>
16
#include <algorithm>
17
#include <limits>
18
#include <random>
19
20
template
<
typename
Po
int
T>
void
21
pcl::FarthestPointSampling<PointT>::applyFilter
(
Indices
&indices)
22
{
23
const
std::size_t size =
indices_
->size();
24
//if requested number of point is equal to the point cloud size, copy original cloud
25
if
(
sample_size_
== size)
26
{
27
indices = *
indices_
;
28
removed_indices_
->clear ();
29
return
;
30
}
31
//check if requested number of points is greater than the point cloud size
32
if
(
sample_size_
> size)
33
{
34
PCL_THROW_EXCEPTION (
BadArgumentException
,
35
"Requested number of points is greater than point cloud size!"
);
36
}
37
38
std::vector<float> distances_to_selected_points (size, std::numeric_limits<float>::max ());
39
40
//set random seed
41
std::mt19937 random_gen(
seed_
);
42
std::uniform_int_distribution<index_t> dis(0, size -1);
43
44
//lambda to map filter indices back to pointcloud indices for increased readability
45
auto
toCloudIndex = [
this
](
const
auto
idx){
return
(*
indices_
)[idx];};
46
47
//pick the first point at random
48
index_t
max_index = dis(random_gen);
49
distances_to_selected_points[max_index] = -1.0;
50
indices.push_back(toCloudIndex(max_index));
51
52
for
(std::size_t j = 1; j <
sample_size_
; ++j)
53
{
54
index_t
next_max_index = 0;
55
56
const
PointT& max_index_point = (*input_)[toCloudIndex(max_index)];
57
//recompute distances
58
for
(std::size_t i = 0; i < size; ++i)
59
{
60
if
(distances_to_selected_points[i] == -1.0)
61
continue
;
62
distances_to_selected_points[i] = std::min(distances_to_selected_points[i],
geometry::distance
((*
input_
)[toCloudIndex(i)], max_index_point));
63
if
(distances_to_selected_points[i] > distances_to_selected_points[next_max_index])
64
next_max_index = i;
65
}
66
67
//select farthest point based on previously calculated distances
68
//since distance is set to -1 for all selected elements,previously selected
69
//elements are guaranteed to not be selected
70
max_index = next_max_index;
71
distances_to_selected_points[max_index] = -1.0;
72
indices.push_back(toCloudIndex(max_index));
73
//set distance to -1 to ignore during max element search
74
}
75
76
if
(
extract_removed_indices_
)
77
{
78
for
(std::size_t k = 0; k < distances_to_selected_points.size(); ++k)
79
{
80
if
(distances_to_selected_points[k] != -1.0)
81
(*removed_indices_).push_back(toCloudIndex(k));
82
}
83
}
84
}
85
86
#define PCL_INSTANTIATE_FarthestPointSampling(T) template class PCL_EXPORTS pcl::FarthestPointSampling<T>;
pcl::BadArgumentException
An exception that is thrown when the arguments number or type is wrong/unhandled.
Definition
exceptions.h:259
pcl::FarthestPointSampling::seed_
unsigned int seed_
Random number seed.
Definition
farthest_point_sampling.h:87
pcl::FarthestPointSampling::applyFilter
void applyFilter(pcl::Indices &indices) override
Sample of point indices.
Definition
farthest_point_sampling.hpp:21
pcl::FarthestPointSampling::sample_size_
std::size_t sample_size_
Number of points that will be returned.
Definition
farthest_point_sampling.h:85
pcl::Filter::extract_removed_indices_
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition
filter.h:161
pcl::Filter::removed_indices_
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition
filter.h:155
pcl::PCLBase::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition
pcl_base.h:147
pcl::PCLBase::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition
pcl_base.h:150
geometry.h
Defines some geometrical functions and utility functions.
point_types.h
Defines all the PCL implemented PointT point type structures.
pcl::geometry::distance
float distance(const PointT &p1, const PointT &p2)
Definition
geometry.h:60
pcl::index_t
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition
types.h:112
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition
types.h:133