Point Cloud Library (PCL)  1.14.1-dev
flare.hpp
1 /*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2016-, Open Perception, 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 the copyright holder(s) 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 *
37 */
38 
39 #ifndef PCL_FEATURES_IMPL_FLARE_H_
40 #define PCL_FEATURES_IMPL_FLARE_H_
41 
42 #include <pcl/features/flare.h>
43 #include <pcl/common/geometry.h>
44 #include <pcl/search/kdtree.h> // for KdTree
45 #include <pcl/search/organized.h> // for OrganizedNeighbor
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template<typename PointInT, typename PointNT, typename PointOutT, typename SignedDistanceT> bool
50 {
52  {
53  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
54  return (false);
55  }
56 
57  if (tangent_radius_ == 0.0f)
58  {
59  PCL_ERROR ("[pcl::%s::initCompute] tangent_radius_ not set.\n", getClassName ().c_str ());
60  return (false);
61  }
62 
63  // If no search sampled_surface_ has been defined, use the surface_ dataset as the search sampled_surface_ itself
64  if (!sampled_surface_)
65  {
66  fake_sampled_surface_ = true;
67  sampled_surface_ = surface_;
68 
69  if (sampled_tree_)
70  {
71  PCL_WARN ("[pcl::%s::initCompute] sampled_surface_ is not set even if sampled_tree_ is already set.", getClassName ().c_str ());
72  PCL_WARN ("sampled_tree_ will be rebuilt from surface_. Use sampled_surface_.\n");
73  }
74  }
75 
76  // Check if a space search locator was given for sampled_surface_
77  if (!sampled_tree_)
78  {
79  if (sampled_surface_->isOrganized () && surface_->isOrganized () && input_->isOrganized ())
80  sampled_tree_.reset (new pcl::search::OrganizedNeighbor<PointInT> ());
81  else
82  sampled_tree_.reset (new pcl::search::KdTree<PointInT> (false));
83  }
84 
85  if (sampled_tree_->getInputCloud () != sampled_surface_) // Make sure the tree searches the sampled surface
86  sampled_tree_->setInputCloud (sampled_surface_);
87 
88  return (true);
89 }
90 
91 //////////////////////////////////////////////////////////////////////////////////////////////
92 template<typename PointInT, typename PointNT, typename PointOutT, typename SignedDistanceT> bool
94 {
95  // Reset the surface
96  if (fake_surface_)
97  {
98  surface_.reset ();
99  fake_surface_ = false;
100  }
101  // Reset the sampled surface
102  if (fake_sampled_surface_)
103  {
104  sampled_surface_.reset ();
105  fake_sampled_surface_ = false;
106  }
107  return (true);
108 }
109 
110 //////////////////////////////////////////////////////////////////////////////////////////////
111 template<typename PointInT, typename PointNT, typename PointOutT, typename SignedDistanceT> SignedDistanceT
113  Eigen::Matrix3f &lrf)
114 {
115  Eigen::Vector3f x_axis, y_axis;
116  Eigen::Vector3f fitted_normal; //z_axis
117 
118  //find Z axis
119 
120  //extract support points for the computation of Z axis
121  pcl::Indices neighbours_indices;
122  std::vector<float> neighbours_distances;
123 
124  const std::size_t n_normal_neighbours =
125  this->searchForNeighbors (index, search_parameter_, neighbours_indices, neighbours_distances);
126  if (n_normal_neighbours < static_cast<std::size_t>(min_neighbors_for_normal_axis_))
127  {
128  if (!pcl::isFinite ((*normals_)[index]))
129  {
130  //normal is invalid
131  //setting lrf to NaN
132  lrf.setConstant (std::numeric_limits<float>::quiet_NaN ());
133  return (std::numeric_limits<SignedDistanceT>::max ());
134  }
135 
136  //set z_axis as the normal of index point
137  fitted_normal = (*normals_)[index].getNormalVector3fMap ();
138  }
139  else
140  {
141  float plane_curvature;
142  normal_estimation_.computePointNormal (*surface_, neighbours_indices, fitted_normal (0), fitted_normal (1), fitted_normal (2), plane_curvature);
143 
144  //disambiguate Z axis with normal mean
145  if (!pcl::flipNormalTowardsNormalsMean<PointNT> (*normals_, neighbours_indices, fitted_normal))
146  {
147  //all normals in the neighbourhood are invalid
148  //setting lrf to NaN
149  lrf.setConstant (std::numeric_limits<float>::quiet_NaN ());
150  return (std::numeric_limits<SignedDistanceT>::max ());
151  }
152  }
153 
154  //setting LRF Z axis
155  lrf.row (2).matrix () = fitted_normal;
156 
157  //find X axis
158 
159  //extract support points for Rx radius
160  const std::size_t n_tangent_neighbours =
161  sampled_tree_->radiusSearch ((*input_)[index], tangent_radius_, neighbours_indices, neighbours_distances);
162 
163  if (n_tangent_neighbours < static_cast<std::size_t>(min_neighbors_for_tangent_axis_))
164  {
165  //set X axis as a random axis
166  x_axis = pcl::geometry::randomOrthogonalAxis (fitted_normal);
167  y_axis = fitted_normal.cross (x_axis);
168 
169  lrf.row (0).matrix () = x_axis;
170  lrf.row (1).matrix () = y_axis;
171 
172  return (std::numeric_limits<SignedDistanceT>::max ());
173  }
174 
175  //find point with the largest signed distance from tangent plane
176 
177  SignedDistanceT shape_score;
178  SignedDistanceT best_shape_score = -std::numeric_limits<SignedDistanceT>::max ();
179  int best_shape_index = -1;
180 
181  Eigen::Vector3f best_margin_point;
182 
183  const float radius2 = tangent_radius_ * tangent_radius_;
184  const float margin_distance2 = margin_thresh_ * margin_thresh_ * radius2;
185 
186  Vector3fMapConst feature_point = (*input_)[index].getVector3fMap ();
187 
188  for (std::size_t curr_neigh = 0; curr_neigh < n_tangent_neighbours; ++curr_neigh)
189  {
190  const int& curr_neigh_idx = neighbours_indices[curr_neigh];
191  const float& neigh_distance_sqr = neighbours_distances[curr_neigh];
192 
193  if (neigh_distance_sqr <= margin_distance2)
194  {
195  continue;
196  }
197 
198  //point curr_neigh_idx is inside the ring between marginThresh and Radius
199 
200  shape_score = fitted_normal.dot ((*sampled_surface_)[curr_neigh_idx].getVector3fMap ());
201 
202  if (shape_score > best_shape_score)
203  {
204  best_shape_index = curr_neigh_idx;
205  best_shape_score = shape_score;
206  }
207  } //for each neighbor
208 
209  if (best_shape_index == -1)
210  {
211  x_axis = pcl::geometry::randomOrthogonalAxis (fitted_normal);
212  y_axis = fitted_normal.cross (x_axis);
213 
214  lrf.row (0).matrix () = x_axis;
215  lrf.row (1).matrix () = y_axis;
216 
217  return (std::numeric_limits<SignedDistanceT>::max ());
218  }
219 
220  //find orthogonal axis directed to best_shape_index point projection on plane with fittedNormal as axis
221  x_axis = pcl::geometry::projectedAsUnitVector (sampled_surface_->at (best_shape_index).getVector3fMap (), feature_point, fitted_normal);
222 
223  y_axis = fitted_normal.cross (x_axis);
224 
225  lrf.row (0).matrix () = x_axis;
226  lrf.row (1).matrix () = y_axis;
227  //z axis already set
228 
229  best_shape_score -= fitted_normal.dot (feature_point);
230  return (best_shape_score);
231 }
232 
233 //////////////////////////////////////////////////////////////////////////////////////////////
234 template<typename PointInT, typename PointNT, typename PointOutT, typename SignedDistanceT> void
236 {
237  //check whether used with search radius or search k-neighbors
238  if (this->getKSearch () != 0)
239  {
240  PCL_ERROR (
241  "[pcl::%s::computeFeature] Error! Search method set to k-neighborhood. Call setKSearch (0) and setRadiusSearch (radius) to use this class.\n",
242  getClassName ().c_str ());
243  return;
244  }
245 
246  signed_distances_from_highest_points_.resize (indices_->size ());
247 
248  for (std::size_t point_idx = 0; point_idx < indices_->size (); ++point_idx)
249  {
250  Eigen::Matrix3f currentLrf;
251  PointOutT &rf = output[point_idx];
252 
253  signed_distances_from_highest_points_[point_idx] = computePointLRF ((*indices_)[point_idx], currentLrf);
254  if (signed_distances_from_highest_points_[point_idx] == std::numeric_limits<SignedDistanceT>::max ())
255  {
256  output.is_dense = false;
257  }
258 
259  rf.getXAxisVector3fMap () = currentLrf.row (0);
260  rf.getYAxisVector3fMap () = currentLrf.row (1);
261  rf.getZAxisVector3fMap () = currentLrf.row (2);
262  }
263 }
264 
265 #define PCL_INSTANTIATE_FLARELocalReferenceFrameEstimation(T,NT,OutT,SdT) template class PCL_EXPORTS pcl::FLARELocalReferenceFrameEstimation<T,NT,OutT,SdT>;
266 
267 #endif // PCL_FEATURES_IMPL_FLARE_H_
bool deinitCompute() override
This method should get called after the actual computation is ended.
Definition: flare.hpp:93
void computeFeature(PointCloudOut &output) override
Abstract feature estimation method.
Definition: flare.hpp:235
bool initCompute() override
This method should get called before starting the actual computation.
Definition: flare.hpp:49
SignedDistanceT computePointLRF(const int index, Eigen::Matrix3f &lrf)
Estimate the LRF descriptor for a given point based on its spatial neighborhood of 3D points with nor...
Definition: flare.hpp:112
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
Definition: organized.h:66
Defines some geometrical functions and utility functions.
Eigen::Vector3f projectedAsUnitVector(Eigen::Vector3f const &point, Eigen::Vector3f const &plane_origin, Eigen::Vector3f const &plane_normal)
Given a plane defined by plane_origin and plane_normal, find the unit vector pointing from plane_orig...
Definition: geometry.h:115
Eigen::Vector3f randomOrthogonalAxis(Eigen::Vector3f const &axis)
Define a random unit vector orthogonal to axis.
Definition: geometry.h:134
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
const Eigen::Map< const Eigen::Vector3f > Vector3fMapConst
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133