Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pfhrgb.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Alexandru-Eugen Ichim
5  * Willow Garage, Inc
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id: pfhrgb.hpp 5026 2012-03-12 02:51:44Z rusu $
36  */
37 
38 #ifndef PCL_FEATURES_IMPL_PFHRGB_H_
39 #define PCL_FEATURES_IMPL_PFHRGB_H_
40 
41 #include <pcl/features/pfhrgb.h>
42 
44 template <typename PointInT, typename PointNT, typename PointOutT> bool
46  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
47  int p_idx, int q_idx,
48  float &f1, float &f2, float &f3, float &f4, float &f5, float &f6, float &f7)
49 {
50  Eigen::Vector4i colors1 (cloud.points[p_idx].r, cloud.points[p_idx].g, cloud.points[p_idx].b, 0),
51  colors2 (cloud.points[q_idx].r, cloud.points[q_idx].g, cloud.points[q_idx].b, 0);
52  pcl::computeRGBPairFeatures (cloud.points[p_idx].getVector4fMap (), normals.points[p_idx].getNormalVector4fMap (),
53  colors1,
54  cloud.points[q_idx].getVector4fMap (), normals.points[q_idx].getNormalVector4fMap (),
55  colors2,
56  f1, f2, f3, f4, f5, f6, f7);
57  return (true);
58 }
59 
61 template <typename PointInT, typename PointNT, typename PointOutT> void
63  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
64  const std::vector<int> &indices, int nr_split, Eigen::VectorXf &pfhrgb_histogram)
65 {
66  int h_index, h_p;
67 
68  // Clear the resultant point histogram
69  pfhrgb_histogram.setZero ();
70 
71  // Factorization constant
72  float hist_incr = 100.0f / static_cast<float> (indices.size () * indices.size () - 1);
73 
74  // Iterate over all the points in the neighborhood
75  for (size_t i_idx = 0; i_idx < indices.size (); ++i_idx)
76  {
77  for (size_t j_idx = 0; j_idx < indices.size (); ++j_idx)
78  {
79  // Avoid unnecessary returns
80  if (i_idx == j_idx)
81  continue;
82 
83  // Compute the pair NNi to NNj
84  if (!computeRGBPairFeatures (cloud, normals, indices[i_idx], indices[j_idx],
85  pfhrgb_tuple_[0], pfhrgb_tuple_[1], pfhrgb_tuple_[2], pfhrgb_tuple_[3],
86  pfhrgb_tuple_[4], pfhrgb_tuple_[5], pfhrgb_tuple_[6]))
87  continue;
88 
89  // Normalize the f1, f2, f3, f5, f6, f7 features and push them in the histogram
90  f_index_[0] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[0] + M_PI) * d_pi_)));
91  if (f_index_[0] < 0) f_index_[0] = 0;
92  if (f_index_[0] >= nr_split) f_index_[0] = nr_split - 1;
93 
94  f_index_[1] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[1] + 1.0) * 0.5)));
95  if (f_index_[1] < 0) f_index_[1] = 0;
96  if (f_index_[1] >= nr_split) f_index_[1] = nr_split - 1;
97 
98  f_index_[2] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[2] + 1.0) * 0.5)));
99  if (f_index_[2] < 0) f_index_[2] = 0;
100  if (f_index_[2] >= nr_split) f_index_[2] = nr_split - 1;
101 
102  // color ratios are in [-1, 1]
103  f_index_[4] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[4] + 1.0) * 0.5)));
104  if (f_index_[4] < 0) f_index_[4] = 0;
105  if (f_index_[4] >= nr_split) f_index_[4] = nr_split - 1;
106 
107  f_index_[5] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[5] + 1.0) * 0.5)));
108  if (f_index_[5] < 0) f_index_[5] = 0;
109  if (f_index_[5] >= nr_split) f_index_[5] = nr_split - 1;
110 
111  f_index_[6] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[6] + 1.0) * 0.5)));
112  if (f_index_[6] < 0) f_index_[6] = 0;
113  if (f_index_[6] >= nr_split) f_index_[6] = nr_split - 1;
114 
115 
116  // Copy into the histogram
117  h_index = 0;
118  h_p = 1;
119  for (int d = 0; d < 3; ++d)
120  {
121  h_index += h_p * f_index_[d];
122  h_p *= nr_split;
123  }
124  pfhrgb_histogram[h_index] += hist_incr;
125 
126  // and the colors
127  h_index = 125;
128  h_p = 1;
129  for (int d = 4; d < 7; ++d)
130  {
131  h_index += h_p * f_index_[d];
132  h_p *= nr_split;
133  }
134  pfhrgb_histogram[h_index] += hist_incr;
135  }
136  }
137 }
138 
140 template <typename PointInT, typename PointNT, typename PointOutT> void
142 {
144  pfhrgb_histogram_.setZero (2 * nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
145  pfhrgb_tuple_.setZero (7);
146 
147  // Allocate enough space to hold the results
148  // \note This resize is irrelevant for a radiusSearch ().
149  std::vector<int> nn_indices (k_);
150  std::vector<float> nn_dists (k_);
151 
152  // Iterating over the entire index vector
153  for (size_t idx = 0; idx < indices_->size (); ++idx)
154  {
155  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists);
156 
157  // Estimate the PFH signature at each patch
158  computePointPFHRGBSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfhrgb_histogram_);
159 
160  // Copy into the resultant cloud
161  for (int d = 0; d < pfhrgb_histogram_.size (); ++d) {
162  output.points[idx].histogram[d] = pfhrgb_histogram_[d];
163 // PCL_INFO ("%f ", pfhrgb_histogram_[d]);
164  }
165 // PCL_INFO ("\n");
166  }
167 }
168 
169 #define PCL_INSTANTIATE_PFHRGBEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PFHRGBEstimation<T,NT,OutT>;
170 
171 #endif /* PCL_FEATURES_IMPL_PFHRGB_H_ */