Point Cloud Library (PCL)  1.14.0-dev
moment_invariants.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_MOMENT_INVARIANTS_H_
42 #define PCL_FEATURES_IMPL_MOMENT_INVARIANTS_H_
43 
44 #include <pcl/features/moment_invariants.h>
45 #include <pcl/common/centroid.h>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointInT, typename PointOutT> void
50  const pcl::PointCloud<PointInT> &cloud, const pcl::Indices &indices,
51  float &j1, float &j2, float &j3)
52 {
53  // Estimate the XYZ centroid
54  compute3DCentroid (cloud, indices, xyz_centroid_);
55 
56  // Initialize the centralized moments
57  float mu200 = 0, mu020 = 0, mu002 = 0, mu110 = 0, mu101 = 0, mu011 = 0;
58 
59  // Iterate over the nearest neighbors set
60  for (const auto &index : indices)
61  {
62  // Demean the points
63  temp_pt_[0] = cloud[index].x - xyz_centroid_[0];
64  temp_pt_[1] = cloud[index].y - xyz_centroid_[1];
65  temp_pt_[2] = cloud[index].z - xyz_centroid_[2];
66 
67  mu200 += temp_pt_[0] * temp_pt_[0];
68  mu020 += temp_pt_[1] * temp_pt_[1];
69  mu002 += temp_pt_[2] * temp_pt_[2];
70  mu110 += temp_pt_[0] * temp_pt_[1];
71  mu101 += temp_pt_[0] * temp_pt_[2];
72  mu011 += temp_pt_[1] * temp_pt_[2];
73  }
74 
75  // Save the moment invariants
76  j1 = mu200 + mu020 + mu002;
77  j2 = mu200*mu020 + mu200*mu002 + mu020*mu002 - mu110*mu110 - mu101*mu101 - mu011*mu011;
78  j3 = mu200*mu020*mu002 + 2*mu110*mu101*mu011 - mu002*mu110*mu110 - mu020*mu101*mu101 - mu200*mu011*mu011;
79 }
80 
81 //////////////////////////////////////////////////////////////////////////////////////////////
82 template <typename PointInT, typename PointOutT> void
84  const pcl::PointCloud<PointInT> &cloud, float &j1, float &j2, float &j3)
85 {
86  // Estimate the XYZ centroid
87  compute3DCentroid (cloud, xyz_centroid_);
88 
89  // Initialize the centralized moments
90  float mu200 = 0, mu020 = 0, mu002 = 0, mu110 = 0, mu101 = 0, mu011 = 0;
91 
92  // Iterate over the nearest neighbors set
93  for (const auto& point: cloud.points)
94  {
95  // Demean the points
96  temp_pt_[0] = point.x - xyz_centroid_[0];
97  temp_pt_[1] = point.y - xyz_centroid_[1];
98  temp_pt_[2] = point.z - xyz_centroid_[2];
99 
100  mu200 += temp_pt_[0] * temp_pt_[0];
101  mu020 += temp_pt_[1] * temp_pt_[1];
102  mu002 += temp_pt_[2] * temp_pt_[2];
103  mu110 += temp_pt_[0] * temp_pt_[1];
104  mu101 += temp_pt_[0] * temp_pt_[2];
105  mu011 += temp_pt_[1] * temp_pt_[2];
106  }
107 
108  // Save the moment invariants
109  j1 = mu200 + mu020 + mu002;
110  j2 = mu200*mu020 + mu200*mu002 + mu020*mu002 - mu110*mu110 - mu101*mu101 - mu011*mu011;
111  j3 = mu200*mu020*mu002 + 2*mu110*mu101*mu011 - mu002*mu110*mu110 - mu020*mu101*mu101 - mu200*mu011*mu011;
112 }
113 
114 //////////////////////////////////////////////////////////////////////////////////////////////
115 template <typename PointInT, typename PointOutT> void
117 {
118  // Allocate enough space to hold the results
119  // \note This resize is irrelevant for a radiusSearch ().
120  pcl::Indices nn_indices (k_);
121  std::vector<float> nn_dists (k_);
122 
123  output.is_dense = true;
124  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
125  if (input_->is_dense)
126  {
127  // Iterating over the entire index vector
128  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
129  {
130  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
131  {
132  output[idx].j1 = output[idx].j2 = output[idx].j3 = std::numeric_limits<float>::quiet_NaN ();
133  output.is_dense = false;
134  continue;
135  }
136 
137  computePointMomentInvariants (*surface_, nn_indices,
138  output[idx].j1, output[idx].j2, output[idx].j3);
139  }
140  }
141  else
142  {
143  // Iterating over the entire index vector
144  for (std::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[idx].j1 = output[idx].j2 = output[idx].j3 = std::numeric_limits<float>::quiet_NaN ();
150  output.is_dense = false;
151  continue;
152  }
153 
154  computePointMomentInvariants (*surface_, nn_indices,
155  output[idx].j1, output[idx].j2, output[idx].j3);
156  }
157  }
158 }
159 
160 #define PCL_INSTANTIATE_MomentInvariantsEstimation(T,NT) template class PCL_EXPORTS pcl::MomentInvariantsEstimation<T,NT>;
161 
162 #endif // PCL_FEATURES_IMPL_MOMENT_INVARIANTS_H_
163 
Define methods for centroid estimation and covariance matrix calculus.
void computeFeature(PointCloudOut &output) override
Estimate moment invariants for all points given in <setInputCloud (), setIndices ()> using the surfac...
void computePointMomentInvariants(const pcl::PointCloud< PointInT > &cloud, const pcl::Indices &indices, float &j1, float &j2, float &j3)
Compute the 3 moment invariants (j1, j2, j3) for a given set of points, using their indices.
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
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:395
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
Definition: centroid.hpp:57
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