bounding_box_marker.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 Willow Garage 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 <interactive_markers/interactive_marker_server.h>
00036 #include <jsk_pcl_ros/Int32Stamped.h>
00037 #include <jsk_pcl_ros/BoundingBoxArray.h>
00038 #include <ros/ros.h>
00039 #include <boost/algorithm/string.hpp>
00040 
00041 
00042 boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
00043 boost::mutex mutex;
00044 ros::Publisher pub, box_pub, box_arr_pub;
00045 jsk_pcl_ros::BoundingBoxArray::ConstPtr box_msg;
00046 
00047 void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
00048 {
00049   boost::mutex::scoped_lock(mutex);
00050   // control_name is "sec nsec index"
00051   if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_DOWN) {
00052     std::string control_name = feedback->control_name;
00053     ROS_INFO_STREAM("control_name: " << control_name);
00054     std::list<std::string> splitted_string;
00055     boost::split(splitted_string, control_name, boost::is_space());
00056     jsk_pcl_ros::Int32Stamped index;
00057     index.header.stamp.sec = boost::lexical_cast<int>(splitted_string.front());
00058     splitted_string.pop_front();
00059     index.header.stamp.nsec = boost::lexical_cast<int>(splitted_string.front());
00060     splitted_string.pop_front();
00061     index.data = boost::lexical_cast<int>(splitted_string.front());
00062     pub.publish(index);
00063     box_pub.publish(box_msg->boxes[index.data]);
00064     jsk_pcl_ros::BoundingBoxArray array_msg;
00065     array_msg.header = box_msg->header;
00066     array_msg.boxes.push_back(box_msg->boxes[index.data]);
00067     box_arr_pub.publish(array_msg);
00068   }
00069 }
00070 
00071 void boxCallback(const jsk_pcl_ros::BoundingBoxArray::ConstPtr& msg)
00072 {
00073   boost::mutex::scoped_lock(mutex);
00074   box_msg = msg;
00075   server->clear();
00076   // create cube markers
00077   for (size_t i = 0; i < msg->boxes.size(); i++) {
00078     jsk_pcl_ros::BoundingBox box = msg->boxes[i];
00079     visualization_msgs::InteractiveMarker int_marker;
00080     int_marker.header.frame_id = box.header.frame_id;
00081     int_marker.pose = box.pose;
00082     {
00083       std::stringstream ss;
00084       ss << "box" << "_" << i;
00085       int_marker.name = ss.str();
00086     }
00087     visualization_msgs::InteractiveMarkerControl control;
00088     control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00089     
00090     {
00091       std::stringstream ss;
00092       // encode several informations into control name
00093       ss << box.header.stamp.sec << " " << box.header.stamp.nsec << " " << i;
00094       control.name = ss.str();
00095     }
00096     visualization_msgs::Marker marker;
00097     marker.type = visualization_msgs::Marker::CUBE;
00098     marker.scale.x = box.dimensions.x + 0.01;
00099     marker.scale.y = box.dimensions.y + 0.01;
00100     marker.scale.z = box.dimensions.z + 0.01;
00101     marker.color.r = 1.0;
00102     marker.color.g = 1.0;
00103     marker.color.b = 1.0;
00104     marker.color.a = 0.0;
00105     control.markers.push_back(marker);
00106     control.always_visible = true;
00107     int_marker.controls.push_back(control);
00108     server->insert(int_marker);
00109     server->setCallback(int_marker.name, &processFeedback);
00110   }
00111   server->applyChanges();
00112 }
00113 
00114 int main(int argc, char** argv)
00115 {
00116   ros::init(argc, argv, "bounding_box_interactive_marker");
00117   ros::NodeHandle n, pnh("~");
00118   server.reset(new interactive_markers::InteractiveMarkerServer("bounding_box_interactive_marker", "", false));
00119   pub = pnh.advertise<jsk_pcl_ros::Int32Stamped>("selected_index", 1);
00120   box_pub = pnh.advertise<jsk_pcl_ros::BoundingBox>("selected_box", 1);
00121   box_arr_pub = pnh.advertise<jsk_pcl_ros::BoundingBoxArray>("selected_box_array", 1);
00122   ros::Subscriber sub = pnh.subscribe("bounding_box_array", 1, boxCallback);
00123   ros::spin();
00124   server.reset();
00125   return 0;
00126 }


jsk_interactive_marker
Author(s): furuta
autogenerated on Mon Oct 6 2014 01:19:15