comparison src/Matcher.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
87 87
88 m_frames = vector<vector<double> > 88 m_frames = vector<vector<double> >
89 (m_blockSize, vector<double>(m_featureSize, 0)); 89 (m_blockSize, vector<double>(m_featureSize, 0));
90 90
91 m_distXSize = m_blockSize * 2; 91 m_distXSize = m_blockSize * 2;
92
92 size(); 93 size();
93 94
94 m_frameCount = 0; 95 m_frameCount = 0;
95 m_runCount = 0; 96 m_runCount = 0;
96 97
99 100
100 void 101 void
101 Matcher::size() 102 Matcher::size()
102 { 103 {
103 int distSize = (m_params.maxRunCount + 1) * m_blockSize; 104 int distSize = (m_params.maxRunCount + 1) * m_blockSize;
104 m_bestPathCost.resize(m_distXSize, vector<int>(distSize, 0)); 105 m_bestPathCost.resize(m_distXSize, vector<float>(distSize, 0));
105 m_distance.resize(m_distXSize, vector<unsigned char>(distSize, 0)); 106 m_distance.resize(m_distXSize, vector<float>(distSize, 0));
107 m_advance.resize(m_distXSize, vector<Advance>(distSize, AdvanceNone));
106 m_distYSizes.resize(m_distXSize, distSize); 108 m_distYSizes.resize(m_distXSize, distSize);
107 m_first.resize(m_distXSize, 0); 109 m_first.resize(m_distXSize, 0);
108 m_last.resize(m_distXSize, 0); 110 m_last.resize(m_distXSize, 0);
109 } 111 }
110 112
156 << m_frameCount << ", allocating " << len << " for " 158 << m_frameCount << ", allocating " << len << " for "
157 << m_frameCount - m_blockSize << std::endl; 159 << m_frameCount - m_blockSize << std::endl;
158 */ 160 */
159 m_distance[m_frameCount] = m_distance[m_frameCount - m_blockSize]; 161 m_distance[m_frameCount] = m_distance[m_frameCount - m_blockSize];
160 m_distance[m_frameCount - m_blockSize].resize(len, 0); 162 m_distance[m_frameCount - m_blockSize].resize(len, 0);
161 for (int i = 0; i < len; ++i) {
162 m_distance[m_frameCount - m_blockSize][i] =
163 m_distance[m_frameCount][i];
164 }
165 163
166 m_bestPathCost[m_frameCount] = m_bestPathCost[m_frameCount - m_blockSize]; 164 m_bestPathCost[m_frameCount] = m_bestPathCost[m_frameCount - m_blockSize];
167 m_bestPathCost[m_frameCount - m_blockSize].resize(len, 0); 165 m_bestPathCost[m_frameCount - m_blockSize].resize(len, 0);
168 for (int i = 0; i < len; ++i) { 166
169 m_bestPathCost[m_frameCount - m_blockSize][i] = 167 m_advance[m_frameCount] = m_advance[m_frameCount - m_blockSize];
170 m_bestPathCost[m_frameCount][i]; 168 m_advance[m_frameCount - m_blockSize].resize(len);
171 } 169
172
173 m_distYSizes[m_frameCount] = m_distYSizes[m_frameCount - m_blockSize]; 170 m_distYSizes[m_frameCount] = m_distYSizes[m_frameCount - m_blockSize];
174 m_distYSizes[m_frameCount - m_blockSize] = len; 171 m_distYSizes[m_frameCount - m_blockSize] = len;
175 } 172 }
176 173
177 int stop = m_otherMatcher->m_frameCount; 174 int stop = m_otherMatcher->m_frameCount;
184 bool overflow = false; 181 bool overflow = false;
185 int mn= -1; 182 int mn= -1;
186 int mx= -1; 183 int mx= -1;
187 for ( ; index < stop; index++) { 184 for ( ; index < stop; index++) {
188 185
189 int dMN = m_metric.calcDistanceScaled 186 float dMN = m_metric.calcDistance
190 (m_frames[frameIndex], 187 (m_frames[frameIndex],
191 m_otherMatcher->m_frames[index % m_blockSize], 188 m_otherMatcher->m_frames[index % m_blockSize]);
192 m_params.distanceScale);
193 189
194 if (mx<0) 190 if (mx<0)
195 mx = mn = dMN; 191 mx = mn = dMN;
196 else if (dMN > mx) 192 else if (dMN > mx)
197 mx = dMN; 193 mx = dMN;
201 overflow = true; 197 overflow = true;
202 dMN = 255; 198 dMN = 255;
203 } 199 }
204 200
205 if ((m_frameCount == 0) && (index == 0)) // first element 201 if ((m_frameCount == 0) && (index == 0)) // first element
206 setValue(0, 0, 0, 0, dMN); 202 setValue(0, 0, AdvanceNone, 0, dMN);
207 else if (m_frameCount == 0) // first row 203 else if (m_frameCount == 0) // first row
208 setValue(0, index, ADVANCE_OTHER, 204 setValue(0, index, AdvanceOther,
209 getValue(0, index-1, true), dMN); 205 getValue(0, index-1, true), dMN);
210 else if (index == 0) // first column 206 else if (index == 0) // first column
211 setValue(m_frameCount, index, ADVANCE_THIS, 207 setValue(m_frameCount, index, AdvanceThis,
212 getValue(m_frameCount - 1, 0, true), dMN); 208 getValue(m_frameCount - 1, 0, true), dMN);
213 else if (index == m_otherMatcher->m_frameCount - m_blockSize) { 209 else if (index == m_otherMatcher->m_frameCount - m_blockSize) {
214 // missing value(s) due to cutoff 210 // missing value(s) due to cutoff
215 // - no previous value in current row (resp. column) 211 // - no previous value in current row (resp. column)
216 // - no diagonal value if prev. dir. == curr. dirn 212 // - no diagonal value if prev. dir. == curr. dirn
217 int min2 = getValue(m_frameCount - 1, index, true); 213 int min2 = getValue(m_frameCount - 1, index, true);
218 // if ((m_firstPM && (first[m_frameCount - 1] == index)) || 214 // if ((m_firstPM && (first[m_frameCount - 1] == index)) ||
219 // (!m_firstPM && (m_last[index-1] < m_frameCount))) 215 // (!m_firstPM && (m_last[index-1] < m_frameCount)))
220 if (m_first[m_frameCount - 1] == index) 216 if (m_first[m_frameCount - 1] == index)
221 setValue(m_frameCount, index, ADVANCE_THIS, min2, dMN); 217 setValue(m_frameCount, index, AdvanceThis, min2, dMN);
222 else { 218 else {
223 int min1 = getValue(m_frameCount - 1, index - 1, true); 219 int min1 = getValue(m_frameCount - 1, index - 1, true);
224 if (min1 + dMN <= min2) 220 if (min1 + dMN <= min2)
225 setValue(m_frameCount, index, ADVANCE_BOTH, min1,dMN); 221 setValue(m_frameCount, index, AdvanceBoth, min1,dMN);
226 else 222 else
227 setValue(m_frameCount, index, ADVANCE_THIS, min2,dMN); 223 setValue(m_frameCount, index, AdvanceThis, min2,dMN);
228 } 224 }
229 } else { 225 } else {
230 int min1 = getValue(m_frameCount, index-1, true); 226 int min1 = getValue(m_frameCount, index-1, true);
231 int min2 = getValue(m_frameCount - 1, index, true); 227 int min2 = getValue(m_frameCount - 1, index, true);
232 int min3 = getValue(m_frameCount - 1, index-1, true); 228 int min3 = getValue(m_frameCount - 1, index-1, true);
233 if (min1 <= min2) { 229 if (min1 <= min2) {
234 if (min3 + dMN <= min1) 230 if (min3 + dMN <= min1)
235 setValue(m_frameCount, index, ADVANCE_BOTH, min3,dMN); 231 setValue(m_frameCount, index, AdvanceBoth, min3,dMN);
236 else 232 else
237 setValue(m_frameCount, index, ADVANCE_OTHER,min1,dMN); 233 setValue(m_frameCount, index, AdvanceOther,min1,dMN);
238 } else { 234 } else {
239 if (min3 + dMN <= min2) 235 if (min3 + dMN <= min2)
240 setValue(m_frameCount, index, ADVANCE_BOTH, min3,dMN); 236 setValue(m_frameCount, index, AdvanceBoth, min3,dMN);
241 else 237 else
242 setValue(m_frameCount, index, ADVANCE_THIS, min2,dMN); 238 setValue(m_frameCount, index, AdvanceThis, min2,dMN);
243 } 239 }
244 } 240 }
245 m_otherMatcher->m_last[index]++; 241 m_otherMatcher->m_last[index]++;
246 } // loop for row (resp. column) 242 } // loop for row (resp. column)
247 243
264 else 260 else
265 return m_otherMatcher->m_bestPathCost[j][i - m_otherMatcher->m_first[j]]; 261 return m_otherMatcher->m_bestPathCost[j][i - m_otherMatcher->m_first[j]];
266 } // getValue() 262 } // getValue()
267 263
268 void 264 void
269 Matcher::setValue(int i, int j, int dir, int value, int dMN) 265 Matcher::setValue(int i, int j, Advance dir, float value, float dMN)
270 { 266 {
271 if (m_firstPM) { 267 if (m_firstPM) {
272 m_distance[i][j - m_first[i]] = (unsigned char)((dMN & MASK) | dir); 268
273 m_bestPathCost[i][j - m_first[i]] = 269 int jdx = j - m_first[i];
274 (value + (dir==ADVANCE_BOTH? dMN*2: dMN)); 270 m_distance[i][jdx] = dMN;
271 m_advance[i][jdx] = dir;
272 m_bestPathCost[i][jdx] =
273 (value + (dir == AdvanceBoth ? dMN*2: dMN));
274
275 } else { 275 } else {
276 if (dir == ADVANCE_THIS) 276
277 dir = ADVANCE_OTHER; 277 if (dir == AdvanceThis) {
278 else if (dir == ADVANCE_OTHER) 278 dir = AdvanceOther;
279 dir = ADVANCE_THIS; 279 } else if (dir == AdvanceOther) {
280 dir = AdvanceThis;
281 }
282
280 int idx = i - m_otherMatcher->m_first[j]; 283 int idx = i - m_otherMatcher->m_first[j];
284
281 if (idx == (int)m_otherMatcher->m_distYSizes[j]) { 285 if (idx == (int)m_otherMatcher->m_distYSizes[j]) {
282 // This should never happen, but if we allow arbitrary 286 // This should never happen, but if we allow arbitrary
283 // pauses in either direction, and arbitrary lengths at 287 // pauses in either direction, and arbitrary lengths at
284 // end, it is better than a segmentation fault. 288 // end, it is better than a segmentation fault.
285 std::cerr << "Emergency resize: " << idx << " -> " << idx * 2 << std::endl; 289 std::cerr << "Emergency resize: " << idx << " -> " << idx * 2 << std::endl;
286 m_otherMatcher->m_distYSizes[j] = idx * 2; 290 m_otherMatcher->m_distYSizes[j] = idx * 2;
287 m_otherMatcher->m_bestPathCost[j].resize(idx * 2, 0); 291 m_otherMatcher->m_bestPathCost[j].resize(idx * 2, 0);
288 m_otherMatcher->m_distance[j].resize(idx * 2, 0); 292 m_otherMatcher->m_distance[j].resize(idx * 2, 0);
289 } 293 }
290 m_otherMatcher->m_distance[j][idx] = (unsigned char)((dMN & MASK) | dir); 294
295 m_otherMatcher->m_distance[j][idx] = dMN;
296 m_otherMatcher->m_advance[j][idx] = dir;
291 m_otherMatcher->m_bestPathCost[j][idx] = 297 m_otherMatcher->m_bestPathCost[j][idx] =
292 (value + (dir==ADVANCE_BOTH? dMN*2: dMN)); 298 (value + (dir == AdvanceBoth ? dMN*2: dMN));
293 } 299 }
294 } // setValue() 300 } // setValue()
295 301