Point Cloud Library (PCL)  1.9.1-dev
boundary.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_BOUNDARY_H_
42 #define PCL_FEATURES_IMPL_BOUNDARY_H_
43 
44 #include <pcl/features/boundary.h>
45 #include <cfloat>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointInT, typename PointNT, typename PointOutT> bool
50  const pcl::PointCloud<PointInT> &cloud, int q_idx,
51  const std::vector<int> &indices,
52  const Eigen::Vector4f &u, const Eigen::Vector4f &v,
53  const float angle_threshold)
54 {
55  return (isBoundaryPoint (cloud, cloud.points[q_idx], indices, u, v, angle_threshold));
56 }
57 
58 //////////////////////////////////////////////////////////////////////////////////////////////
59 template <typename PointInT, typename PointNT, typename PointOutT> bool
61  const pcl::PointCloud<PointInT> &cloud, const PointInT &q_point,
62  const std::vector<int> &indices,
63  const Eigen::Vector4f &u, const Eigen::Vector4f &v,
64  const float angle_threshold)
65 {
66  if (indices.size () < 3)
67  return (false);
68 
69  if (!std::isfinite (q_point.x) || !std::isfinite (q_point.y) || !std::isfinite (q_point.z))
70  return (false);
71 
72  // Compute the angles between each neighboring point and the query point itself
73  std::vector<float> angles (indices.size ());
74  float max_dif = FLT_MIN, dif;
75  int cp = 0;
76 
77  for (const int &index : indices)
78  {
79  if (!std::isfinite (cloud.points[index].x) ||
80  !std::isfinite (cloud.points[index].y) ||
81  !std::isfinite (cloud.points[index].z))
82  continue;
83 
84  Eigen::Vector4f delta = cloud.points[index].getVector4fMap () - q_point.getVector4fMap ();
85  if (delta == Eigen::Vector4f::Zero())
86  continue;
87 
88  angles[cp++] = std::atan2 (v.dot (delta), u.dot (delta)); // the angles are fine between -PI and PI too
89  }
90  if (cp == 0)
91  return (false);
92 
93  angles.resize (cp);
94  std::sort (angles.begin (), angles.end ());
95 
96  // Compute the maximal angle difference between two consecutive angles
97  for (size_t i = 0; i < angles.size () - 1; ++i)
98  {
99  dif = angles[i + 1] - angles[i];
100  if (max_dif < dif)
101  max_dif = dif;
102  }
103  // Get the angle difference between the last and the first
104  dif = 2 * static_cast<float> (M_PI) - angles[angles.size () - 1] + angles[0];
105  if (max_dif < dif)
106  max_dif = dif;
107 
108  // Check results
109  return (max_dif > angle_threshold);
110 }
111 
112 //////////////////////////////////////////////////////////////////////////////////////////////
113 template <typename PointInT, typename PointNT, typename PointOutT> void
115 {
116  // Allocate enough space to hold the results
117  // \note This resize is irrelevant for a radiusSearch ().
118  std::vector<int> nn_indices (k_);
119  std::vector<float> nn_dists (k_);
120 
121  Eigen::Vector4f u = Eigen::Vector4f::Zero (), v = Eigen::Vector4f::Zero ();
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].boundary_point = std::numeric_limits<uint8_t>::quiet_NaN ();
133  output.is_dense = false;
134  continue;
135  }
136 
137  // Obtain a coordinate system on the least-squares plane
138  //v = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().unitOrthogonal ();
139  //u = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().cross3 (v);
140  getCoordinateSystemOnPlane (normals_->points[(*indices_)[idx]], u, v);
141 
142  // Estimate whether the point is lying on a boundary surface or not
143  output.points[idx].boundary_point = isBoundaryPoint (*surface_, input_->points[(*indices_)[idx]], nn_indices, u, v, angle_threshold_);
144  }
145  }
146  else
147  {
148  // Iterating over the entire index vector
149  for (size_t idx = 0; idx < indices_->size (); ++idx)
150  {
151  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
152  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
153  {
154  output.points[idx].boundary_point = std::numeric_limits<uint8_t>::quiet_NaN ();
155  output.is_dense = false;
156  continue;
157  }
158 
159  // Obtain a coordinate system on the least-squares plane
160  //v = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().unitOrthogonal ();
161  //u = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().cross3 (v);
162  getCoordinateSystemOnPlane (normals_->points[(*indices_)[idx]], u, v);
163 
164  // Estimate whether the point is lying on a boundary surface or not
165  output.points[idx].boundary_point = isBoundaryPoint (*surface_, input_->points[(*indices_)[idx]], nn_indices, u, v, angle_threshold_);
166  }
167  }
168 }
169 
170 #define PCL_INSTANTIATE_BoundaryEstimation(PointInT,PointNT,PointOutT) template class PCL_EXPORTS pcl::BoundaryEstimation<PointInT, PointNT, PointOutT>;
171 
172 #endif // PCL_FEATURES_IMPL_BOUNDARY_H_
173 
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
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:426
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: boundary.h:97
bool isBoundaryPoint(const pcl::PointCloud< PointInT > &cloud, int q_idx, const std::vector< int > &indices, const Eigen::Vector4f &u, const Eigen::Vector4f &v, const float angle_threshold)
Check whether a point is a boundary point in a planar patch of projected points given by indices...
Definition: boundary.hpp:49
void computeFeature(PointCloudOut &output) override
Estimate whether a set of points is lying on surface boundaries using an angle criterion for all poin...
Definition: boundary.hpp:114