Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LookoutEvents.cc

Go to the documentation of this file.
00001 #include "LookoutEvents.h"
00002 
00003 #include <sstream>
00004 #include <libxml/tree.h>
00005 #include <iostream>
00006 
00007 using namespace std;
00008 
00009 const EventBase::classTypeID_t LookoutPointAtEvent::autoRegisterLookoutPointAtEvent=getTypeRegistry().registerType<LookoutPointAtEvent>(makeClassTypeID("LOLA"));
00010 const EventBase::classTypeID_t LookoutSketchEvent::autoRegisterLookoutSketchEvent=getTypeRegistry().registerType<LookoutSketchEvent>(makeClassTypeID("LSKC"));
00011 const EventBase::classTypeID_t LookoutIREvent::autoRegisterLookoutIREvent=getTypeRegistry().registerType<LookoutIREvent>(makeClassTypeID("LOIR"));
00012 const EventBase::classTypeID_t LookoutScanEvent::autoRegisterLookoutScanEvent=getTypeRegistry().registerType<LookoutScanEvent>(makeClassTypeID("LSCN"));
00013 
00014 
00015 //================ LookoutPointAtEvent
00016 
00017 std::string
00018 LookoutPointAtEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00019   if(!showTypeSpecific)
00020     return EventBase::getDescription(showTypeSpecific,verbosity);
00021   std::ostringstream logdata;
00022   logdata << EventBase::getDescription(showTypeSpecific,verbosity)
00023     << "toBaseMatrix=\n" << toBaseMatrix << '\n';
00024   return logdata.str();
00025 }
00026 
00027 unsigned int
00028 LookoutPointAtEvent::getBinSize() const {
00029   unsigned int used=EventBase::getBinSize();
00030   if(saveFormat==XML)
00031     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00032   //otherwise need to add our own fields
00033   used+=creatorSize("EventBase::LookoutPointAtEvent");
00034   used+=sizeof(toBaseMatrix);
00035   return used;
00036 }
00037 
00038 unsigned int
00039 LookoutPointAtEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00040   unsigned int origlen=len;
00041   unsigned int used;
00042   if(0==(used=EventBase::loadBinaryBuffer(buf,len))) return 0;
00043   len-=used; buf+=used;
00044   if(0==(used=checkCreator("EventBase::LookoutPointAtEvent",buf,len,true))) return 0;
00045   len-=used; buf+=used;
00046   for (int i = 0; i < 16; i++) {
00047     if(0==(used=decode(toBaseMatrix(1+i/4,1+(i%4)),buf,len))) return 0;
00048     len-=used; buf+=used;
00049   }
00050   return origlen-len; 
00051 }
00052 
00053 unsigned int
00054 LookoutPointAtEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00055   unsigned int origlen=len;
00056   unsigned int used;
00057   if(0==(used=EventBase::saveBinaryBuffer(buf,len))) return 0;
00058   len-=used; buf+=used;
00059   if(0==(used=saveCreator("EventBase::LookoutPointAtEvent",buf,len))) return 0;
00060   len-=used; buf+=used;
00061   for (int i = 0; i < 16; i++) {
00062     if(0==(used=encode(toBaseMatrix(1+i/4,1+(i%4)),buf,len))) return 0;
00063     len-=used; buf+=used;
00064   }
00065   return origlen-len;
00066 }
00067 
00068 void LookoutPointAtEvent::loadXML(xmlNode* node) {
00069   if(node==NULL)
00070     return;
00071   
00072   EventBase::loadXML(node);
00073   
00074   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00075     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00076       continue;
00077     
00078     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00079     if(name==NULL)
00080       throw bad_format(cur,"property missing name");
00081     
00082     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00083     if(val==NULL)
00084       throw bad_format(cur,"property missing value");
00085     
00086     cout << "loadXML: " << name << "=" << val << endl;
00087     
00088     if(xmlStrcmp(name, (const xmlChar *)"toBaseMatrix")==0) {
00089       const string valStr = (const char*) val;
00090       string::size_type pos = valStr.find_first_of(' ', 0);
00091       string::size_type prev_pos = ++pos;
00092       for (unsigned int i = 0; i < 16; i++) {
00093   pos = valStr.find_first_of(' ', pos);
00094   toBaseMatrix(1+i/4,1+(i%4)) = (float)atof(valStr.substr(prev_pos, pos-prev_pos).c_str());
00095   prev_pos = ++pos;
00096   if (prev_pos == string::npos)
00097     break;
00098       }
00099     }
00100     
00101     xmlFree(val);
00102     xmlFree(name);
00103   }
00104 }
00105 
00106 void LookoutPointAtEvent::saveXML(xmlNode * node) const {
00107   if(node==NULL)
00108     return;
00109   EventBase::saveXML(node);
00110   
00111   //clear old params first
00112   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00113     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00114       xmlUnlinkNode(cur);
00115       xmlFreeNode(cur);
00116       cur = skipToElement(node->children); //restart the search (boo)
00117     } else
00118       cur = skipToElement(cur->next);
00119   }
00120   
00121   //  cout << "saveXML: " << toBaseMatrix << endl;
00122 
00123   xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL);
00124   if(cur==NULL)
00125     throw bad_format(node,"Error: LookoutPointAtEvent xml error on saving param");
00126   xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)"toBaseMatrix");
00127   std::ostringstream valbuf;
00128   for (int i = 0; i < 16; i++)
00129     valbuf << toBaseMatrix(1+i/4,1+(i%4)) << ' ';
00130   xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)valbuf.str().c_str());
00131 }
00132 
00133 
00134 //================ LookoutIREvent
00135 
00136 std::string
00137 LookoutIREvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00138   if(!showTypeSpecific)
00139     return LookoutPointAtEvent::getDescription(showTypeSpecific,verbosity);
00140   std::ostringstream logdata;
00141   logdata << LookoutPointAtEvent::getDescription(showTypeSpecific,verbosity)
00142     << '\t' << distance;
00143   return logdata.str();
00144 }
00145 
00146 unsigned int
00147 LookoutIREvent::getBinSize() const {
00148   unsigned int used=LookoutPointAtEvent::getBinSize();
00149   if(saveFormat==XML)
00150     return used; //if using XML, the XMLLoadSave::getBinSize (called by LookoutPointAtEvent::getBinSize) is all we need
00151   //otherwise need to add our own fields
00152   used+=creatorSize("LookoutPointAtEvent::LookoutIREvent");
00153   used+=sizeof(distance);
00154   return used;
00155 }
00156 
00157 unsigned int
00158 LookoutIREvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00159   unsigned int origlen=len;
00160   unsigned int used;
00161   if(0==(used=LookoutPointAtEvent::loadBinaryBuffer(buf,len))) return 0;
00162   len-=used; buf+=used;
00163   if(0==(used=checkCreator("LookoutPointAtEvent::LookoutIREvent",buf,len,true))) return 0;
00164   len-=used; buf+=used;
00165   if(0==(used=decode(distance,buf,len))) return 0;
00166   len-=used; buf+=used;
00167   return origlen-len; 
00168 }
00169 
00170 unsigned int
00171 LookoutIREvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00172   unsigned int origlen=len;
00173   unsigned int used;
00174   if(0==(used=LookoutPointAtEvent::saveBinaryBuffer(buf,len))) return 0;
00175   len-=used; buf+=used;
00176   if(0==(used=saveCreator("LookoutPointAtEvent::LookoutIREvent",buf,len))) return 0;
00177   len-=used; buf+=used;
00178   if(0==(used=encode(distance,buf,len))) return 0;
00179   len-=used; buf+=used;
00180   return origlen-len;
00181 }
00182 
00183 void LookoutIREvent::loadXML(xmlNode* node) {
00184   if(node==NULL)
00185     return;
00186   
00187   LookoutPointAtEvent::loadXML(node);
00188   
00189   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00190     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00191       continue;
00192     
00193     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00194     if(name==NULL)
00195       throw bad_format(cur,"property missing name");
00196     
00197     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00198     if(val==NULL)
00199       throw bad_format(cur,"property missing value");
00200     
00201     cout << "loadXML: " << name << "=" << val << endl;
00202     
00203     
00204     if(xmlStrcmp(name, (const xmlChar *)"distance")==0)
00205       distance=(float)atof((const char*)val);
00206     
00207     xmlFree(val);
00208     xmlFree(name);
00209   }
00210 }
00211 
00212 void LookoutIREvent::saveXML(xmlNode * node) const {
00213   if(node==NULL)
00214     return;
00215   LookoutPointAtEvent::saveXML(node);
00216   
00217   //clear old params first
00218   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00219     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00220       xmlUnlinkNode(cur);
00221       xmlFreeNode(cur);
00222       cur = skipToElement(node->children); //restart the search (boo)
00223     } else
00224       cur = skipToElement(cur->next);
00225   }
00226 
00227   xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL);
00228   if(cur==NULL)
00229     throw bad_format(node,"Error: LocomotionEvent xml error on saving param");
00230   xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)"distance");
00231   char valbuf[20];
00232   snprintf(valbuf,20,"%g",distance);
00233   xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)valbuf);
00234 }

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:44 2016 by Doxygen 1.6.3