Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
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/auto.h>
45
46
47//////////////////////////////////////////////////////////////////////////
48template <typename PointT> void
50 const pcl::PointCloud<PointT> &src,
51 double threshold,
52 const typename pcl::search::Search<PointT>::Ptr &tree,
54{
55 // We're interested in a single nearest neighbor only
56 Indices nn_indices (1);
57 std::vector<float> nn_distances (1);
58
59 // The input cloud indices that do not have a neighbor in the target cloud
60 Indices src_indices;
61
62 // Iterate through the source data set
63 for (index_t i = 0; i < static_cast<index_t> (src.size ()); ++i)
64 {
65 // Ignore invalid points in the input cloud
66 if (!isFinite (src[i]))
67 continue;
68 // Search for the closest point in the target data set (number of neighbors to find = 1)
69 if (!tree->nearestKSearch (src[i], 1, nn_indices, nn_distances))
70 {
71 PCL_WARN ("No neighbor found for point %lu (%f %f %f)!\n", i, src[i].x, src[i].y, src[i].z);
72 continue;
73 }
74 // Add points without a corresponding point in the target cloud to the output cloud
75 if (nn_distances[0] > threshold)
76 src_indices.push_back (i);
77 }
78
79 // Copy all the data fields from the input cloud to the output one
80 copyPointCloud (src, src_indices, output);
81
82 // Output is always dense, as invalid points in the input cloud are ignored
83 output.is_dense = true;
84}
85
86//////////////////////////////////////////////////////////////////////////
87//////////////////////////////////////////////////////////////////////////
88//////////////////////////////////////////////////////////////////////////
89template <typename PointT> void
91{
92 output.header = input_->header;
93
94 if (!initCompute ())
95 {
96 output.width = output.height = 0;
97 output.clear ();
98 return;
99 }
100
101 // If target is empty, input - target = input
102 if (target_->points.empty ())
103 {
104 output = *input_;
105 return;
106 }
107
108 // Initialize the spatial locator
109 if (!tree_)
110 {
111 tree_.reset (pcl::search::autoSelectMethod<PointT>(target_, false, pcl::search::Purpose::one_knn_search));
112 }
113 else
114 // Send the input dataset to the spatial locator
115 tree_->setInputCloud (target_);
116
117 getPointCloudDifference (*input_, distance_threshold_, tree_, output);
118
119 deinitCompute ();
120}
121
122#define PCL_INSTANTIATE_SegmentDifferences(T) template class PCL_EXPORTS pcl::SegmentDifferences<T>;
123#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> &);
124
PointCloud represents the base class in PCL for storing collections of 3D points.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
std::uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
std::uint32_t height
The point cloud height (if organized as an image-structure).
void clear()
Removes all points in a cloud and sets the width and height to 0.
std::size_t size() const
void segment(PointCloud &output)
Segment differences between two input point clouds.
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:208
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...
@ one_knn_search
The search method will mainly be used for nearestKSearch where k is 1.
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:56
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