registration.hpp
Go to the documentation of this file.
00001 
00060 #include <eigen3/Eigen/Dense>
00061 
00062 template<typename _DOF6>
00063 typename _DOF6::TYPE OBJCTXT<_DOF6>::check_assumption(typename SCOR_MEMBER::Ptr m1, typename SCOR_MEMBER::Ptr m2) const
00064 {
00065   typename DOF6::TYPE w_sum=(typename DOF6::TYPE)0, p=(typename DOF6::TYPE)0;
00066   const typename DOF6::TYPE metric=(typename DOF6::TYPE)0.002;
00067 
00068   size_t goods=0;
00069   for(typename SCOR_MEMBER::MAP::const_iterator it = m1->distances_.begin(); it!=m1->distances_.end(); it++)
00070   {
00071     typename DOF6::TYPE mi = (typename DOF6::TYPE)10000;
00072 
00073     for(size_t j=0; j<it->first->candidates_.size(); j++)
00074     {
00075       typename SCOR_MEMBER::MAP::const_iterator it2 = m2->distances_.find(it->first->candidates_[j]);
00076 
00077       if(it2!=m2->distances_.end())
00078       {
00079 
00080         for(size_t k=0; k<it->second.size(); k++)
00081         {
00082           for(size_t l=0; l<it2->second.size(); l++)
00083           {
00084             //ROS_INFO("d %f %f",it->second[k], it2->second[l]);
00085             mi = std::min(mi, std::abs(it->second[k] - it2->second[l]) );
00086           }
00087         }
00088       }
00089     }
00090 
00091     //    ROS_INFO("candid with best %f",mi);
00092 
00093     if(mi<10)
00094     {
00095       p += std::pow(metric, mi);
00096       w_sum += 1;
00097     }
00098 
00099     if(std::pow(metric, mi)>0.7f)
00100       goods++;
00101   }
00102   if(w_sum) p/=w_sum;
00103 
00104 #ifdef DEBUG_
00105   ROS_INFO("Assumption is p=%f (%d, %d) goods=%d",p,(int)m1->candidates_.size(),(int)m2->candidates_.size(),goods);
00106 #endif
00107 
00108   return p;
00109 }
00110 
00111 template<typename _DOF6>
00112 void OBJCTXT<_DOF6>::findCorrespondences1(const OBJCTXT &ctxt, std::list<SCOR> &cors,
00113                                           const DOF6 &tf) const
00114                                           {
00115   std::vector<typename SCOR_MEMBER::Ptr> members, members2;
00116 
00117   for(size_t j=0; j<ctxt.objs_.size(); j++) {
00118     members2.push_back(typename SCOR_MEMBER::Ptr(new SCOR_MEMBER));
00119     members2.back()->obj_ = ctxt.objs_[j];
00120   }
00121 
00122   for(size_t i=0; i<objs_.size(); i++) {
00123     members.push_back(typename SCOR_MEMBER::Ptr(new SCOR_MEMBER));
00124     if(!objs_[i]) continue;
00125 
00126     members.back()->obj_ = objs_[i];
00127 
00128     OBJECT obj = *objs_[i];
00129     obj.transform(tf.getRotation(),tf.getTranslation(),0,0);
00130 
00131     for(size_t j=0; j<ctxt.objs_.size(); j++) {
00132       if(!ctxt.objs_[j]) continue;
00133 
00134       //if( obj.isReachable(*ctxt.objs_[j],tf.getRotationVariance(),tf.getTranslationVariance()) )
00135       if(obj.isReachable(*ctxt.objs_[j],tf.getRotationVariance(),tf.getTranslationVariance())
00136           || (
00137               (
00138                   obj.getData().intersectsBB(ctxt.objs_[j]->getData(), 0.1f /*metres*/) &&
00139                   obj.getData().canMerge(ctxt.objs_[j]->getData(),0.1f))) )
00140       {
00141         members.back()->candidates_.push_back(members2[j]);
00142         members2[j]->candidates_.push_back(members.back());
00143       }
00144     }
00145   }
00146 
00147   for(size_t i=0; i<ctxt.objs_.size(); i++) {
00148     //if(members2[i]->candidates_.size()<1) continue;
00149     for(size_t j=i+1; j<ctxt.objs_.size(); j++) {
00150       //if(members2[j]->candidates_.size()<1) continue;
00151 
00152       std::vector<typename DOF6::TYPE> d = ctxt.objs_[i]->getDistance(*ctxt.objs_[j]);
00153 
00154       members2[i]->distances_[members2[j]] = d;
00155       members2[j]->distances_[members2[i]] = d;
00156     }
00157   }
00158 
00159   for(size_t i=0; i<objs_.size(); i++) {
00160     //if(members[i]->candidates_.size()<1) continue;
00161     for(size_t j=i+1; j<objs_.size(); j++) {
00162       //if(members[j]->candidates_.size()<1) continue;
00163 
00164       std::vector<typename DOF6::TYPE> d = objs_[i]->getDistance(*objs_[j]);
00165 
00166       members[i]->distances_[members[j]] = d;
00167       members[j]->distances_[members[i]] = d;
00168     }
00169   }
00170 
00171   for(size_t i=0; i<members.size(); i++)
00172   {
00173     for(size_t j=0; j<members[i]->candidates_.size(); j++)
00174       if(check_assumption(members[i],members[i]->candidates_[j])>0.49f)
00175       {
00176         SCOR c;
00177         c.a = members[i]->obj_;
00178         c.b = members[i]->candidates_[j]->obj_;
00179         cors.push_back(c);
00180       }
00181 #ifdef DEBUG_
00182     ROS_INFO("---------------------");
00183 #endif
00184   }
00185 
00186 #ifdef DEBUG_
00187   ROS_INFO("found %d correspondences (%d %d)", (int)cors.size(), (int)objs_.size(), (int)ctxt.objs_.size());
00188 #endif
00189 
00190   return;
00191 
00192   for(size_t i=0; i<objs_.size(); i++) {
00193     if(!objs_[i]) continue;
00194 
00195     OBJECT obj = *objs_[i];
00196     obj.transform(tf.getRotation(),tf.getTranslation(),0,0);
00197 
00198     SCOR c;
00199     bool found=false;
00200     float dmin=100.f;
00201 
00202     for(size_t j=0; j<ctxt.objs_.size(); j++) {
00203       if(!ctxt.objs_[j]) continue;
00204 
00205       if( (obj)&(*ctxt.objs_[j]) )
00206       {
00207         float d=(objs_[i]->getNearestPoint()-ctxt.objs_[j]->getNearestPoint()).squaredNorm();
00208         //if(d<dmin)
00209         {
00210           dmin=d;
00211           c.a = objs_[i];
00212           c.b = ctxt.objs_[j];
00213           found=true;
00214           cors.push_back(c);
00215 
00216           std::cout<<"a1:\n"<<c.a->getNearestPoint()<<"\n";
00217           std::cout<<"b1:\n"<<c.b->getNearestPoint()<<"\n";
00218           std::cout<<"a2:\n"<<c.a->getData().getFeatures()[2].v_<<"\n";
00219           std::cout<<"b2:\n"<<c.b->getData().getFeatures()[2].v_<<"\n";
00220         }
00221 
00222 #ifdef DEBUG_
00223         if(i!=j)
00224           ROS_INFO("false cors. in sim");
00225 #endif
00226       }
00227     }
00228 
00229     //    if(found)
00230     //      cors.push_back(c);
00231   }
00232 
00233 #ifdef DEBUG_
00234   ROS_INFO("found %d correspondences", (int)cors.size());
00235 #endif
00236                                           }
00237 
00238 template<typename _DOF6>
00239 _DOF6 OBJCTXT<_DOF6>::optimizeLink1(const DOF6 &_tf, std::list<SCOR> &cors, const typename DOF6::TYPE &thr_rot, const typename DOF6::TYPE &thr_tr, const Eigen::Matrix3f &rot, const Eigen::Vector3f &tr, const int depth) const
00240 {
00241   const float noise = 0.005f;
00242   DOF6 tf;
00243   tf.deepCopy(_tf); //copy
00244 
00245   std::cout<<"rot\n"<<::DOF6::EulerAnglesf(rot)<<"\n";
00246   std::cout<<"tr\n"<<tr<<"\n";
00247   std::cout<<"thr_rot "<<thr_rot<<"\n";
00248   std::cout<<"thr_tr  "<<thr_tr<<"\n";
00249 
00250   if(!(thr_rot<1 && thr_tr<1))
00251     ROS_ERROR("(thr_rot<1 && thr_tr<1) %f %f",thr_rot,thr_tr);
00252 
00253   if(thr_tr<0.01f || thr_rot<0.005f)
00254   {
00255     std::cout<<"break because thr_\n";
00256     return _tf;
00257   }
00258 
00259   ROS_ASSERT(thr_tr>=0.01f);
00260   ROS_ASSERT(thr_rot>=0.005f);
00261 
00262   typename DOF6::SOURCE1 &magic_box(*tf.getSource1());
00263   typedef typename DOF6::SOURCE1::TFLinkObj TFLinkObj;
00264 
00265   magic_box.reset();
00266 
00267   std::map<typename OBJECT::Ptr, unsigned int> cors_a, cors_b;
00268 
00269   int used=0;
00270   int used2=0;
00271   for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00272   {
00273     typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00274     //typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, Eigen::Matrix3f::Identity(), Eigen::Vector3f::Zero());
00275 
00276     it->used_ = list.size()>0;
00277 
00278     if(it->used_)
00279     {
00280       if(cors_a.find(it->a)==cors_a.end())
00281         cors_a[it->a]=0;
00282       if(cors_b.find(it->b)==cors_b.end())
00283         cors_b[it->b]=0;
00284       cors_a[it->a]++;
00285       cors_b[it->b]++;
00286       ++used;
00287     }
00288 
00289     if(list.size()) ++used2;
00290   }
00291   ROS_INFO("USED %d %d",used,used2);
00292 
00293   for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00294   {
00295     typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00296     //typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, Eigen::Matrix3f::Identity(), Eigen::Vector3f::Zero());
00297 
00298     it->used_ = list.size()>0;
00299 
00300     const float w = std::max(1.f/(cors_a[it->a]*cors_a[it->a]), 1.f/(cors_b[it->b]*cors_b[it->b]));
00301 
00302     for(typename OBJECT::TFLIST::iterator k = list.begin(); k!=list.end(); k++)
00303     {
00304       k->a.weight_R_ *= w;
00305       k->a.weight_t_ *= w;
00306       k->b.weight_R_ *= w;
00307       k->b.weight_t_ *= w;
00308       magic_box(k->a, k->b);
00309     }
00310   }
00311 
00312   magic_box.finish();
00313 
00314   if(magic_box.getTranslationVariance()>100)
00315     magic_box.setTranslation(tr);
00316   if(magic_box.getRotationVariance()>100)
00317     magic_box.setRotation(rot);
00318 
00319   //  if(!pcl_isfinite(thr))
00320   //    //return optimizeLink(_tf, cors, tf.getRotationVariance()+tf.getTranslationVariance(), tf.getRotation(), tf.getTranslation());
00321   //    return optimizeLink(_tf, cors, 1.5, tf.getRotation(), tf.getTranslation());
00322   //  else
00323   if(depth<1 || thr_tr>0.05f || thr_rot>0.05f) {
00324 
00325 #ifdef DEBUG_
00326     //DEBUG
00327     for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00328     {
00329       if(!it->used_)
00330       {
00331         typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00332         for(typename OBJECT::TFLIST::const_iterator k = list.begin(); k!=list.end(); k++)
00333         {
00334           Debug::Interface::get().addArrow(k->a.next_point_, k->b.next_point_, 0,255,0);
00335         }
00336         //Debug::Interface::get().addArrow(it->a->getNearestPoint(),it->b->getNearestPoint(), 0,255,0);
00337       }
00338     }
00339     //DEBUG
00340 #endif
00341 
00342     if(depth<1 || (thr_tr>0.05f && thr_rot>0.05f))
00343       return optimizeLink1(_tf, cors, thr_rot*0.6f, thr_tr*0.6f, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00344     else if(thr_tr>0.05f)
00345       return optimizeLink1(_tf, cors, thr_rot, thr_tr*0.6f, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00346     else
00347       return optimizeLink1(_tf, cors, thr_rot*0.6f, thr_tr, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00348   }
00349 
00350 #ifdef DEBUG_
00351   //DEBUG
00352   std::cout<<"WEIGHT LIST\n";
00353   for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00354   {
00355     if(it->used_)
00356     {
00357       float w=0.f;
00358       typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00359       for(typename OBJECT::TFLIST::const_iterator k = list.begin(); k!=list.end(); k++)
00360       {
00361         //        if( k->a.plane_ )
00362         //        {
00363         //          Debug::Interface::get().addArrow(it->a->getNearestPoint(),it->a->getNearestPoint()+0.1f*k->a.rotation_n_/k->a.length_,100);
00364         //          Debug::Interface::get().addArrow(it->b->getNearestPoint(),it->b->getNearestPoint()+0.1f*k->b.rotation_n_/k->b.length_,255,100);
00365         //        }
00366         //        else
00367         //if(k->a.weight_R_>0.05f)
00368         Debug::Interface::get().addArrow(k->a.next_point_, k->b.next_point_, 128*k->a.weight_R_,128*k->a.weight_R_,128*k->a.weight_R_);
00369         w += k->a.weight_R_;
00370         std::cout<<"WEIGHTXX "<<k->a.weight_R_<<"  "<<k->b.weight_R_<<"\n";
00371       }
00372       std::cout<<"WEIGHT "<<it->a->getData().getWeight()<<"  "<<it->b->getData().getWeight()<<"  "<<w<<(it->a->getData().isPlane()?" P":" ")<<(it->b->getData().isPlane()?" P":" ")<<"\n";
00373     }
00374     else
00375     {
00376       Debug::Interface::get().addArrow(it->a->getNearestPoint(),it->b->getNearestPoint(), 0,255,0);
00377     }
00378   }
00379   Debug::Interface::get().addArrow(Eigen::Vector3f::Zero(), tf.getTranslation());
00380 
00381   for(size_t i=0; i<objs_.size(); i++)
00382   {
00383     if(!objs_[i]->getData().isPlane()) continue;
00384 
00385     //Debug::Interface::get().addArrow((Eigen::Matrix3f)tf.getRotation()*objs_[i]->getNearestPoint()+tf.getTranslation(),Eigen::Vector3f::Zero(), 100,0,100);
00386     Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),Eigen::Vector3f::Zero(), 0,255,0);
00387   }
00388   //DEBUG
00389 #endif
00390 
00391   if(//(size_t)used2*4<cors.size() ||
00392       (_tf.getRotation()-tf.getRotation()).norm()>_tf.getRotationVariance() || (_tf.getTranslation()-tf.getTranslation()).norm()>_tf.getTranslationVariance())
00393   {
00394     std::cout<<"OLD\n"<<_tf<<"\n";
00395     std::cout<<"NEW\n"<<tf<<"\n";
00396     if((size_t)used2*4<cors.size())
00397       ROS_INFO("failed reason 1");
00398     if((_tf.getRotation()-tf.getRotation()).norm()>_tf.getRotationVariance())
00399       ROS_INFO("failed reason 2");
00400     if((_tf.getTranslation()-tf.getTranslation()).norm()>_tf.getTranslationVariance())
00401       ROS_INFO("failed reason 3");
00402     tf.getSource1()->reset();
00403   }
00404   std::cout<<"R1\n"<<_tf.getRotation()<<"\n";
00405   std::cout<<"R2\n"<< tf.getRotation()<<"\n";
00406   std::cout<<"rot dist: "<<(_tf.getRotation()-tf.getRotation()).norm()<<"\n";
00407   std::cout<<"tr dist: "<<(_tf.getTranslation()-tf.getTranslation()).norm()<<"\n";
00408   std::cout<<"rot dist A: "<<_tf.getRotationVariance()<<"\n";
00409   std::cout<<"tr dist A: "<<_tf.getTranslationVariance()<<"\n";
00410   ROS_INFO("-------///\\\\\------");
00411 
00412   return tf;
00413 }
00414 
00415 template<typename _DOF6>
00416 _DOF6 OBJCTXT<_DOF6>::optimizeLink2(const DOF6 &_tf, std::list<SCOR> &cors, const typename DOF6::TYPE &thr_rot, const typename DOF6::TYPE &thr_tr, const Eigen::Matrix3f &rot, const Eigen::Vector3f &tr, const int depth) const
00417 {
00418   const float noise = 0.005f;
00419   DOF6 tf;
00420   tf.deepCopy(_tf); //copy
00421 
00422   std::cout<<"rot\n"<<::DOF6::EulerAnglesf(rot)<<"\n";
00423   std::cout<<"tr\n"<<tr<<"\n";
00424   std::cout<<"thr_rot "<<thr_rot<<"\n";
00425   std::cout<<"thr_tr  "<<thr_tr<<"\n";
00426 
00427   if(!(thr_rot<1 && thr_tr<1))
00428     ROS_ERROR("(thr_rot<1 && thr_tr<1) %f %f",thr_rot,thr_tr);
00429 
00430   if(thr_tr<0.01f || thr_rot<0.005f)
00431   {
00432     std::cout<<"break because thr_\n";
00433     return _tf;
00434   }
00435 
00436   ROS_ASSERT(thr_tr>=0.01f);
00437   ROS_ASSERT(thr_rot>=0.005f);
00438 
00439   typename DOF6::SOURCE1 &magic_box(*tf.getSource1());
00440   typedef typename DOF6::SOURCE1::TFLinkObj TFLinkObj;
00441 
00442   magic_box.reset();
00443 
00444   std::map<typename OBJECT::Ptr, SCOR> cors_a;
00445 
00446   int used=0;
00447   int used2=0;
00448   for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00449   {
00450     typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00451     //typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, Eigen::Matrix3f::Identity(), Eigen::Vector3f::Zero());
00452 
00453     it->used_ = list.size()>0;
00454 
00455     if(it->used_)
00456     {
00457       const float s = it->a->getSimilarity( *it->b );
00458       std::cout<<"similiarity "<<s<<std::endl;
00459       if(cors_a.find(it->a)==cors_a.end() || s>cors_a[it->a].prob)
00460       {
00461         cors_a[it->a].prob=s;
00462         cors_a[it->a].b=it->b;
00463         ++used;
00464       }
00465     }
00466 
00467     if(list.size()) ++used2;
00468   }
00469   ROS_INFO("USED %d %d",used,used2);
00470 
00471   for(typename std::map<typename OBJECT::Ptr, SCOR>::const_iterator it = cors_a.begin(); it!=cors_a.end(); it++)
00472   {
00473     typename OBJECT::TFLIST list = it->first->getTFList(*it->second.b, thr_rot+noise, thr_tr+noise, rot, tr);
00474     //typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, Eigen::Matrix3f::Identity(), Eigen::Vector3f::Zero());
00475 
00476     //it->used_ = list.size()>0;
00477 
00478     for(typename OBJECT::TFLIST::const_iterator k = list.begin(); k!=list.end(); k++)
00479     {
00480       magic_box(k->a, k->b);
00481     }
00482   }
00483 
00484   magic_box.finish();
00485 
00486   if(magic_box.getTranslationVariance()>100)
00487     magic_box.setTranslation(tr);
00488   if(magic_box.getRotationVariance()>100)
00489     magic_box.setRotation(rot);
00490 
00491   if(depth<1 || thr_tr>0.05f || thr_rot>0.05f) {
00492 
00493 #ifdef DEBUG_
00494     //DEBUG
00495     for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00496     {
00497       if(!it->used_)
00498       {
00499         typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00500         for(typename OBJECT::TFLIST::const_iterator k = list.begin(); k!=list.end(); k++)
00501         {
00502           Debug::Interface::get().addArrow(k->a.next_point_, k->b.next_point_, 0,255,0);
00503         }
00504         //Debug::Interface::get().addArrow(it->a->getNearestPoint(),it->b->getNearestPoint(), 0,255,0);
00505       }
00506     }
00507     //DEBUG
00508 #endif
00509 
00510     if(depth<1 || (thr_tr>0.05f && thr_rot>0.05f))
00511       return optimizeLink2(_tf, cors, thr_rot*0.6f, thr_tr*0.6f, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00512     else if(thr_tr>0.05f)
00513       return optimizeLink2(_tf, cors, thr_rot, thr_tr*0.6f, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00514     else
00515       return optimizeLink2(_tf, cors, thr_rot*0.6f, thr_tr, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00516   }
00517 
00518 #ifdef DEBUG_
00519   //DEBUG
00520   std::cout<<"WEIGHT LIST\n";
00521   for(typename std::list<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00522   {
00523     if(it->used_)
00524     {
00525       float w=0.f;
00526       typename OBJECT::TFLIST list = it->a->getTFList(*it->b, thr_rot+noise, thr_tr+noise, rot, tr);
00527       for(typename OBJECT::TFLIST::const_iterator k = list.begin(); k!=list.end(); k++)
00528       {
00529         //if(k->a.weight_R_>0.05f)
00530         Debug::Interface::get().addArrow(k->a.next_point_, k->b.next_point_, 128*k->a.weight_R_,128*k->a.weight_R_,128*k->a.weight_R_);
00531         w += k->a.weight_R_;
00532         std::cout<<"WEIGHTXX "<<k->a.weight_R_<<"  "<<k->b.weight_R_<<"\n";
00533       }
00534       std::cout<<"WEIGHT "<<it->a->getData().getWeight()<<"  "<<it->b->getData().getWeight()<<"  "<<w<<(it->a->getData().isPlane()?" P":" ")<<(it->b->getData().isPlane()?" P":" ")<<"\n";
00535     }
00536     else
00537     {
00538       Debug::Interface::get().addArrow(it->a->getNearestPoint(),it->b->getNearestPoint(), 0,255,0);
00539     }
00540   }
00541   Debug::Interface::get().addArrow(Eigen::Vector3f::Zero(), tf.getTranslation());
00542 
00543   for(size_t i=0; i<objs_.size(); i++)
00544   {
00545     if(!objs_[i]->getData().isPlane()) continue;
00546 
00547     //Debug::Interface::get().addArrow((Eigen::Matrix3f)tf.getRotation()*objs_[i]->getNearestPoint()+tf.getTranslation(),Eigen::Vector3f::Zero(), 100,0,100);
00548     Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),Eigen::Vector3f::Zero(), 0,255,0);
00549   }
00550   //DEBUG
00551 #endif
00552 
00553   if(//(size_t)used2*4<cors.size() ||
00554       (_tf.getRotation()-tf.getRotation()).norm()>_tf.getRotationVariance() || (_tf.getTranslation()-tf.getTranslation()).norm()>_tf.getTranslationVariance())
00555   {
00556     std::cout<<"OLD\n"<<_tf<<"\n";
00557     std::cout<<"NEW\n"<<tf<<"\n";
00558     if((size_t)used2*4<cors.size())
00559       ROS_INFO("failed reason 1");
00560     if((_tf.getRotation()-tf.getRotation()).norm()>_tf.getRotationVariance())
00561       ROS_INFO("failed reason 2");
00562     if((_tf.getTranslation()-tf.getTranslation()).norm()>_tf.getTranslationVariance())
00563       ROS_INFO("failed reason 3");
00564     tf.getSource1()->reset();
00565   }
00566   std::cout<<"R1\n"<<_tf.getRotation()<<"\n";
00567   std::cout<<"R2\n"<< tf.getRotation()<<"\n";
00568   std::cout<<"rot dist: "<<(_tf.getRotation()-tf.getRotation()).norm()<<"\n";
00569   std::cout<<"tr dist: "<<(_tf.getTranslation()-tf.getTranslation()).norm()<<"\n";
00570   std::cout<<"rot dist A: "<<_tf.getRotationVariance()<<"\n";
00571   std::cout<<"tr dist A: "<<_tf.getTranslationVariance()<<"\n";
00572   ROS_INFO("-------///\\\\\------");
00573 
00574   return tf;
00575 }
00576 
00577 
00578 
00579 template<typename _DOF6>
00580 void OBJCTXT<_DOF6>::findCorrespondences3(const OBJCTXT &ctxt, std::vector<SCOR> &cors,
00581                                           const DOF6 &tf) {
00582   map_cors_.clear();
00583 
00584   Eigen::Matrix4f M = tf.getTF4().inverse();
00585   Eigen::Vector3f t=M.col(3).head<3>();
00586   Eigen::Matrix3f R=M.topLeftCorner(3,3);
00587 
00588   const float thr = tf.getRotationVariance()+0.05f;
00589 
00590   for(size_t j=0; j<ctxt.objs_.size(); j++)
00591   {
00592     OBJECT obj = *ctxt.objs_[j];
00593     obj.transform(R,t,0,0);
00594 
00595     for(size_t i=0; i<objs_.size(); i++)
00596       if( (obj.getData().getBB().preassumption(objs_[i]->getData().getBB())>=std::cos(thr+objs_[i]->getData().getBB().ratio())) &&
00597           obj.intersectsBB(*objs_[i], tf.getRotationVariance(),tf.getTranslationVariance())
00598           && obj.getData().extensionMatch(objs_[i]->getData(),0.7f,0)
00599       )
00600       {
00601         if(obj.intersectsPts(*objs_[i], tf.getRotationVariance(),tf.getTranslationVariance()) ||
00602             objs_[i]->intersectsPts(obj, tf.getRotationVariance(),tf.getTranslationVariance()))
00603         {
00604           //seccond check
00605           map_cors_[ctxt.objs_[j]].push_back(cors.size());
00606           SCOR c;
00607           c.a = objs_[i];
00608           c.b = ctxt.objs_[j];
00609           cors.push_back(c);
00610         }
00611       }
00612 
00613   }
00614 
00615 #ifdef DEBUG_
00616   ROS_INFO("found %d correspondences (%d %d)", (int)cors.size(), (int)objs_.size(), (int)ctxt.objs_.size());
00617 
00618   for(typename std::vector<SCOR>::iterator it = cors.begin(); it!=cors.end(); it++)
00619   {
00620     Debug::Interface::get().addArrow(it->a->getNearestPoint(),it->b->getNearestPoint(), 0,255,0);
00621   }
00622 #endif
00623 
00624 }
00625 
00626 template<typename _DOF6>
00627 _DOF6 OBJCTXT<_DOF6>::optimizeLink3(const OBJCTXT &ctxt, std::vector<SCOR> &cors, const DOF6 &_tf, const typename DOF6::TYPE &thr_rot, const typename DOF6::TYPE &thr_tr, const Eigen::Matrix3f &rot, const Eigen::Vector3f &tr, const int depth) const
00628 {
00629   const float noiseT = 0.03f, noiseR = 0.01f;
00630   const float _factor = 0.92f;
00631 
00632   DOF6 tf;
00633   tf.deepCopy(_tf); //copy
00634 
00635   const bool goon = depth<10 && !(thr_tr<0.01f && thr_rot<0.005f);// || thr_tr>0.05f || thr_rot>0.05f;
00636 
00637   std::cout<<"rot\n"<<::DOF6::EulerAnglesf(rot)<<"\n";
00638   std::cout<<"tr\n"<<tr<<"\n";
00639   std::cout<<"thr_rot "<<thr_rot<<"\n";
00640   std::cout<<"thr_tr  "<<thr_tr<<"\n";
00641 
00642   if(!(thr_rot<1 && thr_tr<1))
00643     ROS_ERROR("(thr_rot<1 && thr_tr<1) %f %f",thr_rot,thr_tr);
00644 
00645 //  ROS_ASSERT(thr_tr>=0.01f);
00646 //  ROS_ASSERT(thr_rot>=0.005f);
00647 
00648   //debug
00649   int used=0;
00650   int used2=0;
00651   int used3=0;
00652 
00653   typename DOF6::SOURCE1 &magic_box(*tf.getSource1());
00654   typedef typename DOF6::SOURCE1::TFLinkObj TFLinkObj;
00655 
00656   magic_box.reset();
00657 
00658   for(size_t i=0; i<ctxt.objs_.size(); i++)
00659   {
00660     typename std::map<typename OBJECT::Ptr,std::vector<size_t> >::const_iterator it = map_cors_.find(ctxt.objs_[i]);
00661     if(it==map_cors_.end()) continue;
00662 
00663     size_t n = 0;
00664 
00665     typename OBJECT::TFLIST list;
00666     for(size_t j=0; j<it->second.size(); j++)
00667     {
00668       size_t l = list.size();
00669 
00670       cors[it->second[j]].a->addTFList(*ctxt.objs_[i], thr_rot+noiseR, thr_tr+noiseT, rot, tr, list);
00671 
00672       if(list.size()>l) {
00673         ++used2;
00674         ++n;
00675         cors[it->second[j]].used_ = true;
00676       }
00677       else
00678         cors[it->second[j]].used_ = false;
00679 
00680       used3++;
00681       if(!goon) {
00682 #ifdef DEBUG_
00683           Debug::Interface::get().addArrow(ctxt.objs_[i]->getNearestPoint(),rot*cors[it->second[j]].a->getNearestPoint()+tr,255,255,255);
00684 #endif
00685       }
00686     }
00687 
00688     //const float w = std::log(ctxt.objs_[i]->getData().getWeight());
00689     float wsum = 0.f;
00690     for(typename OBJECT::TFLIST::const_iterator k = list.begin(); k!=list.end(); k++)
00691     {
00692       ++used;
00693       wsum += k->a.weight_R_ + k->a.weight_t_;
00694     }
00695 
00696     for(typename OBJECT::TFLIST::iterator k = list.begin(); k!=list.end(); k++)
00697     {
00698 //      k->a.weight_R_ *= w/wsum;
00699 //      k->a.weight_t_ *= w/wsum;
00700       //      k->b.weight_R_ *= w/wsum;
00701       //      k->b.weight_t_ *= w/wsum;
00702       k->a.weight_R_ /= n*n;
00703       k->a.weight_t_ /= n*n;
00704       k->b.weight_R_ /= n*n;
00705       k->b.weight_t_ /= n*n;
00706 
00707       if(k->a.weight_R_==k->b.weight_R_ && k->a.weight_t_==k->b.weight_t_)
00708       {
00709         magic_box(k->a, k->b);
00710 
00711 #ifdef DEBUG_
00712         if(!goon)
00713         {
00714           //ROS_INFO("WEIGHT %f %f",k->a.weight_R_,k->a.weight_t_);
00715           Debug::Interface::get().addArrow(rot*k->a.next_point_+tr,k->b.next_point_,0,0,255);
00716         }
00717         else if(goon)
00718         {
00719           Debug::Interface::get().addArrow(rot*k->a.next_point_+tr,k->b.next_point_,0,101,0);
00720         }
00721 #endif
00722 
00723       }
00724     }
00725   }
00726 
00727   ROS_INFO("USED %d %d %d",used,used2,used3);
00728 
00729   magic_box.finish();
00730 
00731   std::cout<<"magic_box\n"<<magic_box<<"\n";
00732 
00733   if(magic_box.getTranslationVariance()>100)
00734     magic_box.setTranslation(tr);
00735   if(magic_box.getRotationVariance()>100)
00736     magic_box.setRotation(rot);
00737 
00738   if(goon) {
00739 
00740     if(depth<1 || (thr_tr>0.05f && thr_rot>0.05f))
00741       return optimizeLink3(ctxt, cors, _tf, thr_rot*_factor, thr_tr*_factor, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00742     else if(thr_tr>0.05f)
00743       return optimizeLink3(ctxt, cors, _tf, thr_rot, thr_tr*_factor, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00744     else
00745       return optimizeLink3(ctxt, cors, _tf, thr_rot*_factor, thr_tr, magic_box.getRotation(), magic_box.getTranslation(), depth+1);
00746   }
00747 
00748 #ifdef DEBUG_
00749   //DEBUG
00750   Debug::Interface::get().addArrow(Eigen::Vector3f::Zero(), tf.getTranslation());
00751 
00752   //  for(size_t i=0; i<cors.size(); i++)
00753   //  {
00754   //    if(cors[i].used_)
00755   //      Debug::Interface::get().addArrow(cors[i].a->getNearestPoint(),cors[i].b->getNearestPoint());
00756   //    else
00757   //      Debug::Interface::get().addArrow(cors[i].a->getNearestPoint(),cors[i].b->getNearestPoint(), 0,255,0);
00758   //  }
00759   //DEBUG
00760 #endif
00761 
00762   if(//(size_t)used2*4<cors.size() ||
00763       (_tf.getRotation()-tf.getRotation()).norm()>_tf.getRotationVariance() || (_tf.getTranslation()-tf.getTranslation()).norm()>_tf.getTranslationVariance())
00764   {
00765 
00766     std::cout<<"R1\n"<<_tf.getRotation()<<"\n";
00767     std::cout<<"R2\n"<< tf.getRotation()<<"\n";
00768     std::cout<<"rot dist: "<<(_tf.getRotation()-tf.getRotation()).norm()<<"\n";
00769     std::cout<<"tr dist: "<<(_tf.getTranslation()-tf.getTranslation()).norm()<<"\n";
00770     std::cout<<"rot dist A: "<<_tf.getRotationVariance()<<"\n";
00771     std::cout<<"tr dist A: "<<_tf.getTranslationVariance()<<"\n";
00772 
00773     std::cout<<"OLD\n"<<_tf<<"\n";
00774     std::cout<<"NEW\n"<<tf<<"\n";
00775     if((size_t)used2*4<cors.size())
00776       ROS_INFO("failed reason 1");
00777     if((_tf.getRotation()-tf.getRotation()).norm()>_tf.getRotationVariance())
00778       ROS_INFO("failed reason 2");
00779     if((_tf.getTranslation()-tf.getTranslation()).norm()>_tf.getTranslationVariance())
00780       ROS_INFO("failed reason 3");
00781     tf.getSource1()->reset();
00782 
00783     return tf;
00784   }
00785 
00786   //DEBUGGING
00787   //tf.getSource1()->setRotation(tf.getSource2()->getRotation());
00788   //tf.getSource1()->setTranslation(tf.getSource2()->getTranslation());
00789 
00790   return tf;
00791 }


cob_3d_mapping_slam
Author(s): Joshua Hampp
autogenerated on Wed Aug 26 2015 11:04:51