Point Cloud Library (PCL)  1.9.1-dev
ppf.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2011, Alexandru-Eugen Ichim
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 #ifndef PCL_FEATURES_IMPL_PPF_H_
41 #define PCL_FEATURES_IMPL_PPF_H_
42 
43 #include <pcl/features/ppf.h>
44 #include <pcl/features/pfh.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename PointNT, typename PointOutT>
49  : FeatureFromNormals <PointInT, PointNT, PointOutT> ()
50 {
51  feature_name_ = "PPFEstimation";
52  // Slight hack in order to pass the check for the presence of a search method in Feature::initCompute ()
55 }
56 
57 
58 //////////////////////////////////////////////////////////////////////////////////////////////
59 template <typename PointInT, typename PointNT, typename PointOutT> void
61 {
62  // Initialize output container - overwrite the sizes done by Feature::initCompute ()
63  output.points.resize (indices_->size () * input_->points.size ());
64  output.height = 1;
65  output.width = static_cast<uint32_t> (output.points.size ());
66  output.is_dense = true;
67 
68  // Compute point pair features for every pair of points in the cloud
69  for (size_t index_i = 0; index_i < indices_->size (); ++index_i)
70  {
71  size_t i = (*indices_)[index_i];
72  for (size_t j = 0 ; j < input_->points.size (); ++j)
73  {
74  PointOutT p;
75  if (i != j)
76  {
77  if (//pcl::computePPFPairFeature
78  pcl::computePairFeatures (input_->points[i].getVector4fMap (),
79  normals_->points[i].getNormalVector4fMap (),
80  input_->points[j].getVector4fMap (),
81  normals_->points[j].getNormalVector4fMap (),
82  p.f1, p.f2, p.f3, p.f4))
83  {
84  // Calculate alpha_m angle
85  Eigen::Vector3f model_reference_point = input_->points[i].getVector3fMap (),
86  model_reference_normal = normals_->points[i].getNormalVector3fMap (),
87  model_point = input_->points[j].getVector3fMap ();
88  float rotation_angle = std::acos (model_reference_normal.dot (Eigen::Vector3f::UnitX ()));
89  bool parallel_to_x = (model_reference_normal.y() == 0.0f && model_reference_normal.z() == 0.0f);
90  Eigen::Vector3f rotation_axis = (parallel_to_x)?(Eigen::Vector3f::UnitY ()):(model_reference_normal.cross (Eigen::Vector3f::UnitX ()). normalized());
91  Eigen::AngleAxisf rotation_mg (rotation_angle, rotation_axis);
92  Eigen::Affine3f transform_mg (Eigen::Translation3f ( rotation_mg * ((-1) * model_reference_point)) * rotation_mg);
93 
94  Eigen::Vector3f model_point_transformed = transform_mg * model_point;
95  float angle = std::atan2 ( -model_point_transformed(2), model_point_transformed(1));
96  if (std::sin (angle) * model_point_transformed(2) < 0.0f)
97  angle *= (-1);
98  p.alpha_m = -angle;
99  }
100  else
101  {
102  PCL_ERROR ("[pcl::%s::computeFeature] Computing pair feature vector between points %u and %u went wrong.\n", getClassName ().c_str (), i, j);
103  p.f1 = p.f2 = p.f3 = p.f4 = p.alpha_m = std::numeric_limits<float>::quiet_NaN ();
104  output.is_dense = false;
105  }
106  }
107  // Do not calculate the feature for identity pairs (i, i) as they are not used
108  // in the following computations
109  else
110  {
111  p.f1 = p.f2 = p.f3 = p.f4 = p.alpha_m = std::numeric_limits<float>::quiet_NaN ();
112  output.is_dense = false;
113  }
114 
115  output.points[index_i*input_->points.size () + j] = p;
116  }
117  }
118 }
119 
120 #define PCL_INSTANTIATE_PPFEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PPFEstimation<T,NT,OutT>;
121 
122 
123 #endif // PCL_FEATURES_IMPL_PPF_H_
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:61
Class that calculates the "surflet" features for each pair in the given pointcloud.
Definition: ppf.h:76
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:423
std::string feature_name_
The feature name.
Definition: feature.h:222
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:154
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:428
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: feature.h:246
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:426
PointCloudNConstPtr normals_
A pointer to the input dataset that contains the point normals of the XYZ dataset.
Definition: feature.h:354
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:431
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:151
Feature represents the base feature class.
Definition: feature.h:105
PPFEstimation()
Empty Constructor.
Definition: ppf.hpp:48
PCL_EXPORTS bool computePairFeatures(const Eigen::Vector4f &p1, const Eigen::Vector4f &n1, const Eigen::Vector4f &p2, const Eigen::Vector4f &n2, float &f1, float &f2, float &f3, float &f4)
Compute the 4-tuple representation containing the three angles and one distance between two points re...