parallel_edge_finder_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
38 #include <jsk_recognition_msgs/ParallelEdgeArray.h>
39 
40 namespace jsk_pcl_ros
41 {
43  {
44  ConnectionBasedNodelet::onInit();
45 
47  // publishers
49  pub_ = advertise<jsk_recognition_msgs::ParallelEdgeArray>(*pnh_, "output_edges_groups", 1);
50  pub_clusters_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_, "output_clusters", 1);
51 
53  // dynamic reconfigure
55  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
56  dynamic_reconfigure::Server<Config>::CallbackType f =
57  boost::bind (&ParallelEdgeFinder::configCallback, this, _1, _2);
58  srv_->setCallback (f);
59  onInitPostProcess();
60  }
61 
63  // message_filters::Synchronizer needs to be called reset
64  // before message_filters::Subscriber is freed.
65  // Calling reset fixes the following error on shutdown of the nodelet:
66  // terminate called after throwing an instance of
67  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
68  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
69  // Also see https://github.com/ros/ros_comm/issues/720 .
70  sync_.reset();
71  }
72 
74  {
76  // subscription
78  sub_indices_.subscribe(*pnh_, "input_indices", 1);
79  sub_coefficients_.subscribe(*pnh_, "input_coefficients", 1);
80  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
81  sync_->connectInput(sub_indices_, sub_coefficients_);
82  sync_->registerCallback(boost::bind(&ParallelEdgeFinder::estimate,
83  this, _1, _2));
84  }
85 
87  {
90  }
91 
93  Config &config, uint32_t level)
94  {
95  boost::mutex::scoped_lock lock(mutex_);
96  angle_threshold_ = config.angular_threshold;
97  }
98 
100  const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& input_indices,
101  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& input_coefficients)
102  {
103  boost::mutex::scoped_lock lock(mutex_);
105  // first, build Line instances
107  std::vector<jsk_recognition_utils::Line::Ptr> lines;
108  if (input_coefficients->coefficients.size() == 0) {
109  return;
110  }
111  for (size_t i = 0; i < input_coefficients->coefficients.size(); i++) {
112  std::vector<float> the_coefficients
113  = input_coefficients->coefficients[i].values;
114  lines.push_back(jsk_recognition_utils::Line::fromCoefficients(the_coefficients));
115  }
116 
117  std::map<int, std::vector<int> > parallel_map;
118  for (size_t i = 0; i < lines.size() - 1; i++) {
119  parallel_map[i] = std::vector<int>();
121  for (size_t j = i + 1; j < lines.size(); j++) {
122  jsk_recognition_utils::Line::Ptr candidate_line = lines[j];
123  if (the_line->isParallel(*candidate_line, angle_threshold_)) {
124  parallel_map[i].push_back(j);
125  }
126  }
127  }
128 
129  // build 'Group' recursively
130  // list of set of the indices of parallel edges
131  std::vector<std::set<int> > parallel_groups_list;
132 
133  // set of the indices which are already regarded as one of
134  // parallel edges
135  std::set<int> listed_indices;
136 
137  for (size_t i = 0; i < lines.size() - 1; i++) {
138  std::vector<int> parallel_indices = parallel_map[i];
139  if (listed_indices.find(i) == listed_indices.end()) {
140  if (parallel_indices.size() == 0) {
141  // nothing to do
142  }
143  else {
144  std::set<int> new_parallel_set;
146  i,
147  parallel_indices,
148  new_parallel_set);
149  parallel_groups_list.push_back(new_parallel_set);
150  jsk_recognition_utils::addSet(listed_indices, new_parallel_set);
151  }
152  }
153  }
154  // publish the result
155  publishResult(parallel_groups_list, input_indices, input_coefficients);
156  publishResultAsCluser(parallel_groups_list, input_indices,
157  input_coefficients);
158  }
159 
161  const std::vector<std::set<int> >& parallel_groups_list,
162  const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& input_indices,
163  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& input_coefficients)
164  {
165  jsk_recognition_msgs::ParallelEdgeArray ros_output_msg;
166  ros_output_msg.header = input_indices->header;
167  for (size_t i = 0; i < parallel_groups_list.size(); i++) {
168  std::set<int> parallel_groups = parallel_groups_list[i];
169  jsk_recognition_msgs::ParallelEdge edge_group;
170  edge_group.header = input_indices->header;
171  for (std::set<int>::iterator it = parallel_groups.begin();
172  it != parallel_groups.end();
173  ++it) {
174  int the_index = *it;
175  std::vector<int> indices
176  = input_indices->cluster_indices[the_index].indices;
177  std::vector<float> coefficients
178  = input_coefficients->coefficients[the_index].values;
179  PCLIndicesMsg indices_msg;
180  indices_msg.header = input_indices->header;
181  indices_msg.indices = indices;
182  PCLModelCoefficientMsg coefficients_msg;
183  coefficients_msg.header = input_coefficients->header;
184  coefficients_msg.values = coefficients;
185  edge_group.cluster_indices.push_back(indices_msg);
186  edge_group.coefficients.push_back(coefficients_msg);
187  }
188  ros_output_msg.edge_groups.push_back(edge_group);
189  }
190  pub_.publish(ros_output_msg);
191  }
192 
194  const std::vector<std::set<int> >& parallel_groups_list,
195  const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& input_indices,
196  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& input_coefficients)
197  {
198  jsk_recognition_msgs::ClusterPointIndices ros_output_msg;
199  ros_output_msg.header = input_indices->header;
200 
201  for (size_t i = 0; i < parallel_groups_list.size(); i++) {
202  std::set<int> parallel_groups = parallel_groups_list[i];
203  PCLIndicesMsg indices_msg;
204  indices_msg.header = input_indices->header;
205  for (std::set<int>::iterator it = parallel_groups.begin();
206  it != parallel_groups.end();
207  ++it) {
208  indices_msg.indices = jsk_recognition_utils::addIndices(
209  indices_msg.indices,
210  input_indices->cluster_indices[*it].indices);
211  }
212  ros_output_msg.cluster_indices.push_back(indices_msg);
213  }
214  pub_clusters_.publish(ros_output_msg);
215  }
216 
217 }
218 
221 
depth_error_calibration.lock
lock
Definition: depth_error_calibration.py:32
_2
boost::arg< 2 > _2
jsk_pcl_ros::ParallelEdgeFinder::pub_
ros::Publisher pub_
Definition: parallel_edge_finder.h:161
jsk_pcl_ros::ParallelEdgeFinder::subscribe
virtual void subscribe()
Definition: parallel_edge_finder_nodelet.cpp:105
boost::shared_ptr
i
int i
jsk_pcl_ros::ParallelEdgeFinder::sync_
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: parallel_edge_finder.h:160
PCLIndicesMsg
pcl::PointIndices PCLIndicesMsg
jsk_pcl_ros::ParallelEdgeFinder::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: parallel_edge_finder.h:162
jsk_pcl_ros::ParallelEdgeFinder::unsubscribe
virtual void unsubscribe()
Definition: parallel_edge_finder_nodelet.cpp:118
jsk_pcl_ros::ParallelEdgeFinder::publishResult
virtual void publishResult(const std::vector< std::set< int > > &parallel_groups_list, const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &input_indices, const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr &input_coefficients)
Definition: parallel_edge_finder_nodelet.cpp:192
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber::unsubscribe
void unsubscribe()
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::ParallelEdgeFinder, nodelet::Nodelet)
jsk_pcl_ros::ParallelEdgeFinder::sub_coefficients_
message_filters::Subscriber< jsk_recognition_msgs::ModelCoefficientsArray > sub_coefficients_
Definition: parallel_edge_finder.h:159
jsk_pcl_ros::ParallelEdgeFinder::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: parallel_edge_finder_nodelet.cpp:124
class_list_macros.h
jsk_recognition_utils::Line::fromCoefficients
static Ptr fromCoefficients(const std::vector< float > &coefficients)
jsk_pcl_ros::ParallelEdgeFinder::~ParallelEdgeFinder
virtual ~ParallelEdgeFinder()
Definition: parallel_edge_finder_nodelet.cpp:94
PCLModelCoefficientMsg
pcl::ModelCoefficients PCLModelCoefficientMsg
jsk_pcl_ros
Definition: add_color_from_image.h:51
jsk_recognition_utils::buildGroupFromGraphMap
void buildGroupFromGraphMap(IntegerGraphMap graph_map, const int from_index, std::vector< int > &to_indices, std::set< int > &output_set)
jsk_pcl_ros::ParallelEdgeFinder::onInit
virtual void onInit()
Definition: parallel_edge_finder_nodelet.cpp:74
jsk_pcl_ros::ParallelEdgeFinder::pub_clusters_
ros::Publisher pub_clusters_
Definition: parallel_edge_finder.h:161
parallel_edge_finder.h
_1
boost::arg< 1 > _1
jsk_pcl_ros::ParallelEdgeFinder::angle_threshold_
double angle_threshold_
Definition: parallel_edge_finder.h:167
jsk_pcl_ros::ParallelEdgeFinder::mutex_
boost::mutex mutex_
Definition: parallel_edge_finder.h:163
lines
list lines
message_filters::Subscriber::subscribe
void subscribe()
jsk_recognition_utils::addIndices
pcl::PointIndices::Ptr addIndices(const pcl::PointIndices &a, const pcl::PointIndices &b)
nodelet::Nodelet
pcl_util.h
dump_depth_error.f
f
Definition: dump_depth_error.py:39
jsk_pcl_ros::ParallelEdgeFinder
Definition: parallel_edge_finder.h:89
coefficients
coefficients
jsk_pcl_ros::ParallelEdgeFinder::estimate
virtual void estimate(const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &input_indices, const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr &input_coefficients)
Definition: parallel_edge_finder_nodelet.cpp:131
jsk_pcl_ros::ParallelEdgeFinder::publishResultAsCluser
virtual void publishResultAsCluser(const std::vector< std::set< int > > &parallel_groups_list, const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &input_indices, const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr &input_coefficients)
Definition: parallel_edge_finder_nodelet.cpp:225
jsk_pcl_ros::ParallelEdgeFinder::sub_indices_
message_filters::Subscriber< jsk_recognition_msgs::ClusterPointIndices > sub_indices_
Definition: parallel_edge_finder.h:158
config
config
jsk_recognition_utils::addSet
void addSet(std::set< T > &output, const std::set< T > &new_set)


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Tue Jan 7 2025 04:05:45