Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
intensity_gradient.hpp
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: intensity_gradient.hpp 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_
41 #define PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_
42 
44 
46 template <typename PointInT, typename PointNT, typename PointOutT, typename IntensitySelectorT> void
48  const pcl::PointCloud <PointInT> &cloud, const std::vector <int> &indices,
49  const Eigen::Vector3f &point, float mean_intensity, const Eigen::Vector3f &normal, Eigen::Vector3f &gradient)
50 {
51  if (indices.size () < 3)
52  {
53  gradient[0] = gradient[1] = gradient[2] = std::numeric_limits<float>::quiet_NaN ();
54  return;
55  }
56 
57  Eigen::Matrix3f A = Eigen::Matrix3f::Zero ();
58  Eigen::Vector3f b = Eigen::Vector3f::Zero ();
59 
60  for (size_t i_point = 0; i_point < indices.size (); ++i_point)
61  {
62  PointInT p = cloud.points[indices[i_point]];
63  if (!pcl_isfinite (p.x) ||
64  !pcl_isfinite (p.y) ||
65  !pcl_isfinite (p.z) ||
66  !pcl_isfinite (intensity_ (p)))
67  continue;
68 
69  p.x -= point[0];
70  p.y -= point[1];
71  p.z -= point[2];
72  intensity_.demean (p, mean_intensity);
73 
74  A (0, 0) += p.x * p.x;
75  A (0, 1) += p.x * p.y;
76  A (0, 2) += p.x * p.z;
77 
78  A (1, 1) += p.y * p.y;
79  A (1, 2) += p.y * p.z;
80 
81  A (2, 2) += p.z * p.z;
82 
83  b[0] += p.x * intensity_ (p);
84  b[1] += p.y * intensity_ (p);
85  b[2] += p.z * intensity_ (p);
86  }
87  // Fill in the lower triangle of A
88  A (1, 0) = A (0, 1);
89  A (2, 0) = A (0, 2);
90  A (2, 1) = A (1, 2);
91 
92 //*
93  Eigen::Vector3f x = A.colPivHouseholderQr ().solve (b);
94 /*/
95 
96  Eigen::Vector3f eigen_values;
97  Eigen::Matrix3f eigen_vectors;
98  eigen33 (A, eigen_vectors, eigen_values);
99 
100  b = eigen_vectors.transpose () * b;
101 
102  if ( eigen_values (0) != 0)
103  b (0) /= eigen_values (0);
104  else
105  b (0) = 0;
106 
107  if ( eigen_values (1) != 0)
108  b (1) /= eigen_values (1);
109  else
110  b (1) = 0;
111 
112  if ( eigen_values (2) != 0)
113  b (2) /= eigen_values (2);
114  else
115  b (2) = 0;
116 
117 
118  Eigen::Vector3f x = eigen_vectors * b;
119 
120 // if (A.col (0).squaredNorm () != 0)
121 // x [0] /= A.col (0).squaredNorm ();
122 // b -= x [0] * A.col (0);
123 //
124 //
125 // if (A.col (1).squaredNorm () != 0)
126 // x [1] /= A.col (1).squaredNorm ();
127 // b -= x[1] * A.col (1);
128 //
129 // x [2] = b.dot (A.col (2));
130 // if (A.col (2).squaredNorm () != 0)
131 // x[2] /= A.col (2).squaredNorm ();
132  // Fit a hyperplane to the data
133 
134 //*/
135 // std::cout << A << "\n*\n" << bb << "\n=\n" << x << "\nvs.\n" << x2 << "\n\n";
136 // std::cout << A * x << "\nvs.\n" << A * x2 << "\n\n------\n";
137  // Project the gradient vector, x, onto the tangent plane
138  gradient = (Eigen::Matrix3f::Identity () - normal*normal.transpose ()) * x;
139 }
140 
142 template <typename PointInT, typename PointNT, typename PointOutT, typename IntensitySelectorT> void
144 {
145  // Allocate enough space to hold the results
146  // \note This resize is irrelevant for a radiusSearch ().
147  std::vector<int> nn_indices (k_);
148  std::vector<float> nn_dists (k_);
149  output.is_dense = true;
150 
151  // If the data is dense, we don't need to check for NaN
152  if (surface_->is_dense)
153  {
154 #if defined (HAVE_OPENMP) && (defined(_WIN32) || ((__GNUC__ > 4) && (__GNUC_MINOR__ > 2)))
155 #pragma omp parallel for shared (output) private (nn_indices, nn_dists) num_threads(threads_)
156 #endif
157  // Iterating over the entire index vector
158  for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
159  {
160  PointOutT &p_out = output.points[idx];
161 
162  if (!this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
163  {
164  p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
165  output.is_dense = false;
166  continue;
167  }
168 
169  Eigen::Vector3f centroid;
170  float mean_intensity = 0;
171  // Initialize to 0
172  centroid.setZero ();
173  for (size_t i = 0; i < nn_indices.size (); ++i)
174  {
175  centroid += surface_->points[nn_indices[i]].getVector3fMap ();
176  mean_intensity += intensity_ (surface_->points[nn_indices[i]]);
177  }
178  centroid /= static_cast<float> (nn_indices.size ());
179  mean_intensity /= static_cast<float> (nn_indices.size ());
180 
181  Eigen::Vector3f normal = Eigen::Vector3f::Map (normals_->points[(*indices_) [idx]].normal);
182  Eigen::Vector3f gradient;
183  computePointIntensityGradient (*surface_, nn_indices, centroid, mean_intensity, normal, gradient);
184 
185  p_out.gradient[0] = gradient[0];
186  p_out.gradient[1] = gradient[1];
187  p_out.gradient[2] = gradient[2];
188  }
189  }
190  else
191  {
192 #if defined (HAVE_OPENMP) && (defined(_WIN32) || ((__GNUC__ > 4) && (__GNUC_MINOR__ > 2)))
193 #pragma omp parallel for shared (output) private (nn_indices, nn_dists) num_threads(threads_)
194 #endif
195  // Iterating over the entire index vector
196  for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
197  {
198  PointOutT &p_out = output.points[idx];
199  if (!isFinite ((*surface_) [(*indices_)[idx]]) ||
200  !this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
201  {
202  p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
203  output.is_dense = false;
204  continue;
205  }
206  Eigen::Vector3f centroid;
207  float mean_intensity = 0;
208  // Initialize to 0
209  centroid.setZero ();
210  unsigned cp = 0;
211  for (size_t i = 0; i < nn_indices.size (); ++i)
212  {
213  // Check if the point is invalid
214  if (!isFinite ((*surface_) [nn_indices[i]]))
215  continue;
216 
217  centroid += surface_->points [nn_indices[i]].getVector3fMap ();
218  mean_intensity += intensity_ (surface_->points [nn_indices[i]]);
219  ++cp;
220  }
221  centroid /= static_cast<float> (cp);
222  mean_intensity /= static_cast<float> (cp);
223  Eigen::Vector3f normal = Eigen::Vector3f::Map (normals_->points[(*indices_) [idx]].normal);
224  Eigen::Vector3f gradient;
225  computePointIntensityGradient (*surface_, nn_indices, centroid, mean_intensity, normal, gradient);
226 
227  p_out.gradient[0] = gradient[0];
228  p_out.gradient[1] = gradient[1];
229  p_out.gradient[2] = gradient[2];
230  }
231  }
232 }
233 
235 template <typename PointInT, typename PointNT> void
237 {
238  // Resize the output dataset
239  output.points.resize (indices_->size (), 3);
240 
241  // Allocate enough space to hold the results
242  // \note This resize is irrelevant for a radiusSearch ().
243  std::vector<int> nn_indices (k_);
244  std::vector<float> nn_dists (k_);
245 
246  output.is_dense = true;
247  // Iterating over the entire index vector
248  for (size_t idx = 0; idx < indices_->size (); ++idx)
249  {
250  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
251  {
252  output.points.row (idx).setConstant (std::numeric_limits<float>::quiet_NaN ());
253  output.is_dense = false;
254  continue;
255  }
256 
257  Eigen::Vector4f centroid;
258  compute3DCentroid (*surface_, nn_indices, centroid);
259 
260  float mean_intensity = 0;
261  unsigned valid_neighbor_count = 0;
262  for (size_t nIdx = 0; nIdx < nn_indices.size (); ++nIdx)
263  {
264  const PointInT& p = (*surface_)[nn_indices[nIdx]];
265  if (!pcl_isfinite (p.intensity))
266  continue;
267 
268  mean_intensity += p.intensity;
269  ++valid_neighbor_count;
270  }
271 
272  mean_intensity /= static_cast<float> (valid_neighbor_count);
273 
274  Eigen::Vector3f normal = Eigen::Vector3f::Map (normals_->points[idx].normal);
275  Eigen::Vector3f gradient;
276  this->computePointIntensityGradient (*surface_, nn_indices, centroid.head<3> (), mean_intensity, normal, gradient);
277 
278  output.points (idx, 0) = gradient[0];
279  output.points (idx, 1) = gradient[1];
280  output.points (idx, 2) = gradient[2];
281  }
282 }
283 
284 
285 #define PCL_INSTANTIATE_IntensityGradientEstimation(InT,NT,OutT) template class PCL_EXPORTS pcl::IntensityGradientEstimation<InT,NT,OutT>;
286 
287 #endif // PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_