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++] = atan2f (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  if (max_dif > angle_threshold)
110  return (true);
111  else
112  return (false);
113 }
114 
115 //////////////////////////////////////////////////////////////////////////////////////////////
116 template <typename PointInT, typename PointNT, typename PointOutT> void
118 {
119  // Allocate enough space to hold the results
120  // \note This resize is irrelevant for a radiusSearch ().
121  std::vector<int> nn_indices (k_);
122  std::vector<float> nn_dists (k_);
123 
124  Eigen::Vector4f u = Eigen::Vector4f::Zero (), v = Eigen::Vector4f::Zero ();
125 
126  output.is_dense = true;
127  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
128  if (input_->is_dense)
129  {
130  // Iterating over the entire index vector
131  for (size_t idx = 0; idx < indices_->size (); ++idx)
132  {
133  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
134  {
135  output.points[idx].boundary_point = std::numeric_limits<uint8_t>::quiet_NaN ();
136  output.is_dense = false;
137  continue;
138  }
139 
140  // Obtain a coordinate system on the least-squares plane
141  //v = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().unitOrthogonal ();
142  //u = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().cross3 (v);
143  getCoordinateSystemOnPlane (normals_->points[(*indices_)[idx]], u, v);
144 
145  // Estimate whether the point is lying on a boundary surface or not
146  output.points[idx].boundary_point = isBoundaryPoint (*surface_, input_->points[(*indices_)[idx]], nn_indices, u, v, angle_threshold_);
147  }
148  }
149  else
150  {
151  // Iterating over the entire index vector
152  for (size_t idx = 0; idx < indices_->size (); ++idx)
153  {
154  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
155  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
156  {
157  output.points[idx].boundary_point = std::numeric_limits<uint8_t>::quiet_NaN ();
158  output.is_dense = false;
159  continue;
160  }
161 
162  // Obtain a coordinate system on the least-squares plane
163  //v = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().unitOrthogonal ();
164  //u = normals_->points[(*indices_)[idx]].getNormalVector4fMap ().cross3 (v);
165  getCoordinateSystemOnPlane (normals_->points[(*indices_)[idx]], u, v);
166 
167  // Estimate whether the point is lying on a boundary surface or not
168  output.points[idx].boundary_point = isBoundaryPoint (*surface_, input_->points[(*indices_)[idx]], nn_indices, u, v, angle_threshold_);
169  }
170  }
171 }
172 
173 #define PCL_INSTANTIATE_BoundaryEstimation(PointInT,PointNT,PointOutT) template class PCL_EXPORTS pcl::BoundaryEstimation<PointInT, PointNT, PointOutT>;
174 
175 #endif // PCL_FEATURES_IMPL_BOUNDARY_H_
176 
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:53
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:409
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:117
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:417