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

# DAGDatabase.h

Go to the documentation of this file.

    00001
    00040 #ifndef __DAGFILE_H__
    00041 #define __DAGFILE_H__
    00042
    00043 #include <iostream.h>
    00044 #include <LEDA/list.h>
    00045 #include <LEDA/sortseq.h>
    00046 #include <LEDA/array.h>
    00047
    00048 #include "DAGSearchDatabase.h"
    00049 #include "DAGDBFile.h"
    00050
    00051 class DAGList : public leda_list<DAGPtr>
    00052 {
    00053         int nMaxDim;
    00054
    00055 public:
    00056         DAGList() { nMaxDim = 0; }
    00057         int GetMaxDim() const { return nMaxDim; }
    00058         //void SetMaxDim(int nDim) { nMaxDim = nDim; }
    00059         void Append(DAGPtr pDAG)
    00060         {
    00061            int n = ((const DAGPtr)pDAG)->GetMaxTSVDimension();
    00062
    00063            if (n > nMaxDim)
    00064                  nMaxDim = n;
    00065
    00066           append(pDAG);
    00067         }
    00068         int GetSize() const { return size(); }
    00069 };
    00070
    00071 struct MatchInfo
    00072 {
    00073     int nDAGId;
    00074         int nDAGOffset;
    00075         double dSimilarity;
    00076         DAGPtr ptrDAG;
    00077
    00078         MatchInfo()
    00079         {
    00080                 nDAGId = 0;
    00081                 nDAGOffset = 0;
    00082                 dSimilarity = 0;
    00083         }
    00084
    00085         void Set(int id, int offset, double similarity)
    00086         {
    00087                 nDAGId = id;
    00088                 nDAGOffset = offset;
    00089                 dSimilarity = similarity;
    00090         }
    00091
    00092         void Set(DAGPtr ptr, double similarity)
    00093         {
    00094                 ptrDAG = ptr;
    00095                 dSimilarity = similarity;
    00096                 nDAGOffset = ((const DAG*)ptr)->GetFileOffset();
    00097                 nDAGId = ((const DAG*)ptr)->GetDAGId();
    00098         }
    00099
    00100         const char* GetDAGLbl() const
    00101         {
    00102                 return ptrDAG->GetDAGLbl();
    00103         }
    00104
    00105         friend int compare(const MatchInfo& a, const MatchInfo& b);
    00106
    00107         // The stream operators are implemented only to satisfy LEDA requirements
    00108         friend ostream& operator<<(ostream &os, const MatchInfo& mi) { return os; }
    00109         friend istream& operator>>(istream &is, MatchInfo& mi) { return is; }
    00110 };
    00111
    00112 struct Vote
    00113 {
    00114         int modelNode;
    00115         leda_node imgNode;
    00116         double vote;
    00117         int dagId;
    00118     int dagOffset;
    00119     const DAG* pDag;
    00120
    00121         Vote(const DAG* pImgDag) { pDag = pImgDag; modelNode = 0; imgNode = nil; vote = 0; }
    00122         void operator=(const Vote& v)
    00123         {
    00124                 modelNode = v.modelNode;
    00125                 imgNode   = v.imgNode;
    00126                 vote      = v.vote;
    00127                 dagOffset = v.dagOffset;
    00128                 dagId     = v.dagId;
    00129         }
    00130 };
    00131
    00132 class VoteList //: public leda_list<Vote>
    00133 {
    00134         int dagId;              //<! DAG's ID.
    00135         int dagOffset;  //<! In case we need direct acces to the record. Not used anymore.
    00136         LEDA_GRAPH<int, double> bipGraph;
    00137         leda_list<leda_node> modelNodeSet; // Smaller set in general
    00138         leda_list<leda_node> imgNodeSet; // Larger in general
    00139         leda_edge_map<double> votes;
    00140
    00141         leda_sortseq<int, leda_node> modelNodes;
    00142         leda_sortseq<int, leda_node> imgNodes;
    00143
    00144         double totalVote;
    00145
    00146 public:
    00147         VoteList(int prot = -1) { dagId = -1; dagOffset = -1; totalVote = 0; }
    00148         void AddVote(Vote& vote);
    00149         double GetTotalVote(const DAG& dag, const double& minTotalVote);
    00150         double SumAllVotes() const { return totalVote; }
    00151         int GetDAGFileOffset() const { return dagOffset; }
    00152 };
    00153
    00154 typedef SmartPtr<DAGList> DAGListPtr;
    00155
    00156 class DAGDatabase
    00157 {
    00158         DAGDBFile                       m_dagFile;
    00159         DAGSearchDatabase       m_index;
    00160         bool                            m_bIndexOpened;
    00161
    00162 public:
    00163         DAGList                         m_dagList;
    00164
    00165 public:
    00166         DAGDatabase() { m_bIndexOpened = false; }
    00167
    00168         bool Create(const char* szName)
    00169         {
    00170                 bool bFileExist = Open(szName, ios_base::in | ios_base::binary | ios_base::ate) && m_dagFile.tellg() > 0;
    00171                 Close();
    00172
    00173                 if (bFileExist)
    00174                         return false;
    00175
    00176                 return Open(szName, ios_base::out | ios_base::binary | ios_base::trunc); //noreplace
    00177         }
    00178
    00179         bool Open(const char* szName, ios_base::openmode nMode = ios_base::in | ios_base::binary | ios_base::ate);
    00180         bool OpenIndex();
    00181
    00182         void Clear()
    00183         {
    00184                 if (m_dagFile.is_open())
    00185                         m_dagFile.close();
    00186
    00187                 if (m_bIndexOpened)
    00188                 {
    00189                         m_index.Close();
    00190                         m_bIndexOpened = false;
    00191                 }
    00192
    00193                 m_dagList.clear();
    00194         }
    00195
    00196         void Close() { Clear(); }
    00197
    00198         int GetDAGCount() const { return m_dagFile.GetDAGCount(); }
    00199         String GetFileName() const { return m_dagFile.GetName(); }
    00200
    00201         DAGPtr ReadDAG(int nDagId, bool bOnlyDataForMatching = false)
    00202         {
    00203                 return m_dagFile.ReadDAG(nDagId, bOnlyDataForMatching);
    00204         }
    00205
    00206         DAGPtr ReadDAG(streamoff nDagOffset, const String& strClassName)
    00207         {
    00208                 return m_dagFile.ReadDAG(nDagOffset, strClassName);
    00209         }
    00210
    00211         bool AddDAG(const DAG* pDag, bool bReadSaveTailInfo = false)
    00212         {
    00213                 return m_dagFile.AddDAG(pDag, bReadSaveTailInfo);
    00214         }
    00215
    00216         bool ReadObjView(DAG* pDAG, const char* szDAGLabel);
    00217         bool ReadObjView(DAG* pDAG, int nRRN);
    00218         bool ReadObjClass(DAG* pDAGReader, const char* szObjName);
    00219         bool ReadAll(DAG* pDAGReader, int nMaxCount = -1);
    00220
    00221         int GetSimilar(const DAG& dag, leda_array<MatchInfo>& matched, double range,
    00222                 const double& w, const double& minSimilarity);
    00223
    00224         int GetMaxTSVDimension() const
    00225         {
    00226                 return m_index.GetDim();
    00227         }
    00228
    00229         const DAGPtr ChooseDAG() const;
    00230         void ShowDAGList(ostream& os = cout);
    00231         void ListDAGs(ostream& os = cout);
    00232
    00233         DAGSearchDatabase& GetIndexDB() { ASSERT(m_bIndexOpened); return m_index; }
    00234 };
    00235
    00236 #endif //__DAGFILE_H__