Point Cloud Library (PCL)  1.7.1
principal_curvatures.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_PRINCIPAL_CURVATURES_H_
42 #define PCL_FEATURES_IMPL_PRINCIPAL_CURVATURES_H_
43 
44 #include <pcl/features/principal_curvatures.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename PointNT, typename PointOutT> void
49  const pcl::PointCloud<PointNT> &normals, int p_idx, const std::vector<int> &indices,
50  float &pcx, float &pcy, float &pcz, float &pc1, float &pc2)
51 {
52  EIGEN_ALIGN16 Eigen::Matrix3f I = Eigen::Matrix3f::Identity ();
53  Eigen::Vector3f n_idx (normals.points[p_idx].normal[0], normals.points[p_idx].normal[1], normals.points[p_idx].normal[2]);
54  EIGEN_ALIGN16 Eigen::Matrix3f M = I - n_idx * n_idx.transpose (); // projection matrix (into tangent plane)
55 
56  // Project normals into the tangent plane
57  Eigen::Vector3f normal;
58  projected_normals_.resize (indices.size ());
59  xyz_centroid_.setZero ();
60  for (size_t idx = 0; idx < indices.size(); ++idx)
61  {
62  normal[0] = normals.points[indices[idx]].normal[0];
63  normal[1] = normals.points[indices[idx]].normal[1];
64  normal[2] = normals.points[indices[idx]].normal[2];
65 
66  projected_normals_[idx] = M * normal;
67  xyz_centroid_ += projected_normals_[idx];
68  }
69 
70  // Estimate the XYZ centroid
71  xyz_centroid_ /= static_cast<float> (indices.size ());
72 
73  // Initialize to 0
74  covariance_matrix_.setZero ();
75 
76  double demean_xy, demean_xz, demean_yz;
77  // For each point in the cloud
78  for (size_t idx = 0; idx < indices.size (); ++idx)
79  {
80  demean_ = projected_normals_[idx] - xyz_centroid_;
81 
82  demean_xy = demean_[0] * demean_[1];
83  demean_xz = demean_[0] * demean_[2];
84  demean_yz = demean_[1] * demean_[2];
85 
86  covariance_matrix_(0, 0) += demean_[0] * demean_[0];
87  covariance_matrix_(0, 1) += static_cast<float> (demean_xy);
88  covariance_matrix_(0, 2) += static_cast<float> (demean_xz);
89 
90  covariance_matrix_(1, 0) += static_cast<float> (demean_xy);
91  covariance_matrix_(1, 1) += demean_[1] * demean_[1];
92  covariance_matrix_(1, 2) += static_cast<float> (demean_yz);
93 
94  covariance_matrix_(2, 0) += static_cast<float> (demean_xz);
95  covariance_matrix_(2, 1) += static_cast<float> (demean_yz);
96  covariance_matrix_(2, 2) += demean_[2] * demean_[2];
97  }
98 
99  // Extract the eigenvalues and eigenvectors
100  pcl::eigen33 (covariance_matrix_, eigenvalues_);
101  pcl::computeCorrespondingEigenVector (covariance_matrix_, eigenvalues_ [2], eigenvector_);
102 
103  pcx = eigenvector_ [0];
104  pcy = eigenvector_ [1];
105  pcz = eigenvector_ [2];
106  float indices_size = 1.0f / static_cast<float> (indices.size ());
107  pc1 = eigenvalues_ [2] * indices_size;
108  pc2 = eigenvalues_ [1] * indices_size;
109 }
110 
111 
112 //////////////////////////////////////////////////////////////////////////////////////////////
113 template <typename PointInT, typename PointNT, typename PointOutT> void
115 {
116  // Allocate enough space to hold the results
117  // \note This resize is irrelevant for a radiusSearch ().
118  std::vector<int> nn_indices (k_);
119  std::vector<float> nn_dists (k_);
120 
121  output.is_dense = true;
122  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
123  if (input_->is_dense)
124  {
125  // Iterating over the entire index vector
126  for (size_t idx = 0; idx < indices_->size (); ++idx)
127  {
128  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
129  {
130  output.points[idx].principal_curvature[0] = output.points[idx].principal_curvature[1] = output.points[idx].principal_curvature[2] =
131  output.points[idx].pc1 = output.points[idx].pc2 = std::numeric_limits<float>::quiet_NaN ();
132  output.is_dense = false;
133  continue;
134  }
135 
136  // Estimate the principal curvatures at each patch
137  computePointPrincipalCurvatures (*normals_, (*indices_)[idx], nn_indices,
138  output.points[idx].principal_curvature[0], output.points[idx].principal_curvature[1], output.points[idx].principal_curvature[2],
139  output.points[idx].pc1, output.points[idx].pc2);
140  }
141  }
142  else
143  {
144  // Iterating over the entire index vector
145  for (size_t idx = 0; idx < indices_->size (); ++idx)
146  {
147  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
148  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
149  {
150  output.points[idx].principal_curvature[0] = output.points[idx].principal_curvature[1] = output.points[idx].principal_curvature[2] =
151  output.points[idx].pc1 = output.points[idx].pc2 = std::numeric_limits<float>::quiet_NaN ();
152  output.is_dense = false;
153  continue;
154  }
155 
156  // Estimate the principal curvatures at each patch
157  computePointPrincipalCurvatures (*normals_, (*indices_)[idx], nn_indices,
158  output.points[idx].principal_curvature[0], output.points[idx].principal_curvature[1], output.points[idx].principal_curvature[2],
159  output.points[idx].pc1, output.points[idx].pc2);
160  }
161  }
162 }
163 
164 #define PCL_INSTANTIATE_PrincipalCurvaturesEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PrincipalCurvaturesEstimation<T,NT,OutT>;
165 
166 #endif // PCL_FEATURES_IMPL_PRINCIPAL_CURVATURES_H_