nearest_neighbors_graph_builder.h
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_NEAREST_NEIGHBORS_GRAPH_BUILDER_H
39#define PCL_GRAPH_NEAREST_NEIGHBORS_GRAPH_BUILDER_H
40
41#include <pcl/search/search.h>
42
43#include "graph_builder.h"
44
45namespace pcl::graph
46{
47
48 /** This class builds a point cloud graph representing an input dataset by
49 * using nearest neighbor search.
50 *
51 * The points from the input cloud become vertices and the edges are
52 * established between each point and its neighbors (as found by the search
53 * object provided with setSearchMethod()). The user may choose to use the
54 * default search method, which will be either KdTree or OrganizedNeighbor
55 * depending on whether the input point cloud is organized or not.
56 *
57 * The data contained in the points of the input cloud will be copied
58 * inside the vertices of the newly created graph. Note that the points in
59 * the input cloud and the output graph may have different types, in which
60 * case only the intersecting fields will be copied over.
61 *
62 * For additional information see documentation for \ref GraphBuilder.
63 *
64 * \author Sergey Alexandrov
65 * \ingroup graph */
66 template <typename PointT, typename GraphT>
67 class PCL_EXPORTS NearestNeighborsGraphBuilder : public GraphBuilder<PointT, GraphT>
68 {
69
70 using PCLBase<PointT>::initCompute;
71 using PCLBase<PointT>::deinitCompute;
72 using PCLBase<PointT>::indices_;
73 using PCLBase<PointT>::input_;
74 using PCLBase<PointT>::use_indices_;
75 using PCLBase<PointT>::fake_indices_;
76 using GraphBuilder<PointT, GraphT>::point_to_vertex_map_;
77
78 public:
82
83 typedef pcl::search::Search<PointOutT> Search;
84 typedef typename Search::Ptr SearchPtr;
85
86 /** Constructor.
87 *
88 * \param[in] num_neighbors number of neighbors to find when building
89 * a graph (default: \c 14) */
90 NearestNeighborsGraphBuilder(size_t num_neighbors = 14) : num_neighbors_(num_neighbors)
91 {
92 }
93
94 virtual void compute(GraphT& graph);
95
96 /** Set search method that will be used for finding K nearest neighbors
97 * when building a graph. */
98 inline void
100 {
101 search_ = search;
102 }
103
104 /** Get the search method used for finding nearest neighbors when
105 * building a graph. */
106 inline SearchPtr
108 {
109 return (search_);
110 }
111
112 /** Set the number of neighbors to find when building a graph. */
113 inline void
114 setNumberOfNeighbors(size_t num_neighbors)
115 {
116 num_neighbors_ = num_neighbors;
117 }
118
119 /** Returns the number of neighbors to find when building a graph. */
120 inline size_t
122 {
123 return (num_neighbors_);
124 }
125
126 private:
127 /// The search method that will be used for finding K nearest neighbors
128 /// when building a graph.
129 SearchPtr search_;
130
131 /// The number of neighbors to find when building a graph.
132 size_t num_neighbors_;
133 };
134
135} // namespace pcl::graph
136
138
139#endif /* PCL_GRAPH_NEAREST_NEIGHBORS_GRAPH_BUILDER_H */
This is an abstract base class for building a BGL-compatible point cloud graph from a point cloud.
boost::graph_traits< GraphT >::vertex_descriptor VertexId
point_cloud_graph_traits< GraphT >::point_type PointOutT
Type of points in the output graph.
std::vector< VertexId > point_to_vertex_map_
PointT PointInT
Type of points in the input cloud.
void setSearchMethod(const SearchPtr &search)
Set search method that will be used for finding K nearest neighbors when building a graph.
size_t getNumberOfNeighbors() const
Returns the number of neighbors to find when building a graph.
void setNumberOfNeighbors(size_t num_neighbors)
Set the number of neighbors to find when building a graph.
NearestNeighborsGraphBuilder(size_t num_neighbors=14)
Constructor.
SearchPtr getSearchMethod() const
Get the search method used for finding nearest neighbors when building a graph.