Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

GestureGraph.cpp

Go to the documentation of this file.
00001 
00037 #include "DAG.h"
00038 #include "GestureGraph.h"
00039 #include "HelperFunctions.h"
00040 #include "Exceptions.h"
00041 #include "CompareHistogram.h"
00042 
00044 void SkipComments(ifstream& is)
00045 {
00046         char ch;
00047 
00048         is.get(ch);
00049         
00050         while (ch == '#' || ch == '\n' || ch == ' ')
00051         {
00052                 // read comments
00053                 if(ch == '#')
00054                         while (ch != '\n')
00055                                 is.get(ch);
00056                         
00057                 is.get(ch);
00058         }
00059         
00060         is.seekg(-1, ios::cur);
00061 }
00062 
00064 // GestureGraph class implementation
00065 
00066 void GestureGraph::Clear()
00067 { 
00068         DAG::Clear();
00069 
00070         relationMatrix.Clear();
00071         hierarchicalMatrix.Clear();
00072         hierarchyLevels.Clear();
00073         m_nRootNodeCount = 0;
00074 }
00075 
00076 bool GestureGraph::Read(String strFileName)
00077 {
00078         GGNode* p;
00079         int n, m, nVertices;
00080         char szNodeLbl[50];
00081         
00082         ifstream is(strFileName, ios::in);
00083         
00084         if (is.fail()) 
00085         {
00086                 cerr << "Error: Cannot open file " << strFileName << endl;
00087                 return false;
00088         }
00089 
00090         SkipComments(is);
00091 
00092         is >> m_nRootNodeCount;
00093 
00094         SkipComments(is);
00095         
00096         is >> nVertices;
00097 
00098         relationMatrix.ReSize(nVertices, nVertices);
00099         hierarchicalMatrix.ReSize(nVertices, nVertices);
00100         hierarchyLevels.ReSize(nVertices);
00101   
00102         SkipComments(is);
00103         
00104         for(n = 0; n < nVertices; n++)
00105         {
00106                 p = new GGNode;
00107                 
00108                 is >> p->m_nType >> p->m_dSign >> p->m_dScale >> p->m_nXPos 
00109                         >> p->m_nYPos >> p->m_dA >> p->m_dB >> p->m_dOrientation 
00110                         >> p->m_dAnisotropy >> p->m_dUnkField;
00111 
00112                 p->m_nNodeIndex = n;
00113                 sprintf(szNodeLbl, "%d:%d", n, p->m_nType);
00114                 p->SetNodeLbl(szNodeLbl);
00115 
00116                 NewNode(p);
00117         }
00118 
00119         SkipComments(is);
00120         
00121         for(n = 0; n < nVertices; n++)
00122                 for(m = 0; m < nVertices; m++)
00123                         is >> relationMatrix[n][m];
00124 
00125         SkipComments(is);
00126         
00127         for(n = 0; n < nVertices; n++)
00128                 is >> hierarchyLevels[n];
00129 
00130         SkipComments(is);
00131         
00132         for(n = 0; n < nVertices; n++)
00133         {
00134                 for (m = 0; m < nVertices; m++)
00135                 {
00136                         is >> hierarchicalMatrix[n][m];
00137 
00138                         if (hierarchicalMatrix[n][m] == 1)
00139                                 NewEdge(GetNode(n), GetNode(m), 1);
00140                 }
00141         }                       
00142 
00143         SetDAGLbl(strFileName);
00144         strObjectName = strFileName;
00145         ComputeDerivedValues();
00146   BuildMatrices();
00147   BuildNodeHistograms();
00148   
00149         return true;
00150 }
00151 
00152 void GestureGraph::BuildMatrices()
00153 {
00154   int nVertices = ;
00155   leda_node v,u;
00156   float distance;
00157   int indexV, indexU;
00158   
00159   EuclideanDistance.ReSize(nVertices, nVertices);
00160   Scale.ReSize(nVertices,1);
00161   Bearing.ReSize(nVertices,1); //to fill in later
00162 
00163   forall_nodes(v, *this) {
00164     const GGNode* pV = GetNode(v);
00165     indexV = pV->GetDFSIndex();
00166     
00167     forall_nodes(u, *this){
00168       const GGNode* pU = GetNode(u);
00169       
00170       distance = sqrt(pow((pV->m_nXPos - pU->m_nXPos),2)
00171         + pow((pV->m_nYPos - pU->m_nYPos),2));
00172       
00173       indexU = pU->GetDFSIndex();
00174       EuclideanDistance(indexV,indexU) = distance;
00175     }
00176     /*
00177     sum_scales = pV->m_dScale;
00178     forall_adj_nodes(u, v){
00179       const GGNode* pU = GetNode(u);
00180       sum_scales += pV->m_dScale;
00181     }
00182     Scale(indexV,1) = pV->m_dScale / sum_scales;
00183     */
00184     Scale(indexV,1) = pV->m_dScale;
00185     
00186   }
00187   
00188 }
00189 
00195 void GestureGraph::BuildNodeHistograms()
00196 {
00197         float sum_distance = 0, sum_scale = 0;
00198         int indexV, indexU, n, i;
00199         
00200         forall_nodes(v, *this) {
00201                 const GGNode* pV = GetNode(v);
00202                 indexV = pV->GetDFSIndex();
00203 
00204                 sum_scale = pV->m_dScale;
00205                 forall_adj_nodes(u, v) {
00206                         const GGNode* pU = GetNode(u);
00207                         indexU = pU->GetDFSIndex();
00208 
00209                         sum_scales += pU->m_dScale;
00210                         sum_distance += EuclideanDistance(indexV,indexU);
00211                 }
00212         }
00213 
00214         
00215         forall_nodes(v, *this) {
00216                 const GGNode* pV = GetNode(v);
00217                 n = degree(v);
00218                 m_xCoords.ReSize(n);
00219                 m_weights.ReSize(n);
00220                 m_scales.ReSize(n);
00221                 indexV = pV->GetDFSIndex();
00222 
00223                 i = 0;
00224                 forall_adj_nodes(u, v) {
00225                         const GGNode* pU = GetNode(u);
00226                         indexU = pU->GetDFSIndex();
00227 
00228                         m_xCoords[i] = EuclideanDistance(indexV,indexU) / sum_distance;
00229                         m_weights[i] = 1.0;
00230                         m_scales[i] = pU->m_dScale / sum_scale;
00231                         i++;
00232                         
00233                 }
00234         }
00235 
00236         
00237 }
00238 
00240 istream& GestureGraph::Read(istream& is, bool bOnlyDataForMatching /*= false*/)
00241 {
00242         DAG::Read(is);
00243 
00244         hierarchyLevels.Read(is);
00245         hierarchicalMatrix.Read(is);
00246         relationMatrix.Read(is);
00247         is.read((char*) &m_nRootNodeCount, sizeof(m_nRootNodeCount));
00248 
00249         return is;
00250 }
00251 
00253 ostream& GestureGraph::Write(ostream& os) const
00254 {
00255         DAG::Write(os);
00256 
00257         hierarchyLevels.Write(os);
00258         hierarchicalMatrix.Write(os);
00259         relationMatrix.Write(os);
00260         os.write((char*) &m_nRootNodeCount, sizeof(m_nRootNodeCount));
00261 
00262         return os;
00263 }
00264 
00266 void GestureGraph::Print(ostream& os) const
00267 {
00268         DAG::Print(os);
00269 
00270         // add new stuff here
00271 }
00272 
00274 DAG& GestureGraph::operator=(const DAG& rhs)
00275 {
00276         DAG::operator=(rhs);
00277 
00278         const GestureGraph* pRhs = dynamic_cast<const GestureGraph*>(&rhs);
00279         
00280         if (!pRhs)
00281                 THROW_EXCEPTION("Invalid pointer type.");
00282 
00283         hierarchyLevels    = pRhs->hierarchyLevels;
00284         hierarchicalMatrix = pRhs->hierarchicalMatrix;
00285         relationMatrix     = pRhs->relationMatrix;
00286         m_nRootNodeCount   = pRhs->m_nRootNodeCount;
00287 
00288         return *this;
00289 }
00290 
00295 bool GestureGraph::AreNodesRelated(leda_node u, const DAG& from, leda_node v) const
00296 {
00297         const GestureGraph& sgFrom = dynamic_cast<const GestureGraph&>(from);
00298 
00299         // add stuff here
00300 
00301         return true;
00302 }
00303 
00305 DAG* GestureGraph::CreateObject() const
00306 {
00307         return new GestureGraph;
00308 }
00309 
00311 DAGNodePtr GestureGraph::CreateNodeObject(NODE_LABEL lbl) const
00312 {
00313         return new GGNode;
00314 }
00315 
00320 DAGNodePtr GestureGraph::ReadNode(istream& is) const
00321 {
00322         DAGNodePtr node(new GGNode);
00323 
00324         node->Read(is);
00325 
00326         return node;
00327 }
00328 
00330 String GestureGraph::ClassName() const
00331 {
00332         return "GestureGraph";
00333 }
00334 
00335 double GestureGraph::NodeDistance(leda_node u, const DAG& from, leda_node v) const
00336 {
00337  int i;
00338  double hist_distance, hist_scale;
00339  leda_list_item it;
00340  cerr << "GestureGraph::NodeDistance not implemented!" << endl;
00341  const GestureGraph& ggFrom = dynamic_cast<const GestureGraph&>(from);
00342  const GGNode* pU = GetNode(u);
00343  const GGNode* pV = ggFrom->GetNode(v);
00344 
00345  hist_distance = CompareHistograms(pU->m_xCoords, pU->m_weights,
00346                 pV->m_xCoords, pV->m_weights);
00347  hist_scale = CompareHistograms(pU->m_scales, pU->m_scales,
00348                 pV->m_scales, pV->m_scales);
00349  
00350 
00351  //For now, we'll return the average
00352  return (hist_distance + hist_scale) / 2.0;
00353 }
00354 
00355 /*
00356  * For a given node, the         following method counts the number of its siblings,
00357  * parents and children, if any.
00358  * This number is necassary to create an input file to
00359  * feed the earth mover's distance code.
00360  */
00361 int GestureGraph::Get_number_of_relations(int node){
00362                                                                                 
00363  int counter = 0; //The first number accounts for the node itself.
00364  int nVertices = hierarchyLevels.GetSize();
00365 
00366 //cout << "no_vertices from size :" << hierarchyLevels.GetSize() << endl;   
00367                                                                             
00368  for(int i=0; i < nVertices; i++){
00369                                                                                 
00370         if (hierarchicalMatrix[node][i]!=0) //count all the relations.
00371                  counter++;
00372                                                                                 
00373  }
00374 
00375  return counter;
00376                                                                                 
00377 }
00378 
00379 
00380 int GestureGraph::Get_nth_relation(int n, int node){
00381                                                                                 
00382   int no = 0;
00383   int nVertices = hierarchyLevels.GetSize();
00384                                                                                 
00385         for (int i=0; i<nVertices; i++){
00386                 if (hierarchicalMatrix[node][i] != 0)
00387                         no++;
00388                                                                                 
00389                 if (no==n)
00390                         return i;
00391         }
00392                                                                                 
00393  return 0;
00394 }
00395 
00396 /*
00397  * Computing the Euclidean distance between 2 nodes.
00398  */
00399 
00400 float GestureGraph::Get_distance(int node1, int node2){
00401 
00402  const DAGNode *dag_ptr1 = GetNode(GetNode(node1));
00403  const GGNode *p1 = dynamic_cast<const GGNode*>(dag_ptr1);
00404 
00405  const DAGNode *dag_ptr2 = GetNode(GetNode(node2));
00406  const GGNode *p2 = dynamic_cast<const GGNode*>(dag_ptr2);
00407 
00408  int x1 = p1->m_nXPos;
00409  int y1 = p1->m_nYPos;
00410  int x2 = p2->m_nXPos;
00411  int y2 = p2->m_nYPos;
00412  float temp = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
00413  
00414  return sqrt(temp);
00415 }
00416 
00417 
00418 int GestureGraph::Get_xpos(int node){
00419 
00420  const DAGNode *dag_ptr = GetNode(GetNode(node));
00421  const GGNode *p = dynamic_cast<const GGNode*>(dag_ptr);
00422 
00423  return p->m_nXPos;
00424 
00425 }
00426 
00427 int GestureGraph::Get_ypos(int node){
00428 
00429  const DAGNode *dag_ptr = GetNode(GetNode(node));
00430  const GGNode *p = dynamic_cast<const GGNode*>(dag_ptr);
00431 
00432  return p->m_nYPos;
00433 
00434 }
00435 
00436 

Generated on Sat Nov 13 11:21:23 2004 for Noisy DAG Matcher by doxygen1.2.18