edge_weight_computer.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_EDGE_WEIGHT_COMPUTER_CPP
39 #define PCL_GRAPH_IMPL_EDGE_WEIGHT_COMPUTER_CPP
40 
41 #include "point_cloud_graph.h"
42 #include "edge_weight_computer.h"
43 #include "utils.h"
44 
45 namespace pcl
46 {
47 
48  namespace graph
49  {
50 
51  namespace detail
52  {
53 
54  /** A predicate to be used with pcl::graph::remove_edge_if, returns
55  * \c true when the edge wight is below a certain threshold.
56  *
57  * Properly handles both plain graphs and graphs wrapped in
58  * boost::subgraph. */
59  template <typename EdgeWeightMap>
61  {
62 
63  remove_edge_predicate(const EdgeWeightMap& weights, float threshold)
64  : weights_(weights)
65  , threshold_(threshold)
66  {
67  }
68 
69  template <typename EdgeDescriptor> bool
70  operator()(EdgeDescriptor e) const
71  {
72  return (weights_[e] < threshold_);
73  }
74 
75  const EdgeWeightMap& weights_;
76  float threshold_;
77 
78  };
79 
80  /** A helper functor to determine if two points are convex with respect
81  * to each other.
82  *
83  * Returns \c false if the point type does not have *curvature* field.
84  * Otherwise the points are assumed to be relatively convex if both
85  * curvature values are positive. */
86  template <typename PointT, typename Enable = void>
87  struct IsConvex
88  {
89  bool operator()(const PointT& p1, const PointT& p2) const
90  {
91  return false;
92  }
93  };
94 
95  template <typename PointT>
96  struct IsConvex<PointT, typename std::enable_if<pcl::traits::has_curvature<PointT> >::type>
97  {
98  bool operator()(const PointT& p1, const PointT& p2) const
99  {
100  return (p1.curvature > 0.0f && p2.curvature > 0.0f);
101  }
102  };
103 
104  }
105 
106  }
107 
108 }
109 
110 template <typename GraphT>
111 template <class EdgeWeightMap> void
112 pcl::graph::EdgeWeightComputer<GraphT>::compute(GraphT& graph, EdgeWeightMap weights)
113 {
114  typename boost::graph_traits<GraphT>::edge_iterator ei, ee;
115 
116  // Step 1: do precomputation for normalized terms (if any).
117  if (g_terms_.size() || l_terms_.size())
118  {
119  for (size_t i = 0; i < g_terms_.size(); ++i)
120  {
121  g_terms_[i].init(boost::num_edges(graph));
122  }
123  for (size_t i = 0; i < l_terms_.size(); ++i)
124  {
125  l_terms_[i].init(boost::num_edges(graph), boost::num_vertices(graph));
126  }
127 
128  size_t edge_id = 0;
129  for (boost::tie(ei, ee) = boost::edges(graph); ei != ee; ++ei, ++edge_id)
130  {
131  typename boost::graph_traits<GraphT>::vertex_descriptor v1, v2;
132  v1 = boost::source(*ei, graph), v2 = boost::target(*ei, graph);
133  for (size_t i = 0; i < g_terms_.size(); ++i)
134  {
135  g_terms_[i].round1(graph[v1], graph[v2], edge_id);
136  }
137  for (size_t i = 0; i < l_terms_.size(); ++i)
138  {
139  l_terms_[i].round1(graph[v1], graph[v2], v1, v2, edge_id);
140  }
141  }
142 
143  for (size_t i = 0; i < g_terms_.size(); ++i)
144  {
145  g_terms_[i].extract();
146  }
147  for (size_t i = 0; i < l_terms_.size(); ++i)
148  {
149  l_terms_[i].extract();
150  }
151  }
152 
153  // Step 2: compute weight for each edge.
154  detail::IsConvex<PointT> is_convex;
155 
156  size_t edge_id = 0;
157  for (boost::tie(ei, ee) = boost::edges(graph); ei != ee; ++ei, ++edge_id)
158  {
159  typename boost::graph_traits<GraphT>::vertex_descriptor v1, v2;
160  v1 = boost::source(*ei, graph), v2 = boost::target(*ei, graph);
161  const PointT& p1 = graph[v1];
162  const PointT& p2 = graph[v2];
163  float a = 1.0;
164  for (size_t i = 0; i < terms_.size(); ++i)
165  {
166  a *= balancing_function_(terms_[i].compute_(p1, p2), terms_[i].getInfluence(is_convex(p1, p2)));
167  }
168  for (size_t i = 0; i < g_terms_.size(); ++i)
169  {
170  a *= balancing_function_(g_terms_[i].round2(edge_id), g_terms_[i].getInfluence(is_convex(p1, p2)));
171  }
172  for (size_t i = 0; i < l_terms_.size(); ++i)
173  {
174  a *= balancing_function_(l_terms_[i].round2(v1, v2, edge_id), l_terms_[i].getInfluence(is_convex(p1, p2)));
175  }
176  weights[*ei] = a;
177  }
178 
179  // Step 3: find edges with very small weight and modify them according to the
180  // policy set by the user.
181  switch (policy_)
182  {
183  case SMALL_WEIGHT_IGNORE:
184  {
185  break;
186  }
187  case SMALL_WEIGHT_COERCE_TO_THRESHOLD:
188  {
189  typedef typename boost::graph_traits<GraphT>::edge_iterator EdgeIterator;
190  EdgeIterator ei, ee;
191  for (boost::tie(ei, ee) = boost::edges(graph); ei != ee; ++ei)
192  if (weights[*ei] < threshold_)
193  {
194  weights[*ei] = threshold_;
195  }
196  break;
197  }
198  case SMALL_WEIGHT_REMOVE_EDGE:
199  {
200  // Note: it is only okay to remove edges this way before any subgraph
201  // was created.
202  detail::remove_edge_predicate<EdgeWeightMap> predicate(weights, threshold_);
204  re(predicate, graph);
205  break;
206  }
207  }
208 }
209 
210 #endif /* PCL_GRAPH_IMPL_EDGE_WEIGHT_COMPUTER_HPP */
211 
pcl
Definition: pcl_point_operators.cpp:4
pcl::graph::detail::IsConvex
A helper functor to determine if two points are convex with respect to each other.
Definition: edge_weight_computer.hpp:87
pcl::graph::detail::remove_edge_predicate::remove_edge_predicate
remove_edge_predicate(const EdgeWeightMap &weights, float threshold)
Definition: edge_weight_computer.hpp:63
pcl::graph::detail::remove_edge_predicate::weights_
const EdgeWeightMap & weights_
Definition: edge_weight_computer.hpp:75
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
detail
Definition: OpenCVUtil.cpp:127
pcl::graph::detail::remove_edge_predicate::threshold_
float threshold_
Definition: edge_weight_computer.hpp:76
pcl::graph::detail::IsConvex< PointT, typename std::enable_if< pcl::traits::has_curvature< PointT > >::type >::operator()
bool operator()(const PointT &p1, const PointT &p2) const
Definition: edge_weight_computer.hpp:98
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
pcl::graph::detail::remove_edge_predicate
A predicate to be used with pcl::graph::remove_edge_if, returns true when the edge wight is below a c...
Definition: edge_weight_computer.hpp:60
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
utils.h
pcl::graph::detail::IsConvex::operator()
bool operator()(const PointT &p1, const PointT &p2) const
Definition: edge_weight_computer.hpp:89
std
Definition: Application.h:66
armarx::EdgeIterator
boost::graph_traits< Graph >::edge_iterator EdgeIterator
Definition: Common.h:68
pcl::graph::remove_edge_if
remove_edge_if structure is an "extended" version of, boost::remove_edge_if that incorporates a worka...
Definition: utils.h:61
pcl::graph::EdgeWeightComputer::compute
void compute(GraphT &graph, EdgeWeightMap weights)
Compute weights for the edges in a given graph.
Definition: edge_weight_computer.hpp:112
pcl::graph::EdgeWeightComputer::PointT
pcl::graph::point_cloud_graph_traits< GraphT >::point_type PointT
Definition: edge_weight_computer.h:203
point_cloud_graph.h
edge_weight_computer.h
pcl::graph::detail::remove_edge_predicate::operator()
bool operator()(EdgeDescriptor e) const
Definition: edge_weight_computer.hpp:70