Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
principal_curvatures.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: principal_curvatures.hpp 5026 2012-03-12 02:51:44Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_PRINCIPAL_CURVATURES_H_
41 #define PCL_FEATURES_IMPL_PRINCIPAL_CURVATURES_H_
42 
44 
46 template <typename PointInT, typename PointNT, typename PointOutT> void
48  const pcl::PointCloud<PointNT> &normals, int p_idx, const std::vector<int> &indices,
49  float &pcx, float &pcy, float &pcz, float &pc1, float &pc2)
50 {
51  EIGEN_ALIGN16 Eigen::Matrix3f I = Eigen::Matrix3f::Identity ();
52  Eigen::Vector3f n_idx (normals.points[p_idx].normal[0], normals.points[p_idx].normal[1], normals.points[p_idx].normal[2]);
53  EIGEN_ALIGN16 Eigen::Matrix3f M = I - n_idx * n_idx.transpose (); // projection matrix (into tangent plane)
54 
55  // Project normals into the tangent plane
56  Eigen::Vector3f normal;
57  projected_normals_.resize (indices.size ());
58  xyz_centroid_.setZero ();
59  for (size_t idx = 0; idx < indices.size(); ++idx)
60  {
61  normal[0] = normals.points[indices[idx]].normal[0];
62  normal[1] = normals.points[indices[idx]].normal[1];
63  normal[2] = normals.points[indices[idx]].normal[2];
64 
65  projected_normals_[idx] = M * normal;
66  xyz_centroid_ += projected_normals_[idx];
67  }
68 
69  // Estimate the XYZ centroid
70  xyz_centroid_ /= static_cast<float> (indices.size ());
71 
72  // Initialize to 0
73  covariance_matrix_.setZero ();
74 
75  double demean_xy, demean_xz, demean_yz;
76  // For each point in the cloud
77  for (size_t idx = 0; idx < indices.size (); ++idx)
78  {
79  demean_ = projected_normals_[idx] - xyz_centroid_;
80 
81  demean_xy = demean_[0] * demean_[1];
82  demean_xz = demean_[0] * demean_[2];
83  demean_yz = demean_[1] * demean_[2];
84 
85  covariance_matrix_(0, 0) += demean_[0] * demean_[0];
86  covariance_matrix_(0, 1) += static_cast<float> (demean_xy);
87  covariance_matrix_(0, 2) += static_cast<float> (demean_xz);
88 
89  covariance_matrix_(1, 0) += static_cast<float> (demean_xy);
90  covariance_matrix_(1, 1) += demean_[1] * demean_[1];
91  covariance_matrix_(1, 2) += static_cast<float> (demean_yz);
92 
93  covariance_matrix_(2, 0) += static_cast<float> (demean_xz);
94  covariance_matrix_(2, 1) += static_cast<float> (demean_yz);
95  covariance_matrix_(2, 2) += demean_[2] * demean_[2];
96  }
97 
98  // Extract the eigenvalues and eigenvectors
99  pcl::eigen33 (covariance_matrix_, eigenvalues_);
100  pcl::computeCorrespondingEigenVector (covariance_matrix_, eigenvalues_ [2], eigenvector_);
101 
102  pcx = eigenvector_ [0];
103  pcy = eigenvector_ [1];
104  pcz = eigenvector_ [2];
105  float indices_size = 1.0f / static_cast<float> (indices.size ());
106  pc1 = eigenvalues_ [2] * indices_size;
107  pc2 = eigenvalues_ [1] * indices_size;
108 }
109 
110 
112 template <typename PointInT, typename PointNT, typename PointOutT> void
114 {
115  // Allocate enough space to hold the results
116  // \note This resize is irrelevant for a radiusSearch ().
117  std::vector<int> nn_indices (k_);
118  std::vector<float> nn_dists (k_);
119 
120  output.is_dense = true;
121  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
122  if (input_->is_dense)
123  {
124  // Iterating over the entire index vector
125  for (size_t idx = 0; idx < indices_->size (); ++idx)
126  {
127  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
128  {
129  output.points[idx].principal_curvature[0] = output.points[idx].principal_curvature[1] = output.points[idx].principal_curvature[2] =
130  output.points[idx].pc1 = output.points[idx].pc2 = std::numeric_limits<float>::quiet_NaN ();
131  output.is_dense = false;
132  continue;
133  }
134 
135  // Estimate the principal curvatures at each patch
136  computePointPrincipalCurvatures (*normals_, (*indices_)[idx], nn_indices,
137  output.points[idx].principal_curvature[0], output.points[idx].principal_curvature[1], output.points[idx].principal_curvature[2],
138  output.points[idx].pc1, output.points[idx].pc2);
139  }
140  }
141  else
142  {
143  // Iterating over the entire index vector
144  for (size_t idx = 0; idx < indices_->size (); ++idx)
145  {
146  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
147  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
148  {
149  output.points[idx].principal_curvature[0] = output.points[idx].principal_curvature[1] = output.points[idx].principal_curvature[2] =
150  output.points[idx].pc1 = output.points[idx].pc2 = std::numeric_limits<float>::quiet_NaN ();
151  output.is_dense = false;
152  continue;
153  }
154 
155  // Estimate the principal curvatures at each patch
156  computePointPrincipalCurvatures (*normals_, (*indices_)[idx], nn_indices,
157  output.points[idx].principal_curvature[0], output.points[idx].principal_curvature[1], output.points[idx].principal_curvature[2],
158  output.points[idx].pc1, output.points[idx].pc2);
159  }
160  }
161 }
163 template <typename PointInT, typename PointNT> void
165 {
166  // Resize the output dataset
167  output.points.resize (indices_->size (), 5);
168 
169  // Allocate enough space to hold the results
170  // \note This resize is irrelevant for a radiusSearch ().
171  std::vector<int> nn_indices (k_);
172  std::vector<float> nn_dists (k_);
173 
174  output.is_dense = true;
175  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
176  if (input_->is_dense)
177  {
178  // Iterating over the entire index vector
179  for (size_t idx = 0; idx < indices_->size (); ++idx)
180  {
181  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
182  {
183  output.points.row (idx).setConstant (std::numeric_limits<float>::quiet_NaN ());
184  output.is_dense = false;
185  continue;
186  }
187 
188  // Estimate the principal curvatures at each patch
189  this->computePointPrincipalCurvatures (*normals_, (*indices_)[idx], nn_indices,
190  output.points (idx, 0), output.points (idx, 1), output.points (idx, 2),
191  output.points (idx, 3), output.points (idx, 4));
192  }
193  }
194  else
195  {
196  // Iterating over the entire index vector
197  for (size_t idx = 0; idx < indices_->size (); ++idx)
198  {
199  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
200  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
201  {
202  output.points.row (idx).setConstant (std::numeric_limits<float>::quiet_NaN ());
203  output.is_dense = false;
204  continue;
205  }
206 
207  // Estimate the principal curvatures at each patch
208  this->computePointPrincipalCurvatures (*normals_, (*indices_)[idx], nn_indices,
209  output.points (idx, 0), output.points (idx, 1), output.points (idx, 2),
210  output.points (idx, 3), output.points (idx, 4));
211  }
212  }
213 }
214 
215 #define PCL_INSTANTIATE_PrincipalCurvaturesEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PrincipalCurvaturesEstimation<T,NT,OutT>;
216 
217 #endif // PCL_FEATURES_IMPL_PRINCIPAL_CURVATURES_H_