Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

VisionObjectEvent.cc

Go to the documentation of this file.
00001 #include "VisionObjectEvent.h"
00002 #include "Shared/RobotInfo.h"
00003 #include <sstream>
00004 #include <libxml/tree.h>
00005 #include <cmath>
00006 
00007 const EventBase::classTypeID_t VisionObjectEvent::autoRegisterVisionObjectEvent=getTypeRegistry().registerType<VisionObjectEvent>(makeClassTypeID("VISO"));
00008 
00009 std::string
00010 VisionObjectEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00011   if(!showTypeSpecific)
00012     return EventBase::getDescription(showTypeSpecific,verbosity);
00013   std::ostringstream logdata;
00014   logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t' << _x1 <<  '\t' <<_x2 <<'\t' <<_y1 <<'\t' <<_y2<<'\t'<<_frame ;
00015   return logdata.str();
00016 }
00017 
00018 float
00019 VisionObjectEvent::getDistanceEstimate(float diaMajor, float diaMinor) const {
00020   float diaX,diaY;
00021   float w=getWidth();
00022   float h=getHeight();
00023   if(w>h) {
00024     diaX=diaMajor;
00025     diaY=diaMinor;
00026   } else {
00027     diaX=diaMinor;
00028     diaY=diaMajor;
00029   }
00030   // divide dim by two because range magnitude is two: ±1 maps to ±FOV/2
00031   float xest=diaX>0?calcDistance(getWidth()*CameraHorizFOV/2,diaX):0;
00032   float yest=diaY>0?calcDistance(getHeight()*CameraHorizFOV/2,diaY):0;
00033   if(xest>0 && yest>0) {
00034     return (xest+yest)/2;
00035   } else if(xest>0) {
00036     return xest;
00037   } else if(yest>0) {
00038     return yest;
00039   }
00040   return 0;
00041 }
00042 
00043 float
00044 VisionObjectEvent::calcDistance(float visArc, float physDia) {
00045   float r=std::tan(visArc/2);
00046   if(r==0)
00047     return 0;
00048   return physDia/(2*r);
00049 }
00050 
00051 unsigned int
00052 VisionObjectEvent::getBinSize() const {
00053   unsigned int used=EventBase::getBinSize();
00054   if(saveFormat==XML)
00055     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00056   //otherwise need to add our own fields
00057   used+=creatorSize("EventBase::VisionObjectEvent");
00058   used+=getSerializedSize(_x1);
00059   used+=getSerializedSize(_x2);
00060   used+=getSerializedSize(_y1);
00061   used+=getSerializedSize(_y2);
00062   return used;
00063 }
00064 
00065 unsigned int
00066 VisionObjectEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00067   unsigned int origlen=len;
00068   if(!checkInc(EventBase::loadBinaryBuffer(buf,len),buf,len)) return 0;
00069   if(!checkCreatorInc("EventBase::VisionObjectEvent",buf,len,true)) return 0;
00070   if(!decodeInc(_x1,buf,len)) return 0;
00071   if(!decodeInc(_x2,buf,len)) return 0;
00072   if(!decodeInc(_y1,buf,len)) return 0;
00073   if(!decodeInc(_y2,buf,len)) return 0;
00074   return origlen-len; 
00075 }
00076 
00077 unsigned int
00078 VisionObjectEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00079   unsigned int origlen=len;
00080   if(!checkInc(EventBase::saveBinaryBuffer(buf,len),buf,len)) return 0;
00081   if(!saveCreatorInc("EventBase::VisionObjectEvent",buf,len)) return 0;
00082   if(!encodeInc(_x1,buf,len)) return 0;
00083   if(!encodeInc(_x2,buf,len)) return 0;
00084   if(!encodeInc(_y1,buf,len)) return 0;
00085   if(!encodeInc(_y2,buf,len)) return 0;
00086   return origlen-len;
00087 }
00088 
00089 void VisionObjectEvent::loadXML(xmlNode* node) {
00090   if(node==NULL)
00091     return;
00092   
00093   EventBase::loadXML(node);
00094 
00095   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00096     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00097       continue;
00098 
00099     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00100     if(name==NULL)
00101       throw bad_format(cur,"property missing name");
00102 
00103     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00104     if(val==NULL)
00105       throw bad_format(cur,"property missing value");
00106 
00107     if(xmlStrcmp(name, (const xmlChar *)"x1")==0)
00108       _x1=(float)atof((const char*)val);
00109     else if(xmlStrcmp(name, (const xmlChar *)"x2")==0)
00110       _x2=(float)atof((const char*)val);
00111     else if(xmlStrcmp(name, (const xmlChar *)"y1")==0)
00112       _y1=(float)atof((const char*)val);
00113     else if(xmlStrcmp(name, (const xmlChar *)"y2")==0)
00114       _y2=(float)atof((const char*)val);
00115     else if(xmlStrcmp(name, (const xmlChar *)"clipLeft")==0)
00116       _clipLeft=atoi((const char*)val);
00117     else if(xmlStrcmp(name, (const xmlChar *)"clipRight")==0)
00118       _clipRight=atoi((const char*)val);
00119     else if(xmlStrcmp(name, (const xmlChar *)"clipTop")==0)
00120       _clipTop=atoi((const char*)val);
00121     else if(xmlStrcmp(name, (const xmlChar *)"clipBottom")==0)
00122       _clipBottom=atoi((const char*)val);
00123     
00124     xmlFree(val);
00125     xmlFree(name);
00126   }
00127 }
00128 
00129 
00130 //! a little local macro to make saving fields easier
00131 #define SAVE_PARAM(strname,varname,format) {\
00132 xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL); \
00133 if(cur==NULL) \
00134 throw bad_format(node,"Error: VisionObjectEvent xml error on saving param"); \
00135 xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)strname); \
00136 char valbuf[20]; \
00137 snprintf(valbuf,20,format,varname); \
00138 xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)valbuf); }
00139 
00140 void VisionObjectEvent::saveXML(xmlNode * node) const {
00141   if(node==NULL)
00142     return;
00143   EventBase::saveXML(node);
00144   
00145   //clear old params first
00146   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00147     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00148       xmlUnlinkNode(cur);
00149       xmlFreeNode(cur);
00150       cur = skipToElement(node->children); //restart the search (boo)
00151     } else
00152       cur = skipToElement(cur->next);
00153   }
00154   
00155   SAVE_PARAM("x1",_x1,"%g");
00156   SAVE_PARAM("y1",_y1,"%g");
00157   SAVE_PARAM("x2",_x2,"%g");
00158   SAVE_PARAM("y2",_y2,"%g");
00159   SAVE_PARAM("clipLeft",_clipLeft,"%d");
00160   SAVE_PARAM("clipRight",_clipRight,"%d");
00161   SAVE_PARAM("clipTop",_clipTop,"%d");
00162   SAVE_PARAM("clipBottom",_clipBottom,"%d");
00163 }
00164 
00165 /*! @file
00166  * @brief Implements VisionObjectEvent, which provides information about objects recognized in the camera image
00167  * @author alokl (Creator)
00168  * @author Ignacio Herrero Reder &lt; nhr at dte uma es &gt; (VisionObjectInfo Boundary Box - bug 74)
00169  */

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