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

# Main.cpp

Go to the documentation of this file.

    00001
    00052 #include "Main.h"
    00053
    00054 #define CAST(A) (CLP_ACTION)(& DAGCmdLineParser::A)
    00055
    00056 extern fstream g_logFile;
    00057
    00058 int main(int argc, char* argv[])
    00059 {
    00060         DAGCmdLineParser clp;
    00061
    00062         if (!clp.Parse(argc, argv))
    00063                 return -1;
    00064
    00065         if (clp.IsSelected('h'))
    00066         {
    00067                 clp.ShowHelp();
    00068                 return 0;
    00069         }
    00070
    00071         if (clp.GetArgCount() <= 0)
    00072         {
    00073                 clp.ShowUsageMsg();
    00074                 clp.ShowErrorMsg("Missing database file name");
    00075                 return -1;
    00076         }
    00077
    00078         if (clp.Verbose())
    00079                 clp.Print();
    00080
    00081         if (clp.IsSelected('c') || clp.IsSelected("mergedb"))
    00082         {
    00083                 if (!clp.dagDB.Create(clp.GetArg(0)))
    00084                 {
    00085                         ShowError("Can't create object database");
    00086                         return -1;
    00087                 }
    00088         }
    00089         else if (clp.IsSelected('a'))
    00090         {
    00091                 if (!clp.dagDB.Open(clp.GetArg(0), ios::out | ios::binary))
    00092                 {
    00093                         ShowError("Can't open database for adding objects");
    00094                         return -1;
    00095                 }
    00096
    00097                 if (!clp.IsSGComputationSyncMode())
    00098                 {
    00099                         ShowError("Due to a bug, object addition will be set to work in synchronous mode.");
    00100                         clp.SetSGComputationSyncMode();
    00101                 }
    00102         }
    00103         else if (!clp.dagDB.Open(clp.GetArg(0)))
    00104         {
    00105                 ShowError("Can't open object database");
    00106                 return -1;
    00107         }
    00108
    00109         // Set global parameters for the DAGs
    00110         DAG::SetMatchParams(clp.m_matchParams);
    00111
    00112         clp.ExecActions();
    00113
    00114         return 0;
    00115 }
    00116
    00117 /*
    00118         Notice that the order of adm_matchParamsdition of 'Actions' matters since it determines
    00119         the priority of the action. If more than one action are selected,
    00120         the action's priority specifies the execution order.
    00121 */
    00122 void DAGCmdLineParser::InitOptions()
    00123 {
    00124         SetOptArgHelp("db-file-name file1 file2 ...");
    00125
    00126         // ADD ACTIONS
    00127         Add('d', "debug", "Generate a number of log files", CAST(EnterDbgMode));
    00128         Add('c', "create", "Create database of shock graphs", CAST(AddObjsToDB));
    00129         Add('a', "add", "Add DAG to DB", CAST(AddObjsToDB));
    00130         Add('l', "list", "List DAGs in the database", CAST(ListDAGs));
    00131         Add('s', "size", "Shows the database size in num. of views", CAST(ShowDBSize));
    00132         Add('i', "createIndex", "Explicitly creates the DB index", CAST(CreateDBIndex));
    00133         Add('m', "match", "Match DAG [DAG ID]", &m_target, "", CAST(Match));
    00134         Add('p', "plot", "Plot DAG node info", CAST(PlotNodeInfo));
    00135         Add('v', "view", "Visualize a DAG", &m_viewDag, "", CAST(ViewDAG));
    00136         Add('w', "mview", "Visualize all DAGs", CAST(ViewDAGs));
    00137
    00138         Add(0, "matrix", "Creates an affinity matrix for all the models in the database",
    00139                 &m_szMatrixFileName, "matrix.m", CAST(CreateAffinityMatrix));
    00140
    00141         Add(0, "delete", "Delete DAGs from database", CAST(DeleteDAG));
    00142         Add(0, "updatedb", "Read in the db and write it back", CAST(UpdateDB));
    00143         Add(0, "recomputeSG", "Recomputes all SG an saves them in another DB", CAST(RecomputeSG));
    00144         Add(0, "splitdb", "Split database in N%, (100-N)% pieces", &m_nSplitProportion, 50, CAST(SplitDB));
    00145         Add(0, "mergedb", "Merge databases (dest src1 src2 ...)", CAST(MergeDB));
    00146         Add(0, "test", "Test all the object in the DB", CAST(Test));
    00147         Add(0, "objNames", "List object names in the database", CAST(ListObjectNames));
    00148         Add(0, "delObjects", "Removes all dags of given objects", CAST(DeleteObjects));
    00149         Add(0, "closNeigList", "List the 3 closest nighbours of each view", CAST(ClosestNeigList));
    00150         Add(0, "getNeig", "List the neighbours withing a given range in deg.", &m_dNeigRange, 180, CAST(GetNeig));
    00151         Add('h', "help", "Display a man page for this program", CAST(ShowHelp));
    00152
    00153         // ADD DAG MATCHING PARAMETERS
    00154         Add(0, "tsvSimWeight", "TSV similarity weight", &m_matchParams.dTSVSimWeight, 0.3);
    00155         Add(0, "simMassWeight", "Similarity vs Mass weight", &m_matchParams.dSimilMassWeight, 0.7);
    00156         Add(0, "relMassWeight", "Relative mass of the query vs. tot model", &m_matchParams.dRelMassWeight, 0.5);
    00157
    00158         Add(0, "simFuncType", "Node similarity function", (int*)(void*)&m_matchParams.nSimFuncType, 1);
    00159         Add(0, "nodeDistFunc", "Node distance function", &m_matchParams.nNodeDistFunc, 3);
    00160
    00161         Add(0, "minNodeSim", "Minimum node similarity allowed", &m_matchParams.dMinNodeSim, 0.0);
    00162         Add(0, "sibRelPen", "Penalty term for breaking sibling's relationship (1=max)", &m_matchParams.dBreakSibRelPen, 0.2);
    00163
    00164         Add(0, "presAncRel", "Preserve ancestor relationships when matching", &m_matchParams.nPreserveAncestorRel, 1);
    00165         Add(0, "useNewVWF", "Use new vote weighting function", &m_matchParams.nUseNewVoteWeightFunc, 1);
    00166         Add(0, "useMOOVC", "Use multiple one-to-one vote counting", &m_matchParams.nUseMOOVC, 1);
    00167
    00168         // ADD MATCHING PROCESS INFORMATION
    00169         Add(0, "modelSimWeight", "Model similarity weight", &m_matchInfo.modelSimWeight, 0.5);
    00170         Add(0, "idxRange", "Index range", &m_matchInfo.idxrange, 0.4);
    00171         Add(0, "kBest", "Match only k best from index", &m_matchInfo.idxKBest, 99);
    00172         Add(0, "idxTau", "Index similarity threshold", &m_matchInfo.idxtau, 0.3);
    00173         Add(0, "matchTau", "Matcher similarity threshold", &m_matchInfo.matchtau, 0.1);
    00174         Add(0, "stats", "Compute view statistics", &m_matchInfo.computeStats, 1);
    00175         Add(0, "asyncCompu", "Compute SG's in async mode (see timeout)", &m_matchInfo.asyncCompu, 0);
    00176         Add(0, "showResults", "Show matching results", &m_matchInfo.showResults, 1);
    00177         Add(0, "saveResults", "Save matching results", &m_matchInfo.saveResults, 1);
    00178
    00179         // ADD EXPERIMENT PARAMETERS
    00180         Add('o', "objName", "Set object name", &m_objName, "");
    00181         Add(0, "iterations", "Parameter for the SG computation", &m_iterations, 1);
    00182         Add(0, "maxDags", "Maximum number of DAGs to process", &m_maxdags, -1);
    00183         Add(0, "imgStep", "Image step", &m_imgstep, 1);
    00184         Add(0, "from", "Match DAGs from ID N", &m_from, 0);
    00185         Add(0, "to", "Match DAGs up to ID M", &m_to, -1);
    00186         Add(0, "maxOcclRate", "Maximum occlusion rate", &m_maxOcclusionRate, 0.0);
    00187         Add(0, "saveSubsamplingInfo", "Save info to split DB when matching", &m_saveSubsamplingInfo, 0);
    00188         Add(0, "splitByObject", "Split DB by object", &m_nSplitByObject, 1);
    00189
    00190         // Shock Graph parameters
    00191         Add(0, "timeout", "SG computation Timeout in seconds", &m_sgInfo.timeout, 800);
    00192         Add(0, "usenewcode", "Use the new sg code? [0 or 1]", &m_sgInfo.usenewcode, 1);
    00193         Add(0, "split3s", "Should split 3s into left and right 3s", &m_sgInfo.sgparams.nSlipt3s, 0);
    00194         Add(0, "minslope", "Slope that is to be considered as zero", &m_sgInfo.sgparams.dMinSlope, 0.05);
    00195         Add(0, "minerror", "Coeff for max min-least-square error tolerated", &m_sgInfo.sgparams.dMinError, 2.0);
    00196         Add(0, "skthreshold", "Parameter for the skeleton computation", &m_sgInfo.sgparams.dSkTreshold, 2.5);
    00197         Add(0, "maxaccelchg", "Max acceleration chage tolerated", &m_sgInfo.sgparams.dMaxAccelChg, 0.5);
    00198
    00199         Add(0, "verbose", "Execute in verbose mode", &m_nVerbose, 0);
    00200 }
    00201
    00202 void DAGCmdLineParser::ShowUsageExamples()
    00203 {
    00204         cerr << "\nExamples:\n"
    00205
    00206                 << "\nCreate a database from all the pgm files in a directory and subdirectories:\n"
    00207                 << "\n\tdm -c objs.db MyImageDir | dm -c -cutoff .15 objs.db MyImageDir\n"
    00208
    00209                 << "\nCreate a database from pgm files:\n"
    00210                 << "\n\tdm -c objs.db file1.pgm file2.pgm ...\n"
    00211
    00212                 << "\nDelete dags from the database:\n"
    00213                 << "\n\tdm -delete source.db target.db id1 id2 ...\n"
    00214
    00215                 << "\nList objects stored in the database (ID, name, ...):\n"
    00216                 << "\n\tdm -l objs.db\n"
    00217
    00218                 << "\nMatch a random object in the DB against all the objects:\n"
    00219                 << "\n\tdm -m objs.db | dm -m -idxtau .6 objs.db\n"
    00220
    00221                 << "\nMatch a particular object in the DB against all the objects:\n"
    00222                 << "\n\tdm -m obj-id objs.db\n"
    00223
    00224                 << "\nMatch all objects in a db against those in another db:\n"
    00225                 << "\n\tdm -m querys.db models.db\n"
    00226
    00227                 << "\nDisplay DAG(s):\n"
    00228                 << "\n\tdm -v obj-id objs.db | dm -w objs.db [obj-id obj-id...]\n";
    00229
    00230         cerr << endl;
    00231 }
    00232
    00233 bool DAGCmdLineParser::AddObjsToDB()
    00234 {
    00235         DirWalker dirDw, fileDw;
    00236         char szDirName[MAX_PATH], szFileName[MAX_PATH];
    00237         char fileExt[4];
    00238         int imgNumber;
    00239
    00240         ShowMsg("Adding objects to database...");
    00241
    00242         for (int i = 1; i < GetArgCount(); i++)
    00243         {
    00244                 if (DirWalker::IsDirectory(GetArg(i)))
    00245                 {
    00246                         DirWalker::CreateFileName(".", GetArg(i), "log", szFileName);
    00247                         g_logFile.open(szFileName, ios::out | ios::trunc);
    00248
    00249                         if (!dirDw.OpenDir(GetArg(i)))
    00250                         {
    00251                                 g_logFile << "Error: Can't open " << GetArg(i) << endl;
    00252                                 return false;
    00253                         }
    00254
    00255                         strcpy(szDirName, GetArg(i));
    00256
    00257                         do
    00258                         {
    00259                                 g_logFile << endl << szDirName;
    00260                                 g_logFile.flush();
    00261
    00262                                 if (!fileDw.OpenDir(szDirName))
    00263                                 {
    00264                                         g_logFile << "Error: Can't open " << szDirName << endl;
    00265                                         continue;
    00266                                 }
    00267
    00268                                 imgNumber = 0;
    00269
    00270                                 while (fileDw.GetNextFile(szFileName, MAX_PATH, fileExt))
    00271                                 {
    00272                                         if (!(imgNumber++ % m_imgstep))
    00273                                                 ProcessFile(szFileName, dagDB, g_logFile, m_sgInfo, m_matchInfo.asyncCompu);
    00274                                 }
    00275
    00276                                 fileDw.CloseDir();
    00277
    00278                         } while (dirDw.GetNextDir(szDirName, MAX_PATH));
    00279                 }
    00280                 else
    00281                 {
    00282                         g_logFile.open("dbcreation.log", ios::out | ios::app);
    00283                         ProcessFile(GetArg(i), dagDB, g_logFile, m_sgInfo, m_matchInfo.asyncCompu);
    00284                 }
    00285
    00286                 g_logFile.close();
    00287         }
    00288
    00289         ShowMsg("... all objects have been added.\n");
    00290
    00291         return true;
    00292 }
    00293
    00294 bool DAGCmdLineParser::ListDAGs()
    00295 {
    00296         dagDB.ListDAGs();
    00297
    00298         return true;
    00299 }
    00300
    00301 bool DAGCmdLineParser::ListObjectNames()
    00302 {
    00303         String lastObjName;
    00304         DAGPtr ptrDag;
    00305         int nObjIdx = 0;
    00306
    00307         cout.flags(ios::left);
    00308
    00309         for (int i = 0; i < dagDB.GetDAGCount(); i++)
    00310         {
    00311                 ptrDag = dagDB.ReadDAG(i, true);
    00312
    00313                 if (lastObjName != ptrDag->GetObjName())
    00314                 {
    00315                         cout << setw(5) << nObjIdx << setw(30) << ptrDag->GetObjName() << endl;
    00316                         lastObjName = ptrDag->GetObjName();
    00317                         nObjIdx++;
    00318                 }
    00319         }
    00320
    00321         return true;
    00322 }
    00323
    00324 bool DAGCmdLineParser::DeleteObjects()
    00325 {
    00326         DAGPtr ptrDag;
    00327         DAGDatabase db1;
    00328
    00329         if (GetArgCount() < 3)
    00330         {
    00331                 ShowError("Usage: dbname.db newdb.db objname1 objname2 ...");
    00332                 return false;
    00333         }
    00334
    00335         if (!db1.Create(GetArg(1)))
    00336         {
    00337                 ShowErrorMsg("Can't create the requested database");
    00338                 return false;
    00339         }
    00340
    00341         bool bDelObj;
    00342
    00343         for (int i = 0; i < dagDB.GetDAGCount(); i++)
    00344         {
    00345                 ptrDag = dagDB.ReadDAG(i, true);
    00346                 bDelObj = false;
    00347
    00348                 for (int j = 2; j < GetArgCount(); j++)
    00349                         if (!strcasecmp(GetArg(j), ptrDag->GetObjName()))
    00350                         {
    00351                                 bDelObj = true;
    00352                                 break;
    00353                         }
    00354
    00355                 if (!bDelObj)
    00356                         db1.AddDAG(ptrDag);
    00357         }
    00358 }
    00359
    00360 bool DAGCmdLineParser::ShowDBSize()
    00361 {
    00362         cout << dagDB.GetDAGCount() << endl;
    00363
    00364         return true;
    00365 }
    00366
    00367 bool DAGCmdLineParser::UpdateDB()
    00368 {
    00369         DAGPtr ptrDag;
    00370         DAGDatabase db1;
    00371
    00372         ASSERT(GetArgCount() == 2);
    00373
    00374         cerr << "\nUpdating database...\n";
    00375
    00376         if (!db1.Create(GetArg(1)))
    00377         {
    00378                 ShowErrorMsg("Can't create the requested database");
    00379                 return false;
    00380         }
    00381
    00382         for (int i = 0; i < dagDB.GetDAGCount(); i++)
    00383         {
    00384                 ptrDag = dagDB.ReadDAG(i);
    00385                 cerr << endl << ptrDag->GetDAGLbl();
    00386                 ptrDag->ComputeDerivedValues();
    00387                 db1.AddDAG(ptrDag);
    00388         }
    00389
    00390         cerr << "\nDone!\n";
    00391
    00392         return true;
    00393 }
    00394
    00395 bool DAGCmdLineParser::RecomputeSG()
    00396 {
    00397         DAGPtr ptrDag;
    00398         DAGDatabase db1;
    00399
    00400         ASSERT(GetArgCount() == 2);
    00401
    00402         cerr << "\nRecomputing SG database...\n";
    00403
    00404         if (!db1.Create(GetArg(1)))
    00405         {
    00406                 ShowErrorMsg("Can't create the requested database");
    00407                 return false;
    00408         }
    00409
    00410         for (int i = 0; i < dagDB.GetDAGCount(); i++)
    00411         {
    00412                 ptrDag = dagDB.ReadDAG(i);
    00413                 cerr << endl << ptrDag->GetDAGLbl() << endl;
    00414
    00415                 ShockGraph* pSG = (ShockGraph*)(const DAG*) ptrDag;
    00416
    00417                 pSG->RecomputeFromSkeleton(m_sgInfo.sgparams);
    00418
    00419                 db1.AddDAG(ptrDag);
    00420         }
    00421
    00422         cerr << "\nDone!\n";
    00423
    00424         return true;
    00425 }
    00426
    00427 bool DAGCmdLineParser::DeleteDAG()
    00428 {
    00429         DAGPtr ptrDag;
    00430         DAGDatabase db1;
    00431
    00432         ASSERT(GetArgCount() > 2);
    00433
    00434         cerr << "\nCreating copy of database...\n";
    00435
    00436         if (!db1.Create(GetArg(1)))
    00437         {
    00438                 ShowErrorMsg("Can't create the requested database");
    00439                 return false;
    00440         }
    00441
    00442         // Collect all the IDs to remove
    00443         int i, j, n = GetArgCount();
    00444         int ids[n]; //has extra space
    00445         bool bDeleteDAG;
    00446
    00447         for (j = 2; j < n; j++)
    00448         {
    00449                 ids[j] = atoi(GetArg(j));
    00450
    00451                 if (ids[j] == 0 && GetArg(j)[0] != '0')
    00452                 {
    00453                         cerr << "\n" << GetArg(j) << " is not a valid DAG ID\n";
    00454                         return false;
    00455                 }
    00456         }
    00457
    00458         // inefficient but simple batch removal of the dags
    00459         for (i = 0; i < dagDB.GetDAGCount(); i++)
    00460         {
    00461                 bDeleteDAG = false;
    00462
    00463                 for (j = 2; j < n; j++)
    00464                 {
    00465                         if (ids[j] == i)
    00466                         {
    00467                                 cout << "Deleting DAG ID: " << i << endl;
    00468                                 bDeleteDAG = true;
    00469                                 break; // don't copy this dag
    00470                         }
    00471                 }
    00472
    00473                 if (!bDeleteDAG)
    00474                         db1.AddDAG(dagDB.ReadDAG(i));
    00475         }
    00476
    00477         cerr << "\nDone!\n";
    00478
    00479         return true;
    00480 }
    00481
    00482 bool DAGCmdLineParser::SplitDB()
    00483 {
    00484         DAGPtr ptrDag;
    00485         DAGDatabase db1, db2;
    00486         int i, j;
    00487
    00488         ASSERT(GetArgCount() == 3);
    00489
    00490         if (!db1.Create(GetArg(1)) || !db2.Create(GetArg(2)))
    00491         {
    00492                 ShowErrorMsg("Can't create the requested databases");
    00493                 return false;
    00494         }
    00495
    00496         int nDAGCount = dagDB.GetDAGCount();
    00497
    00498         char szFileName[MAX_PATH];
    00499         ViewSubsampler vs;
    00500         VIEWWEIGHT vw;
    00501
    00502         DirWalker::ChangeFileExt(dagDB.GetFileName(), NULL, szFileName);
    00503         strcat(szFileName, "_subsampling.inf");
    00504
    00505         // Views are retrieved weighthed according to how badly they are ranked.
    00506         // the worst ranked ones are ranked first and are to be deleted.
    00507         /*if (!vs.RetrieveViewWeights(szFileName, 128, m_nSplitByObject != 0))
    00508         {
    00509                 cerr << "Can't retrieve subsampling info from " << szFileName << endl;
    00510                 return false;
    00511         }*/
    00512
    00513         int nMaxRankPos = (int)floor((m_nSplitByObject ? 128:nDAGCount) * (m_nSplitProportion / 100.0));
    00514
    00515         /*for (i = 0; i < nDAGCount; i++)
    00516         {
    00517                 ptrDag = dagDB.ReadDAG(i);
    00518                 vw = vs.GetViewRank(ptrDag->GetObjName(), ptrDag->GetViewNumber());
    00519
    00520                 (vw.nViewRankPos < nMaxRankPos) ? db2.AddDAG(ptrDag):db1.AddDAG(ptrDag);
    00521
    00522                 // DBG
    00523                 if (vw.nViewRankPos < nMaxRankPos)
    00524                         cout << "(" << ptrDag->GetObjName() << ", " << ptrDag->GetViewNumber() << ")"
    00525                                 << " is out. Has rank " << vw.nViewRankPos << " and vote " << vw.dVoteWeight << endl;
    00526         }*/
    00527         SmartArray<int> viewsToRemove;
    00528         String strLastObjName;
    00529
    00530         if (!m_nSplitByObject)
    00531                 viewsToRemove = vs.RandomSubsampling(nDAGCount, nMaxRankPos);
    00532
    00533         for (i = 0; i < nDAGCount; i++)
    00534         {
    00535                 ptrDag = dagDB.ReadDAG(i);
    00536
    00537                 if (m_nSplitByObject && strLastObjName != ptrDag->GetObjName())
    00538                 {
    00539                         viewsToRemove = vs.RandomSubsampling(128, nMaxRankPos);
    00540                         strLastObjName = ptrDag->GetObjName();
    00541                 }
    00542
    00543                 (viewsToRemove[ptrDag->GetViewNumber() - 1]) ? db2.AddDAG(ptrDag):db1.AddDAG(ptrDag);
    00544
    00545                 // DBG
    00546                 if (viewsToRemove[ptrDag->GetViewNumber() - 1])
    00547                         cout << "(" << ptrDag->GetObjName() << ", " << ptrDag->GetViewNumber() << ") is out." << endl;
    00548         }
    00549
    00550         return true;
    00551 }
    00552
    00553 bool DAGCmdLineParser::MergeDB()
    00554 {
    00555         DAGDatabase source;
    00556         int i, j;
    00557
    00558         cerr << "\nMerging databases...\n";
    00559
    00560         for (i = 1; i < GetArgCount(); i++)
    00561         {
    00562                 if (!source.Open(GetArg(i)))
    00563                 {
    00564                         cerr << "Can't open " << GetArg(i) << " database." << endl;
    00565                         return false;
    00566                 }
    00567
    00568                 for (j = 0; j < source.GetDAGCount(); j++)
    00569                         dagDB.AddDAG(source.ReadDAG(j));
    00570
    00571                 source.Close();
    00572         }
    00573
    00574         cerr << "\nDone!\n";
    00575
    00576         return true;
    00577 }
    00578
    00579 bool DAGCmdLineParser::ViewDAG()
    00580 {
    00581         int nDagId;
    00582
    00583         if (isdigit(*m_viewDag) && (nDagId = atoi(m_viewDag)) < dagDB.GetDAGCount())
    00584         {
    00585                 DAGPtr ptrDag;
    00586
    00587                 ptrDag = dagDB.ReadDAG(nDagId);
    00588
    00589                 //ptrDag->Print();
    00590
    00591                 ShowSkeleton(ptrDag);
    00592                 //ShowGraph(*ptrDag);
    00593         }
    00594         else
    00595         {
    00596                 ShowErrorMsg("Invalid DAG ID");
    00597                 return false;
    00598         }
    00599
    00600         return true;
    00601 }
    00602
    00603 bool DAGCmdLineParser::ViewDAGs()
    00604 {
    00605         // If there are no arguments (besides the DB name), we show all the SGs.
    00606
    00607         if (GetArgCount() <= 1)
    00608         {
    00609                 char szDagId[100];
    00610                 m_viewDag = szDagId;
    00611
    00612                 for (int i = 0; i < dagDB.GetDAGCount(); i++)
    00613                 {
    00614                         sprintf(szDagId, "%d", i);
    00615                         ViewDAG();
    00616                 }
    00617         }
    00618         else
    00619         {
    00620                 for (int i = 1; i < GetArgCount(); i++)
    00621                 {
    00622                         m_viewDag = GetArg(i);
    00623                         ViewDAG();
    00624                 }
    00625         }
    00626
    00627         return true;
    00628 }
    00629
    00633 bool DAGCmdLineParser::CreateDBIndex()
    00634 {
    00635         dagDB.OpenIndex();
    00636         return true;
    00637 }
    00638
    00639 bool DAGCmdLineParser::Match()
    00640 {
    00641         DAGPtr ptrDag;
    00642         char szFileName[MAX_PATH], szFileNameNoExt[MAX_PATH];
    00643         fstream os;
    00644         ViewSubsampler vs; // Only used id subsampling info is needed
    00645
    00646         dagDB.OpenIndex();
    00647
    00648         if (m_matchInfo.saveResults)
    00649         {
    00650                 DirWalker::ChangeFileExt(dagDB.GetFileName(), NULL, szFileNameNoExt);
    00651                 strcat(szFileNameNoExt, "_results");
    00652
    00653                 DirWalker::CreateFileName(".", szFileNameNoExt, "log", szFileName);
    00654                 cerr << "Writing output to " << szFileName << "...\n";
    00655                 os.open(szFileName, ios::out | ios::trunc);
    00656
    00657                 Print(os);
    00658         }
    00659
    00660         if (strlen(m_target) == 0)
    00661         {
    00662                 DAGPtr pTargetDag;
    00663
    00664                 // Choose target DAG
    00665                 for(int j = 0; j < m_iterations; j++)
    00666                 {
    00667                         STATINFO idxSt, matSt;
    00668                         pTargetDag = dagDB.ChooseDAG();
    00669                         MatchDAG(pTargetDag, dagDB, m_matchInfo, idxSt, matSt);
    00670                         SaveResults(pTargetDag, dagDB, idxSt, matSt, os);
    00671                         ShowResults(pTargetDag, matSt, dagDB, dagDB);
    00672                 }
    00673         }
    00674         else if (DirWalker::CompFileExt(m_target, "db"))
    00675         {
    00676                 DAGDatabase imgDB;
    00677                 double dOcclusionRate = 0;
    00678                 time_t compTime;
    00679                 int nFileIdx;
    00680
    00681                 DirWalker::ChangeFileExt(m_target, NULL, szFileNameNoExt);
    00682                 strcat(szFileNameNoExt, "_stats");
    00683
    00684                 nFileIdx = DirWalker::CreateFileName(".", szFileNameNoExt, "m", szFileName);
    00685                 fstream plot1(szFileName, ios::trunc | ios::out);
    00686
    00687                 int i = 0;
    00688
    00689                 if (imgDB.Open(m_target))
    00690                 {
    00691                         bool bShowRes = m_matchInfo.showResults && AskBool("\nShow matching result for all DAGs");
    00692
    00693                         STATINFO::WriteHeader(plot1, nFileIdx, m_matchInfo.idxKBest, m_maxOcclusionRate);
    00694
    00695                         if (m_to == -1)
    00696                                 m_to = imgDB.GetDAGCount() - 1;
    00697                         else if (m_to >= imgDB.GetDAGCount())
    00698                         {
    00699                                 ShowErrorMsg("'to' value outnumbers the number of objects in the database");
    00700                                 return false;
    00701                         }
    00702
    00703                         for (int i = m_from; i <= m_to; i++)
    00704                         {
    00705                                 STATINFO idxSt, matSt;
    00706
    00707                                 ptrDag = imgDB.ReadDAG(i, true);
    00708
    00709                                 if (m_maxOcclusionRate > 0.0)
    00710                                         dOcclusionRate = OccludeDAG(ptrDag, m_maxOcclusionRate);
    00711
    00712                                 cout << "\nMatching " << ptrDag->GetDAGLbl() << endl;
    00713
    00714                                 compTime = MatchDAG(ptrDag, dagDB, m_matchInfo, idxSt, matSt);
    00715
    00716                                 if (m_matchInfo.saveResults)
    00717                                         SaveResults(ptrDag, dagDB, idxSt, matSt, os);
    00718
    00719                                 if (bShowRes)
    00720                                         bShowRes = ShowResults(ptrDag, matSt, imgDB, dagDB, i);
    00721
    00722                                 STATINFO::Write(plot1, i, ptrDag->GetViewNumber(), idxSt, matSt, compTime, dOcclusionRate);
    00723
    00724                                 if (m_saveSubsamplingInfo)
    00725                                         AddSubsamplingInfo(vs, ptrDag, matSt, m_matchInfo.idxKBest);
    00726                         }
    00727
    00728                         STATINFO::WriteTailer(plot1);
    00729
    00730                         if (m_saveSubsamplingInfo)
    00731                                 SaveSubsamplingInfo(vs, dagDB.GetFileName());
    00732                 }
    00733                 else
    00734                         ShowErrorMsg("Can't open target DB");
    00735         }
    00736         else
    00737         {
    00738                 int nDagId = atoi(m_target);
    00739
    00740                 if (nDagId < dagDB.GetDAGCount())
    00741                 {
    00742                         STATINFO idxSt, matSt;
    00743
    00744                         ptrDag = dagDB.ReadDAG(nDagId);
    00745
    00746                         if (GetArgCount() > 1)
    00747                         {
    00748                                 for (int i = 1; i < GetArgCount(); i++)
    00749                                 {
    00750                                         MatchDAG(ptrDag, dagDB, m_matchInfo, atoi(GetArg(i)), matSt);
    00751
    00752                                         if (m_matchInfo.saveResults)
    00753                                                 SaveResults(ptrDag, dagDB, idxSt, matSt, os);
    00754
    00755                                         ShowResults(ptrDag, matSt, dagDB, dagDB, nDagId);
    00756                                 }
    00757                         }
    00758                         else
    00759                         {
    00760                                 MatchDAG(ptrDag, dagDB, m_matchInfo, idxSt, matSt);
    00761
    00762                                 if (m_matchInfo.saveResults)
    00763                                         SaveResults(ptrDag, dagDB, idxSt, matSt, os);
    00764
    00765                                 ShowResults(ptrDag, matSt, dagDB, dagDB, nDagId);
    00766                         }
    00767                 }
    00768                 else
    00769                         ShowErrorMsg("Invalid DAG ID");
    00770         }
    00771
    00772         cerr << "\nDone!\n";
    00773
    00774         return true;
    00775 }
    00776
    00777 bool DAGCmdLineParser::PlotNodeInfo()
    00778 {
    00779         //char szLbl[100];
    00780         /*int i, j, k;
    00781         bool bRes = true;
    00782         DAGPtr ptrDag;
    00783         leda_node v;
    00784         fstream plt("plots.m", ios::out | ios::trunc);
    00785
    00786         const int ROWS = 4, COLS = 4;
    00787
    00788         for (i = 1; i < GetArgCount() && bRes; i++)
    00789         {
    00790                 const char* sz = GetArg(i);
    00791
    00792                 if (isdigit(*sz))
    00793                         ptrDag = dagDB.ReadDAG(atoi(sz));
    00794                 else
    00795                         ShowErrorMsg("Invalid DAG ID");
    00796
    00797                 if (bRes)
    00798                 {
    00799                         k = 1;
    00800                         plt << "figure(" << i << ");\n";
    00801
    00802                         plt << "hold on;\n";
    00803
    00804                         forall_nodes(v, sg)
    00805                         {
    00806                                 const SGNode& n = *sg.GetSGNode(v);
    00807
    00808                                 plt << "x = 1:" << n.GetShockCount() << ";\n";
    00809                                 plt << "y = [";
    00810
    00811                                 for (j = 0; j < n.GetShockCount(); j++)
    00812                                         plt << n.m_shocks[j].dr_ds << ' ';
    00813
    00814                                 plt << "];\n";
    00815
    00816                                 plt << "subplot(" << ROWS << ", " << COLS << ", " << k++ << ");\n";
    00817                                 plt << "plot(x, y);\n";
    00818                                 plt << "xlabel('node " << n.GetNodeLbl() << "');\n";*/
    00819
    00820                                 /*plt << "xi = 1:(" << n.GetShockCount() << "-1)/100:" << n.GetShockCount() << ";\n";
    00821                                 plt << "xi = xi(1, 1:100);\n";
    00822                                 plt << "yi = interp1(x, y, xi);\n";
    00823                                 plt << "yi(isnan(yi)) = 0;";
    00824                                 plt << "plot(xi, yi);\n";
    00825                                 plt << "xlabel('node " << n.GetNodeLbl() << "');\n";
    00826
    00827                                 plt << "subplot(" << ROWS << ", " << COLS << ", " << k++ << ");\n";
    00828                                 plt << "m(:," << kk++ << ") = fftshift(abs(fft(yi)))';\n";
    00829                                 plt << "plot(fftshift(abs(fft(yi))));\n";
    00830                                 plt << "xlabel('node " << n.GetNodeLbl() << "');\n";*/
    00831
    00832                                 /*if (k > ROWS * COLS)
    00833                                         break;
    00834                         }
    00835
    00836                         plt << "hold off;\npause;\n";
    00837                 }
    00838         }
    00839
    00840         return bRes;*/
    00841
    00842         return true;
    00843 }
    00844
    00845 bool DAGCmdLineParser::EnterDbgMode()
    00846 {
    00847         DAG::EnterDbgMode();
    00848         return true;
    00849 }
    00850
    00854 bool DAGCmdLineParser::ClosestNeigList()
    00855 {
    00856         // Check closest views indices...
    00857         int a, b, c;
    00858
    00859         SmartArray<int> map = GetSymmetricViewMap();
    00860
    00861         for (int i = 1; i <= 128; i++)
    00862         {
    00863                 GetClosestViews(i, 3, a, b, c);
    00864
    00865                 cout << "\n\nview " << i << " (sym. " << map[i] << ") has neighbours "
    00866                         << a << ", " << b << ", " << c;
    00867         }
    00868
    00869         cout << endl << map[0] << endl;
    00870
    00871         return true;
    00872 }
    00873
    00874 bool DAGCmdLineParser::GetNeig()
    00875 {
    00876         SmartArray<int> views;
    00877
    00878         //views = GetClosestViewsByRange(1, 3, m_dNeigRange);
    00879         views = GetFarthestViews((int)m_dNeigRange, 3, 160.0);
    00880
    00881         for (int i = 0; i < views.GetSize(); i++)
    00882                 cout << endl << views[i] + 1;
    00883
    00884         cout << endl << "Number of views: " << views.GetSize() << endl;
    00885
    00886         return true;
    00887 }
    00888
    00889 bool DAGCmdLineParser::Test()
    00890 {
    00891         cout << "\nRunning Test function...\n" << endl;
    00892
    00893         // Read all dags and...
    00894         DAGPtr ptrDag;
    00895         leda_node v;
    00896         int i, j, nShockCount;
    00897
    00898         for (i = 0; i < dagDB.GetDAGCount(); i++)
    00899         {
    00900                 ptrDag = dagDB.ReadDAG(i, true);
    00901
    00902                 if (ptrDag->GetNodeCount() <= 5 || ptrDag->GetNodeCount() >= 40)
    00903                 {
    00904                         cout << "\n[" << i << "] " <<
    00905                                 ptrDag->GetDAGLbl() << " -> " << ptrDag->GetNodeCount() << endl;
    00906
    00907                 // Count number of shocks in each node
    00908                 forall_nodes(v, *ptrDag)
    00909                 {
    00910                         const ShockGraph* pSG = dynamic_cast<const ShockGraph*>((const DAG*) ptrDag);
    00911                         const SGNode* pNode = pSG->GetSGNode(v);
    00912                         nShockCount = pNode->GetShockCount();
    00913
    00914                         for (j = 0; j < nShockCount; j++)
    00915                                 if ((*pNode)[j].radius <= 1.0)
    00916                                         cout << "[" << i << "] " << pSG->GetDAGLbl() << ":Node "
    00917                                                 << pSG->GetNodeLbl(v) << "[" << j << "] has radius = "
    00918                                                 << (*pNode)[j].radius << endl;
    00919                 }
    00920                 }
    00921         }
    00922
    00923         cout << endl;
    00924
    00925         cout << "Objects in database: " << dagDB.GetDAGCount() << endl;
    00926
    00927         return true;
    00928 }
    00929
    00930 bool DAGCmdLineParser::ShowHelp()
    00931 {
    00932         char line[80];
    00933         ifstream helpFile("help.txt", ios::in /*| ios::nocreate*/);
    00934
    00935         if (!helpFile.is_open())
    00936         {
    00937                 cerr << "\nError: it can't find help.txt\n";
    00938                 return false;
    00939         }
    00940
    00941         while (!helpFile.fail())
    00942         {
    00943                 helpFile.getline(line, sizeof(line));
    00944                 cout << line << '\n';
    00945         }
    00946
    00947         cout.flush();
    00948
    00949         return true;
    00950 }
    00951
    00952 bool DAGCmdLineParser::CreateAffinityMatrix()
    00953 {
    00954         DAGPtr ptrImgDag, ptrModelDag;
    00955         int i, j, n = dagDB.GetDAGCount();
    00956         Matrix m(n, n);
    00957         DAGNodeMap nodeMap;
    00958
    00959         m = 0.0; // Init matrix
    00960
    00961         for (i = 0; i < dagDB.GetDAGCount(); i++)
    00962         {
    00963                 ptrImgDag = dagDB.ReadDAG(i);
    00964
    00965                 cout << "\nProcessing " << ptrImgDag->GetObjName()
    00966                         << " view # " << ptrImgDag->GetViewNumber() << "..." << flush;
    00967
    00968                 for (j = 0; j < dagDB.GetDAGCount(); j++)
    00969                 {
    00970                         ptrModelDag = dagDB.ReadDAG(j);
    00971                         m(i + 1, j + 1) = ptrImgDag->Similarity(ptrModelDag, nodeMap);
    00972                 }
    00973         }
    00974
    00975         cout << endl;
    00976
    00977         fstream os(m_szMatrixFileName, ios::out | ios::trunc);
    00978
    00979         os << "m = [\n" << setprecision(3) << m << "];\n";
    00980
    00981         return true;
    00982 }
    00983