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/point_types.h>
42 #include <pcl/common/io.h>
43 #include <pcl/common/point_tests.h>
44 #include <pcl/search/kdtree.h>
45 #include <pcl/search/organized.h>
46 
48 
49 template <typename PointT, typename GraphT> void
51 {
52  if (!initCompute())
53  {
54  graph = GraphT();
55  deinitCompute();
56  return;
57  }
58 
59  size_t k = 0;
60  for (size_t i = 0; i < indices_->size(); ++i)
61  {
62  if (!pcl::isFinite(input_->operator[](indices_->operator[](i))))
63  {
64  fake_indices_ = false;
65  }
66  else
67  {
68  indices_->operator[](k++) = indices_->operator[](i);
69  }
70  }
71  indices_->resize(k);
72 
73  // Create a new point cloud which will be the basis for the constructed graph.
74  // All the fields that are also present in the output point type will be
75  // copied over from the original point cloud.
76  typename pcl::PointCloud<PointOutT>::Ptr cloud(new pcl::PointCloud<PointOutT>);
77  pcl::copyPointCloud(*input_, *indices_, *cloud);
78  graph = GraphT(cloud);
79 
80  // In case a search method has not been given, initialize it using defaults
81  if (!search_)
82  {
83  // For organized datasets, use OrganizedNeighbor
84  if (cloud->isOrganized())
85  {
86  search_.reset(new pcl::search::OrganizedNeighbor<PointOutT>);
87  }
88  // For unorganized data, use KdTree
89  else
90  {
91  search_.reset(new pcl::search::KdTree<PointOutT>);
92  }
93  }
94 
95  // Establish edges with nearest neighbors.
96  std::vector<int> neighbors(num_neighbors_ + 1);
97  std::vector<float> distances(num_neighbors_ + 1);
98  search_->setInputCloud(cloud);
99  for (size_t i = 0; i < cloud->size(); ++i)
100  {
101  // Search for num_neighbors_ + 1 because the first neighbor output by KdTree
102  // is always the query point itself.
103  search_->nearestKSearch(i, num_neighbors_ + 1, neighbors, distances);
104  for (size_t j = 1; j < neighbors.size(); ++j)
105  if (!boost::edge(i, neighbors[j], graph).second)
106  {
107  boost::add_edge(i, neighbors[j], graph);
108  }
109  }
110 
111  // Create point to vertex map
112  point_to_vertex_map_.resize(input_->size(), std::numeric_limits<VertexId>::max());
113  VertexId v = 0;
114  for (size_t i = 0; i < indices_->size(); ++i)
115  {
116  point_to_vertex_map_[indices_->operator[](i)] = v++;
117  }
118 }
119 
120 #endif /* PCL_GRAPH_IMPL_NEAREST_NEIGHBORS_GRAPH_BUILDER_HPP */
121 
pcl::graph::GraphBuilder::VertexId
boost::graph_traits< GraphT >::vertex_descriptor VertexId
Definition: graph_builder.h:81
nearest_neighbors_graph_builder.h
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
pcl::graph::NearestNeighborsGraphBuilder::compute
virtual void compute(GraphT &graph)
Build a graph based on the provided input data.
Definition: nearest_neighbors_graph_builder.hpp:50