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 
45 namespace 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_;
77 
78  public:
79 
83 
84  typedef pcl::search::Search<PointOutT> Search;
85  typedef typename Search::Ptr SearchPtr;
86 
87  /** Constructor.
88  *
89  * \param[in] num_neighbors number of neighbors to find when building
90  * a graph (default: \c 14) */
91  NearestNeighborsGraphBuilder(size_t num_neighbors = 14)
92  : num_neighbors_(num_neighbors)
93  {
94  }
95 
96  virtual void
97  compute(GraphT& graph);
98 
99  /** Set search method that will be used for finding K nearest neighbors
100  * when building a graph. */
101  inline void
102  setSearchMethod(const SearchPtr& search)
103  {
104  search_ = search;
105  }
106 
107  /** Get the search method used for finding nearest neighbors when
108  * building a graph. */
109  inline SearchPtr
111  {
112  return (search_);
113  }
114 
115  /** Set the number of neighbors to find when building a graph. */
116  inline void
117  setNumberOfNeighbors(size_t num_neighbors)
118  {
119  num_neighbors_ = num_neighbors;
120  }
121 
122  /** Returns the number of neighbors to find when building a graph. */
123  inline size_t
125  {
126  return (num_neighbors_);
127  }
128 
129  private:
130 
131  /// The search method that will be used for finding K nearest neighbors
132  /// when building a graph.
133  SearchPtr search_;
134 
135  /// The number of neighbors to find when building a graph.
136  size_t num_neighbors_;
137 
138  };
139 
140 }
141 
142 
144 
145 #endif /* PCL_GRAPH_NEAREST_NEIGHBORS_GRAPH_BUILDER_H */
146 
graph_builder.h
pcl::graph::GraphBuilder
This is an abstract base class for building a BGL-compatible point cloud graph from a point cloud.
Definition: graph_builder.h:67
pcl::graph::GraphBuilder::PointOutT
point_cloud_graph_traits< GraphT >::point_type PointOutT
Type of points in the output graph.
Definition: graph_builder.h:79
pcl::graph::GraphBuilder::PointInT
PointT PointInT
Type of points in the input cloud.
Definition: graph_builder.h:77
pcl::graph::NearestNeighborsGraphBuilder::getSearchMethod
SearchPtr getSearchMethod() const
Get the search method used for finding nearest neighbors when building a graph.
Definition: nearest_neighbors_graph_builder.h:110
pcl::graph::GraphBuilder::VertexId
boost::graph_traits< GraphT >::vertex_descriptor VertexId
Definition: graph_builder.h:81
pcl::graph::NearestNeighborsGraphBuilder
This class builds a point cloud graph representing an input dataset by using nearest neighbor search.
Definition: nearest_neighbors_graph_builder.h:67
pcl::graph
Definition: common.h:45
pcl::graph::NearestNeighborsGraphBuilder::Search
pcl::search::Search< PointOutT > Search
Definition: nearest_neighbors_graph_builder.h:84
pcl::graph::NearestNeighborsGraphBuilder::setSearchMethod
void setSearchMethod(const SearchPtr &search)
Set search method that will be used for finding K nearest neighbors when building a graph.
Definition: nearest_neighbors_graph_builder.h:102
nearest_neighbors_graph_builder.hpp
pcl::graph::NearestNeighborsGraphBuilder::getNumberOfNeighbors
size_t getNumberOfNeighbors() const
Returns the number of neighbors to find when building a graph.
Definition: nearest_neighbors_graph_builder.h:124
pcl::graph::NearestNeighborsGraphBuilder::NearestNeighborsGraphBuilder
NearestNeighborsGraphBuilder(size_t num_neighbors=14)
Constructor.
Definition: nearest_neighbors_graph_builder.h:91
pcl::graph::NearestNeighborsGraphBuilder::SearchPtr
Search::Ptr SearchPtr
Definition: nearest_neighbors_graph_builder.h:85
pcl::graph::NearestNeighborsGraphBuilder::setNumberOfNeighbors
void setNumberOfNeighbors(size_t num_neighbors)
Set the number of neighbors to find when building a graph.
Definition: nearest_neighbors_graph_builder.h:117