comparison src/Matcher.cpp @ 182:a67663dc698d types

Start using properly-named types
author Chris Cannam
date Thu, 19 Feb 2015 16:57:19 +0000
parents 8e7f96432570
children 24ddab06aace
comparison
equal deleted inserted replaced
181:8e7f96432570 182:a67663dc698d
61 void 61 void
62 Matcher::init() 62 Matcher::init()
63 { 63 {
64 if (m_initialised) return; 64 if (m_initialised) return;
65 65
66 m_frames = vector<vector<double> >(m_blockSize); 66 m_features = featureseq_t(m_blockSize);
67 67
68 m_distXSize = m_blockSize * 2; 68 m_distXSize = m_blockSize * 2;
69 69
70 size(); 70 size();
71 71
138 Matcher::getRowRange(int i) 138 Matcher::getRowRange(int i)
139 { 139 {
140 return m_otherMatcher->getColRange(i); 140 return m_otherMatcher->getColRange(i);
141 } 141 }
142 142
143 float 143 distance_t
144 Matcher::getDistance(int i, int j) 144 Matcher::getDistance(int i, int j)
145 { 145 {
146 if (m_firstPM) { 146 if (m_firstPM) {
147 if (!isInRange(i, j)) { 147 if (!isInRange(i, j)) {
148 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): " 148 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
149 << "Location is not in range" << endl; 149 << "Location is not in range" << endl;
150 throw "Distance not available"; 150 throw "Distance not available";
151 } 151 }
152 float dist = m_distance[i][j - m_first[i]]; 152 distance_t dist = m_distance[i][j - m_first[i]];
153 if (dist < 0) { 153 if (dist < 0) {
154 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): " 154 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
155 << "Location is in range, but distance (" 155 << "Location is in range, but distance ("
156 << dist << ") is invalid or has not been set" << endl; 156 << dist << ") is invalid or has not been set" << endl;
157 throw "Distance not available"; 157 throw "Distance not available";
161 return m_otherMatcher->getDistance(j, i); 161 return m_otherMatcher->getDistance(j, i);
162 } 162 }
163 } 163 }
164 164
165 void 165 void
166 Matcher::setDistance(int i, int j, float distance) 166 Matcher::setDistance(int i, int j, distance_t distance)
167 { 167 {
168 if (m_firstPM) { 168 if (m_firstPM) {
169 if (!isInRange(i, j)) { 169 if (!isInRange(i, j)) {
170 cerr << "ERROR: Matcher::setDistance(" << i << ", " << j << ", " 170 cerr << "ERROR: Matcher::setDistance(" << i << ", " << j << ", "
171 << distance << "): Location is out of range" << endl; 171 << distance << "): Location is out of range" << endl;
175 } else { 175 } else {
176 m_otherMatcher->setDistance(j, i, distance); 176 m_otherMatcher->setDistance(j, i, distance);
177 } 177 }
178 } 178 }
179 179
180 double 180 pathcost_t
181 Matcher::getNormalisedPathCost(int i, int j) 181 Matcher::getNormalisedPathCost(int i, int j)
182 { 182 {
183 // normalised for path length. 1+ prevents division by zero here 183 // normalised for path length. 1+ prevents division by zero here
184 return getPathCost(i, j) / (1 + i + j); 184 return getPathCost(i, j) / (1 + i + j);
185 } 185 }
186 186
187 double 187 pathcost_t
188 Matcher::getPathCost(int i, int j) 188 Matcher::getPathCost(int i, int j)
189 { 189 {
190 if (m_firstPM) { 190 if (m_firstPM) {
191 if (!isAvailable(i, j)) { 191 if (!isAvailable(i, j)) {
192 if (!isInRange(i, j)) { 192 if (!isInRange(i, j)) {
205 return m_otherMatcher->getPathCost(j, i); 205 return m_otherMatcher->getPathCost(j, i);
206 } 206 }
207 } 207 }
208 208
209 void 209 void
210 Matcher::setPathCost(int i, int j, advance_t dir, double pathCost) 210 Matcher::setPathCost(int i, int j, advance_t dir, pathcost_t pathCost)
211 { 211 {
212 if (m_firstPM) { 212 if (m_firstPM) {
213 if (!isInRange(i, j)) { 213 if (!isInRange(i, j)) {
214 cerr << "ERROR: Matcher::setPathCost(" << i << ", " << j << ", " 214 cerr << "ERROR: Matcher::setPathCost(" << i << ", " << j << ", "
215 << dir << ", " << pathCost 215 << dir << ", " << pathCost
231 231
232 void 232 void
233 Matcher::size() 233 Matcher::size()
234 { 234 {
235 int distSize = (m_params.maxRunCount + 1) * m_blockSize; 235 int distSize = (m_params.maxRunCount + 1) * m_blockSize;
236 m_bestPathCost.resize(m_distXSize, vector<double>(distSize, -1)); 236 m_bestPathCost.resize(m_distXSize, pathcostvec_t(distSize, -1));
237 m_distance.resize(m_distXSize, vector<float>(distSize, -1)); 237 m_distance.resize(m_distXSize, distancevec_t(distSize, -1));
238 m_advance.resize(m_distXSize, vector<advance_t>(distSize, AdvanceNone)); 238 m_advance.resize(m_distXSize, advancevec_t(distSize, AdvanceNone));
239 m_first.resize(m_distXSize, 0); 239 m_first.resize(m_distXSize, 0);
240 m_last.resize(m_distXSize, 0); 240 m_last.resize(m_distXSize, 0);
241 } 241 }
242 242
243 void 243 void
244 Matcher::consumeFeatureVector(vector<double> feature) 244 Matcher::consumeFeatureVector(const feature_t &feature)
245 { 245 {
246 if (!m_initialised) init(); 246 if (!m_initialised) init();
247 int frameIndex = m_frameCount % m_blockSize; 247 int frameIndex = m_frameCount % m_blockSize;
248 m_frames[frameIndex] = feature; 248 m_features[frameIndex] = feature;
249 calcAdvance(); 249 calcAdvance();
250 } 250 }
251 251
252 void 252 void
253 Matcher::calcAdvance() 253 Matcher::calcAdvance()
267 // We need to copy distance[m_frameCount-m_blockSize] to 267 // We need to copy distance[m_frameCount-m_blockSize] to
268 // distance[m_frameCount], and then truncate 268 // distance[m_frameCount], and then truncate
269 // distance[m_frameCount-m_blockSize] to its first len elements. 269 // distance[m_frameCount-m_blockSize] to its first len elements.
270 // Same for bestPathCost. 270 // Same for bestPathCost.
271 271
272 vector<float> dOld = m_distance[m_frameCount - m_blockSize]; 272 distancevec_t dOld(m_distance[m_frameCount - m_blockSize]);
273 vector<float> dNew(len, -1.f); 273 distancevec_t dNew(len, -1.f);
274 274
275 vector<double> bpcOld = m_bestPathCost[m_frameCount - m_blockSize]; 275 pathcostvec_t bpcOld(m_bestPathCost[m_frameCount - m_blockSize]);
276 vector<double> bpcNew(len, -1.0); 276 pathcostvec_t bpcNew(len, -1.0);
277 277
278 vector<advance_t> adOld = m_advance[m_frameCount - m_blockSize]; 278 advancevec_t adOld(m_advance[m_frameCount - m_blockSize]);
279 vector<advance_t> adNew(len, AdvanceNone); 279 advancevec_t adNew(len, AdvanceNone);
280 280
281 for (int i = 0; i < len; ++i) { 281 for (int i = 0; i < len; ++i) {
282 dNew[i] = dOld[i]; 282 dNew[i] = dOld[i];
283 bpcNew[i] = bpcOld[i]; 283 bpcNew[i] = bpcOld[i];
284 adNew[i] = adOld[i]; 284 adNew[i] = adOld[i];
301 m_first[m_frameCount] = index; 301 m_first[m_frameCount] = index;
302 m_last[m_frameCount] = stop; 302 m_last[m_frameCount] = stop;
303 303
304 for ( ; index < stop; index++) { 304 for ( ; index < stop; index++) {
305 305
306 float distance = (float) m_metric.calcDistance 306 distance_t distance = (distance_t) m_metric.calcDistance
307 (m_frames[frameIndex], 307 (m_features[frameIndex],
308 m_otherMatcher->m_frames[index % m_blockSize]); 308 m_otherMatcher->m_features[index % m_blockSize]);
309 309
310 float diagDistance = distance * m_params.diagonalWeight; 310 distance_t diagDistance = distance * m_params.diagonalWeight;
311 311
312 if ((m_frameCount == 0) && (index == 0)) { // first element 312 if ((m_frameCount == 0) && (index == 0)) { // first element
313 313
314 updateValue(0, 0, AdvanceNone, 314 updateValue(0, 0, AdvanceNone,
315 0, 315 0,
331 331
332 // missing value(s) due to cutoff 332 // missing value(s) due to cutoff
333 // - no previous value in current row (resp. column) 333 // - no previous value in current row (resp. column)
334 // - no diagonal value if prev. dir. == curr. dirn 334 // - no diagonal value if prev. dir. == curr. dirn
335 335
336 double min2 = getPathCost(m_frameCount - 1, index); 336 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
337 337
338 // cerr << "NOTE: missing value at i = " << m_frameCount << ", j = " 338 // cerr << "NOTE: missing value at i = " << m_frameCount << ", j = "
339 // << index << " (first = " << m_firstPM << ")" << endl; 339 // << index << " (first = " << m_firstPM << ")" << endl;
340 340
341 // if ((m_firstPM && (first[m_frameCount - 1] == index)) || 341 // if ((m_firstPM && (first[m_frameCount - 1] == index)) ||
345 updateValue(m_frameCount, index, AdvanceThis, 345 updateValue(m_frameCount, index, AdvanceThis,
346 min2, distance); 346 min2, distance);
347 347
348 } else { 348 } else {
349 349
350 double min1 = getPathCost(m_frameCount - 1, index - 1); 350 pathcost_t min1 = getPathCost(m_frameCount - 1, index - 1);
351 if (min1 + diagDistance <= min2 + distance) { 351 if (min1 + diagDistance <= min2 + distance) {
352 updateValue(m_frameCount, index, AdvanceBoth, 352 updateValue(m_frameCount, index, AdvanceBoth,
353 min1, distance); 353 min1, distance);
354 } else { 354 } else {
355 updateValue(m_frameCount, index, AdvanceThis, 355 updateValue(m_frameCount, index, AdvanceThis,
357 } 357 }
358 } 358 }
359 359
360 } else { 360 } else {
361 361
362 double min1 = getPathCost(m_frameCount, index - 1); 362 pathcost_t min1 = getPathCost(m_frameCount, index - 1);
363 double min2 = getPathCost(m_frameCount - 1, index); 363 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
364 double min3 = getPathCost(m_frameCount - 1, index - 1); 364 pathcost_t min3 = getPathCost(m_frameCount - 1, index - 1);
365 365
366 double cost1 = min1 + distance; 366 pathcost_t cost1 = min1 + distance;
367 double cost2 = min2 + distance; 367 pathcost_t cost2 = min2 + distance;
368 double cost3 = min3 + diagDistance; 368 pathcost_t cost3 = min3 + diagDistance;
369 369
370 // Choosing is easy if there is a strict cheapest of the 370 // Choosing is easy if there is a strict cheapest of the
371 // three. If two or more share the lowest cost, we choose 371 // three. If two or more share the lowest cost, we choose
372 // in order of preference: cost3 (AdvanceBoth), cost2 372 // in order of preference: cost3 (AdvanceBoth), cost2
373 // (AdvanceThis), cost1 (AdvanceOther) if we are the first 373 // (AdvanceThis), cost1 (AdvanceOther) if we are the first
404 404
405 m_otherMatcher->m_runCount = 0; 405 m_otherMatcher->m_runCount = 0;
406 } 406 }
407 407
408 void 408 void
409 Matcher::updateValue(int i, int j, advance_t dir, double value, float distance) 409 Matcher::updateValue(int i, int j, advance_t dir, pathcost_t value, distance_t distance)
410 { 410 {
411 float weighted = distance; 411 distance_t weighted = distance;
412 if (dir == AdvanceBoth) { 412 if (dir == AdvanceBoth) {
413 weighted *= m_params.diagonalWeight; 413 weighted *= m_params.diagonalWeight;
414 } 414 }
415 415
416 if (m_firstPM) { 416 if (m_firstPM) {