comparison data/model/Labeller.h @ 1293:114de081a42d subdivide-instants

Tidy up, with some convenient C++11isms
author Chris Cannam
date Fri, 25 Nov 2016 10:08:09 +0000
parents 4b0c968a581a
children ad5f892c0c4d
comparison
equal deleted inserted replaced
1292:4b0c968a581a 1293:114de081a42d
178 * added to the history. 178 * added to the history.
179 */ 179 */
180 template <typename PointType> 180 template <typename PointType>
181 Command *labelAll(SparseModel<PointType> &model, MultiSelection *ms) { 181 Command *labelAll(SparseModel<PointType> &model, MultiSelection *ms) {
182 182
183 typename SparseModel<PointType>::PointList::iterator i; 183 auto points(model.getPoints());
184 typename SparseModel<PointType>::PointList pl(model.getPoints()); 184 auto command = new typename SparseModel<PointType>::EditCommand
185
186 typename SparseModel<PointType>::EditCommand *command =
187 new typename SparseModel<PointType>::EditCommand
188 (&model, tr("Label Points")); 185 (&model, tr("Label Points"));
189 186
190 PointType prevPoint(0); 187 PointType prevPoint(0);
191 188 bool havePrevPoint(false);
192 for (i = pl.begin(); i != pl.end(); ++i) { 189
193 190 for (auto p: points) {
194 bool inRange = true; 191
195 if (ms) { 192 if (ms) {
196 Selection s(ms->getContainingSelection(i->frame, false)); 193 Selection s(ms->getContainingSelection(p.frame, false));
197 if (s.isEmpty() || !s.contains(i->frame)) { 194 if (!s.contains(p.frame)) {
198 inRange = false; 195 prevPoint = p;
196 havePrevPoint = true;
197 continue;
199 } 198 }
200 } 199 }
201 200
202 PointType p(*i);
203
204 if (!inRange) {
205 prevPoint = p;
206 continue;
207 }
208
209 if (actingOnPrevPoint()) { 201 if (actingOnPrevPoint()) {
210 if (i != pl.begin()) { 202 if (havePrevPoint) {
211 command->deletePoint(prevPoint); 203 command->deletePoint(prevPoint);
212 label<PointType>(p, &prevPoint); 204 label<PointType>(p, &prevPoint);
213 command->addPoint(prevPoint); 205 command->addPoint(prevPoint);
214 } 206 }
215 } else { 207 } else {
217 label<PointType>(p, &prevPoint); 209 label<PointType>(p, &prevPoint);
218 command->addPoint(p); 210 command->addPoint(p);
219 } 211 }
220 212
221 prevPoint = p; 213 prevPoint = p;
214 havePrevPoint = true;
222 } 215 }
223 216
224 return command->finish(); 217 return command->finish();
225 } 218 }
226 219
232 * the history. 225 * the history.
233 */ 226 */
234 template <typename PointType> 227 template <typename PointType>
235 Command *subdivide(SparseModel<PointType> &model, MultiSelection *ms, int n) { 228 Command *subdivide(SparseModel<PointType> &model, MultiSelection *ms, int n) {
236 229
237 typename SparseModel<PointType>::PointList::iterator i; 230 auto points(model.getPoints());
238 typename SparseModel<PointType>::PointList pl(model.getPoints()); 231 auto command = new typename SparseModel<PointType>::EditCommand
239 232 (&model, tr("Subdivide Points"));
240 typename SparseModel<PointType>::EditCommand *command = 233
241 new typename SparseModel<PointType>::EditCommand 234 for (auto i = points.begin(); i != points.end(); ++i) {
242 (&model, tr("Subdivide"));
243
244 for (i = pl.begin(); i != pl.end(); ++i) {
245 235
246 auto j = i; 236 auto j = i;
247 // require a "next point" even if it's not in selection 237 // require a "next point" even if it's not in selection
248 if (++j == pl.end()) { 238 if (++j == points.end()) {
249 break; 239 break;
250 } 240 }
251 241
252 bool inRange = true;
253 if (ms) { 242 if (ms) {
254 Selection s(ms->getContainingSelection(i->frame, false)); 243 Selection s(ms->getContainingSelection(i->frame, false));
255 if (s.isEmpty() || !s.contains(i->frame)) { 244 if (!s.contains(i->frame)) {
256 inRange = false; 245 continue;
257 } 246 }
258 }
259 if (!inRange) {
260 continue;
261 } 247 }
262 248
263 PointType p(*i); 249 PointType p(*i);
264 PointType nextP(*j); 250 PointType nextP(*j);
265 251
283 * the history. 269 * the history.
284 */ 270 */
285 template <typename PointType> 271 template <typename PointType>
286 Command *winnow(SparseModel<PointType> &model, MultiSelection *ms, int n) { 272 Command *winnow(SparseModel<PointType> &model, MultiSelection *ms, int n) {
287 273
288 typename SparseModel<PointType>::PointList::iterator i; 274 auto points(model.getPoints());
289 typename SparseModel<PointType>::PointList pl(model.getPoints()); 275 auto command = new typename SparseModel<PointType>::EditCommand
290 276 (&model, tr("Winnow Points"));
291 typename SparseModel<PointType>::EditCommand *command =
292 new typename SparseModel<PointType>::EditCommand
293 (&model, tr("Subdivide"));
294 277
295 int counter = 0; 278 int counter = 0;
296 279
297 for (i = pl.begin(); i != pl.end(); ++i) { 280 for (auto p: points) {
298 281
299 bool inRange = true;
300 if (ms) { 282 if (ms) {
301 Selection s(ms->getContainingSelection(i->frame, false)); 283 Selection s(ms->getContainingSelection(p.frame, false));
302 if (s.isEmpty() || !s.contains(i->frame)) { 284 if (!s.contains(p.frame)) {
303 inRange = false; 285 counter = 0;
286 continue;
304 } 287 }
305 }
306 if (!inRange) {
307 counter = 0;
308 continue;
309 } 288 }
310 289
311 ++counter; 290 ++counter;
312 291
313 if (counter == n+1) counter = 1; 292 if (counter == n+1) counter = 1;
314 if (counter == 1) { 293 if (counter == 1) {
315 // this is an Nth instant, don't remove it 294 // this is an Nth instant, don't remove it
316 continue; 295 continue;
317 } 296 }
318 297
319 command->deletePoint(*i); 298 command->deletePoint(p);
320 } 299 }
321 300
322 return command->finish(); 301 return command->finish();
323 } 302 }
324 303