Point Cloud Library (PCL)
1.15.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
registration
impl
correspondence_estimation_backprojection.hpp
1
/*
2
* Software License Agreement (BSD License)
3
*
4
* Point Cloud Library (PCL) - www.pointclouds.org
5
* Copyright (c) 2012-, Open Perception, Inc.
6
*
7
* All rights reserved.
8
*
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
11
* are met:
12
*
13
* * Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following
17
* disclaimer in the documentation and/or other materials provided
18
* with the distribution.
19
* * Neither the name of the copyright holder(s) nor the names of its
20
* contributors may be used to endorse or promote products derived
21
* from this software without specific prior written permission.
22
*
23
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
* POSSIBILITY OF SUCH DAMAGE.
35
*
36
* $Id$
37
*
38
*/
39
40
#ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
41
#define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
42
43
#include <pcl/common/copy_point.h>
44
45
namespace
pcl
{
46
47
namespace
registration
{
48
49
template
<
typename
Po
int
Source,
typename
Po
int
Target,
typename
NormalT,
typename
Scalar>
50
bool
51
CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar>::
52
initCompute
()
53
{
54
if
(!source_normals_ || !target_normals_) {
55
PCL_WARN(
"[pcl::registration::%s::initCompute] Datasets containing normals for "
56
"source/target have not been given!\n"
,
57
getClassName
().c_str());
58
return
(
false
);
59
}
60
61
return
(
62
CorrespondenceEstimationBase<PointSource, PointTarget, Scalar>::initCompute
());
63
}
64
65
///////////////////////////////////////////////////////////////////////////////////////////
66
template
<
typename
Po
int
Source,
typename
Po
int
Target,
typename
NormalT,
typename
Scalar>
67
void
68
CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar>::
69
determineCorrespondences
(
pcl::Correspondences
& correspondences,
double
max_distance)
70
{
71
if
(!
initCompute
())
72
return
;
73
74
correspondences.resize(
indices_
->size());
75
76
pcl::Indices
nn_indices(k_);
77
std::vector<float> nn_dists(k_);
78
79
int
min_index = 0;
80
81
pcl::Correspondence
corr;
82
unsigned
int
nr_valid_correspondences = 0;
83
84
// Iterate over the input set of source indices
85
for
(
const
auto
& idx_i : (*
indices_
)) {
86
const
auto
& pt =
detail::pointCopyOrRef<PointTarget, PointSource>
(
input_
, idx_i);
87
tree_
->nearestKSearch(pt, k_, nn_indices, nn_dists);
88
89
// Among the K nearest neighbours find the one with minimum perpendicular distance
90
// to the normal
91
float
min_dist = std::numeric_limits<float>::max();
92
93
// Find the best correspondence
94
for
(std::size_t j = 0; j < nn_indices.size(); j++) {
95
float
cos_angle = (*source_normals_)[idx_i].normal_x *
96
(*target_normals_)[nn_indices[j]].normal_x +
97
(*source_normals_)[idx_i].normal_y *
98
(*target_normals_)[nn_indices[j]].normal_y +
99
(*source_normals_)[idx_i].normal_z *
100
(*target_normals_)[nn_indices[j]].normal_z;
101
float
dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
102
103
if
(dist < min_dist) {
104
min_dist = dist;
105
min_index =
static_cast<
int
>
(j);
106
}
107
}
108
if
(min_dist > max_distance)
109
continue
;
110
111
corr.
index_query
= idx_i;
112
corr.
index_match
= nn_indices[min_index];
113
corr.
distance
= nn_dists[min_index];
// min_dist;
114
correspondences[nr_valid_correspondences++] = corr;
115
}
116
correspondences.resize(nr_valid_correspondences);
117
deinitCompute
();
118
}
119
120
template
<
typename
Po
int
Source,
typename
Po
int
Target,
typename
NormalT,
typename
Scalar>
121
void
122
CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar>::
123
determineReciprocalCorrespondences
(
pcl::Correspondences
& correspondences,
124
double
max_distance)
125
{
126
if
(!
initCompute
())
127
return
;
128
129
// Set the internal point representation of choice
130
if
(!
initComputeReciprocal
())
131
return
;
132
133
correspondences.resize(
indices_
->size());
134
135
pcl::Indices
nn_indices(k_);
136
std::vector<float> nn_dists(k_);
137
pcl::Indices
index_reciprocal(1);
138
std::vector<float> distance_reciprocal(1);
139
140
int
min_index = 0;
141
142
pcl::Correspondence
corr;
143
unsigned
int
nr_valid_correspondences = 0;
144
int
target_idx = 0;
145
146
// Iterate over the input set of source indices
147
for
(
const
auto
& idx_i : (*
indices_
)) {
148
// Check if the template types are the same. If true, avoid a copy.
149
// Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
150
// macro!
151
tree_
->nearestKSearch(
152
detail::pointCopyOrRef<PointTarget, PointSource>
(
input_
, idx_i),
153
k_,
154
nn_indices,
155
nn_dists);
156
157
// Among the K nearest neighbours find the one with minimum perpendicular distance
158
// to the normal
159
float
min_dist = std::numeric_limits<float>::max();
160
161
// Find the best correspondence
162
for
(std::size_t j = 0; j < nn_indices.size(); j++) {
163
float
cos_angle = (*source_normals_)[idx_i].normal_x *
164
(*target_normals_)[nn_indices[j]].normal_x +
165
(*source_normals_)[idx_i].normal_y *
166
(*target_normals_)[nn_indices[j]].normal_y +
167
(*source_normals_)[idx_i].normal_z *
168
(*target_normals_)[nn_indices[j]].normal_z;
169
float
dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
170
171
if
(dist < min_dist) {
172
min_dist = dist;
173
min_index =
static_cast<
int
>
(j);
174
}
175
}
176
if
(min_dist > max_distance)
177
continue
;
178
179
// Check if the correspondence is reciprocal
180
target_idx = nn_indices[min_index];
181
tree_reciprocal_
->nearestKSearch(
182
detail::pointCopyOrRef<PointSource, PointTarget>
(
target_
, target_idx),
183
1,
184
index_reciprocal,
185
distance_reciprocal);
186
187
if
(idx_i != index_reciprocal[0])
188
continue
;
189
190
corr.
index_query
= idx_i;
191
corr.
index_match
= nn_indices[min_index];
192
corr.
distance
= nn_dists[min_index];
// min_dist;
193
correspondences[nr_valid_correspondences++] = corr;
194
}
195
correspondences.resize(nr_valid_correspondences);
196
deinitCompute
();
197
}
198
199
}
// namespace registration
200
}
// namespace pcl
201
202
#endif
// PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
pcl::PCLBase< PointSource >::input_
PointCloudConstPtr input_
Definition
pcl_base.h:147
pcl::PCLBase< PointSource >::indices_
IndicesPtr indices_
Definition
pcl_base.h:150
pcl::PCLBase< PointSource >::deinitCompute
bool deinitCompute()
Definition
pcl_base.hpp:175
pcl::registration::CorrespondenceEstimationBackProjection::initCompute
bool initCompute()
Internal computation initialization.
Definition
correspondence_estimation_backprojection.hpp:52
pcl::registration::CorrespondenceEstimationBackProjection::determineReciprocalCorrespondences
virtual void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max())
Determine the reciprocal correspondences between input and target cloud.
Definition
correspondence_estimation_backprojection.hpp:123
pcl::registration::CorrespondenceEstimationBackProjection::determineCorrespondences
void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max())
Determine the correspondences between input and target cloud.
Definition
correspondence_estimation_backprojection.hpp:69
pcl::registration::CorrespondenceEstimationBase< PointSource, PointTarget, float >::target_
PointCloudTargetConstPtr target_
Definition
correspondence_estimation.h:335
pcl::registration::CorrespondenceEstimationBase::initCompute
bool initCompute()
Internal computation initialization.
Definition
correspondence_estimation.hpp:74
pcl::registration::CorrespondenceEstimationBase< PointSource, PointTarget, float >::getClassName
const std::string & getClassName() const
Definition
correspondence_estimation.h:354
pcl::registration::CorrespondenceEstimationBase< PointSource, PointTarget, float >::tree_
KdTreePtr tree_
Definition
correspondence_estimation.h:329
pcl::registration::CorrespondenceEstimationBase< PointSource, PointTarget, float >::tree_reciprocal_
KdTreeReciprocalPtr tree_reciprocal_
Definition
correspondence_estimation.h:332
pcl::registration::CorrespondenceEstimationBase< PointSource, PointTarget, float >::initComputeReciprocal
bool initComputeReciprocal()
Definition
correspondence_estimation.hpp:98
pcl::registration::detail::pointCopyOrRef
const PointSource & pointCopyOrRef(typename pcl::PointCloud< PointSource >::ConstPtr &input, const Index &idx)
Definition
correspondence_estimation.hpp:124
pcl::registration
Definition
convergence_criteria.h:46
pcl
Definition
convolution.h:46
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition
correspondence.h:89
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition
types.h:133
pcl::Correspondence
Correspondence represents a match between two entities (e.g., points, descriptors,...
Definition
correspondence.h:61
pcl::Correspondence::index_query
index_t index_query
Index of the query (source) point.
Definition
correspondence.h:63
pcl::Correspondence::distance
float distance
Definition
correspondence.h:69
pcl::Correspondence::index_match
index_t index_match
Index of the matching (target) point.
Definition
correspondence.h:65