Point Cloud Library (PCL)  1.9.1-dev
crh.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: cvfh.hpp 5311 2012-03-26 22:02:04Z aaldoma $
38  *
39  */
40 
41 #ifndef PCL_FEATURES_IMPL_CRH_H_
42 #define PCL_FEATURES_IMPL_CRH_H_
43 
44 #include <pcl/features/crh.h>
45 #include <pcl/common/fft/kiss_fftr.h>
46 #include <pcl/common/common.h>
47 #include <pcl/common/transforms.h>
48 
49 //////////////////////////////////////////////////////////////////////////////////////////////
50 template<typename PointInT, typename PointNT, typename PointOutT>
51 void
53 {
54  // Check if input was set
55  if (!normals_)
56  {
57  PCL_ERROR ("[pcl::%s::computeFeature] No input dataset containing normals was given!\n", getClassName ().c_str ());
58  output.width = output.height = 0;
59  output.points.clear ();
60  return;
61  }
62 
63  if (normals_->points.size () != surface_->points.size ())
64  {
65  PCL_ERROR ("[pcl::%s::computeFeature] The number of points in the input dataset differs from the number of points in the dataset containing the normals!\n", getClassName ().c_str ());
66  output.width = output.height = 0;
67  output.points.clear ();
68  return;
69  }
70 
71  Eigen::Vector3f plane_normal;
72  plane_normal[0] = -centroid_[0];
73  plane_normal[1] = -centroid_[1];
74  plane_normal[2] = -centroid_[2];
75  Eigen::Vector3f z_vector = Eigen::Vector3f::UnitZ ();
76  plane_normal.normalize ();
77  Eigen::Vector3f axis = plane_normal.cross (z_vector);
78  double rotation = -asin (axis.norm ());
79  axis.normalize ();
80 
81  int nbins = nbins_;
82  int bin_angle = 360 / nbins;
83 
84  Eigen::Affine3f transformPC (Eigen::AngleAxisf (static_cast<float> (rotation), axis));
85 
87  grid.points.resize (indices_->size ());
88 
89  for (size_t i = 0; i < indices_->size (); i++)
90  {
91  grid.points[i].getVector4fMap () = surface_->points[(*indices_)[i]].getVector4fMap ();
92  grid.points[i].getNormalVector4fMap () = normals_->points[(*indices_)[i]].getNormalVector4fMap ();
93  }
94 
95  pcl::transformPointCloudWithNormals (grid, grid, transformPC);
96 
97  //fill spatial data vector and the zero-initialize or "value-initialize" an array on c++,
98  // the initialization is made with () after the [nbins]
99  kiss_fft_scalar * spatial_data = new kiss_fft_scalar[nbins]();
100 
101 
102  float sum_w = 0, w = 0;
103  int bin = 0;
104  for (const auto &point : grid.points)
105  {
106  bin = static_cast<int> ((((atan2 (point.normal_y, point.normal_x) + M_PI) * 180 / M_PI) / bin_angle)) % nbins;
107  w = std::sqrt (point.normal_y * point.normal_y + point.normal_x * point.normal_x);
108  sum_w += w;
109  spatial_data[bin] += w;
110  }
111 
112  for (int i = 0; i < nbins; ++i)
113  spatial_data[i] /= sum_w;
114 
115  kiss_fft_cpx * freq_data = new kiss_fft_cpx[nbins / 2 + 1];
116  kiss_fftr_cfg mycfg = kiss_fftr_alloc (nbins, 0, nullptr, nullptr);
117  kiss_fftr (mycfg, spatial_data, freq_data);
118 
119  output.points.resize (1);
120  output.width = output.height = 1;
121 
122  output.points[0].histogram[0] = freq_data[0].r / freq_data[0].r; //dc
123  int k = 1;
124  for (int i = 1; i < (nbins / 2); i++, k += 2)
125  {
126  output.points[0].histogram[k] = freq_data[i].r / freq_data[0].r;
127  output.points[0].histogram[k + 1] = freq_data[i].i / freq_data[0].r;
128  }
129 
130  output.points[0].histogram[nbins - 1] = freq_data[nbins / 2].r / freq_data[0].r; //nyquist
131 
132  delete[] spatial_data;
133  delete[] freq_data;
134 
135 }
136 
137 #define PCL_INSTANTIATE_CRHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::CRHEstimation<T,NT,OutT>;
138 
139 #endif // PCL_FEATURES_IMPL_CRH_H_
kiss_fft_scalar i
Definition: kiss_fft.h:54
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
void transformPointCloudWithNormals(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform, bool copy_all_fields=true)
Transform a point cloud and rotate its normals using an Eigen transform.
Definition: transforms.hpp:306
PointCloud represents the base class in PCL for storing collections of 3D points. ...
kiss_fft_scalar r
Definition: kiss_fft.h:53
CRHEstimation estimates the Camera Roll Histogram (CRH) descriptor for a given point cloud dataset co...
Definition: crh.h:60