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