Point Cloud Library (PCL)  1.9.1-dev
intensity_gradient.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_GRADIENT_H_
42 #define PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_
43 
44 #include <pcl/features/intensity_gradient.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename PointNT, typename PointOutT, typename IntensitySelectorT> void
49  const pcl::PointCloud <PointInT> &cloud, const std::vector <int> &indices,
50  const Eigen::Vector3f &point, float mean_intensity, const Eigen::Vector3f &normal, Eigen::Vector3f &gradient)
51 {
52  if (indices.size () < 3)
53  {
54  gradient[0] = gradient[1] = gradient[2] = std::numeric_limits<float>::quiet_NaN ();
55  return;
56  }
57 
58  Eigen::Matrix3f A = Eigen::Matrix3f::Zero ();
59  Eigen::Vector3f b = Eigen::Vector3f::Zero ();
60 
61  for (const int &nn_index : indices)
62  {
63  PointInT p = cloud.points[nn_index];
64  if (!std::isfinite (p.x) ||
65  !std::isfinite (p.y) ||
66  !std::isfinite (p.z) ||
67  !std::isfinite (intensity_ (p)))
68  continue;
69 
70  p.x -= point[0];
71  p.y -= point[1];
72  p.z -= point[2];
73  intensity_.demean (p, mean_intensity);
74 
75  A (0, 0) += p.x * p.x;
76  A (0, 1) += p.x * p.y;
77  A (0, 2) += p.x * p.z;
78 
79  A (1, 1) += p.y * p.y;
80  A (1, 2) += p.y * p.z;
81 
82  A (2, 2) += p.z * p.z;
83 
84  b[0] += p.x * intensity_ (p);
85  b[1] += p.y * intensity_ (p);
86  b[2] += p.z * intensity_ (p);
87  }
88  // Fill in the lower triangle of A
89  A (1, 0) = A (0, 1);
90  A (2, 0) = A (0, 2);
91  A (2, 1) = A (1, 2);
92 
93 //*
94  Eigen::Vector3f x = A.colPivHouseholderQr ().solve (b);
95 /*/
96 
97  Eigen::Vector3f eigen_values;
98  Eigen::Matrix3f eigen_vectors;
99  eigen33 (A, eigen_vectors, eigen_values);
100 
101  b = eigen_vectors.transpose () * b;
102 
103  if ( eigen_values (0) != 0)
104  b (0) /= eigen_values (0);
105  else
106  b (0) = 0;
107 
108  if ( eigen_values (1) != 0)
109  b (1) /= eigen_values (1);
110  else
111  b (1) = 0;
112 
113  if ( eigen_values (2) != 0)
114  b (2) /= eigen_values (2);
115  else
116  b (2) = 0;
117 
118 
119  Eigen::Vector3f x = eigen_vectors * b;
120 
121 // if (A.col (0).squaredNorm () != 0)
122 // x [0] /= A.col (0).squaredNorm ();
123 // b -= x [0] * A.col (0);
124 //
125 //
126 // if (A.col (1).squaredNorm () != 0)
127 // x [1] /= A.col (1).squaredNorm ();
128 // b -= x[1] * A.col (1);
129 //
130 // x [2] = b.dot (A.col (2));
131 // if (A.col (2).squaredNorm () != 0)
132 // x[2] /= A.col (2).squaredNorm ();
133  // Fit a hyperplane to the data
134 
135 //*/
136 // std::cout << A << "\n*\n" << bb << "\n=\n" << x << "\nvs.\n" << x2 << "\n\n";
137 // std::cout << A * x << "\nvs.\n" << A * x2 << "\n\n------\n";
138  // Project the gradient vector, x, onto the tangent plane
139  gradient = (Eigen::Matrix3f::Identity () - normal*normal.transpose ()) * x;
140 }
141 
142 //////////////////////////////////////////////////////////////////////////////////////////////
143 template <typename PointInT, typename PointNT, typename PointOutT, typename IntensitySelectorT> void
145 {
146  // Allocate enough space to hold the results
147  // \note This resize is irrelevant for a radiusSearch ().
148  std::vector<int> nn_indices (k_);
149  std::vector<float> nn_dists (k_);
150  output.is_dense = true;
151 
152  // If the data is dense, we don't need to check for NaN
153  if (surface_->is_dense)
154  {
155 #ifdef _OPENMP
156 #pragma omp parallel for shared (output) private (nn_indices, nn_dists) num_threads(threads_)
157 #endif
158  // Iterating over the entire index vector
159  for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
160  {
161  PointOutT &p_out = output.points[idx];
162 
163  if (!this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
164  {
165  p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
166  output.is_dense = false;
167  continue;
168  }
169 
170  Eigen::Vector3f centroid;
171  float mean_intensity = 0;
172  // Initialize to 0
173  centroid.setZero ();
174  for (const int &nn_index : nn_indices)
175  {
176  centroid += surface_->points[nn_index].getVector3fMap ();
177  mean_intensity += intensity_ (surface_->points[nn_index]);
178  }
179  centroid /= static_cast<float> (nn_indices.size ());
180  mean_intensity /= static_cast<float> (nn_indices.size ());
181 
182  Eigen::Vector3f normal = Eigen::Vector3f::Map (normals_->points[(*indices_) [idx]].normal);
183  Eigen::Vector3f gradient;
184  computePointIntensityGradient (*surface_, nn_indices, centroid, mean_intensity, normal, gradient);
185 
186  p_out.gradient[0] = gradient[0];
187  p_out.gradient[1] = gradient[1];
188  p_out.gradient[2] = gradient[2];
189  }
190  }
191  else
192  {
193 #ifdef _OPENMP
194 #pragma omp parallel for shared (output) private (nn_indices, nn_dists) num_threads(threads_)
195 #endif
196  // Iterating over the entire index vector
197  for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
198  {
199  PointOutT &p_out = output.points[idx];
200  if (!isFinite ((*surface_) [(*indices_)[idx]]) ||
201  !this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists))
202  {
203  p_out.gradient[0] = p_out.gradient[1] = p_out.gradient[2] = std::numeric_limits<float>::quiet_NaN ();
204  output.is_dense = false;
205  continue;
206  }
207  Eigen::Vector3f centroid;
208  float mean_intensity = 0;
209  // Initialize to 0
210  centroid.setZero ();
211  unsigned cp = 0;
212  for (const int &nn_index : nn_indices)
213  {
214  // Check if the point is invalid
215  if (!isFinite ((*surface_) [nn_index]))
216  continue;
217 
218  centroid += surface_->points [nn_index].getVector3fMap ();
219  mean_intensity += intensity_ (surface_->points [nn_index]);
220  ++cp;
221  }
222  centroid /= static_cast<float> (cp);
223  mean_intensity /= static_cast<float> (cp);
224  Eigen::Vector3f normal = Eigen::Vector3f::Map (normals_->points[(*indices_) [idx]].normal);
225  Eigen::Vector3f gradient;
226  computePointIntensityGradient (*surface_, nn_indices, centroid, mean_intensity, normal, gradient);
227 
228  p_out.gradient[0] = gradient[0];
229  p_out.gradient[1] = gradient[1];
230  p_out.gradient[2] = gradient[2];
231  }
232  }
233 }
234 
235 #define PCL_INSTANTIATE_IntensityGradientEstimation(InT,NT,OutT) template class PCL_EXPORTS pcl::IntensityGradientEstimation<InT,NT,OutT>;
236 
237 #endif // PCL_FEATURES_IMPL_INTENSITY_GRADIENT_H_
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:53
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
IntensityGradientEstimation estimates the intensity gradient for a point cloud that contains position...
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:419
void computeFeature(PointCloudOut &output) override
Estimate the intensity gradients for a set of points given in <setInputCloud (), setIndices ()> using...