Signature.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                         Signature.cpp - Copyright klank
00021 
00022 
00023 **************************************************************************/
00024 
00025 #include "Signature.h"
00026 #include "XMLTag.h"
00027 #include "Sensor.h"
00028 
00029 #define XML_NODE_CLASSES "Classes"
00030 
00031 #include "BoostUtils.h"
00032 
00033 #ifdef BOOST_THREAD
00034 #include <boost/thread/mutex.hpp>
00035 #define BOOST(A) A
00036 #else
00037 #define BOOST(A)
00038 #endif
00039 
00040 #ifdef _DEBUG
00041 #define DEBUG(A) A
00042 #else
00043 #define DEBUG(A) ;
00044 #endif
00045 
00046 using namespace cop;
00047 
00048 
00049 // Constructors/Destructors
00050 //
00051 
00052 Signature::Signature ( )
00053 {
00054 
00055 }
00056 
00057 void Signature::SetData ( XMLTag* tag )
00058 {
00059   if(tag == NULL)
00060     throw ("Trying to feed a null to singature constructor");
00061   Object::SetData(tag);
00062   {
00063     ElemWriteLock lk(m_mutexElems);
00064     std::string stName = tag->GetName();
00065     std::string stDefault = GetNodeName();
00066     if(stName.compare(stDefault) != 0)
00067     {
00068       printf("Wrong node detected opening a XML-File for reading a signature\n");
00069       throw "WRONG NODE";
00070     }
00071   }
00072   //Call Elem Factory
00073   //Call Class Factory
00074   try
00075   {
00076     XMLTag* classes = tag->GetChild(XML_NODE_CLASSES);
00077     if(classes  != NULL)
00078     {
00079       for(unsigned int i = 0; i < classes->CountChildren(); i++)
00080       {
00081         SetClass((Class*)Elem::ElemFactory(classes->GetChild(i)));
00082       }
00083     }
00084     else
00085     {
00086       printf("Signature: XML node missing: Classes\n");
00087     }
00088   }
00089   catch(...)
00090   {
00091      printf("Error creating Classes\n");
00092   }
00093   XMLTag* describingElems = tag->GetChild(0);
00094   if(describingElems != NULL)
00095   {
00096     for(unsigned int i = 0; i < describingElems->CountChildren(); i++)
00097     {
00098       XMLTag* tagdescChild = describingElems->GetChild(i);
00099       if(tagdescChild != NULL)
00100       {
00101         try
00102         {
00103           SetElem(Elem::ElemFactory(tagdescChild));
00104         }
00105         catch(...)
00106         {
00107           printf("Error creating Descriptor:  index %d: name %s\n", i, tagdescChild->GetName().c_str());
00108           printf("Content: %s \n", tagdescChild->WriteToString());
00109           tagdescChild->FreeAfterWriteToString();
00110         }
00111       }
00112     }
00113   }
00114   else
00115   {
00116     printf("Signature: XML node missing (descriptors)\n");
00117   }
00118 }
00119 
00120 Signature::~Signature ( )
00121 {
00122   try
00123   {
00124     ElemWriteLock lk(m_mutexElems);
00125     for(std::vector<Elem*>::const_iterator iter = m_elems.begin();
00126       iter != m_elems.end(); iter++)
00127     {
00128       delete (*iter);
00129     }
00130     m_elems.clear();
00131   }
00132   catch(const char * text)
00133   {
00134     printf("Error deleting Elem of Signature: %s\n", text);
00135   }
00136   catch(...)
00137   {
00138     printf("Error deleting Elem of Signature\n");
00139   }
00140 
00141   BOOST(m_mutexClasses.lock());
00142   try
00143   {
00144     for(std::vector<Class*>::const_iterator iter = m_class.begin();
00145       iter != m_class.end(); iter++)
00146     {
00147       delete (*iter);
00148     }
00149     m_class.clear();
00150   }
00151   catch(const char * text)
00152   {
00153     printf("Error deleting Class of Signature: %s\n", text);
00154   }
00155   catch(...)
00156   {
00157     printf("Error deleting Class of Signature\n");
00158   }
00159   BOOST(m_mutexClasses.unlock());
00160 
00161 }
00162 
00163 //
00164 // Methods
00165 //
00166 
00167 Class* Signature::GetClass(int index)
00168 {
00169   BOOST(m_mutexClasses.lock());
00170   Class* ret  = NULL;
00171   if((signed)m_class.size() > index)
00172     ret = m_class[index];
00173   BOOST(m_mutexClasses.unlock());
00174   return ret;
00175 }
00176 
00177 bool Signature::HasClass(Class* classToSet)
00178 {
00179   ElemWriteLock lk(m_mutexClasses);
00180   size_t size = CountClasses();
00181   for (unsigned int i = 0 ; i < size; i++)
00182   {
00183     if(m_class[i]->m_ID == classToSet->m_ID)
00184       return true;
00185   }
00186   for(unsigned int i = 0 ; i < size; i++)
00187   {
00188     if(m_class[i]->GetName().compare(classToSet->GetName()) == 0)
00189       return true;
00190   }
00191   return false;
00192 }
00193 
00194 void Signature::Show(Sensor* cam)
00195 {
00196   Sleeping(0.001);
00197   try
00198   {
00199     if(cam!= NULL)
00200     {
00201       cam->Show();
00202       cam->GetShowLock();
00203 
00204     }
00205   }
00206   catch(const char* text)
00207   {
00208     printf("Display of sensor data failed: %s\n", text);
00209   }
00210   catch (...)
00211   {
00212     printf("Display of sensor data failed\n");
00213   }
00214 
00215   DEBUG(printf("Entering Showing of signature %ld\n", m_ID));
00216   if(GetObjectPose() != NULL)
00217   {
00218     printf("Showing %ld Elements\n",  CountElems());
00219     for(unsigned int i = 0; i < CountElems(); i++)
00220     {
00221       try
00222       {
00223           ((Descriptor*)GetElement(i,ELEM))->Show(GetObjectPose(), cam);
00224       }
00225       catch(...)
00226       {
00227         printf("Showing of elem %d failed ... \n", i);
00228       }
00229     }
00230   }
00231   else
00232   {
00233      printf("Signature has no pose\n");
00234   }
00235   if(cam!= NULL)
00236   {
00237     cam->ReleaseShowLock();
00238   }
00239 }
00240 
00241 void Signature::SaveTo(XMLTag* tag)
00242 {
00243   XMLTag* delimiter = new XMLTag("DescibingElems");
00244   for(std::vector<Elem*>::const_iterator iter = m_elems.begin();
00245     iter != m_elems.end(); iter++)
00246   {
00247     if((*iter) != NULL)
00248     {
00249       printf("Saving Descriptor of Type %s\n", (*iter)->GetNodeName().c_str());
00250       delimiter->AddChild((*iter)->Save(m_fullPose));
00251     }
00252   }
00253   tag->AddChild(delimiter);
00254   delimiter = new XMLTag("Classes");
00255   for(std::vector<Class*>::const_iterator iter = m_class.begin();
00256     iter != m_class.end(); iter++)
00257   {
00258     delimiter->AddChild((*iter)->Save());
00259   }
00260   tag->AddChild(delimiter);
00261   Object::SaveTo(tag);
00262 
00263   //TODO
00264 }
00265 
00266 
00267 Elem* Signature::Duplicate(bool bStaticCopy)
00268 {
00269   Signature* new_sig = new Signature();
00270 
00271   for(std::vector<Elem*>::const_iterator iter = m_elems.begin();
00272     iter != m_elems.end(); iter++)
00273   {
00274     new_sig->SetElem((*iter)->Duplicate(bStaticCopy));
00275   }
00276   for(std::vector<Class*>::const_iterator iter_c = m_class.begin();
00277     iter_c != m_class.end(); iter_c++)
00278   {
00279     new_sig->SetClass((Class*)(*iter_c)->Duplicate(bStaticCopy));
00280   }
00282   if(GetObjectPose() != NULL)
00283     new_sig->GetObjectPose() = RelPoseFactory::FRelPose(GetObjectPose()->m_uniqueID);
00284   new_sig->m_bCommunicationCallBack = m_bCommunicationCallBack;
00285   new_sig->m_com = m_com;
00286 
00288   new_sig->SetTimeStamp(date());
00289   new_sig->SetFullPose(m_fullPose);
00290   SetLastPerceptionPrimitive(GetLastPerceptionPrimitive());
00291 
00292   if(bStaticCopy)
00293     new_sig->m_ID = m_ID;
00294 
00295   return new_sig;
00296 }
00297 
00298 
00299 // Private static attribute accessor methods
00300 //
00301 
00302 
00303 // Private attribute accessor methods
00304 //
00305 
00306 
00307 // Other methods
00308 //
00309 
00310 
00316  Elem* Signature::GetElement (const int &index, const ElemType_t &type ) const
00317  {
00318   Elem* elem = NULL;
00319 
00320   if(type == 0)
00321   {
00322     if(index >= 0 && (unsigned)index < m_elems.size())
00323       elem = m_elems[index];
00324   }
00325   else
00326   {
00327     int count = index;
00328     for(std::vector<Elem*>::const_iterator iter = m_elems.begin();
00329       iter != m_elems.end(); iter++)
00330     {
00331       if((*iter)->GetType() == type)
00332       {
00333         if(count == 0)
00334         {
00335           elem = (*iter);
00336           break;
00337         }
00338         else
00339           count--;
00340       }
00341     }
00342   }
00343   return elem;
00344 }
00345 
00346 
00351 long Signature::SetElem (Elem* elemToSet )
00352 {
00353   if(elemToSet != NULL)
00354   {
00355     BOOST(m_mutexElems.lock());
00356     m_elems.push_back(elemToSet);
00357     BOOST(m_mutexElems.unlock());
00358     int type = elemToSet->GetType();
00359     if(type > ELEM && type < SIGNATURE)
00360     {
00361       SetClass(((Descriptor*)elemToSet)->GetClass());
00362     }
00363 
00364     return m_elems.size() - 1;
00365   }
00366   return -1;
00367 }
00368 void Signature::RemoveElem(Elem* elemToRemove)
00369 {
00370       BOOST(m_mutexClasses.lock());
00371 
00372       for(std::vector<Elem*>::iterator iter = m_elems.begin(); m_elems.size(); iter++)
00373       {
00374         if(*iter == elemToRemove)
00375         {
00376           m_elems.erase(iter);
00377           break;
00378         }
00379       }
00380       BOOST(m_mutexClasses.unlock());
00381 }
00382 long Signature::SetClass (Class* classToSet )
00383 {
00384   if(classToSet != NULL)
00385   {
00386     if(!HasClass(classToSet))
00387     {
00388       BOOST(m_mutexClasses.lock());
00389       m_class.push_back(classToSet);
00390       BOOST(m_mutexClasses.unlock());
00391 
00392       return m_class.size() - 1;
00393     }
00394   }
00395   return -1;
00396 }
00397 
00398 
00399 void Signature::Evaluate(const double quality, const double weight)
00400 {
00401   BOOST(m_mutexElems.lock());
00402   try
00403   {
00404     for(std::vector<Elem*>::const_iterator iter = m_elems.begin();
00405       iter != m_elems.end(); iter++)
00406     {
00407       (*iter)->Evaluate(quality, weight);
00408     }
00409   }
00410   catch(const char * text)
00411   {
00412     printf("Error deleting Elem of Signature: %s\n", text);
00413   }
00414   catch(...)
00415   {
00416     printf("Error deleting Elem of Signature\n");
00417   }
00418   BOOST(m_mutexElems.unlock());
00419 }
00420 
00421 void Signature::SetPose(RelPose* pose)
00422 {
00423   Object::SetPose(pose);
00424   try
00425   {
00426     for(std::vector<Elem*>::const_iterator iter = m_elems.begin();
00427       iter != m_elems.end(); iter++)
00428     {
00429       if(((Descriptor*)*iter) != NULL)
00430         ((Descriptor*)*iter)->PropagatePose(pose);
00431     }
00432   }
00433   catch(const char * text)
00434   {
00435     printf("Error in set pose: %s\n", text);
00436   }
00437   catch(...)
00438   {
00439     printf("Error deleting Elem of Signature\n");
00440   }
00441 
00442 
00443 }
 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