Point Cloud Library (PCL)  1.15.1-dev
rift.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
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
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_FEATURES_IMPL_RIFT_H_
42 #define PCL_FEATURES_IMPL_RIFT_H_
43 
44 #include <pcl/features/rift.h>
45 
46 #include <Eigen/Core> // for Eigen::Map<const Eigen::Vector3f>
47 
48 //////////////////////////////////////////////////////////////////////////////////////////////
49 template <typename PointInT, typename GradientT, typename PointOutT> void
51  const PointCloudIn &cloud, const PointCloudGradient &gradient,
52  int p_idx, float radius, const pcl::Indices &indices,
53  const std::vector<float> &sqr_distances, Eigen::MatrixXf &rift_descriptor)
54 {
55  if (indices.empty ())
56  {
57  PCL_ERROR ("[pcl::RIFTEstimation] Null indices points passed!\n");
58  return;
59  }
60 
61  // Determine the number of bins to use based on the size of rift_descriptor
62  int nr_distance_bins = static_cast<int> (rift_descriptor.rows ());
63  int nr_gradient_bins = static_cast<int> (rift_descriptor.cols ());
64 
65  // Get the center point
66  const Eigen::Map<const Eigen::Vector3f> p0 = cloud[p_idx].getVector3fMap ();
67 
68  // Compute the RIFT descriptor
69  rift_descriptor.setZero ();
70  for (std::size_t idx = 0; idx < indices.size (); ++idx)
71  {
72  // Compute the gradient magnitude and orientation (relative to the center point)
73  const Eigen::Map<const Eigen::Vector3f> point = cloud[indices[idx]].getVector3fMap ();
74  Eigen::Map<const Eigen::Vector3f> gradient_vector (& (gradient[indices[idx]].gradient[0]));
75 
76  float gradient_magnitude = gradient_vector.norm ();
77  float gradient_angle_from_center = std::acos (gradient_vector.dot ((point - p0).normalized ()) / gradient_magnitude);
78  if (!std::isfinite (gradient_angle_from_center))
79  gradient_angle_from_center = 0.0;
80 
81  // Normalize distance and angle values to: 0.0 <= d,g < nr_distances_bins,nr_gradient_bins
82  const float eps = std::numeric_limits<float>::epsilon ();
83  float d = static_cast<float> (nr_distance_bins) * std::sqrt (sqr_distances[idx]) / (radius + eps);
84  float g = static_cast<float> (nr_gradient_bins) * gradient_angle_from_center / (static_cast<float> (M_PI) + eps);
85 
86  // Compute the bin indices that need to be updated
87  int d_idx_min = (std::max)(static_cast<int> (std::ceil (d - 1)), 0);
88  int d_idx_max = (std::min)(static_cast<int> (std::floor (d + 1)), nr_distance_bins - 1);
89  int g_idx_min = static_cast<int> (std::ceil (g - 1));
90  int g_idx_max = static_cast<int> (std::floor (g + 1));
91 
92  // Update the appropriate bins of the histogram
93  for (int g_idx = g_idx_min; g_idx <= g_idx_max; ++g_idx)
94  {
95  // Because gradient orientation is cyclical, out-of-bounds values must wrap around
96  int g_idx_wrapped = ((g_idx + nr_gradient_bins) % nr_gradient_bins);
97 
98  for (int d_idx = d_idx_min; d_idx <= d_idx_max; ++d_idx)
99  {
100  // To avoid boundary effects, use linear interpolation when updating each bin
101  float w = (1.0f - std::abs (d - static_cast<float> (d_idx))) * (1.0f - std::abs (g - static_cast<float> (g_idx)));
102 
103  rift_descriptor (d_idx, g_idx_wrapped) += w * gradient_magnitude;
104  }
105  }
106  }
107 
108  // Normalize the RIFT descriptor to unit magnitude
109  rift_descriptor.normalize ();
110 }
111 
112 
113 //////////////////////////////////////////////////////////////////////////////////////////////
114 template <typename PointInT, typename GradientT, typename PointOutT> void
116 {
117  // Make sure a search radius is set
118  if (search_radius_ == 0.0)
119  {
120  PCL_ERROR ("[pcl::%s::computeFeature] The search radius must be set before computing the feature!\n",
121  getClassName ().c_str ());
122  output.width = output.height = 0;
123  output.clear ();
124  return;
125  }
126 
127  // Make sure the RIFT descriptor has valid dimensions
128  if (nr_gradient_bins_ <= 0)
129  {
130  PCL_ERROR ("[pcl::%s::computeFeature] The number of gradient bins must be greater than zero!\n",
131  getClassName ().c_str ());
132  output.width = output.height = 0;
133  output.clear ();
134  return;
135  }
136  if (nr_distance_bins_ <= 0)
137  {
138  PCL_ERROR ("[pcl::%s::computeFeature] The number of distance bins must be greater than zero!\n",
139  getClassName ().c_str ());
140  output.width = output.height = 0;
141  output.clear ();
142  return;
143  }
144 
145  // Check for valid input gradient
146  if (!gradient_)
147  {
148  PCL_ERROR ("[pcl::%s::computeFeature] No input gradient was given!\n", getClassName ().c_str ());
149  output.width = output.height = 0;
150  output.clear ();
151  return;
152  }
153  if (gradient_->size () != surface_->size ())
154  {
155  PCL_ERROR ("[pcl::%s::computeFeature] ", getClassName ().c_str ());
156  PCL_ERROR ("The number of points in the input dataset differs from the number of points in the gradient!\n");
157  output.width = output.height = 0;
158  output.clear ();
159  return;
160  }
161 
162  Eigen::MatrixXf rift_descriptor (nr_distance_bins_, nr_gradient_bins_);
163  pcl::Indices nn_indices;
164  std::vector<float> nn_dist_sqr;
165 
166  // Iterating over the entire index vector
167  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
168  {
169  // Find neighbors within the search radius
170  tree_->radiusSearch ((*indices_)[idx], search_radius_, nn_indices, nn_dist_sqr);
171 
172  // Compute the RIFT descriptor
173  computeRIFT (*surface_, *gradient_, (*indices_)[idx], static_cast<float> (search_radius_), nn_indices, nn_dist_sqr, rift_descriptor);
174 
175  // Default layout is column major, copy elementwise
176  std::copy (rift_descriptor.data (), rift_descriptor.data () + rift_descriptor.size (), output[idx].histogram);
177  }
178 }
179 
180 #define PCL_INSTANTIATE_RIFTEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::RIFTEstimation<T,NT,OutT>;
181 
182 #endif // PCL_FEATURES_IMPL_RIFT_H_
183 
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:399
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:401
void clear()
Removes all points in a cloud and sets the width and height to 0.
Definition: point_cloud.h:886
void computeFeature(PointCloudOut &output) override
Estimate the Rotation Invariant Feature Transform (RIFT) descriptors at a set of points given by <set...
Definition: rift.hpp:115
void computeRIFT(const PointCloudIn &cloud, const PointCloudGradient &gradient, int p_idx, float radius, const pcl::Indices &indices, const std::vector< float > &squared_distances, Eigen::MatrixXf &rift_descriptor)
Estimate the Rotation Invariant Feature Transform (RIFT) descriptor for a given point based on its sp...
Definition: rift.hpp:50
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define M_PI
Definition: pcl_macros.h:201