nearest_neighbors_graph_builder.hpp
Go to the documentation of this file.
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2014-, Open Perception, Inc.
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the copyright holder(s) nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38#ifndef PCL_GRAPH_IMPL_NEAREST_NEIGHBORS_GRAPH_BUILDER_CPP
39#define PCL_GRAPH_IMPL_NEAREST_NEIGHBORS_GRAPH_BUILDER_CPP
40
41#include <pcl/common/io.h>
42#include <pcl/common/point_tests.h>
43#include <pcl/point_types.h>
44#include <pcl/search/kdtree.h>
45#include <pcl/search/organized.h>
46
48
49template <typename PointT, typename GraphT>
50void
52{
53 if (!initCompute())
54 {
55 graph = GraphT();
56 deinitCompute();
57 return;
58 }
59
60 size_t k = 0;
61 for (size_t i = 0; i < indices_->size(); ++i)
62 {
63 if (!pcl::isFinite(input_->operator[](indices_->operator[](i))))
64 {
65 fake_indices_ = false;
66 }
67 else
68 {
69 indices_->operator[](k++) = indices_->operator[](i);
70 }
71 }
72 indices_->resize(k);
73
74 // Create a new point cloud which will be the basis for the constructed graph.
75 // All the fields that are also present in the output point type will be
76 // copied over from the original point cloud.
77 typename pcl::PointCloud<PointOutT>::Ptr cloud(new pcl::PointCloud<PointOutT>);
78 pcl::copyPointCloud(*input_, *indices_, *cloud);
79 graph = GraphT(cloud);
80
81 // In case a search method has not been given, initialize it using defaults
82 if (!search_)
83 {
84 // For organized datasets, use OrganizedNeighbor
85 if (cloud->isOrganized())
86 {
87 search_.reset(new pcl::search::OrganizedNeighbor<PointOutT>);
88 }
89 // For unorganized data, use KdTree
90 else
91 {
92 search_.reset(new pcl::search::KdTree<PointOutT>);
93 }
94 }
95
96 // Establish edges with nearest neighbors.
97 std::vector<int> neighbors(num_neighbors_ + 1);
98 std::vector<float> distances(num_neighbors_ + 1);
99 search_->setInputCloud(cloud);
100 for (size_t i = 0; i < cloud->size(); ++i)
101 {
102 // Search for num_neighbors_ + 1 because the first neighbor output by KdTree
103 // is always the query point itself.
104 search_->nearestKSearch(i, num_neighbors_ + 1, neighbors, distances);
105 for (size_t j = 1; j < neighbors.size(); ++j)
106 if (!boost::edge(i, neighbors[j], graph).second)
107 {
108 boost::add_edge(i, neighbors[j], graph);
109 }
110 }
111
112 // Create point to vertex map
113 point_to_vertex_map_.resize(input_->size(), std::numeric_limits<VertexId>::max());
114 VertexId v = 0;
115 for (size_t i = 0; i < indices_->size(); ++i)
116 {
117 point_to_vertex_map_[indices_->operator[](i)] = v++;
118 }
119}
120
121#endif /* PCL_GRAPH_IMPL_NEAREST_NEIGHBORS_GRAPH_BUILDER_HPP */
boost::graph_traits< GraphT >::vertex_descriptor VertexId
std::vector< VertexId > point_to_vertex_map_
virtual void compute(GraphT &graph)
Build a graph based on the provided input data.