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