Point Cloud Library (PCL)  1.10.0-dev
pfhrgb.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2011, Alexandru-Eugen Ichim
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 #ifndef PCL_FEATURES_IMPL_PFHRGB_H_
41 #define PCL_FEATURES_IMPL_PFHRGB_H_
42 
43 #include <pcl/features/pfhrgb.h>
44 
45 //////////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointInT, typename PointNT, typename PointOutT> bool
48  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
49  int p_idx, int q_idx,
50  float &f1, float &f2, float &f3, float &f4, float &f5, float &f6, float &f7)
51 {
52  Eigen::Vector4i colors1 (cloud.points[p_idx].r, cloud.points[p_idx].g, cloud.points[p_idx].b, 0),
53  colors2 (cloud.points[q_idx].r, cloud.points[q_idx].g, cloud.points[q_idx].b, 0);
54  pcl::computeRGBPairFeatures (cloud.points[p_idx].getVector4fMap (), normals.points[p_idx].getNormalVector4fMap (),
55  colors1,
56  cloud.points[q_idx].getVector4fMap (), normals.points[q_idx].getNormalVector4fMap (),
57  colors2,
58  f1, f2, f3, f4, f5, f6, f7);
59  return (true);
60 }
61 
62 //////////////////////////////////////////////////////////////////////////////////////////////
63 template <typename PointInT, typename PointNT, typename PointOutT> void
65  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
66  const std::vector<int> &indices, int nr_split, Eigen::VectorXf &pfhrgb_histogram)
67 {
68  int h_index, h_p;
69 
70  // Clear the resultant point histogram
71  pfhrgb_histogram.setZero ();
72 
73  // Factorization constant
74  float hist_incr = 100.0f / static_cast<float> (indices.size () * (indices.size () - 1) / 2);
75 
76  // Iterate over all the points in the neighborhood
77  for (const auto& index_i: indices)
78  {
79  for (const auto& index_j: indices)
80  {
81  // Avoid unnecessary returns
82  if (index_i == index_j)
83  continue;
84 
85  // Compute the pair NNi to NNj
86  if (!computeRGBPairFeatures (cloud, normals, index_i, index_j,
87  pfhrgb_tuple_[0], pfhrgb_tuple_[1], pfhrgb_tuple_[2], pfhrgb_tuple_[3],
88  pfhrgb_tuple_[4], pfhrgb_tuple_[5], pfhrgb_tuple_[6]))
89  continue;
90 
91  // Normalize the f1, f2, f3, f5, f6, f7 features and push them in the histogram
92  f_index_[0] = static_cast<int> (std::floor (nr_split * ((pfhrgb_tuple_[0] + M_PI) * d_pi_)));
93  // @TODO: confirm "not to do for i == 3"
94  for (int i = 1; i < 3; ++i)
95  {
96  const float feature_value = nr_split * ((pfhrgb_tuple_[i] + 1.0) * 0.5);
97  f_index_[i] = static_cast<int> (std::floor (feature_value));
98  }
99  // color ratios are in [-1, 1]
100  for (int i = 4; i < 7; ++i)
101  {
102  const float feature_value = nr_split * ((pfhrgb_tuple_[i] + 1.0) * 0.5);
103  f_index_[i] = static_cast<int> (std::floor (feature_value));
104  }
105  for (auto& feature: f_index_)
106  {
107  feature = std::min(nr_split - 1, std::max(0, feature));
108  }
109 
110  // Copy into the histogram
111  h_index = 0;
112  h_p = 1;
113  for (int d = 0; d < 3; ++d)
114  {
115  h_index += h_p * f_index_[d];
116  h_p *= nr_split;
117  }
118  pfhrgb_histogram[h_index] += hist_incr;
119 
120  // and the colors
121  h_index = 125;
122  h_p = 1;
123  for (int d = 4; d < 7; ++d)
124  {
125  h_index += h_p * f_index_[d];
126  h_p *= nr_split;
127  }
128  pfhrgb_histogram[h_index] += hist_incr;
129  }
130  }
131 }
132 
133 //////////////////////////////////////////////////////////////////////////////////////////////
134 template <typename PointInT, typename PointNT, typename PointOutT> void
136 {
137  /// nr_subdiv^3 for RGB and nr_subdiv^3 for the angular features
138  pfhrgb_histogram_.setZero (2 * nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
139  pfhrgb_tuple_.setZero (7);
140 
141  // Allocate enough space to hold the results
142  // \note This resize is irrelevant for a radiusSearch ().
143  std::vector<int> nn_indices (k_);
144  std::vector<float> nn_dists (k_);
145 
146  // Iterating over the entire index vector
147  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
148  {
149  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists);
150 
151  // Estimate the PFH signature at each patch
152  computePointPFHRGBSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfhrgb_histogram_);
153 
154  std::copy_n (pfhrgb_histogram_.data (), pfhrgb_histogram_.size (),
155  output.points[idx].histogram);
156  }
157 }
158 
159 #define PCL_INSTANTIATE_PFHRGBEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PFHRGBEstimation<T,NT,OutT>;
160 
161 #endif /* PCL_FEATURES_IMPL_PFHRGB_H_ */
void computeFeature(PointCloudOut &output) override
Abstract feature estimation method.
Definition: pfhrgb.hpp:135
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:397
PCL_EXPORTS bool computeRGBPairFeatures(const Eigen::Vector4f &p1, const Eigen::Vector4f &n1, const Eigen::Vector4i &colors1, const Eigen::Vector4f &p2, const Eigen::Vector4f &n2, const Eigen::Vector4i &colors2, float &f1, float &f2, float &f3, float &f4, float &f5, float &f6, float &f7)
void computePointPFHRGBSignature(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, const std::vector< int > &indices, int nr_split, Eigen::VectorXf &pfhrgb_histogram)
Definition: pfhrgb.hpp:64
bool computeRGBPairFeatures(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4, float &f5, float &f6, float &f7)
Definition: pfhrgb.hpp:47