00001 #include <iostream>
00002 #include <pcl/kdtree/kdtree_flann.h>
00003
00004 #include <hrl_phri_2011/utils.h>
00005 #include <hrl_phri_2011/hsl_rgb_conversions.h>
00006 #include <hrl_phri_2011/pcl_basic.h>
00007
00008 #define SE(x, sig) ( std::exp( - (x) / (2.0 * (sig) * (sig))) / (sig) * (sig))
00009
00010 void colorizeDataPC(const PCRGB& data_pc, PCRGB& color_pc,
00011 double saturation=100, double lightness=50, bool use_min=true)
00012 {
00013 int use_min_;
00014 ros::param::param<int>("~use_min", use_min_, 1);
00015 use_min = use_min_;
00016 vector<float> data;
00017 for(size_t i=0;i<data_pc.size();i++)
00018 data.push_back(data_pc.points[i].rgb);
00019
00020 float max_val = *std::max_element(data.begin(), data.end());
00021 float min_val = *std::min_element(data.begin(), data.end());
00022 ROS_INFO("Max data value: %f, Min data_value: %f", max_val, min_val);
00023 double h;
00024 for(size_t i=0;i<data.size();i++) {
00025 PRGB pt;
00026 pt.x = data_pc.points[i].x;
00027 pt.y = data_pc.points[i].y;
00028 pt.z = data_pc.points[i].z;
00029 if(use_min)
00030 h = (double) 240.0 * (data[i] - min_val) / (max_val - min_val);
00031 else
00032 h = (double) 240.0 * data[i] / max_val;
00033 if(h < 0) h = 0; if(h > 240.0) h = 240.0;
00034 writeHSL(240.0 - h, saturation, lightness, pt.rgb);
00035 color_pc.push_back(pt);
00036 }
00037 }
00038
00039 int main(int argc, char **argv)
00040 {
00041 ros::init(argc, argv, "density_estimation");
00042 ros::NodeHandle nh;
00043
00044 double target_force, pilot_ph, pilot_fh, percent_trim;
00045 ros::param::param<double>("~target_force", target_force, 2);
00046 ros::param::param<double>("~pilot_ph", pilot_ph, 0.02);
00047 ros::param::param<double>("~pilot_fh", pilot_fh, 0.5);
00048 ros::param::param<double>("~percent_trim", percent_trim, 0.2);
00049
00050
00051 PCRGB::Ptr head_pc;
00052 vector<PCRGB::Ptr> data_pcs;
00053 pcl::KdTreeFLANN<PRGB> head_kd_tree(new pcl::KdTreeFLANN<PRGB> ());
00054 vector<pcl::KdTreeFLANN<PRGB>::Ptr > data_kd_trees;
00055
00056 PCRGB raw_data;
00057 raw_data.header.frame_id = "/base_link";
00058 vector<PCRGB::Ptr> pc_list;
00059
00060 readBagTopic<PCRGB>(argv[1], pc_list, "/data_cloud");
00061 head_pc = pc_list[0];
00062 head_kd_tree.setInputCloud(head_pc);
00063
00064
00065 for(int i=2;i<argc-1;i++) {
00066 pc_list.clear();
00067 readBagTopic<PCRGB>(argv[i], pc_list, "/data_cloud");
00068 data_pcs.push_back(pc_list[0]);
00069 pcl::KdTreeFLANN<PRGB>::Ptr new_kd_tree(new pcl::KdTreeFLANN<PRGB> ());
00070 new_kd_tree->setInputCloud(pc_list[0]);
00071 data_kd_trees.push_back(new_kd_tree);
00072 raw_data += *pc_list[0];
00073 }
00074
00075
00076 PCRGB joint_den, pos_den, marginal_den, expected_val, force_variance;
00077 vector<int> inds; vector<float> dists;
00078 double cur_force;
00079 double joint_cur_dist, joint_kern_val, joint_kern_sum = 0.0;
00080 double pos_cur_dist, pos_kern_val, pos_kern_sum = 0.0, max_pos_kern = 0;
00081 double marginal_kern_sum = 0.0;
00082 double exp_val_numer;
00083 double force_var, cur_force_err;
00084 PRGB pt;
00088 for(size_t i=0;i<head_pc->size();i++) {
00089
00090 joint_kern_val = 0;
00091 pos_kern_val = 0;
00092 exp_val_numer = 0;
00093 force_var = 0;
00094 pt.x = head_pc->points[i].x;
00095 pt.y = head_pc->points[i].y;
00096 pt.z = head_pc->points[i].z;
00097 for(size_t k=0;k<data_kd_trees.size();k++) {
00098
00099
00100 data_kd_trees[k]->radiusSearch(*head_pc, i, pilot_ph * 3, inds, dists);
00101
00102
00103
00104 if(dists.size() != 0) {
00105
00106
00107 double user_joint_kern_val = 0, user_pos_kern_val = 0, user_exp_val = 0, user_force_var = 0;
00108 for(size_t j=0;j<dists.size();j++) {
00109
00110
00111 cur_force = data_pcs[k]->points[inds[j]].rgb;
00112
00113 joint_cur_dist = dists[j] / SQ(pilot_ph) +
00114 SQ(target_force - cur_force) / SQ(pilot_fh);
00115
00116
00117 pos_cur_dist = dists[j] / SQ(pilot_ph);
00118
00119
00120 user_joint_kern_val += exp(- 0.5 * joint_cur_dist) / (SQ(pilot_ph) * pilot_ph * pilot_fh);
00121
00122
00123
00124 double cur_pos_kern_val = exp(- 0.5 * pos_cur_dist) / (SQ(pilot_ph) * pilot_ph);
00125
00126
00127 user_pos_kern_val += cur_pos_kern_val;
00128 user_exp_val += cur_pos_kern_val * cur_force;
00129 }
00130 if(pos_kern_val > 0.01) {
00131 for(size_t j=0;j<dists.size();j++) {
00132 cur_force = data_pcs[k]->points[inds[j]].rgb;
00133 cur_force_err = cur_force - exp_val_numer / pos_kern_val;
00134 user_force_var += pos_kern_val * SQ(cur_force_err);
00135 }
00136 }
00137 joint_kern_val += user_joint_kern_val / dists.size();
00138 pos_kern_val += user_pos_kern_val / dists.size();
00139 exp_val_numer += user_exp_val / dists.size();
00140 force_var += user_force_var / dists.size();
00141 inds.clear(); dists.clear();
00142 }
00143 }
00144
00145
00146 pt.rgb = joint_kern_val;
00147 joint_den.push_back(pt);
00148 joint_kern_sum += joint_kern_val;
00149
00150
00151 if(pos_kern_val > 0.01) {
00152 pt.rgb = pos_kern_val;
00153 pos_kern_sum += pos_kern_val;
00154 max_pos_kern = max(max_pos_kern, pos_kern_val);
00155 }
00156 pos_den.push_back(pt);
00157
00158
00159 if(pos_kern_val > 0.01) {
00160 pt.rgb = joint_kern_val / pos_kern_val;
00161 marginal_kern_sum += joint_kern_val / pos_kern_val;
00162 }
00163 else
00164 pt.rgb = 0;
00165 marginal_den.push_back(pt);
00166
00167
00168 double expected_value = exp_val_numer / pos_kern_val;
00169 if(pos_kern_val > 0.01) {
00170 pt.rgb = expected_value;
00171 }
00172 else
00173 pt.rgb = 0;
00174 expected_val.push_back(pt);
00175
00176
00177
00178 double variance_numer = 0;
00179 for(size_t k=0;k<data_kd_trees.size();k++) {
00180
00181
00182 data_kd_trees[k]->radiusSearch(*head_pc, i, pilot_ph * 3, inds, dists);
00183
00184
00185
00186 if(dists.size() != 0) {
00187
00188
00189 double user_pos_kern_val = 0, user_var_numer = 0;
00190 for(size_t j=0;j<dists.size();j++) {
00191
00192
00193 cur_force = data_pcs[k]->points[inds[j]].rgb;
00194
00195 pos_cur_dist = dists[j] / SQ(pilot_ph);
00196
00197
00198 double cur_pos_kern_val = exp(- 0.5 * pos_cur_dist) / (SQ(pilot_ph) * pilot_ph);
00199
00200
00201 user_pos_kern_val += cur_pos_kern_val;
00202 user_var_numer += SQ(expected_value - cur_force) * cur_pos_kern_val;
00203 }
00204 variance_numer += user_var_numer / dists.size();
00205 inds.clear(); dists.clear();
00206 }
00207 }
00208 if(pos_kern_val > 0.01) {
00209 pt.rgb = sqrt(variance_numer / pos_kern_val);
00210 }
00211 else
00212 pt.rgb = 0;
00213 force_variance.push_back(pt);
00214 }
00215
00216 for(size_t i=0;i<pos_den.size();i++) {
00217
00218 if(pos_den.points[i].rgb < max_pos_kern * percent_trim) {
00219 pos_den.points[i].rgb = 0;
00220 marginal_den.points[i].rgb = 0;
00221 expected_val.points[i].rgb = 0;
00222 force_variance.points[i].rgb = 0;
00223 }
00224 }
00225
00226 PCRGB trimmed_joint_den, trimmed_pos_den, trimmed_marginal_den, trimmed_expected_val, trimmed_force_variance;
00227 for(size_t i=0;i<head_pc->size();i++) {
00228 if(joint_den.points[i].rgb != 0)
00229 trimmed_joint_den.push_back(joint_den.points[i]);
00230 if(pos_den.points[i].rgb != 0)
00231 trimmed_pos_den.push_back(pos_den.points[i]);
00232 if(marginal_den.points[i].rgb != 0)
00233 trimmed_marginal_den.push_back(marginal_den.points[i]);
00234 if(expected_val.points[i].rgb != 0)
00235 trimmed_expected_val.push_back(expected_val.points[i]);
00236 if(force_variance.points[i].rgb != 0)
00237 trimmed_force_variance.push_back(force_variance.points[i]);
00238 }
00239 joint_den = trimmed_joint_den;
00240 pos_den = trimmed_pos_den;
00241 marginal_den = trimmed_marginal_den;
00242 expected_val = trimmed_expected_val;
00243 force_variance = trimmed_force_variance;
00244
00245 vector<PCRGB::Ptr> pcs;
00246 vector<string> topics;
00247 PCRGB color_joint_den, color_pos_den, color_marginal_den, color_expected_val, color_force_variance,
00248 color_raw_data;
00249
00250 colorizeDataPC(joint_den, color_joint_den);
00251 color_joint_den.header.frame_id = "/base_link";
00252 pcs.push_back(color_joint_den.makeShared()); topics.push_back("/joint_density");
00253
00254 colorizeDataPC(pos_den, color_pos_den);
00255 color_pos_den.header.frame_id = "/base_link";
00256 pcs.push_back(color_pos_den.makeShared()); topics.push_back("/position_density");
00257
00258 colorizeDataPC(marginal_den, color_marginal_den);
00259 color_marginal_den.header.frame_id = "/base_link";
00260 pcs.push_back(color_marginal_den.makeShared()); topics.push_back("/marginal_density");
00261
00262 colorizeDataPC(expected_val, color_expected_val);
00263 color_expected_val.header.frame_id = "/base_link";
00264 pcs.push_back(color_expected_val.makeShared()); topics.push_back("/expected_value");
00265
00266 colorizeDataPC(force_variance, color_force_variance);
00267 color_force_variance.header.frame_id = "/base_link";
00268 pcs.push_back(color_force_variance.makeShared()); topics.push_back("/force_variance");
00269
00270 colorizeDataPC(raw_data, color_raw_data);
00271 color_raw_data.header.frame_id = "/base_link";
00272 pcs.push_back(color_raw_data.makeShared()); topics.push_back("/raw_data");
00273
00274 pcs.push_back(head_pc); topics.push_back("/head_pc");
00275
00276
00277
00278 rosbag::Bag bag;
00279 bag.open(argv[argc-1], rosbag::bagmode::Write);
00280 for(size_t i=0;i<topics.size();i++) {
00281 bag.write(topics[i], ros::Time::now(), pcs[i]);
00282 }
00283 bag.close();
00284 }