feature_map_alg.cpp
Go to the documentation of this file.
00001 #include "feature_map_alg.h"
00002 
00003 FeatureMapAlgorithm::FeatureMapAlgorithm(void) :
00004     last_eval_cluster_max_score(0.0),
00005     last_eval_cluster_min_score(0.0)
00006 { }
00007 
00008 FeatureMapAlgorithm::~FeatureMapAlgorithm(void)
00009 {
00010 }
00011 
00012 void FeatureMapAlgorithm::config_update(Config& new_cfg, uint32_t level)
00013 {
00014   this->lock();
00015 
00016   // save the current configuration
00017   this->config_=new_cfg;
00018   
00019   this->unlock();
00020 }
00021 
00022 float FeatureMapAlgorithm::eval_clusters(const std::vector<float> & holy_vect, bool suppress_nonselected)
00023 {
00024   // sqr dist: dot(A-B, A-B)
00025   float *vect = new float[holy_vect.size()];
00026   int dim = holy_vect.size();
00027   memcpy(vect,&holy_vect[0],sizeof(float)*dim);
00028   Eigen::MatrixXf vect_adapt = Eigen::Map<Eigen::MatrixXf>(&vect[0],1,dim);
00029   //std::cout<<vect_adapt<<std::endl;
00030   vect_adapt = vect_adapt/vect_adapt.sum();
00031   if(isnan(vect_adapt(0)) || vect_adapt(0)==0)
00032     return 0;
00033 
00034   Eigen::MatrixXf vect_full  = this->classifiers_ - vect_adapt.replicate(this->classifiers_.rows(),1);
00035   Eigen::MatrixXf dists      = vect_full*vect_full.transpose();
00036   //std::cout << dists.rows() << " " << dists.cols() << std::endl;
00037   //std::cout << dists << std::endl;
00038   if(!suppress_nonselected)
00039   {
00040     int i=this->sel_vector_;
00041     delete [] vect;
00042     return 1.0/sqrt(dists(i,i));   //inverse to have higher==better
00043   }
00044 
00045   //suppress not selected points
00046   int sel_center=0;
00047   float min_val=dists(0,0);
00048   for(int i=1;i<this->classifiers_.rows();i++)
00049   {
00050     //vect_adapt = this->classifiers_.row(i) - vect_adapt;
00051     //std::cout<<vect_adapt<<std::endl;
00052     //vect_adapt = vect_adapt.cwiseProduct(vect_adapt);
00053     //float val=vect_adapt.norm();
00054     //std::cout<<dists(i,i)<<" "<<std::endl;
00055     if(min_val>dists(i,i))
00056     {
00057       sel_center=i;
00058       min_val=dists(i,i);
00059     }
00060   }
00061   //std::cout<<sel_center<<":"<<min_val<<"  ";
00062   delete [] vect;
00063   if(sel_center==this->sel_vector_)
00064     return 1.0/sqrt(min_val); //inverse to have higher == better
00065   return 0.;
00066 }
00067 
00068 float FeatureMapAlgorithm::eval_linear(const std::vector<float> & vect)
00069 {
00070   return 0;
00071 }
00072 
00073 float FeatureMapAlgorithm::eval_logistic(const std::vector<float> & vect)
00074 {
00075 
00076   return 0;
00077 }
00078 
00079 
00080 // FeatureMapAlgorithm Public API
00081 
00082 void FeatureMapAlgorithm::compute_feature_response_map(const normal_descriptor_node::ndesc_pc &ndesc_pc_msg, pcl::PointCloud<pcl::PointXYZRGB> &hmap)
00083 {
00084   //process descriptors
00085   hmap.width = ndesc_pc_msg.width;
00086   hmap.height = ndesc_pc_msg.height;
00087   hmap.points.resize(hmap.width*hmap.height);
00088   // reset the max score
00089   this->last_eval_cluster_max_score = 0.0;
00090   this->last_eval_cluster_min_score = 0.0;
00091   
00092   //TODO reset vector more efficiently
00093   for(int i=0; i<hmap.points.size(); i++)
00094   {
00095     hmap.points[i].x=std::numeric_limits<float>::quiet_NaN();
00096     hmap.points[i].y=std::numeric_limits<float>::quiet_NaN();
00097     hmap.points[i].z=std::numeric_limits<float>::quiet_NaN();
00098     hmap.points[i].rgb=std::numeric_limits<float>::quiet_NaN();
00099   }
00100   ROS_INFO("size %d %d ndesc %d", hmap.width, hmap.height, ndesc_pc_msg.descriptors.size());
00101   for(uint i=0; i<ndesc_pc_msg.descriptors.size(); i++)
00102   {
00103     int idx            = ndesc_pc_msg.descriptors[i].u + ndesc_pc_msg.descriptors[i].v*hmap.width;
00104     hmap.points[idx].x = ndesc_pc_msg.descriptors[i].point3d.x;
00105     hmap.points[idx].y = ndesc_pc_msg.descriptors[i].point3d.y;
00106     hmap.points[idx].z = ndesc_pc_msg.descriptors[i].point3d.z;
00107     
00108     switch(this->filter_method_)
00109     {
00110     case 0:
00111       hmap.points[idx].rgb = this->eval_clusters(ndesc_pc_msg.descriptors[i].descriptor, false);
00112       // Update max and min if needed
00113       if (fabs(hmap.points[idx].rgb) > this->last_eval_cluster_max_score) {
00114           this->last_eval_cluster_max_score = fabs(hmap.points[idx].rgb);
00115       }
00116       // Min is only update to something diferent than 0.0
00117       if (fabs(hmap.points[idx].rgb) > 0.0)
00118           if ((this->last_eval_cluster_min_score == 0.0) || 
00119              (hmap.points[idx].rgb < this->last_eval_cluster_min_score))
00120                 this->last_eval_cluster_min_score = fabs(hmap.points[idx].rgb);
00121       break;
00122     case 1:
00123       hmap.points[idx].rgb = this->eval_clusters(ndesc_pc_msg.descriptors[i].descriptor, true);
00124       // Update max and min if needed
00125       if (fabs(hmap.points[idx].rgb) > this->last_eval_cluster_max_score) {
00126           this->last_eval_cluster_max_score = fabs(hmap.points[idx].rgb);
00127       }
00128       // Min is only update to something diferent than 0.0
00129       if (fabs(hmap.points[idx].rgb) > 0.0)
00130           if ((this->last_eval_cluster_min_score == 0.0) || 
00131              (hmap.points[idx].rgb < this->last_eval_cluster_min_score))
00132                 this->last_eval_cluster_min_score = fabs(hmap.points[idx].rgb);
00133       break;
00134     case 2:
00135       ROS_ERROR("OPTION NOT IMPLEMENTED YET!");
00136       exit(-1);
00137       hmap.points[idx].rgb = this->eval_linear(ndesc_pc_msg.descriptors[i].descriptor);
00138       break;
00139     case 3:
00140       ROS_ERROR("OPTION NOT IMPLEMENTED YET!");
00141       exit(-1);
00142       hmap.points[idx].rgb = this->eval_logistic(ndesc_pc_msg.descriptors[i].descriptor);
00143       break;
00144     default:
00145       ROS_ERROR("iri_feature_map Unknown filter method");
00146     }
00147   }
00148   ROS_INFO("Exiting feature map computer.");
00149 }
00150     
00151 void FeatureMapAlgorithm::update_params(const iri_publish_params::classifier_update &params)
00152 {
00153   ROS_INFO("iri_feature_map: Updating parameters. %d vectors of size %d. Selected %d, method %d", params.update_params.size(), params.update_params[0].params.size(), params.selected_centroid, params.filter_method);
00154   this->sel_vector_ = params.selected_centroid;
00155   if(this->sel_vector_ >= params.update_params[0].params.size())
00156     ROS_ERROR("iri_feature_map: Selected vector outside range");
00157 
00158   this->filter_method_ = params.filter_method;
00159   this->classifiers_.resize(params.update_params.size(), params.update_params[0].params.size());
00160   for(int j=0;j<params.update_params.size();j++){
00161     for(int i=0;i<params.update_params[0].params.size();i++)
00162     {
00163       //init all vectors in matrix
00164       this->classifiers_(j,i) = params.update_params[j].params[i];
00165     }
00166   }
00167   ROS_INFO("iri_feature_map: Update successful!");
00168 }
00169 
00170 void
00171 FeatureMapAlgorithm::update_selected_vector(const int selected_vector)
00172 {
00173     sel_vector_ = selected_vector;
00174 }
00175 
00176 float
00177 FeatureMapAlgorithm::get_last_cluster_max_score()
00178 {
00179     return this->last_eval_cluster_max_score;
00180 }
00181 
00182 float
00183 FeatureMapAlgorithm::get_last_cluster_min_score()
00184 {
00185     return this->last_eval_cluster_min_score;
00186 }


iri_feature_map
Author(s): Arnau Ramisa
autogenerated on Fri Dec 6 2013 21:14:32