Community Core Vision 1.4
Cross platform, user friendly computer vision.
C:/CCV/1.4/addons/ofxNCore/src/Templates/TemplateUtils.cpp
Go to the documentation of this file.
00001 #include "TemplateUtils.h"
00002 
00003 //Adds template data to the vector
00004 void TemplateUtils::addTemplate(ofRectangle rect,ofRectangle minRect, ofRectangle maxRect,float scaleX=1.0f ,float scaleY=1.0f) 
00005 {
00006         if(templates.size() < MAX_NUM_TEMPLATES)
00007         {
00008                 Template temp = Template();
00009                 temp.width = rect.width * scaleX;
00010                 temp.height = rect.height * scaleY;
00011                 temp.maxWidth = maxRect.width * scaleX;
00012                 temp.maxHeight = maxRect.height * scaleY;
00013                 temp.minWidth = minRect.width * scaleX;
00014                 temp.minHeight = minRect.height * scaleY;
00015                 temp.id = getId();
00016                 temp.trueId = 0;
00017         
00018                 templates.push_back(temp);
00019                 printf("Template added. Number of templates = %d\n",templates.size());
00020         }
00021 }
00022 
00023 //Loads the XML file from templates.xml file. This returns false if the file does not exist
00024 bool TemplateUtils::loadTemplateXml()
00025 {
00026         if( XML.loadFile("templates.xml") )
00027         {
00028                 templates.clear();
00029                 int numTags=XML.getNumTags("TEMPLATE");
00030                 if( numTags > 0 )
00031                 {
00032                         int total = MIN(numTags,MAX_NUM_TEMPLATES);
00033                         ;
00034                         for(int i=0; i < total; i++)
00035                         {
00036                                 float width =(float) XML.getValue("TEMPLATE:WIDTH",0,i);
00037                                 float height =(float)XML.getValue("TEMPLATE:HEIGHT",0,i);
00038                                 float minWidth =(float)  XML.getValue("TEMPLATE:MINWIDTH",0,i);
00039                                 float minHeight = (float) XML.getValue("TEMPLATE:MINHEIGHT",0,i);
00040                                 float maxWidth = (float) XML.getValue("TEMPLATE:MAXWIDTH",0,i);
00041                                 float maxHeight =(float) XML.getValue("TEMPLATE:MAXHEIGHT",0,i);
00042                                 int     trueId = XML.getValue("TEMPLATE:TRUEID",0,i);
00043                                 int id;
00044                                 if(trueId)
00045                                 {
00046                                         id = XML.getValue("TEMPLATE:ID",0,i);
00047                                         assignedIds.push_back(id);
00048                                 }
00049                                 else
00050                                 {
00051                                         id = getId();
00052                                 }
00053 
00054                                 if(width !=0 && height != 0 && minWidth != 0 && minHeight != 0 && maxWidth != 0 && maxHeight != 0)
00055                                 {//Only then add a template
00056 
00057                                         Template temp = Template();
00058                                         temp.width = width;
00059                                         temp.height = height;
00060                                         temp.maxWidth = maxWidth;
00061                                         temp.maxHeight = maxHeight;
00062                                         temp.minWidth = minWidth;
00063                                         temp.minHeight = minHeight;
00064                                         temp.trueId = trueId;
00065                                         temp.id = id;
00066 
00067                                         templates.push_back(temp);
00068                                 }
00069                         }
00070                         isLoaded = true;// <- This is inside the if statement so that if the file exists but no xml data in it,this will still be false
00071                 }
00072                 return true;
00073         }
00074         else
00075         {
00076                 printf("\nTemplates.xml could not be loaded. Make sure it is there in data folder\n");
00077                 templates.clear();
00078                 return false;
00079         }
00080 }
00081 
00082 //Saves into XML file from the template vector.
00083 void TemplateUtils::saveTemplateXml()
00084 {
00085         XML.clear();
00086 
00087         for(int i = 0 ; i < templates.size() ; i++ )
00088         {
00089                 int tagNum=XML.addTag("TEMPLATE");
00090 
00091                 XML.setValue("TEMPLATE:WIDTH",templates[i].width,tagNum);
00092                 XML.setValue("TEMPLATE:HEIGHT",templates[i].height,tagNum);
00093                 XML.setValue("TEMPLATE:MINWIDTH",templates[i].minWidth,tagNum);
00094                 XML.setValue("TEMPLATE:MINHEIGHT",templates[i].minHeight,tagNum);
00095                 XML.setValue("TEMPLATE:MAXWIDTH",templates[i].maxWidth,tagNum);
00096                 XML.setValue("TEMPLATE:MAXHEIGHT",templates[i].maxHeight,tagNum);
00097                 XML.setValue("TEMPLATE:TRUEID",templates[i].trueId,tagNum);
00098                 XML.setValue("TEMPLATE:ID",templates[i].id,tagNum);
00099                 
00100                 XML.popTag();   
00101         }
00102         XML.saveFile("templates.xml");
00103         printf("Template is saved\n");
00104 }
00105 
00106 
00107 // Better funtion to be implemented
00108 int TemplateUtils::getTemplateId(float width,float height)
00109 {
00110         int id = -1;
00111         if(templates.size()==0)
00112         {
00113                 return -1;
00114         }
00115         for(int i = 0 ; i < templates.size() ; i++ )
00116         {
00117                 if(width < templates[i].maxWidth && width > templates[i].minWidth && height < templates[i].maxHeight && height > templates[i].minHeight)
00118                 {
00119                         return templates[i].id;
00120                 }
00121         }
00122         return id;
00123 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines