bag_gui.cpp
Go to the documentation of this file.
00001 
00063 #include <ros/ros.h>
00064 #include <rosbag/bag.h>
00065 #include <rosbag/view.h>
00066 #include <sensor_msgs/PointCloud2.h>
00067 #include <pcl_conversions/pcl_conversions.h>
00068 
00069 #include <pcl/conversions.h>
00070 
00071 #include "cob_3d_mapping_common/point_types.h"
00072 #include "cob_3d_features/organized_normal_estimation_omp.h"
00073 #include "cob_3d_segmentation/depth_segmentation.h"
00074 
00075 #include "cob_3d_mapping_tools/gui/impl/core.hpp"
00076 
00077 class MainApp : public wxApp
00078 {
00079   typedef pcl::PointXYZRGB PT;
00080   typedef pcl::PointCloud<PT> PointCloud;
00081   typedef pcl::PointCloud<pcl::Normal> NormalCloud;
00082   typedef pcl::PointCloud<PointLabel> LabelCloud;
00083   typedef cob_3d_segmentation::PredefinedSegmentationTypes ST;
00084 
00085   typedef Gui::ResourceTypes::OrganizedPointCloud<PT> PC;
00086   typedef Gui::ViewTypes::Color Col;
00087 
00088   // gui stuff
00089   Gui::Core* c_;
00090   PointCloud::Ptr pc_;
00091   PointCloud::Ptr segmented_;
00092   NormalCloud::Ptr normals_;
00093   LabelCloud::Ptr labels_;
00094 
00095   Gui::Resource<PC>* r_rgb_;
00096   Gui::Resource<PC>* r_seg_;
00097   Gui::View<PC,Col>* v_rgb_;
00098   Gui::View<PC,Col>* v_seg_;
00099 
00100   // bag file stuff
00101   rosbag::Bag *p_bag;
00102   rosbag::View *p_view;
00103   ros::NodeHandle* nh;
00104   std::list<rosbag::View::iterator> timeline;
00105   std::list<rosbag::View::iterator>::iterator tl_it;
00106 
00107   cob_3d_features::OrganizedNormalEstimationOMP<PT, pcl::Normal, PointLabel> one_;
00108   cob_3d_segmentation::DepthSegmentation<ST::Graph, ST::Point, ST::Normal, ST::Label> seg_;
00109   ST::Graph::Ptr graph_;
00110 
00111 public:
00112   MainApp()
00113     : pc_(new PointCloud)
00114     , segmented_(new PointCloud)
00115     , normals_(new NormalCloud)
00116     , labels_(new LabelCloud)
00117     , graph_(new ST::Graph)
00118   {
00119     std::cout << "Create MainApp" << std::endl;
00120     c_ = Gui::Core::Get();
00121   }
00122 
00123   void compute()
00124   {
00125     one_.setInputCloud(pc_);
00126     one_.compute(*normals_);
00127     *segmented_ = *pc_;
00128     seg_.setInputCloud(pc_);
00129     seg_.performInitialSegmentation();
00130     graph_->clusters()->mapClusterColor(segmented_);
00131 
00132     ST::CH::ClusterPtr c_it;
00133     for (c_it = graph_->clusters()->begin(); c_it != graph_->clusters()->end(); ++c_it)
00134     {
00135       if (c_it->size() < 20) continue;
00136       if ( c_it->type == I_EDGE || c_it->type == I_NAN || c_it->type == I_BORDER) continue;
00137       graph_->clusters()->computeClusterComponents(c_it);
00138       //if ( (c_it->pca_point_values(1) / c_it->pca_point_values(2)) < 0.01f )
00139       if ( c_it->pca_point_values(1) < 0.00005f && c_it->pca_point_values(2) > 0.001)
00140       {
00141         std::cout << c_it->pca_point_values(1) << " / " << c_it->pca_point_values(2) << std::endl;
00142         for (ST::CH::ClusterType::iterator idx = c_it->begin(); idx != c_it->end(); ++idx)
00143         {
00144           pc_->points[*idx].rgba = 0xff0000;
00145         }
00146       }
00147     }
00148   }
00149 
00150   void mouseShowNext(wxMouseEvent& event, Gui::Resource<PC>* r)
00151   {
00152     if (event.LeftDClick() || event.RightDClick())
00153     {
00154       if(event.LeftDClick()) ++tl_it;
00155       else  --tl_it;
00156 
00157       if(tl_it == timeline.end()) { exit(0); }
00158 
00159       sensor_msgs::PointCloud2::ConstPtr last_msg = (*tl_it)->instantiate<sensor_msgs::PointCloud2>();
00160       std::cout << last_msg->header.stamp << std::endl;
00161       pcl::PCLPointCloud2 pc2;
00162       pcl_conversions::toPCL(*last_msg, pc2);
00163       pcl::fromPCLPointCloud2(pc2, *pc_);
00164       //pcl::fromROSMsg<PT>(*last_msg, *pc_);
00165       compute();
00166       r_rgb_->resourceChanged();
00167       r_seg_->resourceChanged();
00168     }
00169   }
00170 
00171   void keyShowNext(wxKeyEvent& event, Gui::Resource<PC>* r)
00172   {
00173     // 70 -> "f", 66 -> "b"
00174     //std::cout << event.GetKeyCode() << std::endl;
00175     if (event.GetKeyCode() == 70 || event.GetKeyCode() == 66)
00176     {
00177       if(event.GetKeyCode() == 70) ++tl_it;
00178       else  --tl_it;
00179 
00180       if(tl_it == timeline.end()) { exit(0); }
00181 
00182       sensor_msgs::PointCloud2::ConstPtr last_msg = (*tl_it)->instantiate<sensor_msgs::PointCloud2>();
00183       std::cout << last_msg->header.stamp << std::endl;
00184       pcl::fromROSMsg<PT>(*last_msg, *pc_);
00185       compute();
00186       r_rgb_->resourceChanged();
00187       r_seg_->resourceChanged();
00188     }
00189   }
00190 
00191   bool OnInit()
00192   {
00193     std::cout << "Init" << std::endl;
00194     if (this->argc != 3) { std::cout << " please provide path to bag file and a topic name!" << std::endl; exit(0); }
00195     std::string bag_file(wxString(this->argv[1]).mb_str());
00196     std::string topic(wxString(this->argv[2]).mb_str());
00197 
00198     char* av = "bag_gui";
00199     ros::init(argc, &av, "bag_gui");
00200     nh = new ros::NodeHandle();
00201     one_.setOutputLabels(labels_);
00202     one_.setPixelSearchRadius(8,2,2);
00203     one_.setSkipDistantPointThreshold(8);
00204 
00205     seg_.setNormalCloudIn(normals_);
00206     seg_.setLabelCloudInOut(labels_);
00207     seg_.setClusterGraphOut(graph_);
00208 
00209 
00210     p_bag = new rosbag::Bag();
00211     p_view = new rosbag::View();
00212     try { p_bag->open(bag_file, rosbag::bagmode::Read); }
00213     catch (rosbag::BagException) { std::cerr << "Error opening file " << bag_file << std::endl; return false; }
00214     p_view->addQuery(*p_bag, rosbag::TopicQuery(topic));
00215 
00216     rosbag::View::iterator vit = p_view->begin();
00217     while(vit!=p_view->end()) { timeline.push_back(vit); ++vit; }
00218     tl_it = timeline.begin();
00219 
00220     sensor_msgs::PointCloud2::ConstPtr last_msg = (*tl_it)->instantiate<sensor_msgs::PointCloud2>();
00221     pcl::fromROSMsg<PT>(*last_msg, *pc_);
00222     compute();
00223 
00224     r_rgb_ = Gui::Core::rMan()->create<PC>("r_rgb", pc_);
00225     r_seg_ = Gui::Core::rMan()->create<PC>("r_seg", segmented_);
00226     v_rgb_ = r_rgb_->createView<Col>("v_rgb");
00227     v_seg_ = r_seg_->createView<Col>("v_seg");
00228 
00229     boost::function<void (wxMouseEvent&, Gui::Resource<PC>*)> f = boost::bind(&MainApp::mouseShowNext, this, _1, _2);
00230     v_seg_ ->registerMouseCallback(f);
00231     boost::function<void (wxKeyEvent&, Gui::Resource<PC>*)> k = boost::bind(&MainApp::keyShowNext, this, _1, _2);
00232     v_seg_ ->registerKeyCallback(k);
00233 
00234     v_seg_->show();
00235     v_rgb_->show();
00236 
00237     std::cout<<"Done Init"<<std::endl;
00238     return true;
00239   }
00240 
00241   int OnExit()
00242   {
00243     std::cout << "I shut myself down!" << std::endl;
00244     if(p_bag) { p_bag->close(); delete p_bag; }
00245     delete p_view;
00246     return 0;
00247   }
00248 };
00249 
00250 IMPLEMENT_APP(MainApp)


cob_3d_mapping_tools
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:04:26