Point Cloud Library (PCL)  1.14.0-dev
intensity_spin.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_INTENSITY_SPIN_H_
42 #define PCL_FEATURES_IMPL_INTENSITY_SPIN_H_
43 
44 #include <pcl/features/intensity_spin.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename PointOutT> void
49  const PointCloudIn &cloud, float radius, float sigma,
50  int k,
51  const pcl::Indices &indices,
52  const std::vector<float> &squared_distances,
53  Eigen::MatrixXf &intensity_spin_image)
54 {
55  // Determine the number of bins to use based on the size of intensity_spin_image
56  int nr_distance_bins = static_cast<int> (intensity_spin_image.cols ());
57  int nr_intensity_bins = static_cast<int> (intensity_spin_image.rows ());
58 
59  // Find the min and max intensity values in the given neighborhood
60  float min_intensity = std::numeric_limits<float>::max ();
61  float max_intensity = -std::numeric_limits<float>::max ();
62  for (int idx = 0; idx < k; ++idx)
63  {
64  min_intensity = (std::min) (min_intensity, cloud[indices[idx]].intensity);
65  max_intensity = (std::max) (max_intensity, cloud[indices[idx]].intensity);
66  }
67 
68  float constant = 1.0f / (2.0f * sigma_ * sigma_);
69  // Compute the intensity spin image
70  intensity_spin_image.setZero ();
71  for (int idx = 0; idx < k; ++idx)
72  {
73  // Normalize distance and intensity values to: 0.0 <= d,i < nr_distance_bins,nr_intensity_bins
74  const float eps = std::numeric_limits<float>::epsilon ();
75  float d = static_cast<float> (nr_distance_bins) * std::sqrt (squared_distances[idx]) / (radius + eps);
76  float i = static_cast<float> (nr_intensity_bins) *
77  (cloud[indices[idx]].intensity - min_intensity) / (max_intensity - min_intensity + eps);
78 
79  if (sigma == 0)
80  {
81  // If sigma is zero, update the histogram with no smoothing kernel
82  int d_idx = static_cast<int> (d);
83  int i_idx = static_cast<int> (i);
84  intensity_spin_image (i_idx, d_idx) += 1;
85  }
86  else
87  {
88  // Compute the bin indices that need to be updated (+/- 3 standard deviations)
89  int d_idx_min = (std::max)(static_cast<int> (std::floor (d - 3*sigma)), 0);
90  int d_idx_max = (std::min)(static_cast<int> (std::ceil (d + 3*sigma)), nr_distance_bins - 1);
91  int i_idx_min = (std::max)(static_cast<int> (std::floor (i - 3*sigma)), 0);
92  int i_idx_max = (std::min)(static_cast<int> (std::ceil (i + 3*sigma)), nr_intensity_bins - 1);
93 
94  // Update the appropriate bins of the histogram
95  for (int i_idx = i_idx_min; i_idx <= i_idx_max; ++i_idx)
96  {
97  for (int d_idx = d_idx_min; d_idx <= d_idx_max; ++d_idx)
98  {
99  // Compute a "soft" update weight based on the distance between the point and the bin
100  float w = std::exp (-powf (d - static_cast<float> (d_idx), 2.0f) * constant - powf (i - static_cast<float> (i_idx), 2.0f) * constant);
101  intensity_spin_image (i_idx, d_idx) += w;
102  }
103  }
104  }
105  }
106 }
107 
108 //////////////////////////////////////////////////////////////////////////////////////////////
109 template <typename PointInT, typename PointOutT> void
111 {
112  // Make sure a search radius is set
113  if (search_radius_ == 0.0)
114  {
115  PCL_ERROR ("[pcl::%s::computeFeature] The search radius must be set before computing the feature!\n",
116  getClassName ().c_str ());
117  output.width = output.height = 0;
118  output.clear ();
119  return;
120  }
121 
122  // Make sure the spin image has valid dimensions
123  if (nr_intensity_bins_ <= 0)
124  {
125  PCL_ERROR ("[pcl::%s::computeFeature] The number of intensity bins must be greater than zero!\n",
126  getClassName ().c_str ());
127  output.width = output.height = 0;
128  output.clear ();
129  return;
130  }
131  if (nr_distance_bins_ <= 0)
132  {
133  PCL_ERROR ("[pcl::%s::computeFeature] The number of distance bins must be greater than zero!\n",
134  getClassName ().c_str ());
135  output.width = output.height = 0;
136  output.clear ();
137  return;
138  }
139 
140  Eigen::MatrixXf intensity_spin_image (nr_intensity_bins_, nr_distance_bins_);
141  // Allocate enough space to hold the radiusSearch results
142  pcl::Indices nn_indices (surface_->size ());
143  std::vector<float> nn_dist_sqr (surface_->size ());
144 
145  output.is_dense = true;
146  // Iterating over the entire index vector
147  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
148  {
149  // Find neighbors within the search radius
150  // TODO: do we want to use searchForNeighbors instead?
151  int k = tree_->radiusSearch ((*indices_)[idx], search_radius_, nn_indices, nn_dist_sqr);
152  if (k == 0)
153  {
154  for (int bin = 0; bin < nr_intensity_bins_ * nr_distance_bins_; ++bin)
155  output[idx].histogram[bin] = std::numeric_limits<float>::quiet_NaN ();
156  output.is_dense = false;
157  continue;
158  }
159 
160  // Compute the intensity spin image
161  computeIntensitySpinImage (*surface_, static_cast<float> (search_radius_), sigma_, k, nn_indices, nn_dist_sqr, intensity_spin_image);
162 
163  // Copy into the resultant cloud
164  std::size_t bin = 0;
165  for (Eigen::Index bin_j = 0; bin_j < intensity_spin_image.cols (); ++bin_j)
166  for (Eigen::Index bin_i = 0; bin_i < intensity_spin_image.rows (); ++bin_i)
167  output[idx].histogram[bin++] = intensity_spin_image (bin_i, bin_j);
168  }
169 }
170 
171 #define PCL_INSTANTIATE_IntensitySpinEstimation(T,NT) template class PCL_EXPORTS pcl::IntensitySpinEstimation<T,NT>;
172 
173 #endif // PCL_FEATURES_IMPL_INTENSITY_SPIN_H_
174 
void computeFeature(PointCloudOut &output) override
Estimate the intensity-domain descriptors at a set of points given by <setInputCloud (),...
void computeIntensitySpinImage(const PointCloudIn &cloud, float radius, float sigma, int k, const pcl::Indices &indices, const std::vector< float > &squared_distances, Eigen::MatrixXf &intensity_spin_image)
Estimate the intensity-domain spin image descriptor for a given point based on its spatial neighborho...
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
void clear()
Removes all points in a cloud and sets the width and height to 0.
Definition: point_cloud.h:885
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133