From bcc0b5dc437a7123c86acc4a55993dc8f38f47a8 Mon Sep 17 00:00:00 2001 From: dsarrut Date: Thu, 22 Jul 2010 05:52:56 +0000 Subject: [PATCH] now threaded segmentation (can be canceled) --- vv/vvToolExtractLung.cxx | 92 ++++++++++++++++++++++++---------------- vv/vvToolExtractLung.h | 6 ++- 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/vv/vvToolExtractLung.cxx b/vv/vvToolExtractLung.cxx index 0b29ca9..1bf0d32 100644 --- a/vv/vvToolExtractLung.cxx +++ b/vv/vvToolExtractLung.cxx @@ -44,15 +44,17 @@ vvToolExtractLung::vvToolExtractLung(vvMainWindowBase * parent, Qt::WindowFlags // GUI Ui_vvToolExtractLung::setupUi(mToolWidget); mMaskLoaderBox->setEnabled(false); - mOptionsBox->setEnabled(false); + //mOptionsBox->setEnabled(false); mPatientMaskInputWidget->SetText("Patient mask"); connect(mPatientMaskInputWidget, SIGNAL(accepted()), this, SLOT(PatientMaskInputIsSelected())); // Default values - cmdline_parser_clitkExtractLung_init(&mArgsInfo); + mArgsInfo = new ArgsInfoType; + cmdline_parser_clitkExtractLung_init(mArgsInfo); + m_IsThreadInterrupted = false; // Create a new ExtractLung filter - mFilter = FilterType::New(); + mFilter = new FilterType; // used in AddInputSelector // Add input selector AddInputSelector("Select CT thorax image", mFilter); @@ -73,7 +75,8 @@ void vvToolExtractLung::Initialize() { SetToolMenuName("Extract lungs"); SetToolIconFilename(":/common/icons/lung-overlay.png"); SetToolTip("Extract lung mask from thorax CT."); - SetToolExperimental(true); + // SetToolExperimental(true); + // SetToolInMenu("Segmentation"); } //------------------------------------------------------------------------------ @@ -81,7 +84,6 @@ void vvToolExtractLung::Initialize() { //------------------------------------------------------------------------------ void vvToolExtractLung::InputIsSelected(vvSlicerManager *m) { - DD("InputIsSelected"); // Hide selector HideInputSelector(); // splitter mToolInputSelectionWidget->hide(); @@ -96,13 +98,18 @@ void vvToolExtractLung::InputIsSelected(vvSlicerManager *m) //------------------------------------------------------------------------------ void vvToolExtractLung::PatientMaskInputIsSelected() { - DD("PatientMaskInputIsSelected"); - mMaskLoaderBox->setEnabled(false); - mOptionsBox->setEnabled(true); - // Get Patient mask and BG value mPatient = mPatientMaskInputWidget->GetImage(); mPatientBackgroundValue = mPatientMaskInputWidget->GetBackgroundValue(); + + // Check patient dimension + if (mPatient->GetNumberOfDimensions() != 3) { + QMessageBox::information(this,tr("*Error*"), "Mask image must be 3D"); + return; + } + + mMaskLoaderBox->setEnabled(false); + //mOptionsBox->setEnabled(true); } //------------------------------------------------------------------------------ @@ -110,16 +117,15 @@ void vvToolExtractLung::PatientMaskInputIsSelected() //------------------------------------------------------------------------------ void vvToolExtractLung::GetArgsInfoFromGUI() { - DD("GetArgsInfoFromGUI"); - mArgsInfo.patientBG_arg = 0; //mPatientBackgroundValueSpinBox->value(); - mArgsInfo.verboseOption_flag = true; - mArgsInfo.verboseStep_flag = true; - mArgsInfo.writeStep_flag = false; - mArgsInfo.input_given = 0; - mArgsInfo.patient_given = 0; - mArgsInfo.output_given = 0; - mArgsInfo.outputTrachea_given = 0; - mArgsInfo.remove1_given = 0; + mArgsInfo->patientBG_arg = 0; //mPatientBackgroundValueSpinBox->value(); + mArgsInfo->verboseOption_flag = false; + mArgsInfo->verboseStep_flag = false; + mArgsInfo->writeStep_flag = false; + mArgsInfo->input_given = 0; + mArgsInfo->patient_given = 0; + mArgsInfo->output_given = 0; + mArgsInfo->outputTrachea_given = 0; + mArgsInfo->remove1_given = 0; } //------------------------------------------------------------------------------ @@ -127,46 +133,52 @@ void vvToolExtractLung::GetArgsInfoFromGUI() //------------------------------------------------------------------------------ void vvToolExtractLung::apply() { - DD("apply"); + // Change cursor to wait QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + // Read options from GUI and put it in the ArgsInfo struct GetArgsInfoFromGUI(); - mFilter->SetIOVerbose(true); + // Create new filter + if (mFilter) delete mFilter; + mFilter = new FilterType; // needed when thread cancel the filter + mFilter->StopOnErrorOff(); + // mFilter->SetIOVerbose(true); mFilter->AddInputVVImage(mCurrentImage); // CT mFilter->AddInputVVImage(mPatient); // patient mask - mFilter->SetArgsInfo(mArgsInfo); + mFilter->SetArgsInfo(*mArgsInfo); - DD("mfilter->Update"); + // Created threaded execution vvThreadedFilter thread; - // thread.SetFilter((clitk::FilterBase*)&(*mFilter)); - // thread.SetFilter((clitk::FilterBase*)&(*mFilter)); - thread.SetFilter(&(*mFilter)); - // connect(a, SIGNAL(rejected()), this, SLOT(FilterHasBeenCanceled())); + connect(&thread, SIGNAL(ThreadInterrupted()), this, SLOT(ThreadInterrupted())); + thread.SetFilter(mFilter); thread.Update(); - // if (a->HasError()) { DD(a->GetError()); return; } - // mFilter->Update(); - DD("after thread"); - // Check error + // Check if the thread has been canceled. In this case, return + if (m_IsThreadInterrupted) { + m_IsThreadInterrupted = false; + QApplication::restoreOverrideCursor(); + return; + } + disconnect(&thread, SIGNAL(ThreadInterrupted()), this, SLOT(ThreadInterrupted())); + + // Check error during filter if (mFilter->HasError()) { - QMessageBox::information(this,tr("*Error* while finding lung"), mFilter->GetLastError().c_str()); - reject(); + QApplication::restoreOverrideCursor(); + QMessageBox::information(this,tr("Error"), mFilter->GetLastError().c_str()); return; } // Get output std::vector output = mFilter->GetOutputVVImages(); - DD(output.size()); - if (output.size() == 0) { std::cerr << "Error : no output ?" << std::endl; + QApplication::restoreOverrideCursor(); close(); return; } // Set Lung into VV - DD("lung"); vvImage::Pointer lung = output[0]; std::ostringstream osstream; osstream << "Lung_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd"; @@ -177,7 +189,6 @@ void vvToolExtractLung::apply() // Set trachea into VV if (output.size() == 2) { - DD("trachea"); vvImage::Pointer trachea = output[1]; std::ostringstream osstream; osstream << "Trachea_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd"; @@ -201,3 +212,10 @@ bool vvToolExtractLung::close() //------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ +void vvToolExtractLung::ThreadInterrupted() +{ + m_IsThreadInterrupted = true; +} +//------------------------------------------------------------------------------ + diff --git a/vv/vvToolExtractLung.h b/vv/vvToolExtractLung.h index b45c75e..6ea8010 100644 --- a/vv/vvToolExtractLung.h +++ b/vv/vvToolExtractLung.h @@ -53,15 +53,17 @@ public slots: virtual void apply(); virtual bool close(); void PatientMaskInputIsSelected(); + void ThreadInterrupted(); //----------------------------------------------------- protected: typedef args_info_clitkExtractLung ArgsInfoType; - ArgsInfoType mArgsInfo; + ArgsInfoType * mArgsInfo; typedef clitk::ExtractLungGenericFilter FilterType; - FilterType::Pointer mFilter; + FilterType * mFilter; vvImage::Pointer mPatient; double mPatientBackgroundValue; + bool m_IsThreadInterrupted; }; // end class vvToolExtractLung //------------------------------------------------------------------------------ -- 2.47.1