$search
00001 #include "ros/ros.h" 00002 #include <ros/message_traits.h> 00003 #include <ros/serialization.h> 00004 #include "sensor_msgs/PointCloud2.h" 00005 #include "geometry_msgs/Pose.h" 00006 #include "vfh_recognizer_db/normals_vfh_db_estimator.h" 00007 #include <pcl/common/common.h> 00008 #include "household_objects_database/database_view.h" 00009 #include "household_objects_database/database_vfh.h" 00010 #include "household_objects_database/database_vfh_orientation.h" 00011 #include "household_objects_database/objects_database.h" 00012 #include "household_objects_database/database_helper_classes.h" 00013 #include <fstream> 00014 #include <iostream> 00015 #include <pcl_ros/point_cloud.h> 00016 #include <vfh_recognizer_db/vfh_recognition.h> 00017 00018 using namespace std; 00019 namespace ser = ros::serialization; 00020 00021 typedef pcl::PointCloud<pcl::PointXYZ> PointCloud; 00022 household_objects_database::ObjectsDatabase * database; 00023 00024 void 00025 getPointCloudFromView (pcl::PointCloud<pcl::PointNormal> & cloud, boost::shared_ptr< 00026 household_objects_database::DatabaseView> & view) 00027 { 00028 //database->getViewFromVFHId (vfh_id, view); 00029 database->loadFromDatabase (&view->view_point_cloud_data_); 00030 boost::shared_array<uint8_t> bufferRead (new uint8_t[view->view_point_cloud_data_.data ().size ()]); 00031 memcpy (&bufferRead[0], &(view->view_point_cloud_data_.data ()[0]), view->view_point_cloud_data_.data ().size ()); 00032 ser::IStream streamIn (bufferRead.get (), view->view_point_cloud_data_.data ().size ()); 00033 ser::deserialize (streamIn, cloud); 00034 } 00035 00036 int 00037 main (int argc, char **argv) 00038 { 00039 00040 database = new household_objects_database::ObjectsDatabase ("wgs36.willowgarage.com", "5432", "willow", "willow", 00041 "household_objects"); 00042 if (!database->isConnected ()) 00043 { 00044 std::cerr << "Database failed to connect \n"; 00045 return -1; 00046 } 00047 std::cerr << "Database connected successfully \n"; 00048 00049 //read point cloud from database 00050 std::vector<boost::shared_ptr<household_objects_database::DatabaseView> > views; 00051 std::stringstream where; 00052 where << "iteration = " << atoi (argv[1]); 00053 std::string where_clause (where.str ()); 00054 if (!database->getList (views, where_clause)) 00055 { 00056 std::cout << "No views returned" << std::endl; 00057 return false; 00058 } 00059 00060 vfh_recognizer_db::VFHRecognizerDB < flann::ChiSquareDistance > vfh_recognizer; 00061 vfh_recognizer.setApplyVoxelGrid (false); 00062 vfh_recognizer.setApplyRadiusRemoval (false); 00063 00064 std::cout << "NUMBER OF VIEWS:" << views.size () << std::endl; 00065 00066 for (size_t i = 0; i < views.size (); i++) 00067 { 00068 pcl::PointCloud < pcl::PointNormal > cloud_xyz_normals; 00069 getPointCloudFromView (cloud_xyz_normals, views[i]); 00070 00071 //remove old VFH 00072 std::vector<boost::shared_ptr<household_objects_database::DatabaseVFH> > vfhs; 00073 std::stringstream where; 00074 where << "view_id = " << views[i]->view_id_.data (); 00075 std::string where_clause (where.str ()); 00076 if (database->getList (vfhs, where_clause)) 00077 { 00078 00079 if (vfhs.size () > 0) 00080 { 00081 std::cout << "Removing old vfh:" << i << std::endl; 00082 database->deleteFromDatabase (vfhs[0].get ()); 00083 } 00084 } 00085 00086 //compute VFH 00087 pcl::PointCloud<pcl::VFHSignature308>::Ptr vfh_signaturePtr (new pcl::PointCloud<pcl::VFHSignature308> ()); 00088 pcl::PointCloud < pcl::VFHSignature308 > vfh_signature; 00089 vfh_recognizer.computeVFH (cloud_xyz_normals.makeShared (), vfh_signaturePtr); 00090 vfh_signature = *vfh_signaturePtr; 00091 00092 household_objects_database::DatabaseVFH vfh_db; 00093 vfh_db.view_id_.data () = views[i]->view_id_.data (); 00094 vfh_db.iteration_.data () = views[i]->iteration_.data (); 00095 00096 int msg_length_vfh = ser::serializationLength (vfh_signature); 00097 boost::shared_array<uint8_t> buffer_vfh (new uint8_t[msg_length_vfh]); 00098 ser::OStream stream_vfh (buffer_vfh.get (), msg_length_vfh); 00099 ser::serialize (stream_vfh, vfh_signature); 00100 00101 //copy the buffer in the underlying view structure 00102 vfh_db.vfh_descriptor_.data ().resize (msg_length_vfh); 00103 memcpy (&vfh_db.vfh_descriptor_.data ()[0], &buffer_vfh[0], vfh_db.vfh_descriptor_.data ().size ()); 00104 00105 if (!database->insertIntoDatabase (&vfh_db)) 00106 std::cerr << "VFH insertion failed\n"; 00107 else 00108 std::cout << "Inserting new vfh:" << i << std::endl; 00109 00110 if (!database->saveToDatabase (&vfh_db.vfh_descriptor_)) 00111 std::cerr << "VFH descriptor insertion failed\n"; 00112 00113 } 00114 00115 return 0; 00116 } 00117