Point Cloud Library (PCL)  1.7.0
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 std::vector<int> &indices,
51  float &j1, float &j2, float &j3)
52 {
53  // Estimate the XYZ centroid
54  compute3DCentroid (cloud, indices, xyz_centroid_);
55 
56  // Initalize 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 (size_t nn_idx = 0; nn_idx < indices.size (); ++nn_idx)
61  {
62  // Demean the points
63  temp_pt_[0] = cloud.points[indices[nn_idx]].x - xyz_centroid_[0];
64  temp_pt_[1] = cloud.points[indices[nn_idx]].y - xyz_centroid_[1];
65  temp_pt_[2] = cloud.points[indices[nn_idx]].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  // Initalize 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 (size_t nn_idx = 0; nn_idx < cloud.points.size (); ++nn_idx )
94  {
95  // Demean the points
96  temp_pt_[0] = cloud.points[nn_idx].x - xyz_centroid_[0];
97  temp_pt_[1] = cloud.points[nn_idx].y - xyz_centroid_[1];
98  temp_pt_[2] = cloud.points[nn_idx].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  std::vector<int> 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 (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.points[idx].j1 = output.points[idx].j2 = output.points[idx].j3 = std::numeric_limits<float>::quiet_NaN ();
133  output.is_dense = false;
134  continue;
135  }
136 
137  computePointMomentInvariants (*surface_, nn_indices,
138  output.points[idx].j1, output.points[idx].j2, output.points[idx].j3);
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].j1 = output.points[idx].j2 = output.points[idx].j3 = std::numeric_limits<float>::quiet_NaN ();
150  output.is_dense = false;
151  continue;
152  }
153 
154  computePointMomentInvariants (*surface_, nn_indices,
155  output.points[idx].j1, output.points[idx].j2, output.points[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