Mercurial > hg > match-vamp
comparison src/Finder.cpp @ 53:331a17753663 refactors
If the distance metric is a float, the total path cost should be a double (otherwise discriminatory precision is lost as the overall total accumulates)
author | Chris Cannam |
---|---|
date | Fri, 14 Nov 2014 10:09:10 +0000 |
parents | 8cbc15519d2c |
children | 30567d4fb83a 506f0d9880db |
comparison
equal
deleted
inserted
replaced
52:8cbc15519d2c | 53:331a17753663 |
---|---|
70 } // getExpandDirection() | 70 } // getExpandDirection() |
71 | 71 |
72 Matcher::Advance | 72 Matcher::Advance |
73 Finder::getExpandDirection(int row, int col, bool check) | 73 Finder::getExpandDirection(int row, int col, bool check) |
74 { | 74 { |
75 float min = getPathCost(row, col); | 75 double min = getPathCost(row, col); |
76 bestRow = row; | 76 bestRow = row; |
77 bestCol = col; | 77 bestCol = col; |
78 getRowRange(col, rowRange); | 78 getRowRange(col, rowRange); |
79 if (rowRange[1] > row+1) | 79 if (rowRange[1] > row+1) |
80 rowRange[1] = row+1; // don't cheat by looking at future :) | 80 rowRange[1] = row+1; // don't cheat by looking at future :) |
81 for (int index = rowRange[0]; index < rowRange[1]; index++) { | 81 for (int index = rowRange[0]; index < rowRange[1]; index++) { |
82 float tmp = getPathCost(index, col); | 82 double tmp = getPathCost(index, col); |
83 if (tmp < min) { | 83 if (tmp < min) { |
84 min = tmp; | 84 min = tmp; |
85 bestRow = index; | 85 bestRow = index; |
86 } | 86 } |
87 } | 87 } |
88 getColRange(row, colRange); | 88 getColRange(row, colRange); |
89 if (colRange[1] > col+1) | 89 if (colRange[1] > col+1) |
90 colRange[1] = col+1; // don't cheat by looking at future :) | 90 colRange[1] = col+1; // don't cheat by looking at future :) |
91 for (int index = colRange[0]; index < colRange[1]; index++) { | 91 for (int index = colRange[0]; index < colRange[1]; index++) { |
92 float tmp = getPathCost(row, index); | 92 double tmp = getPathCost(row, index); |
93 if (tmp < min) { | 93 if (tmp < min) { |
94 min = tmp; | 94 min = tmp; |
95 bestCol = index; | 95 bestCol = index; |
96 bestRow = row; | 96 bestRow = row; |
97 } | 97 } |
140 } | 140 } |
141 std::cerr << "setDistance(" << row << "," << col << "," << b << "): out of bounds" << std::endl; | 141 std::cerr << "setDistance(" << row << "," << col << "," << b << "): out of bounds" << std::endl; |
142 throw "setDistance index out of bounds"; | 142 throw "setDistance index out of bounds"; |
143 } // setDistance() | 143 } // setDistance() |
144 | 144 |
145 float | 145 double |
146 Finder::getPathCost(int row, int col) | 146 Finder::getPathCost(int row, int col) |
147 { | 147 { |
148 if (find(row, col)) // "1" avoids div by 0 below | 148 if (find(row, col)) // "1" avoids div by 0 below |
149 return pm1->m_bestPathCost[row][col - pm1->m_first[row]] / float(1+row+col); | 149 return pm1->m_bestPathCost[row][col - pm1->m_first[row]] / float(1+row+col); |
150 std::cerr << "getPathCost(" << row << "," << col << "): out of bounds" << std::endl; | 150 std::cerr << "getPathCost(" << row << "," << col << "): out of bounds" << std::endl; |
151 throw "getPathCost index out of bounds"; | 151 throw "getPathCost index out of bounds"; |
152 } // getPathCost() | 152 } // getPathCost() |
153 | 153 |
154 float | 154 double |
155 Finder::getRawPathCost(int row, int col) | 155 Finder::getRawPathCost(int row, int col) |
156 { | 156 { |
157 if (find(row, col)) | 157 if (find(row, col)) |
158 return pm1->m_bestPathCost[row][col - pm1->m_first[row]]; | 158 return pm1->m_bestPathCost[row][col - pm1->m_first[row]]; |
159 std::cerr << "getRawPathCost(" << row << "," << col << "): out of bounds" << std::endl; | 159 std::cerr << "getRawPathCost(" << row << "," << col << "): out of bounds" << std::endl; |
160 throw "getRawPathCost index out of bounds"; | 160 throw "getRawPathCost index out of bounds"; |
161 } // getRawPathCost() | 161 } // getRawPathCost() |
162 | 162 |
163 void | 163 void |
164 Finder::setPathCost(int row, int col, float cost) | 164 Finder::setPathCost(int row, int col, double cost) |
165 { | 165 { |
166 if (find(row, col)) { | 166 if (find(row, col)) { |
167 pm1->m_bestPathCost[row][col - pm1->m_first[row]] = cost; | 167 pm1->m_bestPathCost[row][col - pm1->m_first[row]] = cost; |
168 return; | 168 return; |
169 } | 169 } |
193 Finder::setDistance(float b) | 193 Finder::setDistance(float b) |
194 { | 194 { |
195 pm1->m_distance[index1][index2] = b; | 195 pm1->m_distance[index1][index2] = b; |
196 } // setDistance() | 196 } // setDistance() |
197 | 197 |
198 float | 198 double |
199 Finder::getPathCost() | 199 Finder::getPathCost() |
200 { | 200 { |
201 return pm1->m_bestPathCost[index1][index2]; | 201 return pm1->m_bestPathCost[index1][index2]; |
202 } // getPathCost() | 202 } // getPathCost() |
203 | 203 |
204 void | 204 void |
205 Finder::setPathCost(float cost) | 205 Finder::setPathCost(double cost) |
206 { | 206 { |
207 pm1->m_bestPathCost[index1][index2] = cost; | 207 pm1->m_bestPathCost[index1][index2] = cost; |
208 } // setPathCost() | 208 } // setPathCost() |
209 | 209 |
210 void | 210 void |
224 if (find(r,c)) { | 224 if (find(r,c)) { |
225 int i2 = index2; | 225 int i2 = index2; |
226 float newCost = pm1->m_distance[r][i2]; | 226 float newCost = pm1->m_distance[r][i2]; |
227 Matcher::Advance dir = Matcher::AdvanceNone; | 227 Matcher::Advance dir = Matcher::AdvanceNone; |
228 if (r > r1) { // not first row | 228 if (r > r1) { // not first row |
229 float min = -1; | 229 double min = -1; |
230 if ((c > prevRowStart) && (c <= prevRowStop)) { | 230 if ((c > prevRowStart) && (c <= prevRowStop)) { |
231 // diagonal from (r-1,c-1) | 231 // diagonal from (r-1,c-1) |
232 min = pm1->m_bestPathCost[r-1][c-pm1->m_first[r-1]-1] + | 232 min = pm1->m_bestPathCost[r-1][c-pm1->m_first[r-1]-1] + |
233 newCost * 2; | 233 newCost * 2; |
234 dir = Matcher::AdvanceBoth; | 234 dir = Matcher::AdvanceBoth; |
235 } | 235 } |
236 if ((c >= prevRowStart) && (c < prevRowStop)) { | 236 if ((c >= prevRowStart) && (c < prevRowStop)) { |
237 // vertical from (r-1,c) | 237 // vertical from (r-1,c) |
238 float cost = pm1->m_bestPathCost[r-1][c-pm1->m_first[r-1]] + | 238 double cost = pm1->m_bestPathCost[r-1][c-pm1->m_first[r-1]] + |
239 newCost; | 239 newCost; |
240 if ((min == -1) || (cost < min)) { | 240 if ((min == -1) || (cost < min)) { |
241 min = cost; | 241 min = cost; |
242 dir = Matcher::AdvanceThis; | 242 dir = Matcher::AdvanceThis; |
243 } | 243 } |
244 } | 244 } |
245 if (c > thisRowStart) { | 245 if (c > thisRowStart) { |
246 // horizontal from (r,c-1) | 246 // horizontal from (r,c-1) |
247 float cost =pm1->m_bestPathCost[r][i2-1]+newCost; | 247 double cost =pm1->m_bestPathCost[r][i2-1]+newCost; |
248 if ((min == -1) || (cost < min)) { | 248 if ((min == -1) || (cost < min)) { |
249 min = cost; | 249 min = cost; |
250 dir = Matcher::AdvanceOther; | 250 dir = Matcher::AdvanceOther; |
251 } | 251 } |
252 } | 252 } |