ServiceInterface.cpp
Go to the documentation of this file.
00001 /**********************************************************************************************/
00023 #include "lo/ObjectContainer.h"
00024 #include "lo/ServiceInterface.h"
00025 
00026 /************************************************************************
00027 *                    XMLTag.cpp - Copyright klank
00028 *
00029 *        copied from cop
00030 **************************************************************************/
00031 
00032 #include "libxml/xmlwriter.h"
00033 #include "libxml/xmlreader.h"
00034 #include <time.h>
00035 // Constructors/Destructors
00036 //
00037 #define MY_ENCODING "ISO-8859-1"
00038 
00039 #define XML_NODE_FILE_REFERENCE "FileReference"
00040 #define XML_ATTRIBUTE_TIMESTAMP "TimeStamp"
00041 
00042 #define MAX_TREE_HEIGHT 150
00043 
00044 #define XML_PROPERTY_NUM "Num"
00045 #define XML_NODE_LO_TYPE "LoType"
00046 #define XML_NODE_LOLIST "LoList"
00047 #define XML_NODE_LO "LO"
00048 #define XML_ATTRIBUTE_NAMEMAPPING "NameIDMapping"
00049 
00050 #ifndef WIN32
00051 #define  sprintf_s(_DstBuf, size, _Format, whatever) sprintf(_DstBuf, _Format, whatever)
00052 #endif
00053 
00054 using namespace jlo;
00055 
00056 XMLTag::XMLTag ( std::string name) :
00057         m_name(name)
00058 {
00059         Touch();
00060 }
00061 
00062 XMLTag* XMLTag::Tag(int n, std::string name)
00063 {
00064         XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_INT : name);
00065         tag->SetCData(n);
00066         return tag;
00067 }
00068 
00069 XMLTag* XMLTag::Tag(double d, std::string name)
00070 {
00071         XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_DOUBLE : name);
00072         tag->SetCData(d);
00073         return tag;
00074 }
00075 
00076 XMLTag* XMLTag::Tag(std::string value, std::string name)
00077 {
00078         XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_STRING : name);
00079         tag->SetCData(value);
00080         return tag;
00081 }
00082 
00083 
00084 XMLTag* XMLTag::Tag(const Matrix& mat, std::string name)
00085 {
00086         XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_MATRIX : name);
00087         int rows = mat.nrows();
00088         int cols = mat.ncols();
00089         tag->AddProperty(XML_ATTRIBUTE_ROWS, rows);
00090         tag->AddProperty(XML_ATTRIBUTE_COLS, cols);
00091         for(int r = 0; r < rows; r++)
00092         {
00093                 for(int c = 0; c < cols; c++)
00094                 {
00095                         tag->AddChild(Tag(mat.element(r,c)));
00096                 }
00097         }
00098         return tag;
00099 }
00100 
00101 Matrix XMLTag::Load(XMLTag* tag, Matrix* )
00102 {
00103         if(tag != NULL)
00104         {
00105                 int rows = tag->GetPropertyInt(XML_ATTRIBUTE_ROWS);
00106                 int cols = tag->GetPropertyInt(XML_ATTRIBUTE_ROWS);
00107                 Matrix m(rows, cols);
00108                 double counter = 0;
00109                 for(int r = 0; r < rows; r++)
00110                 {
00111                         for(int c = 0; c < cols; c++)
00112                         {
00113                                 m.Store()[r * rows + c] = Load(tag->GetChild((int)counter), &counter);
00114                                 counter++;
00115                         }
00116                 }
00117                 return m;
00118         }
00119         throw "Wrong Node";
00120 }
00121 
00122 XMLTag::~XMLTag ( )
00123 {
00124                 for(std::vector<XMLTag*>::iterator children = m_children.begin(); children != m_children.end(); children++)
00125                 {
00126                         delete (*children);
00127                         (*children) = NULL;
00128                 }
00129                 m_children.clear();
00130 }
00131 
00132 //
00133 // Methods
00134 //
00135 
00136   //static public Methods
00137 
00138         void XMLTag::WriteToFile(const std::string &stFile, XMLTag** fileReference ) const
00139         {
00140 
00141                 //Open File
00142                 xmlTextWriter* pWriter = xmlNewTextWriterFilename(stFile.c_str(), 0);
00143                 int nError = xmlTextWriterStartDocument(pWriter, NULL, MY_ENCODING, NULL);
00144                 if(nError >= 0 )
00145 
00146                 {
00147                         //Write File
00148                         Write(pWriter);
00149                         //CloseFile
00150                         nError = xmlTextWriterEndDocument(pWriter);
00151                 }
00152                 xmlFreeTextWriter(pWriter);
00153                 if(fileReference != NULL)
00154                 {
00155                         *fileReference = new XMLTag(XML_NODE_FILE_REFERENCE);
00156                         (*fileReference)->m_cData = stFile;
00157                 }
00158                 pWriter = NULL;
00159 
00160         }
00161     char* XMLTag::WriteToString() const
00162     {
00163         xmlOutputBufferPtr xmlbuf = xmlAllocOutputBuffer(NULL);
00164         xmlTextWriter* pWriter = xmlNewTextWriterMemory(xmlbuf->buffer, 0);
00165                 int nError = 0;/* xmlTextWriterStartDocument(pWriter, NULL, MY_ENCODING, NULL);*/
00166                 if(nError >= 0 )
00167 
00168                 {
00169                         //Write File
00170                         Write(pWriter);
00171                         //CloseFile
00172                         nError = xmlTextWriterEndDocument(pWriter);
00173                 }
00174                 xmlFreeTextWriter(pWriter);
00175                 pWriter = NULL;
00176         /*TODO: solve memory leaking here ...*/
00177         return (char*)xmlbuf->buffer->content;
00178     }
00179 
00180         void XMLTag::Write(xmlTextWriter* pWriter) const
00181         {
00182                 xmlTextWriterWriteRaw(pWriter, (xmlChar*)"\n");
00183                 xmlTextWriterStartElement(pWriter, (xmlChar*)m_name.c_str());
00184                 for(std::map<std::string,std::string>::const_iterator iter = m_properties.begin(); iter != m_properties.end(); iter++)
00185                 {
00186                         xmlTextWriterWriteAttribute(pWriter, (xmlChar*)(*iter).first.c_str(), (xmlChar*)(*iter).second.c_str());
00187                         xmlTextWriterEndAttribute(pWriter);
00188                 }
00189                 if(m_cData.length() > 0)
00190                         xmlTextWriterWriteFormatRaw(pWriter, m_cData.c_str());
00191                 for(std::vector<XMLTag*>::const_iterator children = m_children.begin(); children != m_children.end(); children++)
00192                 {
00193                         (*children)->Write(pWriter);
00194                 }
00195 
00196                 xmlTextWriterEndElement(pWriter);
00197                 //xmlTextWriterWriteRaw(pWriter, (xmlChar*)"\n");
00198         }
00199 
00200         XMLTag* XMLTag::ReadFromFile(const std::string &stFile)
00201         {
00202                 XMLTag* tag = NULL;
00203                 xmlTextReader* pReader= xmlNewTextReaderFilename(stFile.c_str());
00204                 if(pReader != NULL)
00205                 {
00206                         int i = xmlGetCompressMode();
00207 
00208                         xmlTextReaderRead(pReader);
00209                         xmlDocPtr doc = xmlTextReaderCurrentDoc(pReader);
00210                         xmlDocGetRootElement(doc);
00211                         //xmlChar* text =  xmlNodeGetContent(node);
00212                         tag = Read(pReader);
00213 
00214                 }
00215                 xmlFreeTextReader(pReader);
00216                 return tag;
00217         }
00218 
00219         XMLTag* XMLTag::Clone() const
00220         {
00221                 XMLTag* tag                     = new XMLTag(this->m_name);
00222                 tag->m_properties       = this->m_properties;
00223                 tag->m_cData            = this->m_cData;
00224                 tag->m_name                     = this->m_name;
00225 
00226                 for(std::vector<XMLTag*>::const_iterator children = m_children.begin(); children != m_children.end(); children++)
00227                 {
00228                         tag->AddChild((*children)->Clone());
00229                 }
00230                 return tag;
00231         }
00232 
00233         XMLTag* XMLTag::Read(xmlTextReader* pReader)
00234         {
00235                 XMLTag* tag = NULL;
00236                 xmlNode* node = xmlTextReaderCurrentNode(pReader);
00237                 if(node != NULL && node->type != XML_TEXT_NODE)
00238                 {
00239                         tag = new XMLTag(std::string((char*)node->name));
00240                         if(xmlTextReaderHasAttributes(pReader))
00241                         {
00242                                 int nAttr = xmlTextReaderAttributeCount(pReader);
00243                                 xmlAttr* attr = node->properties;
00244                                 for(int i = 0; i < nAttr ; i++, attr = attr->next)
00245                                 {
00246 
00247                                         std::string attrValue = (char*)xmlTextReaderGetAttributeNo(pReader, i);
00248                                         std::string attrName = (char*)attr->name;
00249                                         tag->AddProperty(attrName, attrValue);
00250                                 }
00251                         }
00252                         if(!xmlTextReaderIsEmptyElement(pReader))
00253                         {
00254                                 int ChildrenDepth = xmlTextReaderDepth(pReader) + 1;
00255                                 while(true)
00256                                 {
00257                                         xmlTextReaderRead(pReader);
00258                                         xmlNode* check = xmlTextReaderCurrentNode(pReader);
00259                                         if(ChildrenDepth != xmlTextReaderDepth(pReader))
00260                                         {
00261                                                 break;
00262                                         }
00263                                         if(check->type != XML_TEXT_NODE)
00264                                         {
00265                                                 XMLTag* child = Read(pReader);
00266                                                 tag->AddChild(child);
00267                                         }
00268                                         else
00269                                                 tag->SetCData(std::string((char*)check->content));
00270 
00271                                 }
00272                         }
00273                 }
00274                 return tag;
00275         }
00276 
00277         void XMLTag::AddProperty(const std::string &name, const std::string &value)
00278         {
00279                 m_properties[name] = value;
00280                 Touch();
00281         }
00282 
00283 #define MAXINTLONGSIZE 40
00284     void XMLTag::AddProperty(const std::string &name, const unsigned long &value)
00285         {
00286                 char longBuf[MAXINTLONGSIZE];
00287                 sprintf_s(longBuf,MAXINTLONGSIZE, "%d", value);
00288                 m_properties[name] = longBuf;
00289                 Touch();
00290         }
00291 
00292 #define MAXINTSIZE 20
00293         void XMLTag::AddProperty(const std::string& name, const int &value)
00294         {
00295                 char intBuf[MAXINTSIZE];
00296                 sprintf_s(intBuf,MAXINTSIZE, "%d", value);
00297                 m_properties[name] = intBuf;
00298                 Touch();
00299         }
00300 
00301 #define MAXDOUBLESIZE 190
00302         void XMLTag::AddProperty(const std::string &name, const double &value)
00303         {
00304                 char intBuf[MAXDOUBLESIZE];
00305                 sprintf_s(intBuf,MAXDOUBLESIZE, "%f", value);
00306                 m_properties[name] = intBuf;
00307                 Touch();
00308         }
00309 
00310         void XMLTag::SetCData(const double &value)
00311         {
00312                 char intBuf[MAXDOUBLESIZE];
00313                 sprintf_s(intBuf,MAXDOUBLESIZE, "%f", value);
00314                 m_cData = intBuf;
00315                 Touch();
00316         }
00317 
00318         int XMLTag::GetCDataInt() const
00319         {
00320                 return atoi(m_cData.c_str());
00321         }
00322         double XMLTag::GetCDataDouble() const
00323         {
00324                 return atof(m_cData.c_str());
00325         }
00326         std::string XMLTag::GetCDataST() const
00327         {
00328                 return m_cData;
00329         }
00330 
00331         void XMLTag::SetCData(const int &value)
00332         {
00333                 char intBuf[MAXINTSIZE];
00334                 sprintf_s(intBuf,MAXINTSIZE, "%d", value);
00335                 m_cData = intBuf;
00336                 Touch();
00337         }
00338 
00339         void XMLTag::SetCData(const std::string& value)
00340         {
00341                 m_cData = value;
00342                 Touch();
00343         }
00344 
00345         std::string XMLTag::GetProperty(const std::string &name)
00346         {
00347                 if(m_properties.find(name) == m_properties.end())
00348                         return "";
00349                 return m_properties[name];
00350         }
00351 
00352         double XMLTag::GetPropertyDouble(const std::string &name)
00353         {
00354                 if(m_properties.find(name) == m_properties.end())
00355                         return 0.0;
00356                 return atof(m_properties[name].c_str());
00357         }
00358 
00359         int XMLTag::GetPropertyInt(const std::string& name)
00360         {
00361                 if(m_properties.find(name) == m_properties.end())
00362                         return 0;
00363                 return atoi(m_properties[name].c_str());
00364         }
00365 
00366         int XMLTag::AddChild(XMLTag* child, int position)
00367         {
00368                 if(child != NULL)
00369                 {
00370                         if(position < 0 || (unsigned)position > m_children.size())
00371                                 m_children.push_back(child);
00372                         else
00373                                 m_children.insert(m_children.begin() += position, child);
00374                         return (signed int)m_children.size() - 1;
00375                 }
00376                 Touch();
00377                 return -1;
00378         }
00379 
00380         void XMLTag::ReplaceChild(XMLTag* child, int position)
00381         {
00382                 if(child != NULL)
00383                 {
00384                         if(position < 0 || (unsigned)position > m_children.size())
00385                                 m_children.push_back(child);
00386                         else
00387                         {
00388                                 delete m_children[position];
00389                                 m_children[position] = child;
00390                         }
00391                 }
00392                 Touch();
00393         }
00394 
00395         unsigned int XMLTag::CountChildren() const
00396         {
00397                 return (int)m_children.size();
00398         }
00399 
00400         void XMLTag::RemoveChild(const unsigned int &position)
00401         {
00402                 if(position < m_children.size())
00403                 {
00404                         delete m_children[position];
00405                         m_children.erase(m_children.begin() += position);
00406                 }
00407         }
00408 
00409         void XMLTag::RemoveChild(const std::string &name)
00410         {
00411                 for(std::vector<XMLTag*>::iterator iter = m_children.begin();
00412                         iter != m_children.end(); iter++)
00413                 {
00414                         if((*iter)->GetName().compare(name) == 0)
00415                         {
00416                                 delete *iter;
00417                                 m_children.erase(iter);
00418                                 break;
00419                         }
00420                 }
00421         }
00422 
00423 
00424         XMLTag* XMLTag::GetChild(const unsigned int &position)
00425         {
00426                 if(position < m_children.size())
00427                 {
00428                         if(m_children[position]->GetName().compare(XML_NODE_FILE_REFERENCE) == 0)
00429                         {
00430                                 XMLTag* child = ReadFromFile(m_children[position]->m_cData);
00431                                 delete m_children[position];
00432                                 m_children[position] = child;
00433                                 m_children[position]->Touch();
00434                         }
00435                         return m_children[position];
00436                 }
00437                 return NULL;
00438         }
00439 
00440         XMLTag* XMLTag::GetChild(const std::string& name)
00441         {
00442                 XMLTag* tag = NULL;
00443                 int counter = 0;
00444                 for(std::vector<XMLTag*>::const_iterator iter = m_children.begin();
00445                         iter  != m_children.end(); iter++, counter++)
00446                 {
00447                         if((*iter)->GetName().compare(name) == 0)
00448                         {
00449                                 return GetChild(counter);
00450                         }
00451                 }
00452                 return tag;
00453         }
00454 
00455         XMLTag* XMLTag::GetChild(const std::string& name, int innerindex)
00456         {
00457                 XMLTag* tag = NULL;
00458                 int counter = 0;
00459                 int innercounter = 0;
00460                 for(std::vector<XMLTag*>::const_iterator iter = m_children.begin();
00461                         iter  != m_children.end(); iter++, counter++ )
00462                 {
00463                         if((*iter)->GetName().compare(name) == 0)
00464                         {
00465                                 if(innercounter == innerindex)
00466                                         return GetChild(counter);
00467                                 innercounter++;
00468                         }
00469                 }
00470                 return tag;
00471         }
00472 
00473         unsigned long XMLTag::date()
00474         {
00475                 return m_lastChanged;
00476         }
00477 
00478         void XMLTag::Touch()
00479         {
00480                 m_lastChanged = (unsigned long)time(NULL);
00481         }
00482 
00483   jlo::LocatedObjectLoader s_loadLocatedObjectParent;
00487 /*  std::vector<ServiceLocatedObject*> s_ServiceLocatedObjects;*/
00488   std::map<unsigned long, ServiceLocatedObject*> s_ServiceLocatedObjectMap;
00489   std::map<std::string, unsigned long> s_ServiceLocatedObjectNameMap;
00490 
00491 unsigned long ServiceInterface::SetServiceLocatedObject(jlo::ServiceLocatedObject* pose)
00492 {
00493   /*s_ServiceLocatedObjects.push_back(pose);*/
00494   s_ServiceLocatedObjectMap[pose->m_uniqueID] = pose;
00495   jlo::ServiceLocatedObject::SetLastID(pose->m_uniqueID);
00496   return (unsigned long)pose->m_uniqueID;
00497 }
00498 
00499 void jlo::ServiceLocatedObject::UpdateParent(jlo::ServiceLocatedObject* new_parent)
00500 {
00501 
00502   ServiceLocatedObject* old_parent = ServiceInterface::GetServiceLocatedObject(m_parentID);
00503   old_parent->RemoveAttachedObject(this);
00504   m_parentID = new_parent->m_uniqueID;
00505   new_parent->AddAttachedObject(this);
00506 }
00507 
00508 
00509 void jlo::ServiceLocatedObject::Update(Matrix m, Matrix cov, ServiceLocatedObject*(* copy)(ServiceLocatedObject*, ServiceLocatedObject*), unsigned long (*del)(ServiceLocatedObject*), void (*updated)(unsigned long))
00510 {
00511   m_relation->TellParentNeedNoCopy();
00512   PropagateMovement(copy, del, updated, NULL);
00513   Set(m, cov);
00514 }
00515 
00516 
00517 XMLTag* jlo::ServiceLocatedObject::SaveComplete()
00518 {
00519   XMLTag* ret = new XMLTag(XML_NODE_LO);
00520   ret->AddProperty(XML_NODE_LO_TYPE, GetLOType());
00521   if(m_uniqueID > (m_relation == NULL ? 0 : m_relation->m_uniqueID))
00522     ret->AddProperty(XML_ATTRIBUTE_LOID, m_uniqueID);
00523   else
00524   {
00525     printf("Not saving temporary objects, that were relocated (%d)\n", m_uniqueID);
00526     delete ret;
00527     return NULL;
00528   }
00529   if(m_uniqueID != ID_WORLD)
00530     ret->AddProperty(XML_ATTRIBUTE_LOIDFATHER, m_relation == NULL ? ID_WORLD : m_relation->m_uniqueID);
00531   ret->AddChild(XMLTag::Tag(GetMatrix(0), XML_NODE_MATRIX));
00532   ret->AddChild(XMLTag::Tag(GetCovarianceMatrix(0), XML_NODE_COVARIANCE));
00533   ret->AddProperty(XML_ATTRIBUTE_NAMEMAPPING, m_mapstring);
00534   return ret;
00535 }
00536 
00541 jlo::ServiceLocatedObject ServiceLocatedObject::operator- (jlo::ServiceLocatedObject &smallObj )
00542 {
00543   unsigned long id = smallObj.FindCommonFather(this);
00544   Matrix m, n;
00545   Matrix mCov, nCov;
00546   m = smallObj.GetMatrix(id);
00547   mCov = smallObj.GetCovarianceMatrix(id);
00548   n = GetInvMatrix(id);
00549   nCov = GetCovarianceMatrix(id);
00550   Matrix temp = n * m;
00551   return jlo::ServiceLocatedObject(this, temp, UnscentedTrans(nCov+mCov, temp, n));
00552 }
00553 
00554 
00555 
00556 
00557 unsigned long ServiceInterface::FreeServiceLocatedObject(jlo::ServiceLocatedObject* pose)
00558 {
00559   if(pose == NULL)
00560     return 0;
00561   unsigned long id = pose->m_uniqueID;
00562   if(id != ID_WORLD)
00563   {
00564     pose->DecreaseReferenceCounter();
00565     if(pose->GetReferenceCounter() <= 0)
00566     {
00567       if(pose->m_mapstring.length() >  0)
00568       {
00569          ServiceInterface::RemoveMapString(pose->m_mapstring);
00570       }
00574       jlo::ServiceLocatedObject* parent = GetServiceLocatedObject(pose->m_parentID);
00575       if(parent != NULL)
00576       {
00577         parent->RemoveAttachedObject(pose);
00578       }
00579       delete pose;
00580       s_ServiceLocatedObjectMap.erase(s_ServiceLocatedObjectMap.find(id));
00581       pose = NULL;
00582       if(parent != NULL)
00583       {
00584         if(parent->GetReferenceCounter() <= 0)
00585         {
00586           FreeServiceLocatedObject(parent);
00587         }
00588         else
00589         {
00590            /*printf("Not deleting a parent of %ld cause it is still in use: %ld\n", id, parent->m_uniqueID);*/
00591         }
00592       }
00593     }
00594   }
00595   return id;
00596 }
00597 
00598 
00599 void ServiceInterface::DisposeList()
00600 {
00601         for(std::map<unsigned long, jlo::ServiceLocatedObject*>::iterator iter = s_ServiceLocatedObjectMap.begin();
00602                 iter != s_ServiceLocatedObjectMap.end(); iter++)
00603         {
00604                 delete (*iter).second;
00605         }
00606         s_ServiceLocatedObjectMap.clear();
00607 
00608 }
00609 
00610 
00611 XMLTag* ServiceInterface::SaveList()
00612 {
00613   XMLTag* tag = new XMLTag(XML_NODE_LOLIST);
00614   unsigned long counter = 0;
00615   /*tag->AddProperty(XML_PROPERTY_NUM, (int)s_ServiceLocatedObjects.size());*/
00616   for(std::map<unsigned long, jlo::ServiceLocatedObject*>::iterator iter = s_ServiceLocatedObjectMap.begin();
00617           iter != s_ServiceLocatedObjectMap.end(); iter++)
00618   {
00619     if((*iter).second == NULL)
00620     {
00621       printf("NULL entry in lo list\n");
00622       continue;
00623     }
00624     XMLTag* child = (*iter).second->SaveComplete();
00625     if(child != NULL)
00626     {
00627       tag->AddChild(child);
00628       counter++;
00629     }
00630   }
00631   tag->AddProperty(XML_PROPERTY_NUM, (int)counter);
00632   return tag;
00633 }
00634 
00635 
00636 ServiceInterface::ServiceInterface(const char* configfile)
00637 {
00638   XMLTag* tag =  XMLTag::ReadFromFile(configfile);
00639   if(tag != NULL && tag->GetName().compare(XML_NODE_LOLIST) == 0)
00640     LoadList(tag);
00641   else
00642     FServiceLocatedObjectWorld();
00643 }
00644 ServiceInterface::~ServiceInterface()
00645 {
00646   DisposeList();
00647 }
00648 
00649 jlo::ServiceLocatedObject* ServiceInterface ::FServiceLocatedObjectWorld()
00650 {
00651   ServiceLocatedObject* pose = GetServiceLocatedObject(ID_WORLD);
00652   if(pose == NULL)
00653   {
00654     pose = new ServiceLocatedObject();
00655     SetServiceLocatedObject(pose);
00656   }
00657   return pose;
00658 }
00659 
00660 
00661 ServiceLocatedObject* ServiceInterface::FServiceLocatedObject(XMLTag* tag)
00662 {
00663   if(tag != NULL && tag->GetName().compare(XML_NODE_LO) == 0)
00664   {
00665     unsigned long id = tag->GetPropertyInt(XML_ATTRIBUTE_LOID);
00666     if(id == ID_WORLD)
00667     {
00668       ServiceLocatedObject* world = GetServiceLocatedObject(id);
00669       world = ((world == NULL) ? FServiceLocatedObjectWorld() : world);
00670       world->m_mapstring = tag->GetProperty(XML_ATTRIBUTE_NAMEMAPPING);
00671       if(world->m_mapstring.length() != 0)
00672         s_ServiceLocatedObjectNameMap[world->m_mapstring] = ID_WORLD;
00673 
00674       return world;
00675     }
00676     ServiceLocatedObject* dingens = GetServiceLocatedObject(id) ;
00677     if(dingens != NULL)
00678       return dingens;
00679     //throw "Trying to load already existing LocatedObject";
00680     unsigned long father = tag->GetPropertyInt(XML_ATTRIBUTE_LOIDFATHER);
00681     ServiceLocatedObject* fatherRP = GetServiceLocatedObject(father);
00682     if(fatherRP == NULL)
00683     {
00684       if(father == ID_WORLD)
00685         fatherRP = FServiceLocatedObjectWorld();
00686       else
00687       {
00688         printf("ServiceLocatedObject: %d with unknown father %d\n", id, father);
00689         return NULL;
00690       }
00691     }
00692     Matrix m, cov;
00693     m = tag->Load(tag->GetChild(XML_NODE_MATRIX), &m);
00694     cov = tag->Load(tag->GetChild(XML_NODE_COVARIANCE), &cov);
00695     unsigned long type = tag->GetPropertyInt(XML_NODE_LO_TYPE);
00696     if(id > jlo::ServiceLocatedObject::s_lastID)
00697       jlo::ServiceLocatedObject::SetLastID(id-1);
00698     ServiceLocatedObject* pose = FServiceLocatedObject(fatherRP, m, cov, type);
00699     AddMapString(pose, tag->GetProperty(XML_ATTRIBUTE_NAMEMAPPING));
00700     return pose;
00701   }
00702   //TOCHECK return world?
00703   return FServiceLocatedObjectWorld();
00704 }
00705 
00706 void ServiceInterface::AddMapString(ServiceLocatedObject* pose, std::string mapstring)
00707 {
00708     pose->m_mapstring = mapstring;
00709     if(pose->m_mapstring.length() != 0)
00710       s_ServiceLocatedObjectNameMap[pose->m_mapstring] = pose->m_uniqueID;
00711 }
00712 
00713 void ServiceInterface::RemoveMapString(std::string mapstring)
00714 {
00715    s_ServiceLocatedObjectNameMap[mapstring] = 0;
00716 }
00717 
00718 /***
00719   Copy is always an Perceived Object
00720 */
00721 ServiceLocatedObject* ServiceInterface::FServiceLocatedObjectCopy(ServiceLocatedObject* obj_to_copy, ServiceLocatedObject* parent_copy)
00722 {
00723   ServiceLocatedObject* ret = NULL;
00724   if(parent_copy == NULL)
00725     ret = FServiceLocatedObject(obj_to_copy->m_relation, obj_to_copy->GetMatrix(), obj_to_copy->GetCovarianceMatrix(), obj_to_copy->GetLOType());
00726   else
00727     ret = FServiceLocatedObject(parent_copy, obj_to_copy->GetMatrix(), obj_to_copy->GetCovarianceMatrix(), obj_to_copy->GetLOType());
00728   return ret;
00729 }
00730 
00731 
00732 unsigned long jlo::ServiceLocatedObject::s_lastID = ID_WORLD;
00733 // Constructors/Destructors
00734 //
00735 
00736 jlo::ServiceLocatedObject::ServiceLocatedObject ( ) :
00737     LocatedObject(&s_loadLocatedObjectParent),
00738     m_relation(NULL),
00739     m_needCopy(0),
00740     referenceCounter(0)
00741 {
00742   s_lastID = s_lastID > ID_WORLD ? s_lastID : ID_WORLD + 1 ;
00743 
00744   //set the matrix to identity in R.
00745 }
00746 
00747 jlo::ServiceLocatedObject::ServiceLocatedObject (jlo::ServiceLocatedObject* locatedObject, double x , double y , double z , double roll , double pitch , double yaw ,
00748                           double sigmaX, double sigmaY , double sigmaZ,
00749                           double sigmaRoll, double sigmaPitch , double sigmaYaw) :
00750     LocatedObject(&s_loadLocatedObjectParent, s_lastID, locatedObject->m_uniqueID, x,y,z,roll,pitch,yaw,sigmaX, sigmaY, sigmaZ,sigmaRoll,sigmaPitch, sigmaYaw),
00751     m_relation(locatedObject),
00752     m_needCopy(0),
00753     referenceCounter(0)
00754 {
00755   s_lastID++;
00756 }
00757 
00758 
00759 jlo::ServiceLocatedObject::ServiceLocatedObject (jlo::ServiceLocatedObject* locatedObject, const Matrix &matrix , const Matrix &covariance) :
00760     LocatedObject(&s_loadLocatedObjectParent, s_lastID, locatedObject->m_uniqueID, matrix, covariance),
00761     m_relation(locatedObject),
00762     m_needCopy(0),
00763     referenceCounter(0)
00764 {
00765         //Check for circular references
00766         if(!CheckWorldCoordinates(MAX_TREE_HEIGHT))
00767         {
00768                 m_relation = NULL; //This relation will not be accepted
00769                 m_parentID = 1;
00770         }
00771         s_lastID++;
00772 }
00773 
00774 jlo::ServiceLocatedObject::~ServiceLocatedObject ( )
00775 {
00776   //if(m_relation != NULL)
00777   // {
00778   //    m_relation->RemoveAttachedObject(this);
00779   // }
00780 }
00781 
00782 
00783 
00784 ServiceLocatedObject* ServiceInterface::FServiceLocatedObject(ServiceLocatedObject* pose, Matrix m, Matrix cov, unsigned long type)
00785 {
00786   ServiceLocatedObject* ret = NULL;
00787   if(type == LO_TYPE_PHYSICAL)
00788     ret = new ObjectContainer(pose, m, cov);
00789   else
00790     ret = new ServiceLocatedObject(pose, m, cov);
00791 
00792   SetServiceLocatedObject(ret);
00793   pose->AddAttachedObject(ret);
00794   return ret;
00795 }
00796 
00797 
00798 void ServiceInterface::LoadList(XMLTag* tag)
00799 {
00800   if(tag != NULL)
00801   {
00802     int num = tag->GetPropertyInt(XML_PROPERTY_NUM);
00803     for(int i = 0; i< num; i++)
00804     {
00805       FServiceLocatedObject(tag->GetChild(i));
00806       /*TODO build map here*/
00807     }
00808   }
00809 }
00810 
00811 ServiceLocatedObject* ServiceInterface::GetServiceLocatedObject(unsigned long id)
00812 {
00813   if(id == 0)
00814     return NULL;
00815    std::map<unsigned long, jlo::ServiceLocatedObject*>::iterator it = s_ServiceLocatedObjectMap.find(id);
00816   if(it != s_ServiceLocatedObjectMap.end())
00817     return (*it).second;
00818   return NULL;
00819   /*
00820   for(std::vector<ServiceLocatedObject*>::iterator iter = s_ServiceLocatedObjects.begin();
00821                 iter != s_ServiceLocatedObjects.end(); iter++)
00822         {
00823                 if((*iter)->m_uniqueID == id)
00824                         return (*iter);
00825         }
00826         return NULL;
00827         */
00828 }
00829 
00830 
00831 unsigned long ServiceInterface::GetServiceLocatedObjectID(std::string st)
00832 {
00833   std::map<std::string, unsigned long>::iterator it = s_ServiceLocatedObjectNameMap.find(st);
00834   if(it == s_ServiceLocatedObjectNameMap.end())
00835   {
00836     return 0;
00837   }
00838   return (*it).second;
00839 }
00840 
00841 
00842 ServiceLocatedObject* ServiceInterface::GetServiceLocatedObjectIndex(unsigned long index)
00843 {
00844   printf("Deprectaded call\n");
00845   return NULL;
00846   /*return s_ServiceLocatedObjects[index];*/
00847 }
00848 
00849 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


lo
Author(s): U. Klank
autogenerated on Thu May 23 2013 07:34:46