/**************************************************************************** Copyright (C) 2010-2012 the Office National des Forêts (ONF), France All rights reserved. Contact : alexandre.piboule@onf.fr Developers : Alexandre PIBOULE (ONF) This file is part of PluginONF library. PluginONF is free library: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PluginONF is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PluginONF. If not, see . *****************************************************************************/ #include "onf_stepsetaffiliationidfromreference.h" #include "actions/onf_actionmodifyaffiliations.h" #include "interfacesforplugin.h" #include ONF_StepSetAffiliationIDFromReference::ONF_StepSetAffiliationIDFromReference() : SuperClass() { _2Dsearch = true; _manualModeActivated = false; m_doc = nullptr; setManual(true); } QString ONF_StepSetAffiliationIDFromReference::description() const { return tr("Jointure entre deux résultats ç l'aide d'IDs d'affiliation"); } QString ONF_StepSetAffiliationIDFromReference::detailledDescription() const { return tr(""); } QString ONF_StepSetAffiliationIDFromReference::inputDescription() const { return SuperClass::inputDescription() + tr("

"); } QString ONF_StepSetAffiliationIDFromReference::outputDescription() const { return SuperClass::outputDescription() + tr("

"); } QString ONF_StepSetAffiliationIDFromReference::detailsDescription() const { return tr(""); } CT_VirtualAbstractStep* ONF_StepSetAffiliationIDFromReference::createNewInstance() const { // cree une copie de cette etape return new ONF_StepSetAffiliationIDFromReference(); } //////////////////// PROTECTED ////////////////// void ONF_StepSetAffiliationIDFromReference::declareInputModels(CT_StepInModelStructureManager& manager) { manager.addResult(_inResultSource, tr("Résultat de référence"), "", true); manager.setZeroOrMoreRootGroup(_inResultSource, _inZeroOrMoreRootGroupSource); manager.addGroup(_inZeroOrMoreRootGroupSource, _inGroupSource, tr("Groupe de référence")); manager.addItem(_inGroupSource, _inItemSource, tr("Item de référence")); manager.addResult(_inResultTarget, tr("Résultat à affilier"), "", true); manager.setZeroOrMoreRootGroup(_inResultTarget, _inZeroOrMoreRootGroupTarget); manager.addGroup(_inZeroOrMoreRootGroupTarget, _inGroupTarget, tr("Groupe à affilier")); manager.addItem(_inGroupTarget, _inItemTarget, tr("Item à affilier")); } void ONF_StepSetAffiliationIDFromReference::fillPostInputConfigurationDialog(CT_StepConfigurableDialog* postInputConfigDialog) { postInputConfigDialog->addBool(tr("Affiliation par position 2D (3D sinon)"), "", "", _2Dsearch); postInputConfigDialog->addBool(tr("Correction des affiliations en mode manuel"), "", "", _manualModeActivated); } void ONF_StepSetAffiliationIDFromReference::declareOutputModels(CT_StepOutModelStructureManager& manager) { setManual(_manualModeActivated); manager.addItem(_inGroupSource, _outIdSource, tr("ID de référence")); manager.addItem(_inGroupTarget, _outIdTarget, tr("ID à affilier")); } void ONF_StepSetAffiliationIDFromReference::compute() { QMap sourceMap; // Parcours des groupes contenant les scènes à filtrer for(CT_StandardItemGroup* groupSource : _inGroupSource.iterateOutputs(_inResultSource)) { if(isStopped()) break; const CT_AbstractSingularItemDrawable* refItemSource = groupSource->singularItem(_inItemSource); if (refItemSource!=nullptr) { CT_AffiliationID* idSource = new CT_AffiliationID(); groupSource->addSingularItem(_outIdSource, idSource); sourceMap.insert(const_cast(refItemSource), idSource->getValue()); _sourceList.append(const_cast(refItemSource)); } } QMap targetMap; QMap > correspondances; for(CT_StandardItemGroup* groupTarget : _inGroupTarget.iterateOutputs(_inResultTarget)) { if(isStopped()) break; const CT_AbstractSingularItemDrawable* refItemTarget = groupTarget->singularItem(_inItemTarget); if (refItemTarget != nullptr) { CT_AffiliationID* idTarget = new CT_AffiliationID(); groupTarget->addSingularItem(_outIdTarget, idTarget); targetMap.insert(const_cast(refItemTarget), idTarget); _targetList.append(const_cast(refItemTarget)); double xTarget = refItemTarget->centerX(); double yTarget = refItemTarget->centerY(); double zTarget = refItemTarget->centerZ(); if (_2Dsearch) {zTarget = 0;} QMapIterator it(sourceMap); while (it.hasNext() && !isStopped()) { it.next(); double xSource = it.key()->centerX(); double ySource = it.key()->centerY(); double zSource = it.key()->centerZ(); if (_2Dsearch) {zSource = 0;} double distance = pow(xTarget - xSource, 2) + pow(yTarget - ySource, 2) + pow(zTarget - zSource, 2); correspondances.insert(distance, qMakePair(const_cast(refItemTarget), it.key())); } } } while (!correspondances.isEmpty()) { if(isStopped()) break; QPair &pair = correspondances.begin().value(); _pairs.insert(pair.first, pair.second); QMutableMapIterator > it(correspondances); while (it.hasNext()) { it.next(); if ((it.value().first == pair.first) || (it.value().second == pair.second)) { it.remove(); } } } // Mode manuel if (_manualModeActivated && !isStopped()) { m_status = 0; requestManualMode(); m_status = 1; if(!isStopped()) requestManualMode(); } // Affectaction des d'identifiants affiliés QMapIterator itPairs(_pairs); while (itPairs.hasNext()) { itPairs.next(); CT_AffiliationID *idTarget = targetMap.value(itPairs.key()); size_t idSource = sourceMap.value(itPairs.value()); idTarget->setValue(idSource); } } void ONF_StepSetAffiliationIDFromReference::initManualMode() { // create a new 3D document if(m_doc == nullptr) m_doc = guiContext()->documentManager()->new3DDocument(); // change camera type to orthographic if(m_doc != nullptr && !m_doc->views().isEmpty()) dynamic_cast(m_doc->views().at(0))->camera()->setType(CameraInterface::ORTHOGRAPHIC); m_doc->removeAllItemDrawable(); } void ONF_StepSetAffiliationIDFromReference::useManualMode(bool quit) { if(m_status == 0) { if(!quit) { QMessageBox::information(nullptr, tr("Modification des affiliations"), tr("Mode manuel."), QMessageBox::Ok); ONF_ActionModifyAffiliations *action = new ONF_ActionModifyAffiliations(&_sourceList, &_targetList, &_pairs); guiContext()->actionsManager()->removeAction(action->uniqueName()); guiContext()->actionsManager()->addAction(action); m_doc->setCurrentAction(action); } } else if(m_status == 1) { if(!quit) { guiContext()->actionsManager()->removeAction("ONF_ActionModifyAffiliations"); guiContext()->documentManager()->closeDocument(m_doc); m_doc = nullptr; quitManualMode(); } } }