1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include "vvToolExtractPatient.h"
21 #include "vvToolStructureSetManager.h"
23 #include "vvImageReader.h"
24 #include "vvImageWriter.h"
25 #include "vvLabelImageLoaderWidget.h"
26 #include "vvThreadedFilter.h"
29 #include <QMessageBox>
31 //------------------------------------------------------------------------------
32 // Create the tool and automagically (I like this word) insert it in
33 // the main window menu.
34 ADD_TOOL(vvToolExtractPatient);
35 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 vvToolExtractPatient::vvToolExtractPatient(vvMainWindowBase * parent, Qt::WindowFlags f):
40 vvToolWidgetBase(parent,f),
41 vvToolBase<vvToolExtractPatient>(parent),
42 Ui::vvToolExtractPatient()
45 Ui_vvToolExtractPatient::setupUi(mToolWidget);
46 mOptionsBox->setEnabled(true);
47 //connect(mPatientMaskInputWidget, SIGNAL(accepted()), this, SLOT(PatientMaskInputIsSelected()));
50 mArgsInfo = new ArgsInfoType;
51 cmdline_parser_clitkExtractPatient_init(mArgsInfo);
53 m_IsThreadInterrupted = false;
55 // Create a new ExtractPatient filter
56 mFilter = new FilterType; // used in AddInputSelector
59 AddInputSelector("Select CT thorax image", mFilter);
61 //------------------------------------------------------------------------------
64 //------------------------------------------------------------------------------
65 vvToolExtractPatient::~vvToolExtractPatient()
68 //------------------------------------------------------------------------------
71 //------------------------------------------------------------------------------
72 void vvToolExtractPatient::Initialize() {
73 SetToolName("ExtractPatient");
74 SetToolMenuName("Extract Patient");
75 SetToolIconFilename(":/common/icons/Patient-overlay.png");
76 SetToolTip("Extract Patient mask from thorax CT.");
77 // SetToolExperimental(true);
78 // SetToolInMenu("Segmentation");
80 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
84 void vvToolExtractPatient::InputIsSelected(vvSlicerManager *m)
87 HideInputSelector(); // splitter
88 mToolInputSelectionWidget->hide();
89 mCurrentSlicerManager = m;
90 mCurrentImage = m->GetImage();
91 mLabelInput->setText(m->GetFileName().c_str());
93 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
97 // void vvToolExtractPatient::PatientMaskInputIsSelected()
99 // // Get Patient mask and BG value
100 // mPatient = mPatientMaskInputWidget->GetImage();
101 // mPatientBackgroundValue = mPatientMaskInputWidget->GetBackgroundValue();
103 // // Check patient dimension
104 // if (mPatient->GetNumberOfDimensions() != 3) {
105 // QMessageBox::information(this,tr("*Error*"), "Mask image must be 3D");
109 // mMaskLoaderBox->setEnabled(false);
110 // mOptionsBox->setEnabled(true);
112 //------------------------------------------------------------------------------
115 //------------------------------------------------------------------------------
116 void vvToolExtractPatient::SetGUIFromArgsInfo()
119 mAirUpperThresholdSlider->SetText("Upper threshold for air");
120 mAirUpperThresholdSlider->SetMinimum(-1200);
121 mAirUpperThresholdSlider->SetMaximum(2000);
122 DD(mArgsInfo->upper_arg);
123 mAirUpperThresholdSlider->SetValue(mArgsInfo->upper_arg);
125 mAirLowerThresholdSlider->SetText("Lower threshold for air");
126 mAirLowerThresholdSlider->SetMinimum(-1200);
127 mAirLowerThresholdSlider->SetMaximum(2000);
128 mAirLowerThresholdSlider->SetValue(mArgsInfo->lower_arg);
132 //------------------------------------------------------------------------------
135 //------------------------------------------------------------------------------
136 void vvToolExtractPatient::GetArgsInfoFromGUI()
138 //mArgsInfo->patientBG_arg = mPatientMaskInputWidget->GetBackgroundValue();
139 mArgsInfo->verboseOption_flag = true; // DEBUG. TO CHANGE
140 mArgsInfo->verboseStep_flag = true; // DEBUG. TO CHANGE
141 mArgsInfo->writeStep_flag = false;
142 mArgsInfo->input_given = 0;
143 //mArgsInfo->patient_given = 0;
144 mArgsInfo->output_given = 0;
145 //mArgsInfo->outputTrachea_given = 0;
146 //mArgsInfo->remove1_given = 0;
148 // mArgsInfo->upper_arg = mAirUpperThresholdSlider->GetValue();
149 // mArgsInfo->lower_arg = mAirLowerThresholdSlider->GetValue();
150 // if (mRadioButtonLowerThan->isChecked()) mArgsInfo->lower_given = 1;
152 //------------------------------------------------------------------------------
155 //------------------------------------------------------------------------------
156 void vvToolExtractPatient::apply()
158 // Change cursor to wait
159 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
161 // Read options from GUI and put it in the ArgsInfo struct
162 GetArgsInfoFromGUI();
165 // if (mArgsInfo->lower_given) {
166 // if (mArgsInfo->lower_arg > mArgsInfo->upper_arg) {
167 // QApplication::restoreOverrideCursor();
168 // QMessageBox::information(this,tr("Error"), "Lower threshold cannot be greater than upper threshold.");
174 //if (mFilter) delete mFilter;
176 mFilter = new FilterType; // needed when thread cancel the filter
177 mFilter->StopOnErrorOff();
178 // mFilter->SetIOVerbose(true);
179 mFilter->AddInputVVImage(mCurrentImage); // CT
180 // mFilter->AddInputVVImage(mPatient); // patient mask
181 mFilter->SetArgsInfo(*mArgsInfo);
183 // Created threaded execution
185 vvThreadedFilter thread;
186 connect(&thread, SIGNAL(ThreadInterrupted()), this, SLOT(ThreadInterrupted()));
187 thread.SetFilter(mFilter);
190 // Check if the thread has been canceled. In this case, return
191 if (m_IsThreadInterrupted) {
192 m_IsThreadInterrupted = false;
193 QApplication::restoreOverrideCursor();
196 disconnect(&thread, SIGNAL(ThreadInterrupted()), this, SLOT(ThreadInterrupted()));
198 // Check error during filter
199 if (mFilter->HasError()) {
200 QApplication::restoreOverrideCursor();
201 QMessageBox::information(this,tr("Error"), mFilter->GetLastError().c_str());
206 std::vector<vvImage::Pointer> output = mFilter->GetOutputVVImages();
207 if (output.size() == 0) {
208 std::cerr << "Error : no output ?" << std::endl;
209 QApplication::restoreOverrideCursor();
214 // Set Patient into VV
215 vvImage::Pointer Patient = output[0];
216 std::ostringstream osstream;
217 osstream << "Patient_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
218 vvSlicerManager * v = AddImage(Patient,osstream.str());
220 vvToolStructureSetManager::AddImage(mCurrentSlicerManager, "Right Patient", Patient, 1, false); // Right is greater than Left
221 // vvToolStructureSetManager::AddImage(mCurrentSlicerManager, "Left Patient", Patient, 2, false);
223 // // Set trachea into VV
224 // if (output.size() == 2) {
225 // vvImage::Pointer trachea = output[1];
226 // std::ostringstream osstream;
227 // osstream << "Trachea_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
228 // vvSlicerManager * v = AddImage(trachea,osstream.str());
230 // vvToolStructureSetManager::AddImage(mCurrentSlicerManager, "Trachea", trachea, 0);
234 QApplication::restoreOverrideCursor();
237 //------------------------------------------------------------------------------
240 //------------------------------------------------------------------------------
241 bool vvToolExtractPatient::close()
243 return vvToolWidgetBase::close();
245 //------------------------------------------------------------------------------
248 //------------------------------------------------------------------------------
249 void vvToolExtractPatient::ThreadInterrupted()
251 m_IsThreadInterrupted = true;
253 //------------------------------------------------------------------------------