Point Cloud Library (PCL)  1.8.1-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  const pcl::PointCloud<PointT> &,
49  double threshold, 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 src indices that do not have a neighbor in tgt
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  if (!isFinite (src.points[i]))
63  continue;
64  // Search for the closest point in the target data set (number of neighbors to find = 1)
65  if (!tree->nearestKSearch (src.points[i], 1, nn_indices, nn_distances))
66  {
67  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);
68  continue;
69  }
70 
71  if (nn_distances[0] > threshold)
72  src_indices.push_back (i);
73  }
74 
75  // Allocate enough space and copy the basics
76  output.points.resize (src_indices.size ());
77  output.header = src.header;
78  output.width = static_cast<uint32_t> (src_indices.size ());
79  output.height = 1;
80  //if (src.is_dense)
81  output.is_dense = true;
82  //else
83  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
84  // To verify this, we would need to iterate over all points and check for NaNs
85  //output.is_dense = false;
86 
87  // Copy all the data fields from the input cloud to the output one
88  copyPointCloud (src, src_indices, output);
89 }
90 
91 //////////////////////////////////////////////////////////////////////////
92 //////////////////////////////////////////////////////////////////////////
93 //////////////////////////////////////////////////////////////////////////
94 template <typename PointT> void
96 {
97  output.header = input_->header;
98 
99  if (!initCompute ())
100  {
101  output.width = output.height = 0;
102  output.points.clear ();
103  return;
104  }
105 
106  // If target is empty, input - target = input
107  if (target_->points.empty ())
108  {
109  output = *input_;
110  return;
111  }
112 
113  // Initialize the spatial locator
114  if (!tree_)
115  {
116  if (target_->isOrganized ())
117  tree_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
118  else
119  tree_.reset (new pcl::search::KdTree<PointT> (false));
120  }
121  // Send the input dataset to the spatial locator
122  tree_->setInputCloud (target_);
123 
124  getPointCloudDifference (*input_, *target_, distance_threshold_, tree_, output);
125 
126  deinitCompute ();
127 }
128 
129 #define PCL_INSTANTIATE_SegmentDifferences(T) template class PCL_EXPORTS pcl::SegmentDifferences<T>;
130 #define PCL_INSTANTIATE_getPointCloudDifference(T) template PCL_EXPORTS void pcl::getPointCloudDifference<T>(const pcl::PointCloud<T> &, const pcl::PointCloud<T> &, double, const boost::shared_ptr<pcl::search::Search<T> > &, pcl::PointCloud<T> &);
131 
132 #endif // PCL_SEGMENTATION_IMPL_SEGMENT_DIFFERENCES_H_
133 
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
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
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.
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407
void segment(PointCloud &output)
Segment differences between two input point clouds.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:62
void getPointCloudDifference(const pcl::PointCloud< PointT > &src, const pcl::PointCloud< PointT > &tgt, 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...
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values).
Definition: point_cloud.h:418
Generic search class.
Definition: search.h:74
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415