]> Creatis software - clitk.git/blob - vv/vvToolStructureSetManager.cxx
- display binary image as overlay
[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 }
98 //------------------------------------------------------------------------------
99
100
101 //------------------------------------------------------------------------------
102 void vvToolStructureSetManager::selectedItemChangedInTree() {
103   DD("selectedItemChangedInTree");
104   QList<QTreeWidgetItem *> l = mTree->selectedItems();
105   DD(l.size());
106   QTreeWidgetItem * w = l[0];
107   if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) return; // Search for SS (first)
108   clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
109   DD(roi->GetName());
110   setCurrentSelectedROI(roi);
111 }
112 //------------------------------------------------------------------------------
113
114
115 //------------------------------------------------------------------------------
116 void vvToolStructureSetManager::setCurrentSelectedROI(clitk::DicomRT_ROI * roi) {
117   //  mCheckBoxShow = // get roi actor .../
118
119 }
120 //------------------------------------------------------------------------------
121
122
123 //------------------------------------------------------------------------------
124 void vvToolStructureSetManager::LeftButtonReleaseEvent(int slicer) {
125   DD("vvToolStructureSetManager::UpdateSlice");
126   //DD(slicer);
127   //DD(view);
128   //DD(slices);
129   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
130     if (i != slicer);
131     mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
132   }
133 }
134 //------------------------------------------------------------------------------
135
136
137 //------------------------------------------------------------------------------
138 void vvToolStructureSetManager::open(int type) {
139   DD(type);
140   switch (type) {
141   case 0: openBinaryImage(); return; // Open binary image;
142   case 1: DD("TODO"); return; // Open DICOM RT
143   case 2: DD("TODO"); return; // Open mesh
144   default: std::cerr << "Error ????" << std::endl; exit(0);
145   }
146 }
147 //------------------------------------------------------------------------------
148
149
150 //------------------------------------------------------------------------------
151 void vvToolStructureSetManager::addRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTreeWidgetItem * ww) {
152   QTreeWidgetItem * w = new QTreeWidgetItem(ww);
153   w->setText(0, QString("%1").arg(roi->GetROINumber()));
154   w->setText(1, QString("%1").arg(roi->GetName().c_str()));
155   QBrush brush(QColor(roi->GetDisplayColor()[0]*255, roi->GetDisplayColor()[1]*255, roi->GetDisplayColor()[2]*255));
156   brush.setStyle(Qt::SolidPattern);
157   for(int i=0; i<w->columnCount (); i++) {
158     w->setBackground(i, brush);
159   }
160   mMapROIToTreeWidget[roi] = w;
161   mMapTreeWidgetToROI[w] = roi;
162   // Connect ROI TreeWidget
163   // TODO
164 }
165 //------------------------------------------------------------------------------
166
167
168 //------------------------------------------------------------------------------
169 void vvToolStructureSetManager::updateStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
170   QTreeWidgetItem * ss;
171   if (mMapStructureSetIndexToTreeWidget.find(index) == mMapStructureSetIndexToTreeWidget.end()) {
172     // Main row item
173     ss = new QTreeWidgetItem(mTree);
174     //  ss->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsTristate);
175     ss->setText(0, QString("S%1").arg(index));
176     ss->setText(1, QString("%1").arg(s->GetLabel().c_str()));
177     // Insert in list
178     mMapStructureSetIndexToTreeWidget[index] = ss;
179     
180     // Connect Structure TreeWidget
181     // TODO
182   }
183   else ss = mMapStructureSetIndexToTreeWidget[index];
184
185   // Insert ROI
186   const std::vector<clitk::DicomRT_ROI*> & rois = s->GetListOfROI();
187   for(unsigned int i=0; i<rois.size(); i++) {
188     if (mMapROIToTreeWidget.find(rois[i]) == mMapROIToTreeWidget.end())
189       addRoiInTreeWidget(rois[i], ss);
190   }
191 }
192 //------------------------------------------------------------------------------
193
194
195 //------------------------------------------------------------------------------
196 int vvToolStructureSetManager::addStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
197
198   // Create actor for this SS
199   vvStructureSetActor * mStructureSetActor = new vvStructureSetActor;
200   mStructureSetActor->SetStructureSet(mStructureSet);
201   mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
202
203   // Insert in lists and get index
204   mStructureSetsList.push_back(mStructureSet);
205   mStructureSetActorsList.push_back(mStructureSetActor);
206   int index = mStructureSetsList.size()-1;
207
208   // Return index
209   return index;
210 }
211 //------------------------------------------------------------------------------
212
213
214 //------------------------------------------------------------------------------
215 void vvToolStructureSetManager::openBinaryImage() {
216   DD("openBinaryImage");
217   // Select current StructureSet (or create)
218   int index;
219   DD(mCurrentStructureSetIndex);
220   if (mCurrentStructureSet == NULL) {
221     if (mStructureSetsList.size() == 0) { // Create a default SS
222       clitk::DicomRT_StructureSet * mStructureSet = new clitk::DicomRT_StructureSet;
223       index = addStructureSet(mStructureSet);
224       DD(index);
225     }
226     else { // Get first SS
227       index = 0;
228     }
229   }
230   else {
231     index = mCurrentStructureSetIndex;
232   }
233   DD(index);
234   // TODO -> SET THIS SS AS CURRENT
235   mCurrentStructureSet = mStructureSetsList[index];
236   mCurrentStructureSetActor = mStructureSetActorsList[index];
237   mCurrentStructureSetIndex = index;
238   DD(mCurrentStructureSet->GetName());
239
240   // Open images
241   QString Extensions = "Images files ( *.mhd *.hdr *.his)";
242   Extensions += ";;All Files (*)";
243   QStringList filename = 
244     QFileDialog::getOpenFileNames(this,tr("Open binary image"),
245                                  mMainWindowBase->GetInputPathName(),Extensions);
246   if (filename.size() == 0) return;
247
248   std::vector<int> mLoadedROIIndex;
249   for(int i=0; i<filename.size(); i++) {
250     DD(filename[i].toStdString());
251
252     // Open Image
253     //init the progress events
254     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
255     vvImageReader * mReader = new vvImageReader;
256     std::vector<std::string> filenames;
257     filenames.push_back(filename[i].toStdString());
258     mReader->SetInputFilenames(filenames);
259     mReader->Update(IMAGE);
260     QApplication::restoreOverrideCursor();
261     
262     if (mReader->GetLastError().size() != 0) {
263       std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
264       QString error = "Cannot open file \n";
265       error += mReader->GetLastError().c_str();
266       QMessageBox::information(this,tr("Reading problem"),error);
267       delete mReader;
268       return;
269     }
270     vvImage::Pointer binaryImage = mReader->GetOutput();
271     //  delete mReader;
272
273     // Check Dimension
274     int dim = mCurrentImage->GetNumberOfDimensions();
275     DD(dim);
276     int bin_dim = binaryImage->GetNumberOfDimensions();
277     DD(bin_dim);
278     if (dim < bin_dim) {  ////////// TO CHANGE FOR 3D/4D
279       std::ostringstream os;
280       os << "Error. Loaded binary image is " << bin_dim 
281          << "D while selected image is " << dim << "D" << std::endl;
282       QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
283       return;
284     }
285     
286     // Add a new roi to the structure
287     int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename[i].toStdString());
288     //DD(n);
289     mLoadedROIIndex.push_back(n);
290
291     mCurrentStructureSet->GetROI(n)->SetBackgroundValueLabelImage(mBackgroundValueSpinBox->value());
292     
293     // Change color NEED DEFAULT COLOR LIST
294     DD(mDefaultLUTColor->GetNumberOfTableValues ());
295     if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
296       double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
297       DD(color[0]);
298       DD(color[1]);
299       DD(color[2]);
300       mCurrentStructureSet->GetROI(n)->SetDisplayColor(color[0], color[1], color[2]);
301     }
302     
303     // Add a new roi actor
304     mCurrentStructureSetActor->CreateNewROIActor(n);
305   } // end loop on n selected filenames
306
307   // Update the TreeWidget
308   updateStructureSetInTreeWidget(index, mCurrentStructureSet);
309   
310   // Render loaded ROIs (the first is sufficient)
311   for(unsigned int i=0; i<mLoadedROIIndex.size(); i++) {
312     mCurrentStructureSetActor->GetROIActor(mLoadedROIIndex[i])->Update();
313   }
314   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
315     mCurrentSlicerManager->GetSlicer(i)->Render(); 
316   }
317 }
318 //------------------------------------------------------------------------------
319
320
321 //------------------------------------------------------------------------------
322 void vvToolStructureSetManager::apply() {
323   close();
324 }
325 //------------------------------------------------------------------------------