Point Cloud Library (PCL)  1.7.0
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);
75 
76  // Iterate over all the points in the neighborhood
77  for (size_t i_idx = 0; i_idx < indices.size (); ++i_idx)
78  {
79  for (size_t j_idx = 0; j_idx < indices.size (); ++j_idx)
80  {
81  // Avoid unnecessary returns
82  if (i_idx == j_idx)
83  continue;
84 
85  // Compute the pair NNi to NNj
86  if (!computeRGBPairFeatures (cloud, normals, indices[i_idx], indices[j_idx],
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> (floor (nr_split * ((pfhrgb_tuple_[0] + M_PI) * d_pi_)));
93  if (f_index_[0] < 0) f_index_[0] = 0;
94  if (f_index_[0] >= nr_split) f_index_[0] = nr_split - 1;
95 
96  f_index_[1] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[1] + 1.0) * 0.5)));
97  if (f_index_[1] < 0) f_index_[1] = 0;
98  if (f_index_[1] >= nr_split) f_index_[1] = nr_split - 1;
99 
100  f_index_[2] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[2] + 1.0) * 0.5)));
101  if (f_index_[2] < 0) f_index_[2] = 0;
102  if (f_index_[2] >= nr_split) f_index_[2] = nr_split - 1;
103 
104  // color ratios are in [-1, 1]
105  f_index_[4] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[4] + 1.0) * 0.5)));
106  if (f_index_[4] < 0) f_index_[4] = 0;
107  if (f_index_[4] >= nr_split) f_index_[4] = nr_split - 1;
108 
109  f_index_[5] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[5] + 1.0) * 0.5)));
110  if (f_index_[5] < 0) f_index_[5] = 0;
111  if (f_index_[5] >= nr_split) f_index_[5] = nr_split - 1;
112 
113  f_index_[6] = static_cast<int> (floor (nr_split * ((pfhrgb_tuple_[6] + 1.0) * 0.5)));
114  if (f_index_[6] < 0) f_index_[6] = 0;
115  if (f_index_[6] >= nr_split) f_index_[6] = nr_split - 1;
116 
117 
118  // Copy into the histogram
119  h_index = 0;
120  h_p = 1;
121  for (int d = 0; d < 3; ++d)
122  {
123  h_index += h_p * f_index_[d];
124  h_p *= nr_split;
125  }
126  pfhrgb_histogram[h_index] += hist_incr;
127 
128  // and the colors
129  h_index = 125;
130  h_p = 1;
131  for (int d = 4; d < 7; ++d)
132  {
133  h_index += h_p * f_index_[d];
134  h_p *= nr_split;
135  }
136  pfhrgb_histogram[h_index] += hist_incr;
137  }
138  }
139 }
140 
141 //////////////////////////////////////////////////////////////////////////////////////////////
142 template <typename PointInT, typename PointNT, typename PointOutT> void
144 {
145  /// nr_subdiv^3 for RGB and nr_subdiv^3 for the angular features
146  pfhrgb_histogram_.setZero (2 * nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
147  pfhrgb_tuple_.setZero (7);
148 
149  // Allocate enough space to hold the results
150  // \note This resize is irrelevant for a radiusSearch ().
151  std::vector<int> nn_indices (k_);
152  std::vector<float> nn_dists (k_);
153 
154  // Iterating over the entire index vector
155  for (size_t idx = 0; idx < indices_->size (); ++idx)
156  {
157  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists);
158 
159  // Estimate the PFH signature at each patch
160  computePointPFHRGBSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfhrgb_histogram_);
161 
162  // Copy into the resultant cloud
163  for (int d = 0; d < pfhrgb_histogram_.size (); ++d) {
164  output.points[idx].histogram[d] = pfhrgb_histogram_[d];
165 // PCL_INFO ("%f ", pfhrgb_histogram_[d]);
166  }
167 // PCL_INFO ("\n");
168  }
169 }
170 
171 #define PCL_INSTANTIATE_PFHRGBEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PFHRGBEstimation<T,NT,OutT>;
172 
173 #endif /* PCL_FEATURES_IMPL_PFHRGB_H_ */