Mercurial > hg > match-vamp
comparison src/Finder.cpp @ 45:a1b7df871496 refactors
Replace char dist (inc dir in masked section) with float dist + separate dir
author | Chris Cannam |
---|---|
date | Thu, 13 Nov 2014 14:43:03 +0000 |
parents | 6a5d165e5ea4 |
children | b0ebc3e2c016 |
comparison
equal
deleted
inserted
replaced
44:c1112adfd270 | 45:a1b7df871496 |
---|---|
61 { | 61 { |
62 range[0] = pm2->m_first[col]; | 62 range[0] = pm2->m_first[col]; |
63 range[1] = pm2->m_last[col]; | 63 range[1] = pm2->m_last[col]; |
64 } // getRowRange() | 64 } // getRowRange() |
65 | 65 |
66 int | 66 Matcher::Advance |
67 Finder::getExpandDirection(int row, int col) | 67 Finder::getExpandDirection(int row, int col) |
68 { | 68 { |
69 return getExpandDirection(row, col, false); | 69 return getExpandDirection(row, col, false); |
70 } // getExpandDirection() | 70 } // getExpandDirection() |
71 | 71 |
72 int | 72 Matcher::Advance |
73 Finder::getExpandDirection(int row, int col, bool check) | 73 Finder::getExpandDirection(int row, int col, bool check) |
74 { | 74 { |
75 int min = getPathCost(row, col); | 75 int min = getPathCost(row, col); |
76 bestRow = row; | 76 bestRow = row; |
77 bestCol = col; | 77 bestCol = col; |
98 } | 98 } |
99 // System.err.print(" BEST: " + bestRow + " " + bestCol + " " + check); | 99 // System.err.print(" BEST: " + bestRow + " " + bestCol + " " + check); |
100 // System.err.println(" " + pm1->frameCount + " " + pm2->frameCount); | 100 // System.err.println(" " + pm1->frameCount + " " + pm2->frameCount); |
101 if (check) { | 101 if (check) { |
102 // System.err.println(find(row+1, col) + " " + find(row, col+1)); | 102 // System.err.println(find(row+1, col) + " " + find(row, col+1)); |
103 if (!find(row, col+1)) | 103 if (!find(row, col+1)) { |
104 return ADVANCE_THIS; | 104 return Matcher::AdvanceThis; |
105 if (!find(row+1, col)) | 105 } else if (!find(row+1, col)) { |
106 return ADVANCE_OTHER; | 106 return Matcher::AdvanceOther; |
107 } | 107 } |
108 return ((bestRow==row)? ADVANCE_THIS: 0) | | 108 } |
109 ((bestCol==col)? ADVANCE_OTHER: 0); | 109 |
110 if (bestRow == row) { | |
111 if (bestCol == col) { | |
112 return Matcher::AdvanceBoth; | |
113 } else { | |
114 return Matcher::AdvanceThis; | |
115 } | |
116 } else if (bestCol == col) { | |
117 return Matcher::AdvanceOther; | |
118 } else { | |
119 return Matcher::AdvanceBoth; | |
120 } | |
110 | 121 |
111 } // getExpandDirection() | 122 } // getExpandDirection() |
112 | 123 |
113 unsigned char | 124 float |
114 Finder::getDistance(int row, int col) | 125 Finder::getDistance(int row, int col) |
115 { | 126 { |
116 if (find(row, col)) { | 127 if (find(row, col)) { |
117 return pm1->m_distance[row][col - pm1->m_first[row]]; | 128 return pm1->m_distance[row][col - pm1->m_first[row]]; |
118 } | 129 } |
119 std::cerr << "getDistance(" << row << "," << col << "): out of bounds" << std::endl; | 130 std::cerr << "getDistance(" << row << "," << col << "): out of bounds" << std::endl; |
120 throw "getDistance index out of bounds"; | 131 throw "getDistance index out of bounds"; |
121 } // getDistance()/2 | 132 } // getDistance()/2 |
122 | 133 |
123 void | 134 void |
124 Finder::setDistance(int row, int col, unsigned char b) | 135 Finder::setDistance(int row, int col, float b) |
125 { | 136 { |
126 if (find(row, col)) { | 137 if (find(row, col)) { |
127 pm1->m_distance[row][col - pm1->m_first[row]] = b; | 138 pm1->m_distance[row][col - pm1->m_first[row]] = b; |
128 return; | 139 return; |
129 } | 140 } |
130 std::cerr << "setDistance(" << row << "," << col << "," << b << "): out of bounds" << std::endl; | 141 std::cerr << "setDistance(" << row << "," << col << "," << b << "): out of bounds" << std::endl; |
131 throw "setDistance index out of bounds"; | 142 throw "setDistance index out of bounds"; |
132 } // setDistance() | 143 } // setDistance() |
133 | 144 |
134 int | 145 float |
135 Finder::getPathCost(int row, int col) | 146 Finder::getPathCost(int row, int col) |
136 { | 147 { |
137 if (find(row, col)) // "1" avoids div by 0 below | 148 if (find(row, col)) // "1" avoids div by 0 below |
138 return pm1->m_bestPathCost[row][col - pm1->m_first[row]]*100/ (1+row+col); | 149 return pm1->m_bestPathCost[row][col - pm1->m_first[row]] / float(1+row+col); |
139 std::cerr << "getPathCost(" << row << "," << col << "): out of bounds" << std::endl; | 150 std::cerr << "getPathCost(" << row << "," << col << "): out of bounds" << std::endl; |
140 throw "getPathCost index out of bounds"; | 151 throw "getPathCost index out of bounds"; |
141 } // getPathCost() | 152 } // getPathCost() |
142 | 153 |
143 int | 154 float |
144 Finder::getRawPathCost(int row, int col) | 155 Finder::getRawPathCost(int row, int col) |
145 { | 156 { |
146 if (find(row, col)) | 157 if (find(row, col)) |
147 return pm1->m_bestPathCost[row][col - pm1->m_first[row]]; | 158 return pm1->m_bestPathCost[row][col - pm1->m_first[row]]; |
148 std::cerr << "getRawPathCost(" << row << "," << col << "): out of bounds" << std::endl; | 159 std::cerr << "getRawPathCost(" << row << "," << col << "): out of bounds" << std::endl; |
149 throw "getRawPathCost index out of bounds"; | 160 throw "getRawPathCost index out of bounds"; |
150 } // getRawPathCost() | 161 } // getRawPathCost() |
151 | 162 |
152 void | 163 void |
153 Finder::setPathCost(int row, int col, int i) | 164 Finder::setPathCost(int row, int col, float cost) |
154 { | 165 { |
155 if (find(row, col)) { | 166 if (find(row, col)) { |
156 pm1->m_bestPathCost[row][col - pm1->m_first[row]] = i; | 167 pm1->m_bestPathCost[row][col - pm1->m_first[row]] = cost; |
157 return; | 168 return; |
158 } | 169 } |
159 std::cerr << "setPathCost(" << row << "," << col << "," << i << "): out of bounds" << std::endl; | 170 std::cerr << "setPathCost(" << row << "," << col << "," << cost << "): out of bounds" << std::endl; |
160 throw "setPathCost index out of bounds"; | 171 throw "setPathCost index out of bounds"; |
161 } // setPathCost() | 172 } // setPathCost() |
162 | 173 |
163 unsigned char | 174 Matcher::Advance |
175 Finder::getAdvance() | |
176 { | |
177 return pm1->m_advance[index1][index2]; | |
178 } | |
179 | |
180 void | |
181 Finder::setAdvance(Matcher::Advance a) | |
182 { | |
183 pm1->m_advance[index1][index2] = a; | |
184 } | |
185 | |
186 float | |
164 Finder::getDistance() | 187 Finder::getDistance() |
165 { | 188 { |
166 return pm1->m_distance[index1][index2]; | 189 return pm1->m_distance[index1][index2]; |
167 } // getDistance()/0 | 190 } // getDistance()/0 |
168 | 191 |
169 void | 192 void |
170 Finder::setDistance(int b) | 193 Finder::setDistance(float b) |
171 { | 194 { |
172 pm1->m_distance[index1][index2] = (unsigned char)b; | 195 pm1->m_distance[index1][index2] = b; |
173 } // setDistance() | 196 } // setDistance() |
174 | 197 |
175 int | 198 float |
176 Finder::getPathCost() | 199 Finder::getPathCost() |
177 { | 200 { |
178 return pm1->m_bestPathCost[index1][index2]; | 201 return pm1->m_bestPathCost[index1][index2]; |
179 } // getPathCost() | 202 } // getPathCost() |
180 | 203 |
181 void | 204 void |
182 Finder::setPathCost(int i) | 205 Finder::setPathCost(float cost) |
183 { | 206 { |
184 pm1->m_bestPathCost[index1][index2] = i; | 207 pm1->m_bestPathCost[index1][index2] = cost; |
185 } // setPathCost() | 208 } // setPathCost() |
186 | 209 |
187 void | 210 void |
188 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2) | 211 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2) |
189 { | 212 { |
199 thisRowStart = c1; | 222 thisRowStart = c1; |
200 for (c = thisRowStart; c <= c2; c++) { | 223 for (c = thisRowStart; c <= c2; c++) { |
201 if (find(r,c)) { | 224 if (find(r,c)) { |
202 int i2 = index2; | 225 int i2 = index2; |
203 int newCost = pm1->m_distance[r][i2]; | 226 int newCost = pm1->m_distance[r][i2]; |
204 int dir = 0; | 227 Matcher::Advance dir = Matcher::AdvanceNone; |
205 if (r > r1) { // not first row | 228 if (r > r1) { // not first row |
206 int min = -1; | 229 int min = -1; |
207 if ((c > prevRowStart) && (c <= prevRowStop)) { | 230 if ((c > prevRowStart) && (c <= prevRowStop)) { |
208 // diagonal from (r-1,c-1) | 231 // diagonal from (r-1,c-1) |
209 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] + |
210 newCost * 2; | 233 newCost * 2; |
211 dir = ADVANCE_BOTH; | 234 dir = Matcher::AdvanceBoth; |
212 } | 235 } |
213 if ((c >= prevRowStart) && (c < prevRowStop)) { | 236 if ((c >= prevRowStart) && (c < prevRowStop)) { |
214 // vertical from (r-1,c) | 237 // vertical from (r-1,c) |
215 int cost = pm1->m_bestPathCost[r-1][c-pm1->m_first[r-1]] + | 238 int cost = pm1->m_bestPathCost[r-1][c-pm1->m_first[r-1]] + |
216 newCost; | 239 newCost; |
217 if ((min == -1) || (cost < min)) { | 240 if ((min == -1) || (cost < min)) { |
218 min = cost; | 241 min = cost; |
219 dir = ADVANCE_THIS; | 242 dir = Matcher::AdvanceThis; |
220 } | 243 } |
221 } | 244 } |
222 if (c > thisRowStart) { | 245 if (c > thisRowStart) { |
223 // horizontal from (r,c-1) | 246 // horizontal from (r,c-1) |
224 int cost =pm1->m_bestPathCost[r][i2-1]+newCost; | 247 int cost =pm1->m_bestPathCost[r][i2-1]+newCost; |
225 if ((min == -1) || (cost < min)) { | 248 if ((min == -1) || (cost < min)) { |
226 min = cost; | 249 min = cost; |
227 dir = ADVANCE_OTHER; | 250 dir = Matcher::AdvanceOther; |
228 } | 251 } |
229 } | 252 } |
230 pm1->m_bestPathCost[r][i2] = min; | 253 pm1->m_bestPathCost[r][i2] = min; |
231 } else if (c > thisRowStart) { // first row | 254 } else if (c > thisRowStart) { // first row |
232 // horizontal from (r,c-1) | 255 // horizontal from (r,c-1) |
233 pm1->m_bestPathCost[r][i2] = pm1->m_bestPathCost[r][i2-1] + | 256 pm1->m_bestPathCost[r][i2] = pm1->m_bestPathCost[r][i2-1] + |
234 newCost; | 257 newCost; |
235 dir = ADVANCE_OTHER; | 258 dir = Matcher::AdvanceOther; |
236 } | 259 } |
237 if ((r != r1) || (c != c1)) { | 260 pm1->m_advance[r][i2] = dir; |
238 pm1->m_distance[r][i2] = (unsigned char) | |
239 ((pm1->m_distance[r][i2] & MASK) | dir); | |
240 } | |
241 } else | 261 } else |
242 break; // end of row | 262 break; // end of row |
243 } | 263 } |
244 prevRowStart = thisRowStart; | 264 prevRowStart = thisRowStart; |
245 prevRowStop = c; | 265 prevRowStop = c; |
260 // cerr << "x = " << x << ", y = " << y; | 280 // cerr << "x = " << x << ", y = " << y; |
261 | 281 |
262 pathx.push_back(x); | 282 pathx.push_back(x); |
263 pathy.push_back(y); | 283 pathy.push_back(y); |
264 | 284 |
265 switch (getDistance() & ADVANCE_BOTH) { | 285 switch (getAdvance()) { |
266 case ADVANCE_THIS: | 286 case Matcher::AdvanceThis: |
267 // cerr << ", going down (dist = " << (int)getDistance() << ")" << endl; | 287 // cerr << ", going down (dist = " << (int)getDistance() << ")" << endl; |
268 y--; | 288 y--; |
269 break; | 289 break; |
270 case ADVANCE_OTHER: | 290 case Matcher::AdvanceOther: |
271 // cerr << ", going left (dist = " << (int)getDistance() << ")" << endl; | 291 // cerr << ", going left (dist = " << (int)getDistance() << ")" << endl; |
272 x--; | 292 x--; |
273 break; | 293 break; |
274 case ADVANCE_BOTH: | 294 case Matcher::AdvanceBoth: |
275 // cerr << ", going diag (dist = " << (int)getDistance() << ")" << endl; | 295 // cerr << ", going diag (dist = " << (int)getDistance() << ")" << endl; |
276 x--; | 296 x--; |
277 y--; | 297 y--; |
278 break; | 298 break; |
279 default: // this would indicate a bug, but we wouldn't want to hang | 299 case Matcher::AdvanceNone: // this would indicate a bug, but we wouldn't want to hang |
280 // cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl; | 300 // cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl; |
281 if (x > y) { | 301 if (x > y) { |
282 x--; | 302 x--; |
283 } else { | 303 } else { |
284 y--; | 304 y--; |