1 /**
2  * File: QueryResults.h
3  * Date: March, November 2011
4  * Author: Dorian Galvez-Lopez
5  * Description: structure to store results of database queries
6  * License: see the LICENSE.txt file
7  *
8  */
9 
10 #ifndef __D_T_QUERY_RESULTS__
11 #define __D_T_QUERY_RESULTS__
12 
13 #include <vector>
14 
15 namespace DBoW2 {
16 
17 /// Id of entries of the database
18 typedef unsigned int EntryId;
19 
20 /// Single result of a query
21 class Result
22 {
23 public:
24 
25   /// Entry id
26   EntryId Id;
27 
28   /// Score obtained
29   double Score;
30 
31   /// debug
32   int nWords; // words in common
33   // !!! this is filled only by Bhatt score!
34   // (and for BCMatching, BCThresholding then)
35 
36   double bhatScore, chiScore;
37   /// debug
38 
39   // only done by ChiSq and BCThresholding
40   double sumCommonVi;
41   double sumCommonWi;
42   double expectedChiScore;
43   /// debug
44 
45   /**
46    * Empty constructors
47    */
Result()48   inline Result(){}
49 
50   /**
51    * Creates a result with the given data
52    * @param _id entry id
53    * @param _score score
54    */
Result(EntryId _id,double _score)55   inline Result(EntryId _id, double _score): Id(_id), Score(_score){}
56 
57   /**
58    * Compares the scores of two results
59    * @return true iff this.score < r.score
60    */
61   inline bool operator<(const Result &r) const
62   {
63     return this->Score < r.Score;
64   }
65 
66   /**
67    * Compares the scores of two results
68    * @return true iff this.score > r.score
69    */
70   inline bool operator>(const Result &r) const
71   {
72     return this->Score > r.Score;
73   }
74 
75   /**
76    * Compares the entry id of the result
77    * @return true iff this.id == id
78    */
79   inline bool operator==(EntryId id) const
80   {
81     return this->Id == id;
82   }
83 
84   /**
85    * Compares the score of this entry with a given one
86    * @param s score to compare with
87    * @return true iff this score < s
88    */
89   inline bool operator<(double s) const
90   {
91     return this->Score < s;
92   }
93 
94   /**
95    * Compares the score of this entry with a given one
96    * @param s score to compare with
97    * @return true iff this score > s
98    */
99   inline bool operator>(double s) const
100   {
101     return this->Score > s;
102   }
103 
104   /**
105    * Compares the score of two results
106    * @param a
107    * @param b
108    * @return true iff a.Score > b.Score
109    */
gt(const Result & a,const Result & b)110   static inline bool gt(const Result &a, const Result &b)
111   {
112     return a.Score > b.Score;
113   }
114 
115   /**
116    * Compares the scores of two results
117    * @return true iff a.Score > b.Score
118    */
ge(const Result & a,const Result & b)119   inline static bool ge(const Result &a, const Result &b)
120   {
121     return a.Score > b.Score;
122   }
123 
124   /**
125    * Returns true iff a.Score >= b.Score
126    * @param a
127    * @param b
128    * @return true iff a.Score >= b.Score
129    */
geq(const Result & a,const Result & b)130   static inline bool geq(const Result &a, const Result &b)
131   {
132     return a.Score >= b.Score;
133   }
134 
135   /**
136    * Returns true iff a.Score >= s
137    * @param a
138    * @param s
139    * @return true iff a.Score >= s
140    */
geqv(const Result & a,double s)141   static inline bool geqv(const Result &a, double s)
142   {
143     return a.Score >= s;
144   }
145 
146 
147   /**
148    * Returns true iff a.Id < b.Id
149    * @param a
150    * @param b
151    * @return true iff a.Id < b.Id
152    */
ltId(const Result & a,const Result & b)153   static inline bool ltId(const Result &a, const Result &b)
154   {
155     return a.Id < b.Id;
156   }
157 
158   /**
159    * Prints a string version of the result
160    * @param os ostream
161    * @param ret Result to print
162    */
163   friend std::ostream & operator<<(std::ostream& os, const Result& ret );
164 };
165 
166 /// Multiple results from a query
167 class QueryResults: public std::vector<Result>
168 {
169 public:
170 
171   /**
172    * Multiplies all the scores in the vector by factor
173    * @param factor
174    */
175   inline void scaleScores(double factor);
176 
177   /**
178    * Prints a string version of the results
179    * @param os ostream
180    * @param ret QueryResults to print
181    */
182   friend std::ostream & operator<<(std::ostream& os, const QueryResults& ret );
183 
184   /**
185    * Saves a matlab file with the results
186    * @param filename
187    */
188   void saveM(const std::string &filename) const;
189 
190 };
191 
192 // --------------------------------------------------------------------------
193 
scaleScores(double factor)194 inline void QueryResults::scaleScores(double factor)
195 {
196   for(QueryResults::iterator qit = begin(); qit != end(); ++qit)
197     qit->Score *= factor;
198 }
199 
200 // --------------------------------------------------------------------------
201 
202 } // namespace TemplatedBoW
203 
204 #endif
205 
206