comparison src/Finder.cpp @ 181:8e7f96432570 types

Introduce (though don't use properly) types header; get to build
author Chris Cannam
date Thu, 19 Feb 2015 16:45:42 +0000
parents d1bc89794cd4
children a67663dc698d
comparison
equal deleted inserted replaced
180:d1bc89794cd4 181:8e7f96432570
120 bestRow = row; 120 bestRow = row;
121 } 121 }
122 } 122 }
123 } 123 }
124 124
125 Matcher::Advance 125 advance_t
126 Finder::getExpandDirection() 126 Finder::getExpandDirection()
127 { 127 {
128 return getExpandDirection(m_m->getFrameCount() - 1, 128 return getExpandDirection(m_m->getFrameCount() - 1,
129 m_m->getOtherFrameCount() - 1); 129 m_m->getOtherFrameCount() - 1);
130 } 130 }
131 131
132 Matcher::Advance 132 advance_t
133 Finder::getExpandDirection(int row, int col) 133 Finder::getExpandDirection(int row, int col)
134 { 134 {
135 // To determine which direction to expand the search area in, we 135 // To determine which direction to expand the search area in, we
136 // look at the path costs along the leading edges of the search 136 // look at the path costs along the leading edges of the search
137 // area (the final row and column within the area). We find the 137 // area (the final row and column within the area). We find the
154 154
155 // cerr << "at [" << row << "," << col << "] (cost " << m_m->getPathCost(row, col) << ") blocksize = " << m_m->getBlockSize() << " best is [" << bestRow << "," << bestCol << "] (cost " << bestCost << ")" << endl; 155 // cerr << "at [" << row << "," << col << "] (cost " << m_m->getPathCost(row, col) << ") blocksize = " << m_m->getBlockSize() << " best is [" << bestRow << "," << bestCol << "] (cost " << bestCost << ")" << endl;
156 156
157 if (bestRow == row) { 157 if (bestRow == row) {
158 if (bestCol == col) { 158 if (bestCol == col) {
159 return Matcher::AdvanceBoth; 159 return AdvanceBoth;
160 } else { 160 } else {
161 return Matcher::AdvanceThis; 161 return AdvanceThis;
162 } 162 }
163 } else if (bestCol == col) { 163 } else if (bestCol == col) {
164 return Matcher::AdvanceOther; 164 return AdvanceOther;
165 } else { 165 } else {
166 return Matcher::AdvanceNone; 166 return AdvanceNone;
167 } 167 }
168 } 168 }
169 169
170 void 170 void
171 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2) 171 Finder::recalculatePathCostMatrix(int r1, int c1, int r2, int c2)
182 int rowStop = min(c2 + 1, colRange.second); 182 int rowStop = min(c2 + 1, colRange.second);
183 183
184 for (int c = rowStart; c < rowStop; c++) { 184 for (int c = rowStart; c < rowStop; c++) {
185 185
186 float newCost = m_m->getDistance(r, c); 186 float newCost = m_m->getDistance(r, c);
187 Matcher::Advance dir = Matcher::AdvanceNone; 187 advance_t dir = AdvanceNone;
188 188
189 if (r > r1) { // not first row 189 if (r > r1) { // not first row
190 double min = -1; 190 double min = -1;
191 if ((c > prevRowStart) && (c <= prevRowStop)) { 191 if ((c > prevRowStart) && (c <= prevRowStop)) {
192 // diagonal from (r-1,c-1) 192 // diagonal from (r-1,c-1)
193 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight; 193 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight;
194 dir = Matcher::AdvanceBoth; 194 dir = AdvanceBoth;
195 } 195 }
196 if ((c >= prevRowStart) && (c < prevRowStop)) { 196 if ((c >= prevRowStart) && (c < prevRowStop)) {
197 // vertical from (r-1,c) 197 // vertical from (r-1,c)
198 double cost = m_m->getPathCost(r-1, c) + newCost; 198 double cost = m_m->getPathCost(r-1, c) + newCost;
199 if ((min < 0) || (cost < min)) { 199 if ((min < 0) || (cost < min)) {
200 min = cost; 200 min = cost;
201 dir = Matcher::AdvanceThis; 201 dir = AdvanceThis;
202 } 202 }
203 } 203 }
204 if (c > rowStart) { 204 if (c > rowStart) {
205 // horizontal from (r,c-1) 205 // horizontal from (r,c-1)
206 double cost = m_m->getPathCost(r, c-1) + newCost; 206 double cost = m_m->getPathCost(r, c-1) + newCost;
207 if ((min < 0) || (cost < min)) { 207 if ((min < 0) || (cost < min)) {
208 min = cost; 208 min = cost;
209 dir = Matcher::AdvanceOther; 209 dir = AdvanceOther;
210 } 210 }
211 } 211 }
212 212
213 m_m->setPathCost(r, c, dir, min); 213 m_m->setPathCost(r, c, dir, min);
214 214
215 } else if (c > rowStart) { // first row 215 } else if (c > rowStart) { // first row
216 // horizontal from (r,c-1) 216 // horizontal from (r,c-1)
217 m_m->setPathCost(r, c, Matcher::AdvanceOther, 217 m_m->setPathCost(r, c, AdvanceOther,
218 m_m->getPathCost(r, c-1) + newCost); 218 m_m->getPathCost(r, c-1) + newCost);
219 } 219 }
220 } 220 }
221 221
222 prevRowStart = rowStart; 222 prevRowStart = rowStart;
252 252
253 for (int c = rowStart; c < rowStop; c++) { 253 for (int c = rowStart; c < rowStop; c++) {
254 254
255 float newCost = m_m->getDistance(r, c); 255 float newCost = m_m->getDistance(r, c);
256 double updateTo = -1.0; 256 double updateTo = -1.0;
257 Matcher::Advance dir = Matcher::AdvanceNone; 257 advance_t dir = AdvanceNone;
258 258
259 if (r > r1) { // not first row 259 if (r > r1) { // not first row
260 double min = -1; 260 double min = -1;
261 if ((c > prevRowStart) && (c <= prevRowStop)) { 261 if ((c > prevRowStart) && (c <= prevRowStop)) {
262 // diagonal from (r-1,c-1) 262 // diagonal from (r-1,c-1)
263 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight; 263 min = m_m->getPathCost(r-1, c-1) + newCost * diagonalWeight;
264 err.prevCost = m_m->getPathCost(r-1, c-1); 264 err.prevCost = m_m->getPathCost(r-1, c-1);
265 err.distance = newCost * diagonalWeight; 265 err.distance = newCost * diagonalWeight;
266 dir = Matcher::AdvanceBoth; 266 dir = AdvanceBoth;
267 } 267 }
268 if ((c >= prevRowStart) && (c < prevRowStop)) { 268 if ((c >= prevRowStart) && (c < prevRowStop)) {
269 // vertical from (r-1,c) 269 // vertical from (r-1,c)
270 double cost = m_m->getPathCost(r-1, c) + newCost; 270 double cost = m_m->getPathCost(r-1, c) + newCost;
271 if ((min < 0) || (cost < min)) { 271 if ((min < 0) || (cost < min)) {
272 min = cost; 272 min = cost;
273 err.prevCost = m_m->getPathCost(r-1, c); 273 err.prevCost = m_m->getPathCost(r-1, c);
274 err.distance = newCost; 274 err.distance = newCost;
275 dir = Matcher::AdvanceThis; 275 dir = AdvanceThis;
276 } 276 }
277 } 277 }
278 if (c > rowStart) { 278 if (c > rowStart) {
279 // horizontal from (r,c-1) 279 // horizontal from (r,c-1)
280 double cost = m_m->getPathCost(r, c-1) + newCost; 280 double cost = m_m->getPathCost(r, c-1) + newCost;
281 if ((min < 0) || (cost < min)) { 281 if ((min < 0) || (cost < min)) {
282 min = cost; 282 min = cost;
283 err.prevCost = m_m->getPathCost(r, c-1); 283 err.prevCost = m_m->getPathCost(r, c-1);
284 err.distance = newCost; 284 err.distance = newCost;
285 dir = Matcher::AdvanceOther; 285 dir = AdvanceOther;
286 } 286 }
287 } 287 }
288 288
289 updateTo = min; 289 updateTo = min;
290 290
293 if (c > rowStart) { 293 if (c > rowStart) {
294 // horizontal from (r,c-1) 294 // horizontal from (r,c-1)
295 updateTo = m_m->getPathCost(r, c-1) + newCost; 295 updateTo = m_m->getPathCost(r, c-1) + newCost;
296 err.prevCost = m_m->getPathCost(r, c-1); 296 err.prevCost = m_m->getPathCost(r, c-1);
297 err.distance = newCost; 297 err.distance = newCost;
298 dir = Matcher::AdvanceOther; 298 dir = AdvanceOther;
299 } 299 }
300 } 300 }
301 301
302 if (dir != Matcher::AdvanceNone) { 302 if (dir != AdvanceNone) {
303 if (m_m->getAdvance(r, c) != dir) { 303 if (m_m->getAdvance(r, c) != dir) {
304 err.type = ErrorPosition::WrongAdvance; 304 err.type = ErrorPosition::WrongAdvance;
305 err.r = r; 305 err.r = r;
306 err.c = c; 306 err.c = c;
307 err.costWas = m_m->getPathCost(r, c); 307 err.costWas = m_m->getPathCost(r, c);
474 474
475 pathx.push_back(x); 475 pathx.push_back(x);
476 pathy.push_back(y); 476 pathy.push_back(y);
477 477
478 switch (m_m->getAdvance(y, x)) { 478 switch (m_m->getAdvance(y, x)) {
479 case Matcher::AdvanceThis: 479 case AdvanceThis:
480 // cerr << ", going down (dist = " << getDistance() << ")" << endl; 480 // cerr << ", going down (dist = " << getDistance() << ")" << endl;
481 y--; 481 y--;
482 break; 482 break;
483 case Matcher::AdvanceOther: 483 case AdvanceOther:
484 // cerr << ", going left (dist = " << getDistance() << ")" << endl; 484 // cerr << ", going left (dist = " << getDistance() << ")" << endl;
485 x--; 485 x--;
486 break; 486 break;
487 case Matcher::AdvanceBoth: 487 case AdvanceBoth:
488 // cerr << ", going diag (dist = " << getDistance() << ")" << endl; 488 // cerr << ", going diag (dist = " << getDistance() << ")" << endl;
489 x--; 489 x--;
490 y--; 490 y--;
491 break; 491 break;
492 case Matcher::AdvanceNone: // this would indicate a bug, but we wouldn't want to hang 492 case AdvanceNone: // this would indicate a bug, but we wouldn't want to hang
493 cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl; 493 cerr << "WARNING: Neither matcher advanced in path backtrack at (" << x << "," << y << ")" << endl;
494 if (x > y) { 494 if (x > y) {
495 x--; 495 x--;
496 } else { 496 } else {
497 y--; 497 y--;