/**************************************************************************** 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_stepmatchclusterbygrids.h" #include #include #include ONF_StepMatchClusterByGrids::ONF_StepMatchClusterByGrids() : SuperClass() { _exportInLoop = false; } QString ONF_StepMatchClusterByGrids::description() const { // Gives the descrption to print in the GUI return tr("Attribuer les points de cluster à des grilles booléenes"); } QString ONF_StepMatchClusterByGrids::detailledDescription() const { return tr("Pour chaque point de chaque cluster d'entrée, cette étape identifie la grille voxel contenant le point." "L'identifiant correspondant est attribué au point. Les données sont exportées dans un fichier ascii. "); } QString ONF_StepMatchClusterByGrids::inputDescription() const { return SuperClass::inputDescription() + tr("

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

"); } QString ONF_StepMatchClusterByGrids::detailsDescription() const { return tr(""); } CT_VirtualAbstractStep* ONF_StepMatchClusterByGrids::createNewInstance() const { // Creates an instance of this step return new ONF_StepMatchClusterByGrids(); } void ONF_StepMatchClusterByGrids::declareInputModels(CT_StepInModelStructureManager& manager) { manager.addResult(_inResultClusters, tr("Clusters")); manager.setZeroOrMoreRootGroup(_inResultClusters, _inZeroOrMoreRootGroupClusters); manager.addGroup(_inZeroOrMoreRootGroupClusters, _inGroupClusters); manager.addItem(_inGroupClusters, _inCluster, tr("Cluster à attribuer")); manager.addResult(_inResultGrid, tr("Grilles")); manager.setZeroOrMoreRootGroup(_inResultGrid, _inZeroOrMoreRootGroupGrid); manager.addGroup(_inZeroOrMoreRootGroupGrid, _inGroupGrid); manager.addItem(_inGroupGrid, _inGrid, tr("Gille (bool)")); manager.addItemAttribute(_inGrid, _inGridName, CT_AbstractCategory::DATA_VALUE, tr("Nom")); if (_exportInLoop) { manager.addResult(_inResultCounter, tr("Résultat compteur"), "", true); manager.setZeroOrMoreRootGroup(_inResultCounter, _inZeroOrMoreRootGroupCounter); manager.addItem(_inZeroOrMoreRootGroupCounter, _inCounter, tr("Compteur")); } } void ONF_StepMatchClusterByGrids::declareOutputModels(CT_StepOutModelStructureManager& manager) { manager.addResultCopy(_inResultClusters); } void ONF_StepMatchClusterByGrids::fillPreInputConfigurationDialog(CT_StepConfigurableDialog* preInputConfigDialog) { preInputConfigDialog->addBool(tr("Export dans une boucle"), "", tr("Activer"), _exportInLoop); } void ONF_StepMatchClusterByGrids::fillPostInputConfigurationDialog(CT_StepConfigurableDialog* postInputConfigDialog) { postInputConfigDialog->addFileChoice(tr("Fichier d'export des points affiliés"),CT_FileChoiceButton::OneNewFile, tr("Fichier ASCII (*.*)"), _outputFileName); } void ONF_StepMatchClusterByGrids::compute() { if (_outputFileName.isEmpty()) {return;} QFile outputFile(_outputFileName.first()); int nclusters = 0; for (CT_AbstractItemDrawableWithPointCloud* cluster : _inCluster.iterateOutputs(_inResultClusters)) { if (isStopped()) {return;} if (cluster != nullptr) {nclusters++;} } QMap* > grids; for (const CT_Grid3D_Sparse* grid : _inGrid.iterateInputs(_inResultGrid)) { if (isStopped()) {return;} const CT_AbstractItemAttribute* att = grid->itemAttribute(_inGridName); QString name = att->toString(grid,nullptr); if (!name.isEmpty()) { grids.insert(name, grid); } } QString exportBaseName = ""; bool header = true; if (_exportInLoop) { header = false; for (const CT_LoopCounter* counter : _inCounter.iterateInputs(_inResultCounter)) { QFileInfo fileinfo(counter->turnName()); if (fileinfo.exists()) { exportBaseName = fileinfo.baseName(); } else { exportBaseName = counter->turnName(); } if (counter->currentTurn() <= 1) { header = true; } } } if (header && outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&outputFile); stream << "PlotID\tPointID\tX\tY\tZ\tClusterID\tRefID\n"; outputFile.close(); } if (outputFile.open(QIODevice::Append | QIODevice::Text)) { QTextStream stream(&outputFile); int cpt = 0; // iterate over all groups for (CT_AbstractItemDrawableWithPointCloud* cluster : _inCluster.iterateOutputs(_inResultClusters)) { if (isStopped()) {return;} CT_PointIterator itP(cluster->pointCloudIndex()) ; while(itP.hasNext()) { const CT_Point &point = itP.next().currentPoint(); size_t indice = itP.currentGlobalIndex(); QString correspName; QMapIterator* > itGrds(grids); while (itGrds.hasNext() && correspName.isEmpty()) { itGrds.next(); QString name = itGrds.key(); const CT_Grid3D_Sparse* grid = itGrds.value(); bool val = grid->valueAtXYZ(point(0), point(1), point(2)); if (val) { correspName = name; } } stream << exportBaseName << "\t" << indice << "\t" << CT_NumericToStringConversionT::toString(point(0)) << "\t" << CT_NumericToStringConversionT::toString(point(1)) << "\t" << CT_NumericToStringConversionT::toString(point(2)) << "\t" << cluster->id()<< "\t" << correspName << "\n"; } setProgress(100 * cpt++ / nclusters); } outputFile.close(); } setProgress(99); }