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://www.centreleonberard.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 "vvToolCropImage.h"
24 #include "clitkCropImage_ggo.h"
25 #include "clitkCropImageGenericFilter.h"
26 #include "clitkExceptionObject.h"
31 #include <QApplication>
32 #include <QMessageBox>
33 #include <QSignalMapper>
36 #include <vtkImageClip.h>
37 #include <vtkImageTranslateExtent.h>
38 #include <vtkImageData.h>
39 #include <vtkSmartPointer.h>
41 //------------------------------------------------------------------------------
42 // Create the tool and automagically (I like this word) insert it in
43 // the main window menu.
44 ADD_TOOL(vvToolCropImage);
45 //------------------------------------------------------------------------------
47 //same order of int* returned by VtkImageData::WholeExtent
48 enum sliderId {xmin, xmax, ymin, ymax, zmin, zmax, tmin, tmax};
49 //------------------------------------------------------------------------------
50 vvToolCropImage::vvToolCropImage(vvMainWindowBase * parent, Qt::WindowFlags f):
51 vvToolWidgetBase(parent, f),
52 vvToolBase<vvToolCropImage>(parent),
53 Ui::vvToolCropImage(),mSliders(8)
57 Ui_vvToolCropImage::setupUi(mToolWidget);
58 mTLabel1->setHidden(true);
59 mTLabel2->setHidden(true);
60 tminSlider->setHidden(true);
61 tmaxSlider->setHidden(true);
62 spin_tmin->setHidden(true);
63 spin_tmax->setHidden(true);
64 mLabelTimeCropping->setHidden(true);
66 // Set how many inputs are needed for this tool
67 AddInputSelector("Select one image");
69 mSliders[xmin]=xminSlider;
70 mSliders[xmax]=xmaxSlider;
71 mSliders[ymin]=yminSlider;
72 mSliders[ymax]=ymaxSlider;
73 mSliders[zmin]=zminSlider;
74 mSliders[zmax]=zmaxSlider;
75 mSliders[tmin]=tminSlider;
76 mSliders[tmax]=tmaxSlider;
78 // Record initial extend
79 mReducedExtent = new int[6];
80 mInitialExtent = new int[6];
82 //------------------------------------------------------------------------------
85 //------------------------------------------------------------------------------
86 vvToolCropImage::~vvToolCropImage()
88 delete [] mReducedExtent;
89 delete [] mInitialExtent;
91 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 void vvToolCropImage::closeEvent(QCloseEvent *event)
97 if(mCurrentSlicerManager){
99 for(int i=0; i<mExtentSize; i++){
100 mReducedExtent[i] = mInitialExtent[i];
104 vvToolWidgetBase::closeEvent(event);
106 //------------------------------------------------------------------------------
109 //------------------------------------------------------------------------------
110 bool vvToolCropImage::close()
112 return vvToolWidgetBase::close();
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 void vvToolCropImage::reject()
120 for(int i=0; i<mExtentSize; i++) mReducedExtent[i] = mInitialExtent[i];
122 return vvToolWidgetBase::reject();
124 //------------------------------------------------------------------------------
127 //------------------------------------------------------------------------------
128 void vvToolCropImage::sliderValueChanged(int dim)
131 if(dim%2==0){//case we are minimum
132 mSliders[dim+1]->setMinimum(mSliders[dim]->value());
134 mSliders[--dimMin]->setMaximum(mSliders[dim]->value());
136 mReducedExtent[dim] = mSliders[dim]->value() + mInitialExtent[dimMin];
139 //------------------------------------------------------------------------------
142 //------------------------------------------------------------------------------
143 void vvToolCropImage::UpdateExtent()
145 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
146 mCurrentSlicerManager->GetSlicer(i)->SetReducedExtent(mReducedExtent);
147 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
148 mCurrentSlicerManager->GetSlicer(i)->Render();
151 //------------------------------------------------------------------------------
154 //------------------------------------------------------------------------------
155 void vvToolCropImage::InputIsSelected(vvSlicerManager * slicer)
157 // Change interface according to number of dimension
158 mExtentSize = 2*slicer->GetDimension();
159 if (slicer->GetDimension()<4) {
160 mTLabel1->setHidden(true);
161 mTLabel2->setHidden(true);
162 tminSlider->setHidden(true);
163 tmaxSlider->setHidden(true);
164 spin_tmin->setHidden(true);
165 spin_tmax->setHidden(true);
166 mLabelTimeCropping->setHidden(true);
168 if (slicer->GetDimension()<3) {
169 mZLabel1->setHidden(true);
170 mZLabel2->setHidden(true);
171 zminSlider->setHidden(true);
172 zmaxSlider->setHidden(true);
173 spin_zmin->setHidden(true);
174 spin_zmax->setHidden(true);
177 int *a = mCurrentImage->GetFirstVTKImageData()->GetWholeExtent();
178 for(int i=0; i<6; i++){
179 mInitialExtent[i] = a[i];
180 mReducedExtent[i] = a[i];
182 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
183 mCurrentSlicerManager->GetSlicer(i)->EnableReducedExtent(true);
184 mCurrentSlicerManager->GetSlicer(i)->SetReducedExtent(mInitialExtent);
187 // Set initial sliders values
188 std::vector<int> imsize = mCurrentSlicerManager->GetImage()->GetSize();
189 for(int dim=0; dim<slicer->GetDimension() && dim<3; ++dim){
190 mSliders[dim*2]->setMaximum(imsize[dim]-1);
191 mSliders[dim*2+1]->setMaximum(imsize[dim]-1);
192 mSliders[dim*2+1]->setValue(imsize[dim]-1);
194 spin_xmin->setMaximum(imsize[0]-1);
195 spin_xmax->setMaximum(imsize[0]-1);
196 spin_xmax->setValue(imsize[0]-1);
198 spin_ymin->setMaximum(imsize[1]-1);
199 spin_ymax->setMaximum(imsize[1]-1);
200 spin_ymax->setValue(imsize[1]-1);
202 if (slicer->GetDimension() >2) {
203 spin_zmin->setMaximum(imsize[2]-1);
204 spin_zmax->setMaximum(imsize[2]-1);
205 spin_zmax->setValue(imsize[2]-1);
208 if (slicer->GetDimension() >3) {
209 spin_tmin->setMaximum(imsize[3]-1);
210 spin_tmax->setMaximum(imsize[3]-1);
211 spin_tmax->setValue(imsize[3]-1);
214 QSignalMapper* signalMapper = new QSignalMapper(this);
215 connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(sliderValueChanged(int)));
216 for(unsigned int i=0; i<mSliders.size(); ++i){
217 signalMapper->setMapping(mSliders[i], i);
218 connect(mSliders[i], SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
222 //------------------------------------------------------------------------------
225 //------------------------------------------------------------------------------
226 void vvToolCropImage::autoCropValueChanged(double v)
230 //------------------------------------------------------------------------------
233 //------------------------------------------------------------------------------
234 void vvToolCropImage::apply()
236 if (!mCurrentSlicerManager) close();
237 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
240 typedef args_info_clitkCropImage ArgsInfoType;
241 typedef clitk::CropImageGenericFilter CropFilterType;
244 ArgsInfoType mArgsInfo;
245 cmdline_parser_clitkCropImage_init(&mArgsInfo); // Initialisation to default
246 int n = mCurrentSlicerManager->GetDimension()*2; // 2D and 3D only
247 mArgsInfo.boundingBox_given = n;
248 mArgsInfo.boundingBox_arg = new int[n];
250 for(int dim=0; dim<mCurrentSlicerManager->GetDimension() && dim<3; ++dim){
251 mArgsInfo.boundingBox_arg[dim*2] = mSliders[dim*2]->value();
252 mArgsInfo.boundingBox_arg[dim*2+1] = mSliders[dim*2+1]->value();
255 mArgsInfo.boundingBox_arg[6] = 0;
256 mArgsInfo.boundingBox_arg[7] = mCurrentImage->GetSize()[3]-1;
258 // We MUST reset initial extend to input image before using the
259 // filter to retrieve the correct image size
260 for(int i=0; i<mExtentSize; i++) {
261 mReducedExtent[i] = mInitialExtent[i];
266 CropFilterType::Pointer filter = CropFilterType::New();
267 filter->SetInputVVImage(mCurrentImage);
268 filter->SetArgsInfo(mArgsInfo);
270 // Go ! (not threaded)
274 catch(clitk::ExceptionObject & e) {
276 QApplication::restoreOverrideCursor();
277 delete [] mArgsInfo.boundingBox_arg;
280 std::ostringstream croppedImageName;
281 croppedImageName << "Cropped_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
282 // Retrieve result and display it
283 vvImage::Pointer output = filter->GetOutputVVImage();
285 AddImage(output,croppedImageName.str());
288 QApplication::restoreOverrideCursor();
289 delete [] mArgsInfo.boundingBox_arg;
292 //------------------------------------------------------------------------------