00001 #include <hrl_phri_2011/utils.h> 00002 #include <hrl_phri_2011/ellipsoid_space.h> 00003 #include <hrl_phri_2011/ForceProcessed.h> 00004 #include <hrl_phri_2011/EllipsoidParams.h> 00005 #include <pcl/kdtree/kdtree_flann.h> 00006 #include <boost/random.hpp> 00007 #include <boost/random/normal_distribution.hpp> 00008 00009 float extractValue(const hrl_phri_2011::ForceProcessed& fp, const string& value) 00010 { 00011 if(value == "force_magnitude") 00012 return fp.force_magnitude; 00013 return -9999; 00014 } 00015 00016 void extractToolFrameCloud(const vector<hrl_phri_2011::ForceProcessed::Ptr>& fp_list, const string& value, 00017 PCRGB& data_cloud) 00018 { 00019 for(size_t i=0;i<fp_list.size();i++) { 00020 PRGB pt; 00021 pt.x = fp_list[i]->tool_frame.transform.translation.x; 00022 pt.y = fp_list[i]->tool_frame.transform.translation.y; 00023 pt.z = fp_list[i]->tool_frame.transform.translation.z; 00024 pt.rgb = extractValue(*fp_list[i], value); 00025 data_cloud.points.push_back(pt); 00026 } 00027 } 00028 00029 void extractEllipsoidFrameCloud(const vector<hrl_phri_2011::ForceProcessed::Ptr>& fp_list, const string& value, 00030 PCRGB& data_cloud, Ellipsoid& e, bool use_ell_height = false) 00031 { 00032 double x, y, z, lat, lon, height; 00033 for(size_t i=0;i<fp_list.size();i++) { 00034 PRGB pt; 00035 lat = fp_list[i]->ell_coords.x; 00036 lon = fp_list[i]->ell_coords.y; 00037 if(!use_ell_height) 00038 height = fp_list[i]->ell_coords.z; 00039 else 00040 height = e.height; 00041 e.ellipsoidalToCart(lat, lon, height, x, y, z); 00042 pt.x = x; pt.y = y; pt.z = z; 00043 pt.rgb = extractValue(*fp_list[i], value); 00044 data_cloud.points.push_back(pt); 00045 } 00046 } 00047 00048 void projectCloudEllipsoid(const PCRGB& in_pc, PCRGB& out_pc, Ellipsoid& e) 00049 { 00050 double x, y, z, lat, lon, height; 00051 for(size_t i=0;i<in_pc.size();i++) { 00052 PRGB pt; 00053 x = in_pc.points[i].x; y = in_pc.points[i].y; z = in_pc.points[i].z; 00054 e.cartToEllipsoidal(x, y, z, lat, lon, height); 00055 e.ellipsoidalToCart(lat, lon, e.height, x, y, z); 00056 pt.x = x; pt.y = y; pt.z = z; 00057 pt.rgb = in_pc.points[i].rgb; 00058 out_pc.points.push_back(pt); 00059 } 00060 } 00061 00062 void projectDataEllipsoidHead(const PCRGB& head_pc, const PCRGB& ell_head_pc, const PCRGB& data_pc, 00063 PCRGB& out_pc, double noise_sigma) 00064 { 00065 boost::mt19937 rand_gen; 00066 boost::normal_distribution<> norm_dist(0.0, noise_sigma); 00067 boost::variate_generator<boost::mt19937, boost::normal_distribution<> > norm_gen(rand_gen, norm_dist); 00068 00069 pcl::KdTreeFLANN<PRGB> head_kd(new pcl::KdTreeFLANN<PRGB> ()); 00070 head_kd.setInputCloud(ell_head_pc.makeShared()); 00071 vector<int> inds(1); vector<float> dists(1); 00072 for(size_t i=0;i<data_pc.size();i++) { 00073 inds[0] = -1; 00074 PRGB pt; 00075 head_kd.nearestKSearch(data_pc, i, 1, inds, dists); 00076 pt.x = head_pc.points[inds[0]].x + norm_gen(); 00077 pt.y = head_pc.points[inds[0]].y + norm_gen(); 00078 pt.z = head_pc.points[inds[0]].z + norm_gen(); 00079 pt.rgb = data_pc.points[i].rgb; 00080 out_pc.push_back(pt); 00081 } 00082 } 00083 00084 void multiplyCloud(PCRGB& pc, double multiplier) 00085 { 00086 for(size_t i=0;i<pc.size();i++) 00087 pc.points[i].rgb = pc.points[i].rgb * multiplier; 00088 } 00089 00090 void extractContacts(PCRGB& pc, double force_thresh, double time_thresh) 00091 { 00092 PCRGB tmp_pc, new_pc; 00093 ROS_INFO("IN %d", pc.size()); 00094 for(size_t i=0;i<pc.size();i++) { 00095 if(pc.points[i].rgb > force_thresh) { 00096 tmp_pc.push_back(pc.points[i]); 00097 } else { 00098 if(tmp_pc.size() > time_thresh * 100) 00099 new_pc += tmp_pc; 00100 tmp_pc.clear(); 00101 } 00102 } 00103 pc = new_pc; 00104 ROS_INFO("OUT %d", pc.size()); 00105 } 00106 00107 int main(int argc, char **argv) 00108 { 00109 ros::init(argc, argv, "function_extractor"); 00110 ros::NodeHandle nh; 00111 if(argc < 5 || argc > 8) { 00112 printf("Usage: function_extractor force_processed_bag value output_bag output_nonproj_data [ellipsoid_registration] [user_projection] [multiplier] [noise_sigma]\n"); 00113 return 1; 00114 } 00115 vector<hrl_phri_2011::ForceProcessed::Ptr> fp_list; 00116 readBagTopic<hrl_phri_2011::ForceProcessed>(argv[1], fp_list, "/force_processed"); 00117 PCRGB data_cloud, head_pc, ell_head_pc, ell_data_pc; 00118 if(argc >= 6) { 00119 vector<hrl_phri_2011::EllipsoidParams::Ptr> ep_list; 00120 readBagTopic<hrl_phri_2011::EllipsoidParams>(argv[5], ep_list, "/ellipsoid_params"); 00121 Ellipsoid e(*ep_list[0]); 00122 if(argc >= 7) { 00123 loadRegisteredHead(argv[6], argv[5], head_pc, e); 00124 projectCloudEllipsoid(head_pc, ell_head_pc, e); 00125 extractEllipsoidFrameCloud(fp_list, argv[2], ell_data_pc, e, false); 00126 double noise_sigma = atof(argv[7]); 00127 projectDataEllipsoidHead(head_pc, ell_head_pc, ell_data_pc, data_cloud, noise_sigma); 00128 } else 00129 extractEllipsoidFrameCloud(fp_list, argv[2], data_cloud, e); 00130 } else { 00131 extractToolFrameCloud(fp_list, argv[2], data_cloud); 00132 } 00133 double force_thresh, time_thresh, multiplier; 00134 ros::param::param<double>("~force_thresh", force_thresh, -1); 00135 ros::param::param<double>("~time_thresh", time_thresh, -1); 00136 ros::param::param<double>("~multiplier", multiplier, -1); 00137 if(force_thresh != -1 && time_thresh != -1) 00138 extractContacts(data_cloud, force_thresh, time_thresh); 00139 if(multiplier != -1) 00140 multiplyCloud(data_cloud, multiplier); 00141 00142 data_cloud.header.frame_id = "/base_link"; 00143 rosbag::Bag bag; 00144 bag.open(argv[3], rosbag::bagmode::Write); 00145 bag.write("/data_cloud", ros::Time::now(), data_cloud); 00146 bag.close(); 00147 00148 ell_data_pc.header.frame_id = "/base_link"; 00149 rosbag::Bag bag2; 00150 bag2.open(argv[4], rosbag::bagmode::Write); 00151 bag2.write("/nonproj_data_cloud", ros::Time::now(), ell_data_pc); 00152 bag2.close(); 00153 }