Point Cloud Library (PCL)  1.9.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 #ifndef PCL_SEGMENTATION_IMPL_SEGMENT_DIFFERENCES_H_
39 #define PCL_SEGMENTATION_IMPL_SEGMENT_DIFFERENCES_H_
40 
41 #include <pcl/segmentation/segment_differences.h>
42 #include <pcl/common/io.h>
43 
44 //////////////////////////////////////////////////////////////////////////
45 template <typename PointT> void
47  const pcl::PointCloud<PointT> &src,
48  double threshold,
49  const boost::shared_ptr<pcl::search::Search<PointT> > &tree,
51 {
52  // We're interested in a single nearest neighbor only
53  std::vector<int> nn_indices (1);
54  std::vector<float> nn_distances (1);
55 
56  // The input cloud indices that do not have a neighbor in the target cloud
57  std::vector<int> src_indices;
58 
59  // Iterate through the source data set
60  for (int i = 0; i < static_cast<int> (src.points.size ()); ++i)
61  {
62  // Ignore invalid points in the inpout cloud
63  if (!isFinite (src.points[i]))
64  continue;
65  // Search for the closest point in the target data set (number of neighbors to find = 1)
66  if (!tree->nearestKSearch (src.points[i], 1, nn_indices, nn_distances))
67  {
68  PCL_WARN ("No neighbor found for point %lu (%f %f %f)!\n", i, src.points[i].x, src.points[i].y, src.points[i].z);
69  continue;
70  }
71  // Add points without a corresponding point in the target cloud to the output cloud
72  if (nn_distances[0] > threshold)
73  src_indices.push_back (i);
74  }
75 
76  // Copy all the data fields from the input cloud to the output one
77  copyPointCloud (src, src_indices, output);
78 
79  // Output is always dense, as invalid points in the input cloud are ignored
80  output.is_dense = true;
81 }
82 
83 //////////////////////////////////////////////////////////////////////////
84 //////////////////////////////////////////////////////////////////////////
85 //////////////////////////////////////////////////////////////////////////
86 template <typename PointT> void
88 {
89  output.header = input_->header;
90 
91  if (!initCompute ())
92  {
93  output.width = output.height = 0;
94  output.points.clear ();
95  return;
96  }
97 
98  // If target is empty, input - target = input
99  if (target_->points.empty ())
100  {
101  output = *input_;
102  return;
103  }
104 
105  // Initialize the spatial locator
106  if (!tree_)
107  {
108  if (target_->isOrganized ())
109  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
110  else
111  tree_.reset (new pcl::search::KdTree<PointT> (false));
112  }
113  // Send the input dataset to the spatial locator
114  tree_->setInputCloud (target_);
115 
116  getPointCloudDifference (*input_, distance_threshold_, tree_, output);
117 
118  deinitCompute ();
119 }
120 
121 #define PCL_INSTANTIATE_SegmentDifferences(T) template class PCL_EXPORTS pcl::SegmentDifferences<T>;
122 #define PCL_INSTANTIATE_getPointCloudDifference(T) template PCL_EXPORTS void pcl::getPointCloudDifference<T>(const pcl::PointCloud<T> &, double, const boost::shared_ptr<pcl::search::Search<T> > &, pcl::PointCloud<T> &);
123 
124 #endif // PCL_SEGMENTATION_IMPL_SEGMENT_DIFFERENCES_H_
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:54
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
void segment(PointCloud &output)
Segment differences between two input point clouds.
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
void getPointCloudDifference(const pcl::PointCloud< PointT > &src, double threshold, const boost::shared_ptr< pcl::search::Search< PointT > > &tree, pcl::PointCloud< PointT > &output)
Obtain the difference between two aligned point clouds as another point cloud, given a distance thres...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407
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:418
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:62
Generic search class.
Definition: search.h:74