00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <ros/ros.h>
00040 #include <boost/thread/mutex.hpp>
00041
00042 #include <pcl/io/io.h>
00043 #include <pcl/io/pcd_io.h>
00044 #include <pcl/point_types.h>
00045
00046 #include <pcl_visualization/pcl_visualizer.h>
00047 #include <terminal_tools/print.h>
00048 #include <terminal_tools/parse.h>
00049
00050 using namespace std;
00051 using terminal_tools::print_highlight;
00052 using terminal_tools::parse_argument;
00053
00054 typedef pcl::PointXYZ Point;
00055 typedef pcl_visualization::PointCloudColorHandler<sensor_msgs::PointCloud2> ColorHandler;
00056 typedef ColorHandler::Ptr ColorHandlerPtr;
00057 typedef ColorHandler::ConstPtr ColorHandlerConstPtr;
00058
00059 sensor_msgs::PointCloud2ConstPtr cloud_, cloud_old_;
00060 boost::mutex m;
00061
00062 void
00063 cloud_cb (const sensor_msgs::PointCloud2ConstPtr& cloud)
00064 {
00065 ROS_INFO ("PointCloud with %d data points (%s), stamp %f, and frame %s.", cloud->width * cloud->height, pcl::getFieldsList (*cloud).c_str (), cloud->header.stamp.toSec (), cloud->header.frame_id.c_str ());
00066 m.lock ();
00067 cloud_ = cloud;
00068 m.unlock ();
00069 }
00070
00071
00072 int
00073 main (int argc, char** argv)
00074 {
00075 ros::init (argc, argv, "pointcloud_online_viewer");
00076 ros::NodeHandle nh;
00077
00078
00079 int queue_size = 1;
00080 parse_argument (argc, argv, "-qsize", queue_size);
00081 print_highlight ("Using a queue size of %d\n", queue_size);
00082
00083
00084 int nr_clouds = 1;
00085 parse_argument (argc, argv, "-nclouds", nr_clouds);
00086
00087
00088 ros::Subscriber sub = nh.subscribe ("input", queue_size, cloud_cb);
00089
00090 pcl_visualization::PCLVisualizer p (argc, argv, "Online PointCloud2 Viewer");
00091 pcl::PointCloud<Point> cloud_xyz;
00092 ColorHandlerPtr color_handler;
00093
00094 int color_handler_idx = 0;
00095 double psize = 0;
00096 while (nh.ok ())
00097 {
00098
00099 ros::spinOnce ();
00100 ros::Duration (0.001).sleep ();
00101 p.spinOnce (10);
00102
00103
00104 if (!cloud_)
00105 continue;
00106
00107 if (cloud_ == cloud_old_)
00108 continue;
00109
00110
00111 color_handler_idx = p.getColorHandlerIndex ("cloud");
00112
00113 p.getPointCloudRenderingProperties (pcl_visualization::PCL_VISUALIZER_POINT_SIZE, psize, "cloud");
00114
00115 p.removePointCloud ("cloud");
00116
00117 m.lock ();
00118
00119 {
00120 pcl::fromROSMsg (*cloud_, cloud_xyz);
00121
00122
00123 color_handler.reset (new pcl_visualization::PointCloudColorHandlerCustom<sensor_msgs::PointCloud2> (*cloud_, 255, 255, 255));
00124 p.addPointCloud (cloud_xyz, color_handler, "cloud");
00125
00126
00127 for (size_t i = 0; i < cloud_->fields.size (); ++i)
00128 {
00129 if (cloud_->fields[i].name == "rgb" || cloud_->fields[i].name == "rgba")
00130 color_handler.reset (new pcl_visualization::PointCloudColorHandlerRGBField<sensor_msgs::PointCloud2> (*cloud_));
00131 else
00132 color_handler.reset (new pcl_visualization::PointCloudColorHandlerGenericField<sensor_msgs::PointCloud2> (*cloud_, cloud_->fields[i].name));
00133 p.addPointCloud (cloud_xyz, color_handler, "cloud");
00134 }
00135
00136
00137 p.setPointCloudRenderingProperties (pcl_visualization::PCL_VISUALIZER_POINT_SIZE, psize, "cloud");
00138
00139
00140 if (color_handler_idx != -1)
00141 p.updateColorHandlerIndex ("cloud", color_handler_idx);
00142 cloud_old_ = cloud_;
00143 }
00144 m.unlock ();
00145 }
00146
00147 return (0);
00148 }
00149