00001
00061 #include <eigen3/Eigen/Dense>
00062
00063 template<typename _DOF6>
00064 bool OBJCTXT<_DOF6>::registration(const OBJCTXT &ctxt, DOF6 &tf, typename DOF6::TYPE &probability_success_rate, typename DOF6::TYPE &probability_error_rate){
00065
00066 #ifdef DEBUG_
00067 ROS_INFO("registration %d %d",(int)objs_.size(), (int)ctxt.objs_.size());
00068 Debug::Interface::get().sayTook("reg. start");
00069 ROS_INFO("nextPoint ctr %d", Slam_Surface::___CTR___);
00070 #endif
00071
00072 tf.getSource1()->reset();
00073
00074
00075 used_cors_.clear();
00076
00077 findCorrespondences3(ctxt, used_cors_, tf);
00078
00079 Debug::Interface::get().sayTook("reg. cor");
00080 ROS_INFO("nextPoint ctr %d", Slam_Surface::___CTR___);
00081
00082
00083
00084 #if 0
00085 if(tf.getRotationVariance()+tf.getTranslationVariance()<0.3f)
00086 tf = optimizeLink1(tf, used_cors_, tf.getRotationVariance(), tf.getTranslationVariance(), tf.getRotation(), tf.getTranslation());
00087 else
00088 tf = optimizeLink2(tf, used_cors_, tf.getRotationVariance(), tf.getTranslationVariance(), tf.getRotation(), tf.getTranslation());
00089 #else
00090 tf = optimizeLink3(ctxt, used_cors_, tf, tf.getRotationVariance(), tf.getTranslationVariance(), tf.getRotation(), tf.getTranslation());
00091 #endif
00092
00093 Debug::Interface::get().sayTook("reg. opt");
00094 ROS_INFO("nextPoint ctr %d", Slam_Surface::___CTR___);
00095
00096 std::cout<<"INPUT TF\n"<<tf<<"\n";
00097
00098
00099
00100
00101 tf.setVariance( tf.getTranslationVariance(), tf.getTranslation(), tf.getRotationVariance(), tf.getRotation() );
00102
00103 std::cout<<tf<<"\n";
00104
00105 ROS_INFO("nextPoint ctr %d", Slam_Surface::___CTR___);
00106
00107 if((tf.getSource1()->getTranslationVariance()+tf.getSource1()->getRotationVariance())>0.3)
00108 {
00109 tf.getSource1()->reset();
00110 ROS_INFO("failed to register");
00111 return false;
00112 }
00113
00114 return true;
00115 }
00116
00117
00118 template<typename _DOF6>
00119 bool OBJCTXT<_DOF6>::merge(const OBJCTXT &ctxt, const DOF6 &tf, std::map<typename OBJECT::Ptr,bool> &used_out, const BoundingBox::TransformedFoVBB &fov, const bool only_merge)
00120 {
00121 Debug::Interface::get().sayTook("merge start");
00122 ROS_INFO("nextPoint ctr %d", (int)Slam_Surface::___CTR___);
00123
00124 ++frames_;
00125
00126 const size_t old = objs_.size();
00127 const float uncertainity = 0.01f;
00128
00129 if(old>0 && (tf.getTranslationVariance()+tf.getRotationVariance())>0.3f )
00130 return false;
00131
00132 ROS_INFO("add ctxt %d", (int)used_out.size());
00133 std::cout<<tf<<"\n";
00134
00135 Eigen::Vector3f edges[8];
00136 for(size_t i=0; i<objs_.size(); i++)
00137 {
00138 bool in=fov&objs_[i]->getNearestPoint();
00139 if(!in) {
00140 objs_[i]->getBB().get8Edges(edges);
00141 for(size_t i=0; i<8; i++)
00142 if(fov&edges[i]) {in=true; break;}
00143 }
00144
00145 if(objs_[i]->getBB().extension()>2.5f)
00146 in=false;
00147
00148 if( in ) {
00149 #ifdef DEBUG_OUTPUT_
00150 ROS_INFO("FoV: in");
00151 #endif
00152 objs_[i]->processed();
00153 }
00154 else {
00155 #ifdef DEBUG_OUTPUT_
00156 ROS_INFO("FoV: out");
00157 #endif
00158 objs_[i]->notProcessed();
00159 }
00160 }
00161
00162 Debug::Interface::get().sayTook("merge 1");
00163 ROS_INFO("nextPoint ctr %d", (int)Slam_Surface::___CTR___);
00164
00165 std::map<typename OBJECT::Ptr,std::vector<typename OBJECT::Ptr> > used;
00166
00167 DOF6 tmp_link = tf.transpose();
00168
00169 for(typename std::vector<SCOR>::const_iterator it = used_cors_.begin(); it!=used_cors_.end(); it++)
00170 {
00171 if(!it->a || !it->b || !it->used_) continue;
00172
00173
00174
00175 typename OBJECT::Ptr o=it->b->makeShared();
00176 o->transform(tmp_link.getRotation(), tmp_link.getTranslation(),
00177 tf.getRotationVariance()+uncertainity, tf.getTranslationVariance()+uncertainity);
00178
00179 it->a->used();
00180
00181 if(((*it->a) += *o)
00182 )
00183 {
00184 used_out[it->b] = true;
00185
00186 used[it->b].push_back(it->a);
00187 ROS_INFO("update object");
00188 }
00189 else {
00190
00191 ROS_INFO("NOT update object");
00192 }
00193 }
00194
00195 Debug::Interface::get().sayTook("merge 2");
00196 ROS_INFO("nextPoint ctr %d", (int)Slam_Surface::___CTR___);
00197
00198 for(size_t i=0; i<ctxt.objs_.size(); i++)
00199 {
00200 typename std::map<typename OBJECT::Ptr,std::vector<typename OBJECT::Ptr> >::const_iterator it = used.find(ctxt.objs_[i]);
00201
00202 if(used.end() == it)
00203 {
00204 if(used_out.find(ctxt.objs_[i])!=used_out.end())
00205 continue;
00206
00207 if(!only_merge || (bb_.transform(tf.getRotation(), tf.getTranslation())&ctxt.objs_[i]->getNearestPoint()) )
00208 {
00209
00210 typename OBJECT::Ptr o=ctxt.objs_[i]->makeShared();
00211 o->transform(((Eigen::Matrix3f)tmp_link.getRotation()),tmp_link.getTranslation(),
00212 tmp_link.getRotationVariance()+uncertainity, tmp_link.getTranslationVariance()+uncertainity);
00213
00214 bool found = false;
00215 for(size_t j=0; j<objs_.size(); j++)
00216 {
00217 if( (objs_[j]->getBB()&(o->getBB()
00218 )) &&
00219 (((*objs_[j]) += *o)
00220 ) )
00221 {
00222 ROS_INFO("found object");
00223 found = true;
00224 used_out[ctxt.objs_[i]] = true;
00225 used[ctxt.objs_[i]].push_back(objs_[j]);
00226 }
00227 }
00228
00229 if(!found)
00230 {
00231 ROS_INFO("adding object");
00232 *this += o;
00233 used_out[ctxt.objs_[i]] = true;
00234 }
00235 else
00236 ROS_INFO("NOT adding object");
00237 }
00238 }
00239
00240 it=used.find(ctxt.objs_[i]);
00241 if(used.end() != it && it->second.size()>1)
00242 {
00243 ROS_INFO("merging objects %d", (int)it->second.size());
00244
00245 for(size_t k=1; k<it->second.size(); k++)
00246 {
00247 if(*(it->second)[0] += *(it->second)[k])
00248 {
00249 for(size_t j=0; j<objs_.size(); j++)
00250 if(objs_[j] == (it->second)[k]
00251 )
00252 {
00253 objs_.erase(objs_.begin() + j);
00254 --j;
00255 ROS_INFO("erase object");
00256 }
00257 }
00258 }
00259 }
00260 }
00261 Debug::Interface::get().sayTook("merge up2");
00262
00263 if(enabled_all_merge_) {
00264 for(size_t i=0; i<objs_.size(); i++)
00265 for(size_t j=i+1; j<objs_.size(); j++)
00266 {
00267 int n=-1;
00268 bool b1, b2, b3;
00269 if( (b1=(objs_[i]->getBB().changeSize(0.7f)&(objs_[j]->getBB().setMinSize(0.05f).changeSize(0.7f))
00270 )) &&
00271 (b2=(objs_[j]->getBB().preassumption(objs_[i]->getData().getBB())>=std::cos(objs_[i]->getData().getBB().ratio()))) &&
00272 (((*objs_[i]).op_plus(*objs_[j],n))
00273 ) )
00274 {
00275 objs_.erase(objs_.begin() + j);
00276 --j;
00277 ROS_INFO("erase objectA23");
00278 }
00279 else if(n!=-1) {
00280 switch(n) {
00281 case 0:Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),objs_[j]->getNearestPoint(),255,0,0);break;
00282 case 1:Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),objs_[j]->getNearestPoint(),0,0,0);break;
00283 case 2:Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),objs_[j]->getNearestPoint(),0,0,255);break;
00284 case -1:
00285 if(!b1)
00286 Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),objs_[j]->getNearestPoint(),128,0,0);
00287 else if(!b2)
00288 Debug::Interface::get().addArrow(objs_[i]->getNearestPoint(),objs_[j]->getNearestPoint(),128,44,255);
00289 break;
00290 }
00291 }
00292 }
00293 }
00294
00295
00296
00297
00298
00299
00300 Debug::Interface::get().sayTook("merge up");
00301 ROS_INFO("nextPoint ctr %d", (int)Slam_Surface::___CTR___);
00302
00303 update();
00304
00305 Debug::Interface::get().sayTook("merge stop");
00306 ROS_INFO("nextPoint ctr %d", (int)Slam_Surface::___CTR___);
00307 Slam_Surface::___CTR___=0;
00308
00309 return true;
00310 }
00311
00312
00313 template<typename _DOF6>
00314 void OBJCTXT<_DOF6>::update()
00315 {
00316 filter();
00317 updateBB();
00318 }
00319
00320 template<typename _DOF6>
00321 void OBJCTXT<_DOF6>::filter()
00322 {
00323 for(size_t i=0; i<objs_.size(); i++)
00324 {
00325 size_t c = objs_[i]->getCreationCounter(), u = objs_[i]->getUsedCounter();
00326 const bool inv = objs_[i]->invalid();
00327
00328 if((objs_[i]->getProcessed() && (c>5 && std::log(u)/std::log(c) < 0.6f)) ||
00329 inv)
00330 {
00331 objs_.erase(objs_.begin()+i);
00332 --i;
00333 ROS_INFO(inv?"removed object because INVALID %d %d":"removed object %d %d", c,u);
00334 }
00335 }
00336 }
00337
00338 template<typename _DOF6>
00339 void OBJCTXT<_DOF6>::updateBB()
00340 {
00341 bb_.update(*this);
00342 }
00343
00344 template<typename _DOF6>
00345 typename OBJCTXT<_DOF6>::Ptr OBJCTXT<_DOF6>::clone() const
00346 {
00347 OBJCTXT::Ptr r(new OBJCTXT(*this));
00348 for(size_t i=0; i<objs_.size(); i++)
00349 r->objs_[i].reset( new OBJECT(*objs_[i]) );
00350 return r;
00351 }
00352
00353 template<typename _DOF6>
00354 OBJCTXT<_DOF6> &OBJCTXT<_DOF6>::transform(const DOF6 &tf)
00355 {
00356 for(size_t i=0; i<objs_.size(); i++)
00357 objs_[i]->transform(tf.getRotation(), tf.getTranslation(), tf.getRotationVariance(), tf.getTranslationVariance());
00358 updateBB();
00359 return *this;
00360 }
00361