Loading...
Searching...
No Matches
correspondence_estimation_normal_shooting.hpp
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2012, Willow Garage, Inc.
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
#define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
#include <pcl/common/copy_point.h>
namespace pcl {
namespace registration {
template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
bool
CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar>::
initCompute()
{
if (!source_normals_) {
PCL_WARN("[pcl::registration::%s::initCompute] Datasets containing normals for "
"source have not been given!\n",
getClassName().c_str());
return (false);
}
return (
CorrespondenceEstimationBase<PointSource, PointTarget, Scalar>::initCompute());
}
template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
void
CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar>::
determineCorrespondences(pcl::Correspondences& correspondences, double max_distance)
{
if (!initCompute())
return;
correspondences.resize(indices_->size());
pcl::Indices nn_indices(k_);
std::vector<float> nn_dists(k_);
int min_index = 0;
pcl::Correspondence corr;
unsigned int nr_valid_correspondences = 0;
PointTarget pt;
// Iterate over the input set of source indices
for (const auto& idx_i : (*indices_)) {
// Check if the template types are the same. If true, avoid a copy.
// Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
// macro!
tree_->nearestKSearch(
detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx_i),
k_,
nn_indices,
nn_dists);
// Among the K nearest neighbours find the one with minimum perpendicular distance
// to the normal
double min_dist = std::numeric_limits<double>::max();
// Find the best correspondence
for (std::size_t j = 0; j < nn_indices.size(); j++) {
// computing the distance between a point and a line in 3d.
// Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
pt.x = (*target_)[nn_indices[j]].x - (*input_)[idx_i].x;
pt.y = (*target_)[nn_indices[j]].y - (*input_)[idx_i].y;
pt.z = (*target_)[nn_indices[j]].z - (*input_)[idx_i].z;
const NormalT& normal = (*source_normals_)[idx_i];
Eigen::Vector3d N(normal.normal_x, normal.normal_y, normal.normal_z);
Eigen::Vector3d V(pt.x, pt.y, pt.z);
Eigen::Vector3d C = N.cross(V);
// Check if we have a better correspondence
double dist = C.dot(C);
if (dist < min_dist) {
min_dist = dist;
min_index = static_cast<int>(j);
}
}
if (min_dist > max_distance)
continue;
corr.index_query = idx_i;
corr.index_match = nn_indices[min_index];
corr.distance = nn_dists[min_index]; // min_dist;
correspondences[nr_valid_correspondences++] = corr;
}
correspondences.resize(nr_valid_correspondences);
deinitCompute();
}
template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
void
CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar>::
determineReciprocalCorrespondences(pcl::Correspondences& correspondences,
double max_distance)
{
if (!initCompute())
return;
// setup tree for reciprocal search
// Set the internal point representation of choice
if (!initComputeReciprocal())
return;
correspondences.resize(indices_->size());
pcl::Indices nn_indices(k_);
std::vector<float> nn_dists(k_);
pcl::Indices index_reciprocal(1);
std::vector<float> distance_reciprocal(1);
int min_index = 0;
pcl::Correspondence corr;
unsigned int nr_valid_correspondences = 0;
int target_idx = 0;
PointTarget pt;
// Iterate over the input set of source indices
for (const auto& idx_i : (*indices_)) {
// Check if the template types are the same. If true, avoid a copy.
// Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
// macro!
tree_->nearestKSearch(
detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx_i),
k_,
nn_indices,
nn_dists);
// Among the K nearest neighbours find the one with minimum perpendicular distance
// to the normal
double min_dist = std::numeric_limits<double>::max();
// Find the best correspondence
for (std::size_t j = 0; j < nn_indices.size(); j++) {
// computing the distance between a point and a line in 3d.
// Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
pt.x = (*target_)[nn_indices[j]].x - (*input_)[idx_i].x;
pt.y = (*target_)[nn_indices[j]].y - (*input_)[idx_i].y;
pt.z = (*target_)[nn_indices[j]].z - (*input_)[idx_i].z;
const NormalT& normal = (*source_normals_)[idx_i];
Eigen::Vector3d N(normal.normal_x, normal.normal_y, normal.normal_z);
Eigen::Vector3d V(pt.x, pt.y, pt.z);
Eigen::Vector3d C = N.cross(V);
// Check if we have a better correspondence
double dist = C.dot(C);
if (dist < min_dist) {
min_dist = dist;
min_index = static_cast<int>(j);
}
}
if (min_dist > max_distance)
continue;
// Check if the correspondence is reciprocal
target_idx = nn_indices[min_index];
tree_reciprocal_->nearestKSearch(
detail::pointCopyOrRef<PointSource, PointTarget>(target_, target_idx),
1,
index_reciprocal,
distance_reciprocal);
if (idx_i != index_reciprocal[0])
continue;
// Correspondence IS reciprocal, save it and continue
corr.index_query = idx_i;
corr.index_match = nn_indices[min_index];
corr.distance = nn_dists[min_index]; // min_dist;
correspondences[nr_valid_correspondences++] = corr;
}
correspondences.resize(nr_valid_correspondences);
deinitCompute();
}
} // namespace registration
} // namespace pcl
#endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
pcl::registration::CorrespondenceEstimationBase
Abstract CorrespondenceEstimationBase class.
Definition correspondence_estimation.h:60
pcl::registration::CorrespondenceEstimationNormalShooting::determineReciprocalCorrespondences
void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the reciprocal correspondences between input and target cloud.
Definition correspondence_estimation_normal_shooting.hpp:135
pcl::registration::CorrespondenceEstimationNormalShooting::determineCorrespondences
void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the correspondences between input and target cloud.
Definition correspondence_estimation_normal_shooting.hpp:69
pcl::registration::CorrespondenceEstimationNormalShooting::initCompute
bool initCompute()
Internal computation initialization.
Definition correspondence_estimation_normal_shooting.hpp:53
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
pcl::Normal
A point structure representing normal coordinates and the surface curvature estimate.
Definition point_types.hpp:823