comparison framework/Document.cpp @ 329:f72d58d1ccb0 tonioni

Adapt additional models created during a transform so they can be returned through a callback in an async method
author Chris Cannam
date Wed, 29 Jan 2014 17:32:47 +0000
parents 1e61f0c26593
children 151b7c5864e3
comparison
equal deleted inserted replaced
327:d2c13ec0f148 329:f72d58d1ccb0
222 vector<Layer *> 222 vector<Layer *>
223 Document::createDerivedLayers(const Transforms &transforms, 223 Document::createDerivedLayers(const Transforms &transforms,
224 const ModelTransformer::Input &input) 224 const ModelTransformer::Input &input)
225 { 225 {
226 QString message; 226 QString message;
227 vector<Model *> newModels = addDerivedModels(transforms, input, message); 227 vector<Model *> newModels = addDerivedModels(transforms, input, message, 0);
228 228
229 if (newModels.empty()) { 229 if (newModels.empty()) {
230 //!!! This identifier may be wrong! 230 //!!! This identifier may be wrong!
231 emit modelGenerationFailed(transforms[0].getIdentifier(), message); 231 emit modelGenerationFailed(transforms[0].getIdentifier(), message);
232 return vector<Layer *>(); 232 return vector<Layer *>();
233 } else if (message != "") { 233 } else if (message != "") {
234 //!!! This identifier may be wrong! 234 //!!! This identifier may be wrong!
235 emit modelGenerationWarning(transforms[0].getIdentifier(), message); 235 emit modelGenerationWarning(transforms[0].getIdentifier(), message);
236 } 236 }
237 237
238 QStringList names;
239 for (int i = 0; i < newModels.size(); ++i) {
240 names.push_back(getUniqueLayerName
241 (TransformFactory::getInstance()->
242 getTransformFriendlyName
243 (transforms[i].getIdentifier())));
244 }
245
246 vector<Layer *> layers = createLayersForDerivedModels(newModels, names);
247 return layers;
248 }
249
250 class AdditionalModelConverter :
251 public ModelTransformerFactory::AdditionalModelHandler
252 {
253 public:
254 AdditionalModelConverter(Document *doc,
255 Document::LayerCreationHandler *handler) :
256 m_doc(doc),
257 m_handler(handler) {
258 }
259
260 virtual ~AdditionalModelConverter() { }
261
262 void
263 setPrimaryLayers(vector<Layer *> layers) {
264 m_primary = layers;
265 }
266
267 void
268 moreModelsAvailable(vector<Model *> models) {
269 std::cerr << "AdditionalModelConverter::moreModelsAvailable: " << models.size() << " model(s)" << std::endl;
270 // We can't automatically regenerate the additional models on
271 // reload -- we should delete them instead
272 QStringList names;
273 foreach (Model *model, models) {
274 m_doc->addAdditionalModel(model);
275 names.push_back(QString());
276 }
277 vector<Layer *> layers = m_doc->createLayersForDerivedModels
278 (models, names);
279 m_handler->layersCreated(m_primary, layers);
280 delete this;
281 }
282
283 void
284 noMoreModelsAvailable() {
285 std::cerr << "AdditionalModelConverter::noMoreModelsAvailable" << std::endl;
286 delete this;
287 }
288
289 private:
290 Document *m_doc;
291 vector<Layer *> m_primary;
292 Document::LayerCreationHandler *m_handler; //!!! how to handle destruction of this?
293 };
294
295 void
296 Document::createDerivedLayersAsync(const Transforms &transforms,
297 const ModelTransformer::Input &input,
298 LayerCreationHandler *handler)
299 {
300 QString message;
301
302 AdditionalModelConverter *amc = new AdditionalModelConverter(this, handler);
303
304 vector<Model *> newModels = addDerivedModels
305 (transforms, input, message, amc);
306
307 QStringList names;
308 for (int i = 0; i < newModels.size(); ++i) {
309 names.push_back(getUniqueLayerName
310 (TransformFactory::getInstance()->
311 getTransformFriendlyName
312 (transforms[i].getIdentifier())));
313 }
314
315 vector<Layer *> layers = createLayersForDerivedModels(newModels, names);
316 amc->setPrimaryLayers(layers);
317
318 if (newModels.empty()) {
319 //!!! This identifier may be wrong!
320 emit modelGenerationFailed(transforms[0].getIdentifier(), message);
321 } else if (message != "") {
322 //!!! This identifier may be wrong!
323 emit modelGenerationWarning(transforms[0].getIdentifier(), message);
324 }
325 }
326
327 vector<Layer *>
328 Document::createLayersForDerivedModels(vector<Model *> newModels,
329 QStringList names)
330 {
238 vector<Layer *> layers; 331 vector<Layer *> layers;
239 332
240 for (int i = 0; i < (int)newModels.size(); ++i) { 333 for (int i = 0; i < (int)newModels.size(); ++i) {
241 334
242 Model *newModel = newModels[i]; 335 Model *newModel = newModels[i];
243 336
244 LayerFactory::LayerTypeSet types = 337 LayerFactory::LayerTypeSet types =
245 LayerFactory::getInstance()->getValidLayerTypes(newModel); 338 LayerFactory::getInstance()->getValidLayerTypes(newModel);
246 339
247 if (types.empty()) { 340 if (types.empty()) {
248 cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transforms[i].getIdentifier() << endl; 341 cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << names[i] << endl;
249 //!!! inadequate cleanup: 342 //!!! inadequate cleanup:
250 newModel->aboutToDelete(); 343 newModel->aboutToDelete();
251 emit modelAboutToBeDeleted(newModel); 344 emit modelAboutToBeDeleted(newModel);
252 m_models.erase(newModel); 345 m_models.erase(newModel);
253 delete newModel; 346 delete newModel;
273 // _while it's still being calculated in the first_ and have it 366 // _while it's still being calculated in the first_ and have it
274 // work quickly. That means we need to put the same physical 367 // work quickly. That means we need to put the same physical
275 // model pointer in both layers, so they can't actually be cloned. 368 // model pointer in both layers, so they can't actually be cloned.
276 369
277 if (newLayer) { 370 if (newLayer) {
278 newLayer->setObjectName(getUniqueLayerName 371 newLayer->setObjectName(names[i]);
279 (TransformFactory::getInstance()->
280 getTransformFriendlyName
281 (transforms[i].getIdentifier())));
282 } 372 }
283 373
284 emit layerAdded(newLayer); 374 emit layerAdded(newLayer);
285 layers.push_back(newLayer); 375 layers.push_back(newLayer);
286 } 376 }
421 for (size_t k = 0; k < obsoleteLayers.size(); ++k) { 511 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
422 deleteLayer(obsoleteLayers[k], true); 512 deleteLayer(obsoleteLayers[k], true);
423 } 513 }
424 514
425 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { 515 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
516 if (i->second.additional) {
517 Model *m = i->first;
518 emit modelAboutToBeDeleted(m);
519 delete m;
520 }
521 }
522
523 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
426 524
427 Model *m = i->first; 525 Model *m = i->first;
428 526
429 #ifdef DEBUG_DOCUMENT 527 #ifdef DEBUG_DOCUMENT
430 SVDEBUG << "considering alignment for model " << m << " (name \"" 528 SVDEBUG << "considering alignment for model " << m << " (name \""
455 553
456 delete oldMainModel; 554 delete oldMainModel;
457 } 555 }
458 556
459 void 557 void
460 Document::addDerivedModel(const Transform &transform, 558 Document::addAlreadyDerivedModel(const Transform &transform,
461 const ModelTransformer::Input &input, 559 const ModelTransformer::Input &input,
462 Model *outputModelToAdd) 560 Model *outputModelToAdd)
463 { 561 {
464 if (m_models.find(outputModelToAdd) != m_models.end()) { 562 if (m_models.find(outputModelToAdd) != m_models.end()) {
465 cerr << "WARNING: Document::addDerivedModel: Model already added" 563 cerr << "WARNING: Document::addAlreadyDerivedModel: Model already added"
466 << endl; 564 << endl;
467 return; 565 return;
468 } 566 }
469 567
470 #ifdef DEBUG_DOCUMENT 568 #ifdef DEBUG_DOCUMENT
471 if (input.getModel()) { 569 if (input.getModel()) {
472 cerr << "Document::addDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName() << "\"" << endl; 570 cerr << "Document::addAlreadyDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName() << "\"" << endl;
473 } else { 571 } else {
474 cerr << "Document::addDerivedModel: source is " << input.getModel() << endl; 572 cerr << "Document::addAlreadyDerivedModel: source is " << input.getModel() << endl;
475 } 573 }
476 #endif 574 #endif
477 575
478 ModelRecord rec; 576 ModelRecord rec;
479 rec.source = input.getModel(); 577 rec.source = input.getModel();
480 rec.channel = input.getChannel(); 578 rec.channel = input.getChannel();
481 rec.transform = transform; 579 rec.transform = transform;
580 rec.additional = false;
482 rec.refcount = 0; 581 rec.refcount = 0;
483 582
484 outputModelToAdd->setSourceModel(input.getModel()); 583 outputModelToAdd->setSourceModel(input.getModel());
485 584
486 m_models[outputModelToAdd] = rec; 585 m_models[outputModelToAdd] = rec;
487 586
488 #ifdef DEBUG_DOCUMENT 587 #ifdef DEBUG_DOCUMENT
489 SVDEBUG << "Document::addDerivedModel: Added model " << outputModelToAdd << endl; 588 cerr << "Document::addAlreadyDerivedModel: Added model " << outputModelToAdd << endl;
490 cerr << "Models now: "; 589 cerr << "Models now: ";
491 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { 590 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
492 cerr << i->first << " "; 591 cerr << i->first << " ";
493 } 592 }
494 cerr << endl; 593 cerr << endl;
508 } 607 }
509 608
510 ModelRecord rec; 609 ModelRecord rec;
511 rec.source = 0; 610 rec.source = 0;
512 rec.refcount = 0; 611 rec.refcount = 0;
612 rec.additional = false;
513 613
514 m_models[model] = rec; 614 m_models[model] = rec;
515 615
516 #ifdef DEBUG_DOCUMENT 616 #ifdef DEBUG_DOCUMENT
517 SVDEBUG << "Document::addImportedModel: Added model " << model << endl; 617 SVDEBUG << "Document::addImportedModel: Added model " << model << endl;
618 cerr << "Models now: ";
619 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
620 cerr << i->first << " ";
621 }
622 cerr << endl;
623 #endif
624
625 if (m_autoAlignment) alignModel(model);
626
627 emit modelAdded(model);
628 }
629
630 void
631 Document::addAdditionalModel(Model *model)
632 {
633 if (m_models.find(model) != m_models.end()) {
634 cerr << "WARNING: Document::addAdditionalModel: Model already added"
635 << endl;
636 return;
637 }
638
639 ModelRecord rec;
640 rec.source = 0;
641 rec.refcount = 0;
642 rec.additional = true;
643
644 m_models[model] = rec;
645
646 #ifdef DEBUG_DOCUMENT
647 SVDEBUG << "Document::addAdditionalModel: Added model " << model << endl;
518 cerr << "Models now: "; 648 cerr << "Models now: ";
519 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { 649 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
520 cerr << i->first << " "; 650 cerr << i->first << " ";
521 } 651 }
522 cerr << endl; 652 cerr << endl;
541 } 671 }
542 } 672 }
543 673
544 Transforms tt; 674 Transforms tt;
545 tt.push_back(transform); 675 tt.push_back(transform);
546 vector<Model *> mm = addDerivedModels(tt, input, message); 676 vector<Model *> mm = addDerivedModels(tt, input, message, 0);
547 if (mm.empty()) return 0; 677 if (mm.empty()) return 0;
548 else return mm[0]; 678 else return mm[0];
549 } 679 }
550 680
551 vector<Model *> 681 vector<Model *>
552 Document::addDerivedModels(const Transforms &transforms, 682 Document::addDerivedModels(const Transforms &transforms,
553 const ModelTransformer::Input &input, 683 const ModelTransformer::Input &input,
554 QString &message) 684 QString &message,
685 AdditionalModelConverter *amc)
555 { 686 {
556 vector<Model *> mm = 687 vector<Model *> mm =
557 ModelTransformerFactory::getInstance()->transformMultiple 688 ModelTransformerFactory::getInstance()->transformMultiple
558 (transforms, input, message); 689 (transforms, input, message, amc);
559 690
560 for (int j = 0; j < (int)mm.size(); ++j) { 691 for (int j = 0; j < (int)mm.size(); ++j) {
561 692
562 Model *model = mm[j]; 693 Model *model = mm[j];
563 694
577 .getPluginVersion()); 708 .getPluginVersion());
578 709
579 if (!model) { 710 if (!model) {
580 cerr << "WARNING: Document::addDerivedModel: no output model for transform " << applied.getIdentifier() << endl; 711 cerr << "WARNING: Document::addDerivedModel: no output model for transform " << applied.getIdentifier() << endl;
581 } else { 712 } else {
582 addDerivedModel(applied, input, model); 713 addAlreadyDerivedModel(applied, input, model);
583 } 714 }
584 } 715 }
585 716
586 return mm; 717 return mm;
587 } 718 }