Loading...
Searching...
No Matches
usc.hpp
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2011, 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$
*
*/
#pragma once
#include <numeric> // for partial_sum
#include <pcl/features/usc.h>
#include <pcl/features/shot_lrf.h>
#include <pcl/common/angles.h>
#include <pcl/common/geometry.h>
#include <pcl/common/point_tests.h> // for pcl::isFinite
#include <pcl/common/utils.h>
//////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointInT, typename PointOutT, typename PointRFT> bool
pcl::UniqueShapeContext<PointInT, PointOutT, PointRFT>::initCompute ()
{
if (!Feature<PointInT, PointOutT>::initCompute ())
{
PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
return (false);
}
// Default LRF estimation alg: SHOTLocalReferenceFrameEstimation
typename SHOTLocalReferenceFrameEstimation<PointInT, PointRFT>::Ptr lrf_estimator(new SHOTLocalReferenceFrameEstimation<PointInT, PointRFT>());
lrf_estimator->setRadiusSearch (local_radius_);
lrf_estimator->setInputCloud (input_);
lrf_estimator->setIndices (indices_);
if (!fake_surface_)
lrf_estimator->setSearchSurface(surface_);
if (!FeatureWithLocalReferenceFrames<PointInT, PointRFT>::initLocalReferenceFrames (indices_->size (), lrf_estimator))
{
PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
return (false);
}
if (search_radius_< min_radius_)
{
PCL_ERROR ("[pcl::%s::initCompute] search_radius_ must be GREATER than min_radius_.\n", getClassName ().c_str ());
return (false);
}
// Update descriptor length
descriptor_length_ = elevation_bins_ * azimuth_bins_ * radius_bins_;
// Compute radial, elevation and azimuth divisions
float azimuth_interval = 360.0f / static_cast<float> (azimuth_bins_);
float elevation_interval = 180.0f / static_cast<float> (elevation_bins_);
// Reallocate divisions and volume lut
radii_interval_.clear ();
phi_divisions_.clear ();
theta_divisions_.clear ();
volume_lut_.clear ();
// Fills radii interval based on formula (1) in section 2.1 of Frome's paper
radii_interval_.resize (radius_bins_ + 1);
for (std::size_t j = 0; j < radius_bins_ + 1; j++)
radii_interval_[j] = static_cast<float> (std::exp (std::log (min_radius_) + ((static_cast<float> (j) / static_cast<float> (radius_bins_)) * std::log (search_radius_/min_radius_))));
// Fill theta divisions of elevation
theta_divisions_.resize (elevation_bins_ + 1, elevation_interval);
theta_divisions_[0] = 0;
std::partial_sum(theta_divisions_.begin (), theta_divisions_.end (), theta_divisions_.begin ());
// Fill phi divisions of elevation
phi_divisions_.resize (azimuth_bins_ + 1, azimuth_interval);
phi_divisions_[0] = 0;
std::partial_sum(phi_divisions_.begin (), phi_divisions_.end (), phi_divisions_.begin ());
// LookUp Table that contains the volume of all the bins
// "phi" term of the volume integral
// "integr_phi" has always the same value so we compute it only one time
float integr_phi = pcl::deg2rad (phi_divisions_[1]) - pcl::deg2rad (phi_divisions_[0]);
// exponential to compute the cube root using pow
float e = 1.0f / 3.0f;
// Resize volume look up table
volume_lut_.resize (radius_bins_ * elevation_bins_ * azimuth_bins_);
// Fill volumes look up table
for (std::size_t j = 0; j < radius_bins_; j++)
{
// "r" term of the volume integral
float integr_r = (radii_interval_[j+1]*radii_interval_[j+1]*radii_interval_[j+1] / 3) - (radii_interval_[j]*radii_interval_[j]*radii_interval_[j]/ 3);
for (std::size_t k = 0; k < elevation_bins_; k++)
{
// "theta" term of the volume integral
float integr_theta = std::cos (deg2rad (theta_divisions_[k])) - std::cos (deg2rad (theta_divisions_[k+1]));
// Volume
float V = integr_phi * integr_theta * integr_r;
// Compute cube root of the computed volume commented for performance but left
// here for clarity
// float cbrt = pow(V, e);
// cbrt = 1 / cbrt;
for (std::size_t l = 0; l < azimuth_bins_; l++)
// Store in lut 1/cbrt
//volume_lut_[ (l*elevation_bins_*radius_bins_) + k*radius_bins_ + j ] = cbrt;
volume_lut_[(l*elevation_bins_*radius_bins_) + k*radius_bins_ + j] = 1.0f / powf (V, e);
}
}
return (true);
}
//////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointInT, typename PointOutT, typename PointRFT> void
pcl::UniqueShapeContext<PointInT, PointOutT, PointRFT>::computePointDescriptor (std::size_t index, /*float rf[9],*/ std::vector<float> &desc)
{
pcl::Vector3fMapConst origin = (*input_)[(*indices_)[index]].getVector3fMap ();
const Eigen::Vector3f x_axis ((*frames_)[index].x_axis[0],
(*frames_)[index].x_axis[1],
(*frames_)[index].x_axis[2]);
//const Eigen::Vector3f& y_axis = (*frames_)[index].y_axis.getNormalVector3fMap ();
const Eigen::Vector3f normal ((*frames_)[index].z_axis[0],
(*frames_)[index].z_axis[1],
(*frames_)[index].z_axis[2]);
// Find every point within specified search_radius_
pcl::Indices nn_indices;
std::vector<float> nn_dists;
const std::size_t neighb_cnt = searchForNeighbors ((*indices_)[index], search_radius_, nn_indices, nn_dists);
// For each point within radius
for (std::size_t ne = 0; ne < neighb_cnt; ne++)
{
if (pcl::utils::equal(nn_dists[ne], 0.0f))
continue;
// Get neighbours coordinates
Eigen::Vector3f neighbour = (*surface_)[nn_indices[ne]].getVector3fMap ();
// ----- Compute current neighbour polar coordinates -----
// Get distance between the neighbour and the origin
float r = std::sqrt (nn_dists[ne]);
// Project point into the tangent plane
Eigen::Vector3f proj;
pcl::geometry::project (neighbour, origin, normal, proj);
proj -= origin;
// Normalize to compute the dot product
proj.normalize ();
// Compute the angle between the projection and the x axis in the interval [0,360]
Eigen::Vector3f cross = x_axis.cross (proj);
float phi = rad2deg (std::atan2 (cross.norm (), x_axis.dot (proj)));
phi = cross.dot (normal) < 0.f ? (360.0f - phi) : phi;
/// Compute the angle between the neighbour and the z axis (normal) in the interval [0, 180]
Eigen::Vector3f no = neighbour - origin;
no.normalize ();
float theta = normal.dot (no);
theta = pcl::rad2deg (std::acos (std::min (1.0f, std::max (-1.0f, theta))));
/// Compute the Bin(j, k, l) coordinates of current neighbour
const auto rad_min = std::lower_bound(std::next (radii_interval_.cbegin ()), radii_interval_.cend (), r);
const auto theta_min = std::lower_bound(std::next (theta_divisions_.cbegin ()), theta_divisions_.cend (), theta);
const auto phi_min = std::lower_bound(std::next (phi_divisions_.cbegin ()), phi_divisions_.cend (), phi);
/// Bin (j, k, l)
const auto j = std::distance(radii_interval_.cbegin (), std::prev(rad_min));
const auto k = std::distance(theta_divisions_.cbegin (), std::prev(theta_min));
const auto l = std::distance(phi_divisions_.cbegin (), std::prev(phi_min));
/// Local point density = number of points in a sphere of radius "point_density_radius_" around the current neighbour
pcl::Indices neighbour_indices;
std::vector<float> neighbour_didtances;
float point_density = static_cast<float> (searchForNeighbors (*surface_, nn_indices[ne], point_density_radius_, neighbour_indices, neighbour_didtances));
/// point_density is always bigger than 0 because FindPointsWithinRadius returns at least the point itself
float w = (1.0f / point_density) * volume_lut_[(l*elevation_bins_*radius_bins_) +
(k*radius_bins_) +
j];
assert (w >= 0.0);
if (w == std::numeric_limits<float>::infinity ())
PCL_ERROR ("Shape Context Error INF!\n");
if (std::isnan(w))
PCL_ERROR ("Shape Context Error IND!\n");
/// Accumulate w into correspondent Bin(j,k,l)
desc[(l*elevation_bins_*radius_bins_) + (k*radius_bins_) + j] += w;
assert (desc[(l*elevation_bins_*radius_bins_) + (k*radius_bins_) + j] >= 0);
} // end for each neighbour
}
//////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointInT, typename PointOutT, typename PointRFT> void
pcl::UniqueShapeContext<PointInT, PointOutT, PointRFT>::computeFeature (PointCloudOut &output)
{
assert (descriptor_length_ == 1960);
output.is_dense = true;
for (std::size_t point_index = 0; point_index < indices_->size (); ++point_index)
{
//output[point_index].descriptor.resize (descriptor_length_);
// If the point is not finite, set the descriptor to NaN and continue
const PointRFT& current_frame = (*frames_)[point_index];
if (!isFinite ((*input_)[(*indices_)[point_index]]) ||
!std::isfinite (current_frame.x_axis[0]) ||
!std::isfinite (current_frame.y_axis[0]) ||
!std::isfinite (current_frame.z_axis[0]) )
{
std::fill_n (output[point_index].descriptor, descriptor_length_,
std::numeric_limits<float>::quiet_NaN ());
std::fill_n (output[point_index].rf, 9, 0);
output.is_dense = false;
continue;
}
for (int d = 0; d < 3; ++d)
{
output[point_index].rf[0 + d] = current_frame.x_axis[d];
output[point_index].rf[3 + d] = current_frame.y_axis[d];
output[point_index].rf[6 + d] = current_frame.z_axis[d];
}
std::vector<float> descriptor (descriptor_length_);
computePointDescriptor (point_index, descriptor);
std::copy (descriptor.cbegin (), descriptor.cend (), output[point_index].descriptor);
}
}
#define PCL_INSTANTIATE_UniqueShapeContext(T,OutT,RFT) template class PCL_EXPORTS pcl::UniqueShapeContext<T,OutT,RFT>;
angles.h
Define standard C methods to do angle calculations.
pcl::Feature< PointInT, pcl::UniqueShapeContext1960 >::getClassName
const std::string & getClassName() const
Definition feature.h:244
pcl::Feature< PointInT, pcl::UniqueShapeContext1960 >::searchForNeighbors
int searchForNeighbors(std::size_t index, double parameter, pcl::Indices &indices, std::vector< float > &distances) const
Definition feature.h:268
pcl::Feature::setSearchSurface
void setSearchSurface(const PointCloudInConstPtr &cloud)
Provide a pointer to a dataset to add additional information to estimate the features for every point...
Definition feature.h:146
pcl::Feature< PointInT, pcl::UniqueShapeContext1960 >::search_radius_
double search_radius_
Definition feature.h:237
pcl::Feature::initCompute
virtual bool initCompute()
This method should get called before starting the actual computation.
Definition feature.hpp:95
pcl::Feature::setRadiusSearch
void setRadiusSearch(double radius)
Set the sphere radius that is to be used for determining the nearest neighbors used for the feature e...
Definition feature.h:198
pcl::Feature< PointInT, pcl::UniqueShapeContext1960 >::surface_
PointCloudInConstPtr surface_
Definition feature.h:228
pcl::Feature< PointInT, pcl::UniqueShapeContext1960 >::fake_surface_
bool fake_surface_
Definition feature.h:255
pcl::FeatureWithLocalReferenceFrames< PointInT, pcl::ReferenceFrame >::frames_
PointCloudLRFConstPtr frames_
Definition feature.h:475
pcl::FeatureWithLocalReferenceFrames::initLocalReferenceFrames
virtual bool initLocalReferenceFrames(const std::size_t &indices_size, const LRFEstimationPtr &lrf_estimation=LRFEstimationPtr())
Definition feature.hpp:302
pcl::PCLBase< PointInT >::input_
PointCloudConstPtr input_
Definition pcl_base.h:147
pcl::PCLBase::setInputCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition pcl_base.hpp:65
pcl::PCLBase< PointInT >::indices_
IndicesPtr indices_
Definition pcl_base.h:150
pcl::PCLBase::setIndices
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition pcl_base.hpp:72
pcl::SHOTLocalReferenceFrameEstimation
SHOTLocalReferenceFrameEstimation estimates the Local Reference Frame used in the calculation of the ...
Definition shot_lrf.h:66
pcl::SHOTLocalReferenceFrameEstimation::Ptr
shared_ptr< SHOTLocalReferenceFrameEstimation< PointInT, PointOutT > > Ptr
Definition shot_lrf.h:68
pcl::UniqueShapeContext::descriptor_length_
std::size_t descriptor_length_
Descriptor length.
Definition usc.h:183
pcl::UniqueShapeContext::azimuth_bins_
std::size_t azimuth_bins_
Bins along the azimuth dimension.
Definition usc.h:168
pcl::UniqueShapeContext::elevation_bins_
std::size_t elevation_bins_
Bins along the elevation dimension.
Definition usc.h:171
pcl::UniqueShapeContext::point_density_radius_
double point_density_radius_
Point density radius.
Definition usc.h:180
pcl::UniqueShapeContext::theta_divisions_
std::vector< float > theta_divisions_
Theta divisions interval.
Definition usc.h:159
pcl::UniqueShapeContext::computeFeature
void computeFeature(PointCloudOut &output) override
The actual feature computation.
Definition usc.hpp:226
pcl::UniqueShapeContext::phi_divisions_
std::vector< float > phi_divisions_
Phi divisions interval.
Definition usc.h:162
pcl::UniqueShapeContext::local_radius_
double local_radius_
Radius to compute local RF.
Definition usc.h:186
pcl::UniqueShapeContext::initCompute
bool initCompute() override
Initialize computation by allocating all the intervals and the volume lookup table.
Definition usc.hpp:54
pcl::UniqueShapeContext::computePointDescriptor
void computePointDescriptor(std::size_t index, std::vector< float > &desc)
Compute 3D shape context feature descriptor.
Definition usc.hpp:146
pcl::UniqueShapeContext::PointCloudOut
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition usc.h:78
pcl::UniqueShapeContext::volume_lut_
std::vector< float > volume_lut_
Volumes look up table.
Definition usc.h:165
pcl::UniqueShapeContext::radii_interval_
std::vector< float > radii_interval_
values of the radii interval.
Definition usc.h:156
pcl::UniqueShapeContext::min_radius_
double min_radius_
Minimal radius value.
Definition usc.h:177
pcl::UniqueShapeContext::radius_bins_
std::size_t radius_bins_
Bins along the radius dimension.
Definition usc.h:174
geometry.h
Defines some geometrical functions and utility functions.
pcl::deg2rad
float deg2rad(float alpha)
Convert an angle from degrees to radians.
Definition angles.hpp:67
pcl::rad2deg
float rad2deg(float alpha)
Convert an angle from radians to degrees.
Definition angles.hpp:61
pcl::geometry::project
void project(const PointT &point, const PointT &plane_origin, const NormalT &plane_normal, PointT &projected)
Definition geometry.h:81
pcl::utils::equal
bool equal(T val1, T val2, T eps=std::numeric_limits< T >::min())
Check if val1 and val2 are equal to an epsilon extent.
Definition utils.h:55
pcl::Vector3fMapConst
const Eigen::Map< const Eigen::Vector3f > Vector3fMapConst
Definition point_types.hpp:200
pcl::isFinite
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition point_tests.h:55
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133