Point Cloud Library (PCL)  1.14.0-dev
segment_differences.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder(s) nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/segmentation/segment_differences.h>
41 
42 #include <pcl/common/io.h>
43 #include <pcl/common/point_tests.h> // for pcl::isFinite
44 #include <pcl/search/organized.h> // for OrganizedNeighbor
45 #include <pcl/search/kdtree.h> // for KdTree
46 
47 
48 //////////////////////////////////////////////////////////////////////////
49 template <typename PointT> void
51  const pcl::PointCloud<PointT> &src,
52  double threshold,
53  const typename pcl::search::Search<PointT>::Ptr &tree,
55 {
56  // We're interested in a single nearest neighbor only
57  Indices nn_indices (1);
58  std::vector<float> nn_distances (1);
59 
60  // The input cloud indices that do not have a neighbor in the target cloud
61  Indices src_indices;
62 
63  // Iterate through the source data set
64  for (index_t i = 0; i < static_cast<index_t> (src.size ()); ++i)
65  {
66  // Ignore invalid points in the input cloud
67  if (!isFinite (src[i]))
68  continue;
69  // Search for the closest point in the target data set (number of neighbors to find = 1)
70  if (!tree->nearestKSearch (src[i], 1, nn_indices, nn_distances))
71  {
72  PCL_WARN ("No neighbor found for point %lu (%f %f %f)!\n", i, src[i].x, src[i].y, src[i].z);
73  continue;
74  }
75  // Add points without a corresponding point in the target cloud to the output cloud
76  if (nn_distances[0] > threshold)
77  src_indices.push_back (i);
78  }
79 
80  // Copy all the data fields from the input cloud to the output one
81  copyPointCloud (src, src_indices, output);
82 
83  // Output is always dense, as invalid points in the input cloud are ignored
84  output.is_dense = true;
85 }
86 
87 //////////////////////////////////////////////////////////////////////////
88 //////////////////////////////////////////////////////////////////////////
89 //////////////////////////////////////////////////////////////////////////
90 template <typename PointT> void
92 {
93  output.header = input_->header;
94 
95  if (!initCompute ())
96  {
97  output.width = output.height = 0;
98  output.clear ();
99  return;
100  }
101 
102  // If target is empty, input - target = input
103  if (target_->points.empty ())
104  {
105  output = *input_;
106  return;
107  }
108 
109  // Initialize the spatial locator
110  if (!tree_)
111  {
112  if (target_->isOrganized ())
113  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
114  else
115  tree_.reset (new pcl::search::KdTree<PointT> (false));
116  }
117  // Send the input dataset to the spatial locator
118  tree_->setInputCloud (target_);
119 
120  getPointCloudDifference (*input_, distance_threshold_, tree_, output);
121 
122  deinitCompute ();
123 }
124 
125 #define PCL_INSTANTIATE_SegmentDifferences(T) template class PCL_EXPORTS pcl::SegmentDifferences<T>;
126 #define PCL_INSTANTIATE_getPointCloudDifference(T) template PCL_EXPORTS void pcl::getPointCloudDifference<T>(const pcl::PointCloud<T> &, double, const typename pcl::search::Search<T>::Ptr &, pcl::PointCloud<T> &);
127 
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
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:403
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:392
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
void clear()
Removes all points in a cloud and sets the width and height to 0.
Definition: point_cloud.h:885
std::size_t size() const
Definition: point_cloud.h:443
void segment(PointCloud &output)
Segment differences between two input point clouds.
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:65
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:81
virtual int nearestKSearch(const PointT &point, int k, Indices &k_indices, std::vector< float > &k_sqr_distances) const =0
Search for the k-nearest neighbors for the given query point.
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
Definition: io.hpp:142
void getPointCloudDifference(const pcl::PointCloud< PointT > &src, double threshold, const typename pcl::search::Search< PointT >::Ptr &tree, pcl::PointCloud< PointT > &output)
Obtain the difference between two aligned point clouds as another point cloud, given a distance thres...
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
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:112
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133