Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
moment_invariants.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: moment_invariants.hpp 5026 2012-03-12 02:51:44Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_MOMENT_INVARIANTS_H_
41 #define PCL_FEATURES_IMPL_MOMENT_INVARIANTS_H_
42 
44 
46 template <typename PointInT, typename PointOutT> void
48  const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
49  float &j1, float &j2, float &j3)
50 {
51  // Estimate the XYZ centroid
52  compute3DCentroid (cloud, indices, xyz_centroid_);
53 
54  // Initalize the centralized moments
55  float mu200 = 0, mu020 = 0, mu002 = 0, mu110 = 0, mu101 = 0, mu011 = 0;
56 
57  // Iterate over the nearest neighbors set
58  for (size_t nn_idx = 0; nn_idx < indices.size (); ++nn_idx)
59  {
60  // Demean the points
61  temp_pt_[0] = cloud.points[indices[nn_idx]].x - xyz_centroid_[0];
62  temp_pt_[1] = cloud.points[indices[nn_idx]].y - xyz_centroid_[1];
63  temp_pt_[2] = cloud.points[indices[nn_idx]].z - xyz_centroid_[2];
64 
65  mu200 += temp_pt_[0] * temp_pt_[0];
66  mu020 += temp_pt_[1] * temp_pt_[1];
67  mu002 += temp_pt_[2] * temp_pt_[2];
68  mu110 += temp_pt_[0] * temp_pt_[1];
69  mu101 += temp_pt_[0] * temp_pt_[2];
70  mu011 += temp_pt_[1] * temp_pt_[2];
71  }
72 
73  // Save the moment invariants
74  j1 = mu200 + mu020 + mu002;
75  j2 = mu200*mu020 + mu200*mu002 + mu020*mu002 - mu110*mu110 - mu101*mu101 - mu011*mu011;
76  j3 = mu200*mu020*mu002 + 2*mu110*mu101*mu011 - mu002*mu110*mu110 - mu020*mu101*mu101 - mu200*mu011*mu011;
77 }
78 
80 template <typename PointInT, typename PointOutT> void
82  const pcl::PointCloud<PointInT> &cloud, float &j1, float &j2, float &j3)
83 {
84  // Estimate the XYZ centroid
85  compute3DCentroid (cloud, xyz_centroid_);
86 
87  // Initalize the centralized moments
88  float mu200 = 0, mu020 = 0, mu002 = 0, mu110 = 0, mu101 = 0, mu011 = 0;
89 
90  // Iterate over the nearest neighbors set
91  for (size_t nn_idx = 0; nn_idx < cloud.points.size (); ++nn_idx )
92  {
93  // Demean the points
94  temp_pt_[0] = cloud.points[nn_idx].x - xyz_centroid_[0];
95  temp_pt_[1] = cloud.points[nn_idx].y - xyz_centroid_[1];
96  temp_pt_[2] = cloud.points[nn_idx].z - xyz_centroid_[2];
97 
98  mu200 += temp_pt_[0] * temp_pt_[0];
99  mu020 += temp_pt_[1] * temp_pt_[1];
100  mu002 += temp_pt_[2] * temp_pt_[2];
101  mu110 += temp_pt_[0] * temp_pt_[1];
102  mu101 += temp_pt_[0] * temp_pt_[2];
103  mu011 += temp_pt_[1] * temp_pt_[2];
104  }
105 
106  // Save the moment invariants
107  j1 = mu200 + mu020 + mu002;
108  j2 = mu200*mu020 + mu200*mu002 + mu020*mu002 - mu110*mu110 - mu101*mu101 - mu011*mu011;
109  j3 = mu200*mu020*mu002 + 2*mu110*mu101*mu011 - mu002*mu110*mu110 - mu020*mu101*mu101 - mu200*mu011*mu011;
110 }
111 
113 template <typename PointInT, 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].j1 = output.points[idx].j2 = output.points[idx].j3 = std::numeric_limits<float>::quiet_NaN ();
131  output.is_dense = false;
132  continue;
133  }
134 
135  computePointMomentInvariants (*surface_, nn_indices,
136  output.points[idx].j1, output.points[idx].j2, output.points[idx].j3);
137  }
138  }
139  else
140  {
141  // Iterating over the entire index vector
142  for (size_t idx = 0; idx < indices_->size (); ++idx)
143  {
144  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
145  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
146  {
147  output.points[idx].j1 = output.points[idx].j2 = output.points[idx].j3 = std::numeric_limits<float>::quiet_NaN ();
148  output.is_dense = false;
149  continue;
150  }
151 
152  computePointMomentInvariants (*surface_, nn_indices,
153  output.points[idx].j1, output.points[idx].j2, output.points[idx].j3);
154  }
155  }
156 }
157 
159 template <typename PointInT> void
161 {
162  // Resize the output dataset
163  output.points.resize (indices_->size (), 3);
164 
165  // Allocate enough space to hold the results
166  // \note This resize is irrelevant for a radiusSearch ().
167  std::vector<int> nn_indices (k_);
168  std::vector<float> nn_dists (k_);
169 
170  output.is_dense = true;
171  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
172  if (input_->is_dense)
173  {
174  // Iterating over the entire index vector
175  for (size_t idx = 0; idx < indices_->size (); ++idx)
176  {
177  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
178  {
179  output.points (idx, 0) = output.points (idx, 1) = output.points (idx, 2) = std::numeric_limits<float>::quiet_NaN ();
180  output.is_dense = false;
181  continue;
182  }
183 
184  this->computePointMomentInvariants (*surface_, nn_indices,
185  output.points (idx, 0), output.points (idx, 1), output.points (idx, 2));
186  }
187  }
188  else
189  {
190  // Iterating over the entire index vector
191  for (size_t idx = 0; idx < indices_->size (); ++idx)
192  {
193  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
194  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
195  {
196  output.points (idx, 0) = output.points (idx, 1) = output.points (idx, 2) = std::numeric_limits<float>::quiet_NaN ();
197  output.is_dense = false;
198  continue;
199  }
200 
201  this->computePointMomentInvariants (*surface_, nn_indices,
202  output.points (idx, 0), output.points (idx, 1), output.points (idx, 2));
203  }
204  }
205 }
206 
207 
208 #define PCL_INSTANTIATE_MomentInvariantsEstimation(T,NT) template class PCL_EXPORTS pcl::MomentInvariantsEstimation<T,NT>;
209 
210 #endif // PCL_FEATURES_IMPL_MOMENT_INVARIANTS_H_
211