selected_cluster_publisher_nodelet.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Ryohei Ueda and JSK Lab
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/o2r other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the JSK Lab nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 #include "jsk_pcl_ros/selected_cluster_publisher.h"
00036 #include <pluginlib/class_list_macros.h>
00037 #include <pcl/filters/extract_indices.h>
00038 
00039 #include "jsk_recognition_utils/pcl_conversion_util.h"
00040 
00041 namespace jsk_pcl_ros
00042 {
00043   void SelectedClusterPublisher::onInit()
00044   {
00045     ConnectionBasedNodelet::onInit();
00046     pnh_->param("keep_organized", keep_organized_, false);
00047     pub_ = advertise<sensor_msgs::PointCloud2>(*pnh_, "output", 1);
00048     onInitPostProcess();
00049   }
00050 
00051   void SelectedClusterPublisher::subscribe()
00052   {
00053     sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(300); // 100 is enough?
00054     sub_input_.subscribe(*pnh_, "input", 1);
00055     sub_indices_.subscribe(*pnh_, "indices", 1);
00056     sub_index_.subscribe(*pnh_, "selected_index", 1);
00057     sync_->connectInput(sub_input_, sub_indices_, sub_index_);
00058     sync_->registerCallback(boost::bind(&SelectedClusterPublisher::extract, this, _1, _2, _3));
00059   }
00060 
00061   void SelectedClusterPublisher::unsubscribe()
00062   {
00063     sub_input_.unsubscribe();
00064     sub_indices_.unsubscribe();
00065     sub_index_.unsubscribe();
00066   }
00067 
00068   void SelectedClusterPublisher::extract(const sensor_msgs::PointCloud2::ConstPtr& input,
00069                                          const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& indices,
00070                                          const jsk_recognition_msgs::Int32Stamped::ConstPtr& index)
00071   {
00072     if (indices->cluster_indices.size() <= index->data) {
00073       NODELET_ERROR("the selected index %d is out of clusters array %lu",
00074                     index->data,
00075                     indices->cluster_indices.size());
00076       return;
00077     }
00078     pcl::PointCloud<pcl::PointXYZRGB>::Ptr input_cloud (new pcl::PointCloud<pcl::PointXYZRGB>());
00079     pcl::fromROSMsg(*input, *input_cloud);
00080     pcl::ExtractIndices<pcl::PointXYZRGB> extract;
00081     pcl::PointIndices::Ptr pcl_indices (new pcl::PointIndices);
00082     pcl_indices->indices = indices->cluster_indices[index->data].indices;
00083     extract.setInputCloud(input_cloud);
00084     extract.setIndices(pcl_indices);
00085     pcl::PointCloud<pcl::PointXYZRGB>::Ptr extracted_cloud(new pcl::PointCloud<pcl::PointXYZRGB>());
00086     if(keep_organized_){
00087       extract.setKeepOrganized(true);
00088     }
00089     extract.filter(*extracted_cloud);
00090     sensor_msgs::PointCloud2 ros_msg;
00091     pcl::toROSMsg(*extracted_cloud, ros_msg);
00092     ros_msg.header = input->header;
00093     pub_.publish(ros_msg);
00094   }
00095 }
00096 
00097 PLUGINLIB_EXPORT_CLASS (jsk_pcl_ros::SelectedClusterPublisher,
00098                         nodelet::Nodelet);


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Sun Oct 8 2017 02:43:50