VisFinder.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 /************************************************************************
00020                         VisFinder.cpp - Copyright klank
00021 
00022 
00023 L**************************************************************************/
00024 
00025 #include "VisFinder.h"
00026 #include "XMLTag.h"
00027 #include <algorithm>
00028 
00029 #ifdef BOOST_THREAD
00030 #include <boost/bind.hpp>
00031 #include <boost/thread.hpp>
00032 #include <boost/thread/mutex.hpp>
00033 #include "boost/date_time/posix_time/posix_time.hpp"
00034 #include <iostream>
00035 boost::mutex s_mutexDisplay;
00036 #define BOOST(A) A
00037 #else
00038 #define BOOST (A) ;
00039 #endif
00040 
00041 //#ifdef _DEBUG
00042 #define  DEBUG(A) A
00043 //#else
00044 //#define  DEBUG(A) ;
00045 //#endif
00046 
00047 using namespace cop;
00048 
00049 
00050 // Constructors/Destructors
00051 //
00052 
00053 VisFinder::VisFinder( XMLTag* configFile, ImageInputSystem* imageSystem,
00054 SignatureDB* db, AttentionManager* manager, VisLearner* visLearner
00055 #ifdef LOGFILE
00056                       ,LogFile& log) :
00057     m_selLocate(configFile != NULL ? configFile->GetChild(0) : NULL, log),
00058 #else /*LOGFILE*/
00059      ) :
00060     m_selLocate(configFile != NULL ? configFile->GetChild(0) : NULL),
00061 #endif /*LOGFILE*/
00062     m_imageSys(imageSystem),
00063     m_sigdb(db),
00064     m_visLearner(visLearner),
00065     m_attentionMan(manager)
00066 {
00067     m_selLocate.SetName(XML_NODE_VISFINDER);
00068 }
00069 
00070 VisFinder::~VisFinder ( )
00071 {
00072     for(std::map<int, TrackAlgorithm*>::iterator it = m_runningTracks.begin();
00073     it != m_runningTracks.end(); it++)
00074     {
00075         delete (*it).second;
00076     }
00077 }
00078 
00079 
00080 
00081 
00082 //
00083 // Methods
00084 //
00088 XMLTag* VisFinder::Save()
00089 {
00090     XMLTag* tag = new XMLTag(XML_NODE_VISFINDER);
00091     tag->AddChild(m_selLocate.Save());
00092     return tag;
00093 }
00094 
00095 
00096 bool VisFinder::GetPlaneClusterCall(PossibleLocations_t *cluster_ids, RelPose* pose, PerceptionPrimitive& visPrim, const std::vector<Sensor*> &sensors)
00097 {
00098   Signature &sig = *visPrim.GetSignature();
00099   LocateAlgorithm* det = (LocateAlgorithm*)m_selLocate.BestAlgorithm(123, sig, sensors);
00100   if(det == NULL)
00101     return false;
00102   int num;
00103   double qual;
00104   std::vector<RelPose*>  results =  det->Perform(sensors, pose, sig, num, qual);
00105   for(size_t i = 0; i < results.size(); i++)
00106   {
00107     cluster_ids->push_back(std::pair<RelPose*, double>(results[i], qual));
00108   }
00109   return true;
00110 }
00111 
00112 void ShowSearchSpace(PossibleLocations_t* lastKnownPoses, std::vector<Sensor*> cam)
00113 {
00114   if(cam[0] != NULL)
00115   {
00116     /*TODO*/
00117     /*cam->Show();*/
00118     printf("Showing searchspaces\n");
00119     for(size_t i = 0; i < lastKnownPoses->size(); i++)
00120     {
00121       /*TODO*/
00122     }
00123   }
00124 }
00125 
00126 
00127 
00128 bool comp_qual (const Results_t &t1, const Results_t &t2)
00129 {
00130   return t1.quality > t2.quality;
00131 }
00132 
00139 SignatureLocations_t VisFinder::Locate (PossibleLocations_t* lastKnownPoses, PerceptionPrimitive& visPrim, int &numOfObjects)
00140 {
00141         std::vector<Sensor*> cameras;
00142         Signature& object = *visPrim.GetSignature();
00143         double qualityMeasure = 0.0;
00144         Algorithm<std::vector<RelPose*> >* alg_fail = NULL;
00145         SignatureLocations_t ret;
00146         PossibleLocations_t::const_iterator it = lastKnownPoses->begin();
00147         std::vector<Results_t> all_matches;
00148         int maxObjToAdd = numOfObjects;
00149         BOOST(boost::system_time t0);
00150         BOOST(boost::system_time t1);
00151         //int count_poses = 1;
00152         if(lastKnownPoses->size() == 0)
00153         {
00154           std::vector<Sensor*> allsensors = m_imageSys->GetAllSensors();
00155           if(allsensors.size() > 0 && allsensors[0] != NULL)
00156           {
00157             if(!GetPlaneClusterCall(lastKnownPoses, allsensors[0]->GetRelPose(), visPrim, allsensors))
00158             {
00159               numOfObjects = 0;
00160               printf("No search position specified: 0 Results, no search\n");
00161             }
00162             printf("Got a new Searchspace\n");
00163             printf("lkp size %ld\n", lastKnownPoses->size());
00164             if(lastKnownPoses->size() > 0)
00165               printf("lastKnownPoses[0]: id %ld\n", (*lastKnownPoses)[0].first->m_uniqueID);
00166           }
00167         }
00168         it = lastKnownPoses->begin();
00169         Algorithm<std::vector<RelPose*> > * alg = NULL;
00170         bool anysensor = false;
00171         for(;it != lastKnownPoses->end(); it++)
00172         {
00173             RelPose* lastKnownPose = (*it).first;
00174             if(lastKnownPoses == NULL)
00175             {
00176               printf("VisFinder: Errorn in pose information\n");
00177               continue;
00178             }
00179             try
00180             {
00181                 cameras = m_imageSys->GetBestSensor(*lastKnownPose);
00182                 if(cameras.size() == 0)
00183                 {
00184                   printf("No Sensor for this location available\n");
00185                   continue;
00186                 }
00187                 else
00188                 {
00189                  anysensor = true;
00190                 }
00191                 int locatertype = ALGORITHMTYPE_LOCATE;
00192                 if(object.GetObjectPose() == NULL)
00193                   object.SetPose(&lastKnownPose[0]);
00194 
00195                 /*if(count_poses == 1)
00196                 {
00197                   ShowSearchSpace(lastKnownPoses, cameras);
00198                   count_poses++;
00199                 }*/
00200 
00201 
00202                 locatertype += numOfObjects > 1 ? ALGORITHMSPEC_SEVERALTARGET : ALGORITHMSPEC_ONETARGET ;
00203                  alg = m_selLocate.BestAlgorithm(locatertype, object, cameras);
00204                 DEBUG(printf("Selected Algorithm: %s\n", alg != NULL ? alg->GetName().c_str() : "None" ));
00205 
00206                 if(alg != NULL)
00207                 {
00208                   /* TODO remove next line?*/
00209                   m_selLocate.m_logfile.ReportEvent(visPrim.GetID(), alg->GetName());
00210 
00211 
00212                   int numOfObjects_tmp = numOfObjects;
00213                   BOOST(t0 = boost::get_system_time());
00214                   std::vector<RelPose*> r = alg->Perform(cameras, lastKnownPose, object, numOfObjects_tmp, qualityMeasure);
00215                   BOOST(t1 = boost::get_system_time());
00216 
00218                   numOfObjects_tmp = r.size() < (unsigned)numOfObjects_tmp ?  r.size() : (unsigned)numOfObjects_tmp;
00219                   printf("Results num = %d\n", numOfObjects_tmp);
00220                   alg_fail = alg;
00221                   BOOST(boost::posix_time::time_duration td = t1 - t0);
00222                   BOOST(printf("Calc time: %s\n", boost::posix_time::to_simple_string(td).c_str()));
00223 
00225                   for(std::vector<RelPose*>::const_iterator it_poses = r.begin(); it_poses != r.end(); it_poses++)
00226                   {
00227                     Results_t res_tmp;
00228                     res_tmp.pose = *it_poses;
00229                     res_tmp.quality = it_poses == r.begin() ? qualityMeasure : (*it_poses)->m_qualityMeasure;
00230                     res_tmp.camera = cameras[0];/*TODO fix! BestAlgorithm?!?*/
00231                     res_tmp.alg = alg;
00232                     m_selLocate.ReevaluatePose(alg, res_tmp.pose);
00233                     all_matches.push_back(res_tmp);
00234                   }
00235                 }
00236                 else
00237                 {
00238                   printf("No algorithm found for the quieried Problem\n");
00239                 }
00240             }
00241             catch(char const* message)
00242             {
00243                 printf("Error in Locate: %s\n", message);
00244 
00245             }
00246         }
00247         if(!anysensor)
00248         {
00249           throw "No Sensor could see any of the requested locations";
00250         }
00252         std::sort(all_matches.begin(), all_matches.end(), comp_qual);
00253         if(all_matches.size() > 0 && numOfObjects > 0)
00254         {
00255           for(unsigned int i = 0; i < all_matches.size(); i++)
00256           {
00257             if(i == 0)
00258             {
00259               numOfObjects = 0;
00260             }
00261             RelPose* pose = all_matches[i].pose;
00262             if(numOfObjects >= maxObjToAdd)
00263             {
00264               RelPoseFactory::FreeRelPose(&pose, true);
00265               continue;
00266             }
00267             Signature* sig = (Signature*)(object.Duplicate(false));
00268             sig->SetLastPerceptionPrimitive(visPrim.GetID());
00269             visPrim.AddResult(alg, &m_selLocate, sig->m_ID, pose->m_qualityMeasure, ((double)((t1 - t0).total_milliseconds()) /  1000.0));
00270             sig->SetPose(pose);
00271             /*m_selLocate.EvalAlgorithm(all_matches[i].alg, pose->m_qualityMeasure, ((double)((t1 - t0).total_milliseconds()) /  1000.0), sig);
00272               */
00273 
00274             /*pose->m_qualityMeasure = qualityMeasure;*/
00275             ret.push_back(std::pair<RelPose*, Signature*>(pose, sig));
00276 #ifdef BOOST_THREAD
00277              all_matches[i].camera = cameras[0];
00278              all_matches[i].signature = sig;
00279 #else
00280             try
00281             {
00282               if(all_matches[i].camera->IsCamera())
00283                 sig->Show(all_matches[i].camera);
00284             }
00285             catch(char* error)
00286             {
00287               printf("Error in Display: %s\n", error);
00288             }
00289             m_sigdb->AddSignature(sig);
00290 #endif
00291             numOfObjects++;
00292             /*if(qualityMeasure > 0.70)
00293                m_visLearner.RefineObject(sig);*/
00294           }
00295           m_sigdb->AddAndShowSignaturesAsync(all_matches, numOfObjects);
00296 
00297         }
00298         else
00299         {
00300            //m_selLocate.EvalAlgorithm(alg_fail, qualityMeasure,((double)((t1 - t0).total_milliseconds()) /  1000.0), &object);
00301 #ifdef BOOST_THREAD
00302            m_sigdb->AddAndShowSignatureAsync(&object, NULL);
00303 #else
00304            m_sigdb->AddSignature(&object);
00305 #endif
00306         }
00307    return ret;
00308 }
00309 
00310 
00315 void VisFinder::StartTrack  ( PerceptionPrimitive& visPrim, RelPose* poseEstimation)
00316 {
00317     try
00318     {
00319       Signature& object = *visPrim.GetSignature();
00320       std::vector<Sensor*> cameras = m_imageSys->GetBestSensor(*poseEstimation);
00321       int locatertype = ALGORITHMTYPE_TRACK;
00322       locatertype = ALGORITHMSPEC_SEVERALTARGET; 
00323       Algorithm<std::vector<RelPose*> > * alg = m_selLocate.BestAlgorithm(locatertype, object, cameras);
00324       if(alg != NULL)
00325       {
00326         if(m_runningTracks.find(object.m_ID) == m_runningTracks.end())
00327         {
00328           if(poseEstimation != NULL)
00329             object.SetPose(poseEstimation);
00330           TrackAlgorithm*  tracking = new TrackAlgorithm(visPrim, &m_selLocate, alg, m_imageSys);
00331           m_runningTracks[object.m_ID] = tracking;
00332         }
00333         else
00334         {
00335           ROS_ERROR("There was already a query for the same object, ignoring the new (FIX necessary if this feature is required)\n"); /*TODO fix this*/
00336           visPrim.SetTerminated();
00337         }
00338       }
00339       else
00340       {
00341         printf("No Algorithm for Tracking of object %ld found\n", object.m_ID);
00342       }
00343     }
00344     catch(...)
00345     {
00346     }
00347     return;
00348 }
00349 
00353 void VisFinder::StopTrack (Signature& object)
00354 {
00355     std::map<int, TrackAlgorithm*>::iterator it = m_runningTracks.find(object.m_ID);
00356     if(it != m_runningTracks.end())
00357     {
00358         delete (*it).second;
00359         m_runningTracks.erase(it);
00360     }
00361 
00362 }
00363 
00371 RelPose* VisFinder::RelTwoObjects (const RelPose &Pose, Signature& Obj1, Signature& Obj2 )
00372 {
00373      /* TODO reason about best match, not yet integrated*/
00374     return NULL;
00375 }
00376 
00381 void VisFinder::AddAlgorithm(Algorithm<std::vector<RelPose*> >* alg)
00382 {
00383     m_selLocate.AddAlgorithm(alg, ALGORITHMSPEC_ONETARGET, 1.0, 0.0);
00384 }
00385 
00386 #ifndef WIN32
00387 #include "AlgorithmSelector.hpp"
00388 template class AlgorithmSelector<std::vector<RelPose*> >;;
00389 #else
00390 #include "AlgorithmSelector.hpp"
00391 template AlgorithmSelector<std::vector<RelPose*> >;
00392 #endif
00393 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Thu May 23 2013 07:38:35