]> Creatis software - clitk.git/blob - vv/vvToolStructureSetManager.cxx
- cosmetic
[clitk.git] / vv / vvToolStructureSetManager.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #include "vvToolStructureSetManager.h"
20 #include "vvImageReader.h"
21 #include "vvStructureSetActor.h"
22 #include "vvSlicer.h"
23 #include "vvROIActor.h"
24 #include <QFileDialog>
25 #include <QMessageBox>
26 #include <vtkLookupTable.h>
27 #include <vtkRenderWindow.h>
28
29 //------------------------------------------------------------------------------
30 // Create the tool and automagically (I like this word) insert it in
31 // the main window menu.
32 ADD_TOOL(vvToolStructureSetManager);
33 //------------------------------------------------------------------------------
34
35 //------------------------------------------------------------------------------
36 vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent, Qt::WindowFlags f)
37   :vvToolWidgetBase(parent, f), 
38    vvToolBase<vvToolStructureSetManager>(parent), 
39    Ui::vvToolStructureSetManager() {
40   // Setup the UI
41   Ui_vvToolStructureSetManager::setupUi(mToolWidget);
42   mTree->clear();
43   mCurrentStructureSet = NULL;
44   
45   mDefaultLUTColor = vtkLookupTable::New();
46   for(unsigned int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
47     double r = (rand()/(RAND_MAX+1.0));
48     double v = (rand()/(RAND_MAX+1.0));
49     double b = (rand()/(RAND_MAX+1.0));
50     mDefaultLUTColor->SetTableValue(i, r, v, b); 
51     //    std::cout << "mDefaultLUTColor->SetTableValue(" << i << ", " << r << ", " << v << ", " << b << ");" << std::endl;
52   }
53   #include "vvDefaultLut.h"
54
55   // Add input selector
56   AddInputSelector("Select image");
57 }
58 //------------------------------------------------------------------------------
59
60
61 //------------------------------------------------------------------------------
62 vvToolStructureSetManager::~vvToolStructureSetManager() {
63   DD("vvToolStructureSetManager DESTRUCTOR");
64 }
65 //------------------------------------------------------------------------------
66
67
68 //------------------------------------------------------------------------------
69 void vvToolStructureSetManager::Initialize() {
70   SetToolName("StructureSetManager");
71   SetToolMenuName("StructureSet");
72   SetToolIconFilename(":/common/icons/ducky.png");
73   SetToolTip("Display Structure Set.");
74   SetToolExperimental(true);
75 }
76 //------------------------------------------------------------------------------
77
78
79 //------------------------------------------------------------------------------
80 void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m) {
81   // Hide the input selector
82   QList<int> s;
83   s.push_back(0);
84   s.push_back(1);
85   splitter->setSizes(s);
86   // Connect open menus
87   //  connect(mOpenComboBox, SIGNAL(activated(int)), this, SLOT(open(int)));
88   
89   connect(mOpenBinaryButton, SIGNAL(clicked()), this, SLOT(openBinaryImage()));
90
91   DD(mCurrentImage->GetNumberOfDimensions());
92
93   // Seems that the following is not needed to refresh ...
94   //  connect(m, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
95
96   connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(selectedItemChangedInTree()));
97   connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(visibleROIToggled(bool)));
98
99   //  TODO connect(mOpacitySlider, SIGNAL(valueChanged(int), this, SLOT(opacityChanged(int))));
100 }
101 //------------------------------------------------------------------------------
102
103
104 //------------------------------------------------------------------------------
105 void vvToolStructureSetManager::LeftButtonReleaseEvent(int slicer) {
106   DD("vvToolStructureSetManager::UpdateSlice");
107   //DD(slicer);
108   //DD(view);
109   //DD(slices);
110   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
111     if (i != slicer);
112     mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
113   }
114 }
115 //------------------------------------------------------------------------------
116
117
118 //------------------------------------------------------------------------------
119 void vvToolStructureSetManager::open(int type) {
120   DD(type);
121   switch (type) {
122   case 0: openBinaryImage(); return; // Open binary image;
123   case 1: DD("TODO"); return; // Open DICOM RT
124   case 2: DD("TODO"); return; // Open mesh
125   default: std::cerr << "Error ????" << std::endl; exit(0);
126   }
127 }
128 //------------------------------------------------------------------------------
129
130
131 //------------------------------------------------------------------------------
132 void vvToolStructureSetManager::addRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTreeWidgetItem * ww) {
133   QTreeWidgetItem * w = new QTreeWidgetItem(ww);
134   w->setText(0, QString("%1").arg(roi->GetROINumber()));
135   w->setText(1, QString("%1").arg(roi->GetName().c_str()));
136   QBrush brush(QColor(roi->GetDisplayColor()[0]*255, roi->GetDisplayColor()[1]*255, roi->GetDisplayColor()[2]*255));
137   brush.setStyle(Qt::SolidPattern);
138   for(int i=0; i<w->columnCount (); i++) {
139     w->setBackground(i, brush);
140   }
141   mMapROIToTreeWidget[roi] = w;
142   mMapTreeWidgetToROI[w] = roi;
143   // Connect ROI TreeWidget
144   // TODO
145 }
146 //------------------------------------------------------------------------------
147
148
149 //------------------------------------------------------------------------------
150 void vvToolStructureSetManager::updateStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
151   QTreeWidgetItem * ss;
152   if (mMapStructureSetIndexToTreeWidget.find(index) == mMapStructureSetIndexToTreeWidget.end()) {
153     // Main row item
154     ss = new QTreeWidgetItem(mTree);
155     //  ss->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsTristate);
156     ss->setText(0, QString("S%1").arg(index));
157     ss->setText(1, QString("%1").arg(s->GetLabel().c_str()));
158     // Insert in list
159     mMapStructureSetIndexToTreeWidget[index] = ss;
160     
161     // Connect Structure TreeWidget
162     // TODO
163   }
164   else ss = mMapStructureSetIndexToTreeWidget[index];
165
166   // Insert ROI
167   const std::vector<clitk::DicomRT_ROI*> & rois = s->GetListOfROI();
168   for(unsigned int i=0; i<rois.size(); i++) {
169     if (mMapROIToTreeWidget.find(rois[i]) == mMapROIToTreeWidget.end())
170       addRoiInTreeWidget(rois[i], ss);
171   }
172 }
173 //------------------------------------------------------------------------------
174
175
176 //------------------------------------------------------------------------------
177 int vvToolStructureSetManager::addStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
178
179   // Create actor for this SS
180   vvStructureSetActor * mStructureSetActor = new vvStructureSetActor;
181   mStructureSetActor->SetStructureSet(mStructureSet);
182   mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
183
184   // Insert in lists and get index
185   mStructureSetsList.push_back(mStructureSet);
186   mStructureSetActorsList.push_back(mStructureSetActor);
187   int index = mStructureSetsList.size()-1;
188
189   // Return index
190   return index;
191 }
192 //------------------------------------------------------------------------------
193
194
195 //------------------------------------------------------------------------------
196 void vvToolStructureSetManager::openBinaryImage() {
197   DD("openBinaryImage");
198   // Select current StructureSet (or create)
199   int index;
200   DD(mCurrentStructureSetIndex);
201   if (mCurrentStructureSet == NULL) {
202     if (mStructureSetsList.size() == 0) { // Create a default SS
203       clitk::DicomRT_StructureSet * mStructureSet = new clitk::DicomRT_StructureSet;
204       index = addStructureSet(mStructureSet);
205       DD(index);
206     }
207     else { // Get first SS
208       index = 0;
209     }
210   }
211   else {
212     index = mCurrentStructureSetIndex;
213   }
214   DD(index);
215   // TODO -> SET THIS SS AS CURRENT
216   mCurrentStructureSet = mStructureSetsList[index];
217   mCurrentStructureSetActor = mStructureSetActorsList[index];
218   mCurrentStructureSetIndex = index;
219   DD(mCurrentStructureSetIndex);
220   DD(mCurrentStructureSet->GetName());
221
222   // Open images
223   QString Extensions = "Images files ( *.mhd *.hdr *.his)";
224   Extensions += ";;All Files (*)";
225   QStringList filename = 
226     QFileDialog::getOpenFileNames(this,tr("Open binary image"),
227                                  mMainWindowBase->GetInputPathName(),Extensions);
228   if (filename.size() == 0) return;
229
230   std::vector<int> mLoadedROIIndex;
231   for(int i=0; i<filename.size(); i++) {
232     DD(filename[i].toStdString());
233
234     // Open Image
235     //init the progress events
236     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
237     vvImageReader * mReader = new vvImageReader;
238     std::vector<std::string> filenames;
239     filenames.push_back(filename[i].toStdString());
240     mReader->SetInputFilenames(filenames);
241     mReader->Update(IMAGE);
242     QApplication::restoreOverrideCursor();
243     
244     if (mReader->GetLastError().size() != 0) {
245       std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
246       QString error = "Cannot open file \n";
247       error += mReader->GetLastError().c_str();
248       QMessageBox::information(this,tr("Reading problem"),error);
249       delete mReader;
250       return;
251     }
252     vvImage::Pointer binaryImage = mReader->GetOutput();
253     //  delete mReader;
254
255     // Check Dimension
256     int dim = mCurrentImage->GetNumberOfDimensions();
257     DD(dim);
258     int bin_dim = binaryImage->GetNumberOfDimensions();
259     DD(bin_dim);
260     if (dim < bin_dim) {  ////////// TO CHANGE FOR 3D/4D
261       std::ostringstream os;
262       os << "Error. Loaded binary image is " << bin_dim 
263          << "D while selected image is " << dim << "D" << std::endl;
264       QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
265       return;
266     }
267     
268     // Add a new roi to the structure
269     int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename[i].toStdString());
270     //DD(n);
271     mLoadedROIIndex.push_back(n);
272
273     mCurrentStructureSet->GetROI(n)->SetBackgroundValueLabelImage(mBackgroundValueSpinBox->value());
274     
275     // Change color NEED DEFAULT COLOR LIST
276     DD(mDefaultLUTColor->GetNumberOfTableValues ());
277     if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
278       double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
279       DD(color[0]);
280       DD(color[1]);
281       DD(color[2]);
282       mCurrentStructureSet->GetROI(n)->SetDisplayColor(color[0], color[1], color[2]);
283     }
284     
285     // Add a new roi actor
286     mCurrentStructureSetActor->CreateNewROIActor(n);
287   } // end loop on n selected filenames
288
289   // Update the TreeWidget
290   updateStructureSetInTreeWidget(index, mCurrentStructureSet);
291   
292   // Render loaded ROIs (the first is sufficient)
293   for(unsigned int i=0; i<mLoadedROIIndex.size(); i++) {
294     mCurrentStructureSetActor->GetROIActor(mLoadedROIIndex[i])->Update();
295   }
296   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
297     mCurrentSlicerManager->GetSlicer(i)->Render(); 
298   }
299 }
300 //------------------------------------------------------------------------------
301
302
303 //------------------------------------------------------------------------------
304 void vvToolStructureSetManager::apply() {
305   close();
306 }
307 //------------------------------------------------------------------------------
308
309
310
311 //------------------------------------------------------------------------------
312 // CURRENT ROI INTERACTION
313 //------------------------------------------------------------------------------
314
315
316 //------------------------------------------------------------------------------
317 void vvToolStructureSetManager::selectedItemChangedInTree() {
318   DD("selectedItemChangedInTree");
319   QList<QTreeWidgetItem *> l = mTree->selectedItems();
320   DD(l.size());
321   QTreeWidgetItem * w = l[0];
322   if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) return; // Search for SS (first)
323   clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
324   DD(roi->GetName());
325   //setCurrentSelectedROI(roi);
326
327   mROInameLabel->setText(roi->GetName().c_str());
328   DD(roi->GetROINumber());
329   DD(mCurrentStructureSetIndex);
330   vvROIActor * actor = mStructureSetActorsList[mCurrentStructureSetIndex]->GetROIActor(roi->GetROINumber());
331   mCurrentROI = roi;
332   mCurrentROIActor = actor;
333
334   DD(actor);
335   DD(actor->IsVisible());
336   mCheckBoxShow->setChecked(actor->IsVisible());
337
338   //actor->SetSelected(true); // remove old selection
339
340   DD("ici");
341 }
342 //------------------------------------------------------------------------------
343
344
345 //------------------------------------------------------------------------------
346 void vvToolStructureSetManager::visibleROIToggled(bool b) {
347   DD(b);
348   mCurrentROIActor->SetVisible(b);
349   //mCurrentROIActor->Update();
350 }
351 //------------------------------------------------------------------------------
352
353
354 //------------------------------------------------------------------------------
355 void vvToolStructureSetManager::opacityChanged(int v) {
356   DD(v);
357   mCurrentROIActor->SetOpacity((double)v/100.0);
358   mCurrentROIActor->Update();
359 }
360 //------------------------------------------------------------------------------
361
362
363
364
365 //------------------------------------------------------------------------------
366 //void vvToolStructureSetManager::getActorFromROI() {
367 //  mStructureSetActorsList[mCurrentStructureSetIndex]->GetROIActor(n);
368 //}
369 //------------------------------------------------------------------------------
370
371