incremental_model_registration_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 #define BOOST_PARAMETER_MAX_ARITY 7
36 
37 #include <sstream>
40 #include <pcl/common/transforms.h>
41 #include <pcl/filters/extract_indices.h>
42 #include <jsk_recognition_msgs/ICPAlign.h>
43 
44 namespace jsk_pcl_ros
45 {
47  {
48 
49  }
50 
51  CapturedSamplePointCloud::CapturedSamplePointCloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud,
52  const Eigen::Affine3f& pose):
53  original_cloud_(cloud), original_pose_(pose),
54  refined_cloud_(new pcl::PointCloud<pcl::PointXYZRGB>)
55  {
56 
57  }
58 
59  pcl::PointCloud<pcl::PointXYZRGB>::Ptr
61  {
62  return original_cloud_;
63  }
64 
66  {
67  return original_pose_;
68  }
69 
70  pcl::PointCloud<pcl::PointXYZRGB>::Ptr
72  {
73  return refined_cloud_;
74  }
75 
77  {
78  return refined_pose_;
79  }
80 
82  pcl::PointCloud<pcl::PointXYZRGB> cloud)
83  {
84  *refined_cloud_ = cloud; // copying
85  }
86 
88  Eigen::Affine3f pose)
89  {
90  refined_pose_ = Eigen::Affine3f(pose);
91  }
92 
94  {
95  DiagnosticNodelet::onInit();
96  pnh_->param("frame_id", frame_id_,
97  std::string("multisense/left_camera_optical_frame"));
99  = pnh_->advertiseService(
101  this);
103  = pnh_->advertise<sensor_msgs::PointCloud2>("output/non_registered", 1);
105  = pnh_->advertise<sensor_msgs::PointCloud2>("output/registered", 1);
106  sub_cloud_.subscribe(*pnh_, "input", 1);
107  sub_indices_.subscribe(*pnh_, "input/indices", 1);
108  sub_pose_.subscribe(*pnh_, "input/pose", 1);
109  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
110  sync_->connectInput(sub_cloud_, sub_indices_, sub_pose_);
111  sync_->registerCallback(boost::bind(
113  this, _1, _2, _3));
114  onInitPostProcess();
115  }
116 
118  // message_filters::Synchronizer needs to be called reset
119  // before message_filters::Subscriber is freed.
120  // Calling reset fixes the following error on shutdown of the nodelet:
121  // terminate called after throwing an instance of
122  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
123  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
124  // Also see https://github.com/ros/ros_comm/issues/720 .
125  sync_.reset();
126  }
127 
129  pcl::PointCloud<pcl::PointXYZRGB>::Ptr input,
130  pcl::PointCloud<pcl::PointXYZRGB>::Ptr output,
131  const geometry_msgs::PoseStamped::ConstPtr& pose_msg)
132  {
133  Eigen::Affine3f posef;
134  tf::poseMsgToEigen(pose_msg->pose, posef);
135  Eigen::Affine3f transform = posef.inverse();
136 
137  pcl::transformPointCloud<pcl::PointXYZRGB>(
138  *input, *output, transform);
139  }
140 
142  const sensor_msgs::PointCloud2::ConstPtr& cloud_msg,
143  const pcl_msgs::PointIndices::ConstPtr& indices_msg,
144  const geometry_msgs::PoseStamped::ConstPtr& pose_msg)
145  {
146  boost::mutex::scoped_lock lock(mutex_);
147  pcl::PointCloud<pcl::PointXYZRGB>::Ptr
148  cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
149  pcl::PointCloud<pcl::PointXYZRGB>::Ptr
150  filtered_cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
151  pcl::fromROSMsg(*cloud_msg, *cloud);
152  pcl::PointCloud<pcl::PointXYZRGB>::Ptr
153  transformed_cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
154  pcl::PointIndices::Ptr
155  indices (new pcl::PointIndices);
156  indices->indices = indices_msg->indices;
157  pcl::ExtractIndices<pcl::PointXYZRGB> ex;
158  ex.setInputCloud(cloud);
159  ex.setIndices(indices);
160  ex.filter(*filtered_cloud);
162  filtered_cloud, transformed_cloud, pose_msg);
163  Eigen::Affine3f initial_pose;
164  if (samples_.size() == 0) {
165  // first pointcloud, it will be the `origin`
166  tf::poseMsgToEigen(pose_msg->pose, origin_);
167  initial_pose = origin_;
168  }
169  else {
170  // compute transformation from origin_
171  Eigen::Affine3f offset;
172  tf::poseMsgToEigen(pose_msg->pose, offset);
173  initial_pose = origin_.inverse() * offset;
174  }
175  CapturedSamplePointCloud::Ptr sample (new CapturedSamplePointCloud(transformed_cloud, initial_pose));
176  samples_.push_back(sample);
177 
178  all_cloud_ = all_cloud_ + *transformed_cloud;
179  sensor_msgs::PointCloud2 ros_all_cloud;
180  pcl::toROSMsg(all_cloud_, ros_all_cloud);
181  ros_all_cloud.header = cloud_msg->header;
182  pub_cloud_non_registered_.publish(ros_all_cloud);
183  }
184 
186  pcl::PointCloud<pcl::PointXYZRGB>::Ptr reference,
187  pcl::PointCloud<pcl::PointXYZRGB>::Ptr target,
188  Eigen::Affine3f& output_transform)
189  {
191  = pnh_->serviceClient<jsk_recognition_msgs::ICPAlign>("icp_service");
192  sensor_msgs::PointCloud2 reference_ros, target_ros;
193  pcl::toROSMsg(*reference, reference_ros);
194  pcl::toROSMsg(*target, target_ros);
196  reference_ros.header.stamp = target_ros.header.stamp = now;
197  reference_ros.header.frame_id = target_ros.header.frame_id = "map";
198  jsk_recognition_msgs::ICPAlign srv;
199  srv.request.reference_cloud = reference_ros;
200  srv.request.target_cloud = target_ros;
201  icp.call(srv);
202  tf::poseMsgToEigen(srv.response.result.pose,
203  output_transform);
204  }
205 
207  std_srvs::Empty::Request& req,
208  std_srvs::Empty::Response& res)
209  {
210  boost::mutex::scoped_lock lock(mutex_);
211  if (samples_.size() <= 1) {
212  ROS_ERROR("no enough samples");
213  return false;
214  }
215  ROS_INFO("Starting registration %lu samples", samples_.size());
216  // setup initial
217  CapturedSamplePointCloud::Ptr initial_sample = samples_[0];
218  initial_sample->setRefinedPointCloud(
219  *(initial_sample->getOriginalPointCloud()));
220  initial_sample->setRefinedPose(initial_sample->getOriginalPose());
221  for (size_t i = 0; i < samples_.size() - 1; i++) {
224  pcl::PointCloud<pcl::PointXYZRGB>::Ptr reference_cloud
225  = from_sample->getRefinedPointCloud();
226  pcl::PointCloud<pcl::PointXYZRGB>::Ptr target_cloud
227  = to_sample->getOriginalPointCloud();
228  Eigen::Affine3f transform;
229  callICP(reference_cloud, target_cloud, transform);
230  to_sample->setRefinedPose(to_sample->getOriginalPose() * transform);
231  // transform pointcloud
232  pcl::PointCloud<pcl::PointXYZRGB>::Ptr transformed_cloud
233  (new pcl::PointCloud<pcl::PointXYZRGB>);
234  pcl::transformPointCloud<pcl::PointXYZRGB>(
235  *target_cloud, *transformed_cloud, transform);
236  to_sample->setRefinedPointCloud(*transformed_cloud);
237  }
238  pcl::PointCloud<pcl::PointXYZRGB>::Ptr registered_cloud
239  (new pcl::PointCloud<pcl::PointXYZRGB>);
240  pcl::PointCloud<pcl::PointXYZRGB>::Ptr non_registered_cloud
241  (new pcl::PointCloud<pcl::PointXYZRGB>);
242  for (size_t i = 0; i < samples_.size(); i++) {
243  *registered_cloud = *(samples_[i]->getRefinedPointCloud()) + *registered_cloud;
244  *non_registered_cloud = *(samples_[i]->getOriginalPointCloud()) + *non_registered_cloud;
245  }
246  sensor_msgs::PointCloud2 registered_ros_cloud, nonregistered_ros_cloud;
247  pcl::toROSMsg(*registered_cloud, registered_ros_cloud);
248  registered_ros_cloud.header.stamp = ros::Time::now();
249  registered_ros_cloud.header.frame_id = frame_id_;
250  pub_registered_.publish(registered_ros_cloud);
251  pcl::toROSMsg(*non_registered_cloud, nonregistered_ros_cloud);
252  nonregistered_ros_cloud.header.stamp = ros::Time::now();
253  nonregistered_ros_cloud.header.frame_id = frame_id_;
254  pub_cloud_non_registered_.publish(nonregistered_ros_cloud);
255  return true;
256  }
257 
258 }
259 
tf::poseMsgToEigen
void poseMsgToEigen(const geometry_msgs::Pose &m, Eigen::Affine3d &e)
extract_top_polygon_likelihood.ex
ex
Definition: extract_top_polygon_likelihood.py:51
pcl
depth_error_calibration.lock
lock
Definition: depth_error_calibration.py:32
jsk_pcl_ros::IncrementalModelRegistration::origin_
Eigen::Affine3f origin_
Definition: incremental_model_registration.h:158
_2
boost::arg< 2 > _2
jsk_pcl_ros::IncrementalModelRegistration::start_registration_srv_
ros::ServiceServer start_registration_srv_
Definition: incremental_model_registration.h:149
_3
boost::arg< 3 > _3
boost::shared_ptr
i
int i
jsk_pcl_ros::IncrementalModelRegistration::sub_cloud_
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_cloud_
Definition: incremental_model_registration.h:145
jsk_pcl_ros::IncrementalModelRegistration::newsampleCallback
virtual void newsampleCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud_msg, const pcl_msgs::PointIndices::ConstPtr &indices_msg, const geometry_msgs::PoseStamped::ConstPtr &pose_msg)
Definition: incremental_model_registration_nodelet.cpp:141
jsk_pcl_ros::CapturedSamplePointCloud::getRefinedPose
virtual Eigen::Affine3f getRefinedPose()
Definition: incremental_model_registration_nodelet.cpp:76
jsk_pcl_ros::IncrementalModelRegistration::sync_
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: incremental_model_registration.h:144
jsk_pcl_ros::CapturedSamplePointCloud
Definition: incremental_model_registration.h:84
jsk_pcl_ros::CapturedSamplePointCloud::original_cloud_
pcl::PointCloud< pcl::PointXYZRGB >::Ptr original_cloud_
Definition: incremental_model_registration.h:132
jsk_pcl_ros::IncrementalModelRegistration::sub_pose_
message_filters::Subscriber< geometry_msgs::PoseStamped > sub_pose_
Definition: incremental_model_registration.h:147
jsk_pcl_ros::IncrementalModelRegistration::samples_
std::vector< CapturedSamplePointCloud::Ptr > samples_
Definition: incremental_model_registration.h:157
jsk_pcl_ros::IncrementalModelRegistration
Definition: incremental_model_registration.h:108
transform
template Halfspace< double > transform(const Halfspace< double > &a, const Transform3< double > &tf)
pcl::fromROSMsg
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
jsk_pcl_ros::IncrementalModelRegistration::sub_indices_
message_filters::Subscriber< pcl_msgs::PointIndices > sub_indices_
Definition: incremental_model_registration.h:146
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
jsk_pcl_ros::IncrementalModelRegistration::startRegistration
virtual bool startRegistration(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
Definition: incremental_model_registration_nodelet.cpp:206
cloud
cloud
pose
pose
class_list_macros.h
jsk_pcl_ros::IncrementalModelRegistration::mutex_
boost::mutex mutex_
Definition: incremental_model_registration.h:148
jsk_pcl_ros::IncrementalModelRegistration::callICP
virtual void callICP(pcl::PointCloud< pcl::PointXYZRGB >::Ptr reference, pcl::PointCloud< pcl::PointXYZRGB >::Ptr target, Eigen::Affine3f &output_transform)
Definition: incremental_model_registration_nodelet.cpp:185
jsk_pcl_ros
Definition: add_color_from_image.h:51
jsk_pcl_ros::CapturedSamplePointCloud::setRefinedPose
virtual void setRefinedPose(Eigen::Affine3f pose)
Definition: incremental_model_registration_nodelet.cpp:87
jsk_pcl_ros::IncrementalModelRegistration::pub_registered_
ros::Publisher pub_registered_
Definition: incremental_model_registration.h:151
ROS_ERROR
#define ROS_ERROR(...)
ros::ServiceClient
_1
boost::arg< 1 > _1
jsk_pcl_ros::CapturedSamplePointCloud::original_pose_
Eigen::Affine3f original_pose_
Definition: incremental_model_registration.h:134
pcl_conversion_util.h
jsk_pcl_ros::CapturedSamplePointCloud::getOriginalPose
virtual Eigen::Affine3f getOriginalPose()
Definition: incremental_model_registration_nodelet.cpp:65
jsk_pcl_ros::CapturedSamplePointCloud::getRefinedPointCloud
virtual pcl::PointCloud< pcl::PointXYZRGB >::Ptr getRefinedPointCloud()
Definition: incremental_model_registration_nodelet.cpp:71
jsk_pcl_ros::IncrementalModelRegistration::onInit
virtual void onInit()
Definition: incremental_model_registration_nodelet.cpp:93
message_filters::Subscriber::subscribe
void subscribe()
jsk_pcl_ros::IncrementalModelRegistration::frame_id_
std::string frame_id_
Definition: incremental_model_registration.h:160
jsk_pcl_ros::IncrementalModelRegistration::all_cloud_
pcl::PointCloud< pcl::PointXYZRGB > all_cloud_
Definition: incremental_model_registration.h:159
PointCloud
sensor_msgs::PointCloud2 PointCloud
jsk_pcl_ros::CapturedSamplePointCloud::setRefinedPointCloud
virtual void setRefinedPointCloud(pcl::PointCloud< pcl::PointXYZRGB > cloud)
Definition: incremental_model_registration_nodelet.cpp:81
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::IncrementalModelRegistration, nodelet::Nodelet)
nodelet::Nodelet
ros::Time
jsk_pcl_ros::CapturedSamplePointCloud::refined_cloud_
pcl::PointCloud< pcl::PointXYZRGB >::Ptr refined_cloud_
Definition: incremental_model_registration.h:133
ros::ServiceClient::call
bool call(const MReq &req, MRes &resp, const std::string &service_md5sum)
pcl::toROSMsg
void toROSMsg(const pcl::PointCloud< T > &cloud, sensor_msgs::Image &msg)
jsk_pcl_ros::CapturedSamplePointCloud::getOriginalPointCloud
virtual pcl::PointCloud< pcl::PointXYZRGB >::Ptr getOriginalPointCloud()
Definition: incremental_model_registration_nodelet.cpp:60
jsk_pcl_ros::IncrementalModelRegistration::pub_cloud_non_registered_
ros::Publisher pub_cloud_non_registered_
Definition: incremental_model_registration.h:150
jsk_pcl_ros::CapturedSamplePointCloud::refined_pose_
Eigen::Affine3f refined_pose_
Definition: incremental_model_registration.h:135
srv
srv
jsk_pcl_ros::IncrementalModelRegistration::~IncrementalModelRegistration
virtual ~IncrementalModelRegistration()
Definition: incremental_model_registration_nodelet.cpp:117
jsk_pcl_ros::CapturedSamplePointCloud::CapturedSamplePointCloud
CapturedSamplePointCloud()
Definition: incremental_model_registration_nodelet.cpp:46
jsk_pcl_ros::IncrementalModelRegistration::transformPointCloudRepsectedToPose
virtual void transformPointCloudRepsectedToPose(pcl::PointCloud< pcl::PointXYZRGB >::Ptr input, pcl::PointCloud< pcl::PointXYZRGB >::Ptr output, const geometry_msgs::PoseStamped::ConstPtr &pose_msg)
Definition: incremental_model_registration_nodelet.cpp:128
depth_error_calibration.input
input
Definition: depth_error_calibration.py:43
sample_snapit_pose_publisher.now
now
Definition: sample_snapit_pose_publisher.py:15
ROS_INFO
#define ROS_INFO(...)
sample_pointcloud_localization_client.req
req
Definition: sample_pointcloud_localization_client.py:15
sample_empty_service_caller.res
res
Definition: sample_empty_service_caller.py:14
incremental_model_registration.h
ros::Time::now
static Time now()


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